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.
This commit is contained in:
Daan De Meyer
2023-06-01 12:16:15 +02:00
parent c4b7e0f812
commit cc45eb592b
2 changed files with 12 additions and 2 deletions
+11 -2
View File
@@ -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(
+1
View File
@@ -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 {