diff options
author | Michael A Cassaniti <michael@cassaniti.id.au> | 2023-08-11 08:41:56 +0200 |
---|---|---|
committer | Daan De Meyer <daan.j.demeyer@gmail.com> | 2023-08-11 14:30:54 +0200 |
commit | c2dfcbd48e780e49bfc11f1c34077f6c98891e40 (patch) | |
tree | 774d118fe31dbcfc2d9f3d2db239c092ec49b26e /src/test/test-copy.c | |
parent | docs: update link to RHEL/CentOS Stream tracker (diff) | |
download | systemd-c2dfcbd48e780e49bfc11f1c34077f6c98891e40.tar.xz systemd-c2dfcbd48e780e49bfc11f1c34077f6c98891e40.zip |
file-io: Fix copying sparse files
This change makes sure a data copy using copy_bytes() does not exceed the
max_bytes value when using COPY_HOLES and max_bytes stops before the next
data section.
Diffstat (limited to 'src/test/test-copy.c')
-rw-r--r-- | src/test/test-copy.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 72aea4efb6..e22a620ade 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -11,10 +11,12 @@ #include "fileio.h" #include "fs-util.h" #include "hexdecoct.h" +#include "io-util.h" #include "log.h" #include "macro.h" #include "mkdir.h" #include "path-util.h" +#include "random-util.h" #include "rm-rf.h" #include "string-util.h" #include "strv.h" @@ -436,6 +438,77 @@ TEST_RET(copy_holes) { return 0; } +TEST_RET(copy_holes_with_gaps) { + _cleanup_(rm_rf_physical_and_freep) char *t = NULL; + _cleanup_close_ int tfd = -EBADF, fd = -EBADF, fd_copy = -EBADF; + struct stat st; + off_t blksz; + char *buf; + int r; + + assert_se((tfd = mkdtemp_open(NULL, 0, &t)) >= 0); + assert_se((fd = openat(tfd, "src", O_CREAT | O_RDWR, 0600)) >= 0); + assert_se((fd_copy = openat(tfd, "dst", O_CREAT | O_WRONLY, 0600)) >= 0); + + assert_se(fstat(fd, &st) >= 0); + blksz = st.st_blksize; + buf = alloca_safe(blksz); + memset(buf, 1, blksz); + + /* Create a file with: + * - hole of 1 block + * - data of 2 block + * - hole of 2 blocks + * - data of 1 block + * + * Since sparse files are based on blocks and not bytes, we need to make + * sure that the holes are aligned to the block size. + */ + + r = RET_NERRNO(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, blksz)); + if (ERRNO_IS_NOT_SUPPORTED(r)) + return log_tests_skipped("Filesystem doesn't support hole punching"); + + assert_se(lseek(fd, blksz, SEEK_CUR) >= 0); + assert_se(loop_write(fd, buf, blksz, 0) >= 0); + assert_se(loop_write(fd, buf, blksz, 0) >= 0); + assert_se(lseek(fd, 2 * blksz, SEEK_CUR) >= 0); + assert_se(loop_write(fd, buf, blksz, 0) >= 0); + assert_se(lseek(fd, 0, SEEK_SET) >= 0); + assert_se(fsync(fd) >= 0); + + /* Copy to the start of the second hole */ + assert_se(copy_bytes(fd, fd_copy, 3 * blksz, COPY_HOLES) >= 0); + assert_se(fstat(fd_copy, &st) >= 0); + assert_se(st.st_size == 3 * blksz); + + /* Copy to the middle of the second hole */ + assert_se(lseek(fd, 0, SEEK_SET) >= 0); + assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0); + assert_se(ftruncate(fd_copy, 0) >= 0); + assert_se(copy_bytes(fd, fd_copy, 4 * blksz, COPY_HOLES) >= 0); + assert_se(fstat(fd_copy, &st) >= 0); + assert_se(st.st_size == 4 * blksz); + + /* Copy to the end of the second hole */ + assert_se(lseek(fd, 0, SEEK_SET) >= 0); + assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0); + assert_se(ftruncate(fd_copy, 0) >= 0); + assert_se(copy_bytes(fd, fd_copy, 5 * blksz, COPY_HOLES) >= 0); + assert_se(fstat(fd_copy, &st) >= 0); + assert_se(st.st_size == 5 * blksz); + + /* Copy everything */ + assert_se(lseek(fd, 0, SEEK_SET) >= 0); + assert_se(lseek(fd_copy, 0, SEEK_SET) >= 0); + assert_se(ftruncate(fd_copy, 0) >= 0); + assert_se(copy_bytes(fd, fd_copy, UINT64_MAX, COPY_HOLES) >= 0); + assert_se(fstat(fd_copy, &st) >= 0); + assert_se(st.st_size == 6 * blksz); + + return 0; +} + TEST(copy_lock) { _cleanup_(rm_rf_physical_and_freep) char *t = NULL; _cleanup_close_ int tfd = -EBADF, fd = -EBADF; |