From dc745d1d49d968166c37eb7f88fc7f692cc26be1 Mon Sep 17 00:00:00 2001 From: Kamil Monicz Date: Sun, 22 Nov 2026 20:48:46 +0100 Subject: [PATCH] Prevent nob_copy_file from truncating its source Open the destination before comparing device and inode so hard links and symlinks are handled like identical paths. Truncate only a distinct regular file. --- nob.c | 1 + nob.h | 17 ++++++++++++++++- tests/copy_alias.c | 19 +++++++++++++++++++ tests/copy_alias.stdout.txt | 1 + tests/copy_alias.win32.stdout.txt | 1 + 5 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/copy_alias.c create mode 100644 tests/copy_alias.stdout.txt create mode 100644 tests/copy_alias.win32.stdout.txt diff --git a/nob.c b/nob.c index a2e3bb4..0c308ad 100644 --- a/nob.c +++ b/nob.c @@ -5,6 +5,7 @@ const char *test_names[] = { "cmd_redirect", "cmd_args_passing", "read_entire_dir", + "copy_alias", "da_resize", "da_last", "da_remove_unordered", diff --git a/nob.h b/nob.h index 2ec5bcc..a00bf17 100644 --- a/nob.h +++ b/nob.h @@ -1238,12 +1238,27 @@ NOBDEF bool nob_copy_file(const char *src_path, const char *dst_path) nob_return_defer(false); } - dst_fd = open(dst_path, O_CREAT | O_TRUNC | O_WRONLY, src_stat.st_mode); + dst_fd = open(dst_path, O_CREAT | O_WRONLY, src_stat.st_mode); if (dst_fd < 0) { nob_log(NOB_ERROR, "Could not create file %s: %s", dst_path, strerror(errno)); nob_return_defer(false); } + // Distinct paths (including hard links) may name the same file. + struct stat dst_stat; + if (fstat(dst_fd, &dst_stat) < 0) { + nob_log(NOB_ERROR, "Could not stat destination %s: %s", dst_path, strerror(errno)); + nob_return_defer(false); + } + if (src_stat.st_dev == dst_stat.st_dev && src_stat.st_ino == dst_stat.st_ino) { + nob_log(NOB_ERROR, "%s and %s are the same file", src_path, dst_path); + nob_return_defer(false); + } + if (S_ISREG(dst_stat.st_mode) && ftruncate(dst_fd, 0) < 0) { + nob_log(NOB_ERROR, "Could not truncate %s: %s", dst_path, strerror(errno)); + nob_return_defer(false); + } + for (;;) { ssize_t n = read(src_fd, buf, buf_size); if (n == 0) break; diff --git a/tests/copy_alias.c b/tests/copy_alias.c new file mode 100644 index 0000000..3f771fb --- /dev/null +++ b/tests/copy_alias.c @@ -0,0 +1,19 @@ +#include "shared.h" + +int main(void) +{ + const char bytes[] = {'a', '\0', 'b'}; + if (!write_entire_file("source", bytes, sizeof(bytes))) return 1; +#ifndef _WIN32 + const char *alias = link("source", "alias") == 0 ? "alias" : "./source"; + if (copy_file("source", alias)) return 1; +#endif + if (!write_entire_file("dest", "longer", 6)) return 1; + if (!copy_file("source", "dest")) return 1; + String_Builder sb = {0}; + if (!read_entire_file("dest", &sb)) return 1; + if (sb.count != sizeof(bytes) || memcmp(sb.items, bytes, sb.count) != 0) return 1; + printf("Copied %zu bytes\n", sb.count); + sb_free(sb); + return 0; +} diff --git a/tests/copy_alias.stdout.txt b/tests/copy_alias.stdout.txt new file mode 100644 index 0000000..fdcd576 --- /dev/null +++ b/tests/copy_alias.stdout.txt @@ -0,0 +1 @@ +Copied 3 bytes diff --git a/tests/copy_alias.win32.stdout.txt b/tests/copy_alias.win32.stdout.txt new file mode 100644 index 0000000..20eafa9 --- /dev/null +++ b/tests/copy_alias.win32.stdout.txt @@ -0,0 +1 @@ +Copied 3 bytes