From cc45eb592b38a6a5ebb5f9494b5d7cbdaab3675c Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Thu, 1 Jun 2023 11:44:03 +0200 Subject: [PATCH] copy: Add COPY_TRUNCATE The file might grow more than the amount of data we end up writing, so let's add a flag to truncate after writing to make sure the file is only as large as it needs to be. --- src/shared/copy.c | 13 +++++++++++-- src/shared/copy.h | 1 + 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/shared/copy.c b/src/shared/copy.c index 14b9b61d8a2..1ed5c7424c9 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 c4482eba7e1..b02fb946943 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 { -- 2.47.3