if (max_bytes != UINT64_MAX && m > max_bytes)
m = max_bytes;
+ if (copy_flags & COPY_HOLES) {
+ off_t c, e;
+
+ c = lseek(fdf, 0, SEEK_CUR);
+ if (c < 0)
+ return -errno;
+
+ /* To see if we're in a hole, we search for the next data offset. */
+ e = lseek(fdf, c, SEEK_DATA);
+ if (e < 0 && errno == ENXIO) {
+ /* If errno == ENXIO, that means we've reached the final hole of the file and
+ * that hole isn't followed by more data. */
+ e = lseek(fdf, 0, SEEK_END);
+ if (e < 0)
+ return -errno;
+ } else if (e < 0)
+ return -errno;
+
+ /* If we're in a hole (current offset is not a data offset), create a hole of the same size
+ * in the target file. */
+ if (e > c && lseek(fdt, e - c, SEEK_CUR) < 0)
+ return -errno;
+
+ c = e; /* Set c to the start of the data segment. */
+
+ /* After copying a potential hole, find the end of the data segment by looking for the next
+ * hole. If we get ENXIO, we're at EOF. */
+ e = lseek(fdf, c, SEEK_HOLE);
+ if (e < 0 && errno == ENXIO)
+ break;
+ else if (e < 0)
+ return -errno;
+
+ /* SEEK_HOLE modifies the file offset so we need to move back to the initial offset. */
+ if (lseek(fdf, c, SEEK_SET) < 0)
+ return -errno;
+
+ /* Make sure we're not copying more than the current data segment. */
+ m = MIN(m, (size_t) e - c);
+ }
+
/* First try copy_file_range(), unless we already tried */
if (try_cfr) {
n = try_copy_file_range(fdf, NULL, fdt, NULL, m, 0u);
COPY_FSYNC_FULL = 1 << 11, /* fsync_full() after we are done */
COPY_SYNCFS = 1 << 12, /* syncfs() the *top-level* dir after we are done */
COPY_ALL_XATTRS = 1 << 13, /* Preserve all xattrs when copying, not just those in the user namespace */
+ COPY_HOLES = 1 << 14, /* Copy holes */
} CopyFlags;
typedef int (*copy_progress_bytes_t)(uint64_t n_bytes, void *userdata);