Merge pull request #9199 from poettering/copy-file-atomic

make copy_file_atomic() use O_TMPFILE to create the destination file
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2018-06-14 11:19:22 +02:00
committed by GitHub
4 changed files with 77 additions and 24 deletions
+21
View File
@@ -240,7 +240,27 @@ static void test_copy_bytes_regular_file(const char *src, bool try_reflink, uint
unlink(fn3);
}
static void test_copy_atomic(void) {
_cleanup_(rm_rf_physical_and_freep) char *p = NULL;
const char *q;
int r;
assert_se(mkdtemp_malloc(NULL, &p) >= 0);
q = strjoina(p, "/fstab");
r = copy_file_atomic("/etc/fstab", q, 0644, 0, COPY_REFLINK);
if (r == -ENOENT)
return;
assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, COPY_REFLINK) == -EEXIST);
assert_se(copy_file_atomic("/etc/fstab", q, 0644, 0, COPY_REPLACE) >= 0);
}
int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG);
test_copy_file();
test_copy_file_fd();
test_copy_tree();
@@ -251,6 +271,7 @@ int main(int argc, char *argv[]) {
test_copy_bytes_regular_file(argv[0], true, 1000);
test_copy_bytes_regular_file(argv[0], false, 32000); /* larger than copy buffer size */
test_copy_bytes_regular_file(argv[0], true, 32000);
test_copy_atomic();
return 0;
}