From e63d070373bb6a0f550d86a58eab635b5bd51ac4 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sat, 8 Apr 2023 17:33:25 +0900 Subject: [PATCH] copy: make copy_bytes() support O_PATH fds --- src/shared/copy.c | 8 ++++++++ src/test/test-copy.c | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/shared/copy.c b/src/shared/copy.c index 23d72ad1ca8..f283394545b 100644 --- a/src/shared/copy.c +++ b/src/shared/copy.c @@ -157,6 +157,7 @@ int copy_bytes_full( copy_progress_bytes_t progress, void *userdata) { + _cleanup_close_ int fdf_opened = -EBADF, fdt_opened = -EBADF; bool try_cfr = true, try_sendfile = true, try_splice = true, copied_something = false; int r, nonblock_pipe = -1; size_t m = SSIZE_MAX; /* that is the maximum that sendfile and c_f_r accept */ @@ -177,6 +178,13 @@ int copy_bytes_full( if (ret_remains_size) *ret_remains_size = 0; + fdf = fd_reopen_condition(fdf, O_CLOEXEC | O_NOCTTY | O_RDONLY, O_PATH, &fdf_opened); + if (fdf < 0) + return fdf; + fdt = fd_reopen_condition(fdt, O_CLOEXEC | O_NOCTTY | O_RDWR, O_PATH, &fdt_opened); + if (fdt < 0) + return fdt; + /* Try btrfs reflinks first. This only works on regular, seekable files, hence let's check the file offsets of * source and destination first. */ if ((copy_flags & COPY_REFLINK)) { diff --git a/src/test/test-copy.c b/src/test/test-copy.c index 4cd83dbeeaa..df08b7ed618 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -301,7 +301,7 @@ static void test_copy_bytes_regular_file_one(const char *src, bool try_reflink, log_info("%s try_reflink=%s max_bytes=%" PRIu64, __func__, yes_no(try_reflink), max_bytes); - fd = open(src, O_RDONLY | O_CLOEXEC | O_NOCTTY); + fd = open(src, O_CLOEXEC | O_PATH); assert_se(fd >= 0); fd2 = mkostemp_safe(fn2); -- 2.47.3