diff --git a/src/shared/copy.c b/src/shared/copy.c index 14b9b61d8a..1ed5c7424c 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -245,7 +245,7 @@ int copy_bytes_full( ssize_t n; if (max_bytes <= 0) - return 1; /* return > 0 if we hit the max_bytes limit */ + break; r = look_for_signals(copy_flags); if (r < 0) @@ -468,7 +468,16 @@ int copy_bytes_full( copied_something = true; } - return 0; /* return 0 if we hit EOF earlier than the size limit */ + if (copy_flags & COPY_TRUNCATE) { + off_t off = lseek(fdt, 0, SEEK_CUR); + if (off < 0) + return -errno; + + if (ftruncate(fdt, off) < 0) + return -errno; + } + + return max_bytes <= 0; /* return 0 if we hit EOF earlier than the size limit */ } static int fd_copy_symlink( diff --git a/src/shared/copy.h b/src/shared/copy.h index c4482eba7e..b02fb94694 100644 --- a/src/shared/copy.h +++ b/src/shared/copy.h @@ -28,6 +28,7 @@ typedef enum CopyFlags { COPY_ALL_XATTRS = 1 << 13, /* Preserve all xattrs when copying, not just those in the user namespace */ COPY_HOLES = 1 << 14, /* Copy holes */ COPY_GRACEFUL_WARN = 1 << 15, /* Skip copying file types that aren't supported by the target filesystem */ + COPY_TRUNCATE = 1 << 16, /* Truncate to current file offset after copying */ } CopyFlags; typedef enum DenyType {