From 6ab6679de598f43a33c955b7debe10638a090aa0 Mon Sep 17 00:00:00 2001 From: zombocoder Date: Sat, 24 Oct 2026 16:06:29 +0300 Subject: [PATCH] fix(extract): improve symlink handling to replace existing entries atomically --- src/cli/cmd_extract.c | 74 +++++++++++++++++++++++++++++++------------ tests/cli/test_cli.sh | 35 ++++++++++++++++++++ 2 files changed, 89 insertions(+), 20 deletions(-) diff --git a/src/cli/cmd_extract.c b/src/cli/cmd_extract.c index ed3078d..0e33f92 100644 --- a/src/cli/cmd_extract.c +++ b/src/cli/cmd_extract.c @@ -370,24 +370,48 @@ static int extract_directory(const char* output_path, const bfc_entry_t* entry, #endif } -static int extract_symlink(bfc_t* reader, const bfc_entry_t* entry, const char* output_path, - int force) { - // Check if file exists - struct stat st; - if (lstat(output_path, &st) == 0) { - if (!S_ISLNK(st.st_mode)) { - if (!force) { - print_error("'%s' exists but is not a symlink. Use -f to overwrite.", output_path); - return -1; +#ifndef _WIN32 +// Put a symlink in place of whatever is at output_path, without a separate +// check of what is there: create it under a temporary name next to it and +// rename() it over. rename() replaces a file or symlink atomically, does not +// follow symlinks, and fails on a directory, so a swap between steps cannot +// redirect the write. +static int replace_with_symlink(const char* target, const char* output_path) { + char tmp[1100]; + for (unsigned attempt = 0; attempt < 100; attempt++) { + int n = snprintf(tmp, sizeof(tmp), "%s.bfc-tmp.%ld.%u", output_path, (long) getpid(), attempt); + if (n < 0 || (size_t) n >= sizeof(tmp)) { + errno = ENAMETOOLONG; + return -1; + } + if (symlink(target, tmp) == 0) { + if (rename(tmp, output_path) == 0) { + return 0; } + int saved = errno; + unlink(tmp); + errno = saved; + return -1; } - // Remove existing file/symlink - if (unlink(output_path) != 0) { - print_error("Cannot remove '%s': %s", output_path, strerror(errno)); + if (errno != EEXIST) { return -1; } } + errno = EEXIST; + return -1; +} +#endif +static int extract_symlink(bfc_t* reader, const bfc_entry_t* entry, const char* output_path, + int force) { +#ifdef _WIN32 + // Nothing to create, so leave whatever is at output_path untouched + (void) reader; + (void) output_path; + (void) force; + print_verbose("Warning: symlinks are not supported on Windows, skipping '%s'", entry->path); + return 0; +#else // Read symlink target from container char* target = malloc(entry->size + 1); if (!target) { @@ -403,12 +427,24 @@ static int extract_symlink(bfc_t* reader, const bfc_entry_t* entry, const char* } target[entry->size] = '\0'; -// Create symlink -#ifndef _WIN32 + // Create the symlink; only if something is already there, and -f was given, + // replace it. Like regular files, existing entries need -f. if (symlink(target, output_path) != 0) { - print_error("Cannot create symlink '%s' -> '%s': %s", output_path, target, strerror(errno)); - free(target); - return -1; + if (errno != EEXIST) { + print_error("Cannot create symlink '%s' -> '%s': %s", output_path, target, strerror(errno)); + free(target); + return -1; + } + if (!force) { + print_error("'%s' already exists. Use -f to overwrite.", output_path); + free(target); + return -1; + } + if (replace_with_symlink(target, output_path) != 0) { + print_error("Cannot replace '%s' with a symlink: %s", output_path, strerror(errno)); + free(target); + return -1; + } } // Set timestamps using lutimes (for symlinks) @@ -423,9 +459,6 @@ static int extract_symlink(bfc_t* reader, const bfc_entry_t* entry, const char* print_verbose("Warning: cannot set timestamps on symlink '%s': %s", output_path, strerror(errno)); } -#else - print_verbose("Warning: symlinks are not supported on Windows, skipping '%s'", entry->path); -#endif if (!g_options.quiet) { printf("Extracted: %s -> %s\n", output_path, target); @@ -433,6 +466,7 @@ static int extract_symlink(bfc_t* reader, const bfc_entry_t* entry, const char* free(target); return 0; +#endif } // Refuse entry paths that could land outside the output directory. The writer diff --git a/tests/cli/test_cli.sh b/tests/cli/test_cli.sh index df85412..c15f9e9 100644 --- a/tests/cli/test_cli.sh +++ b/tests/cli/test_cli.sh @@ -120,6 +120,41 @@ check_fails "extract fails on an entry below a symlink" test $? -eq 0 check " the refusal names the symlink" grep -q "symlink" "$T/err" check_fails " nothing is written through the symlink" test -e outside/file.txt +# --- symlinks over existing entries ---------------------------------------- + +mkdir -p l/tree +echo target >l/tree/target.txt +ln -s target.txt l/tree/link +"$BFC" create l.bfc l >/dev/null + +mkdir -p s1/l/tree +ln -s elsewhere s1/l/tree/link +check_fails "an existing symlink is not replaced without -f" \ + "$BFC" extract -C s1 l.bfc +check " the existing symlink is left alone" test "$(readlink s1/l/tree/link)" = elsewhere + +mkdir -p s2/l/tree +ln -s elsewhere s2/l/tree/link +check "an existing symlink is replaced with -f" "$BFC" extract -f -C s2 l.bfc +check " it now points at the archived target" test "$(readlink s2/l/tree/link)" = target.txt + +mkdir -p s3/l/tree +echo regular >s3/l/tree/link +check_fails "an existing regular file is not replaced by a symlink without -f" \ + "$BFC" extract -C s3 l.bfc +check " the file is left alone" grep -qx regular s3/l/tree/link +check "an existing regular file is replaced by the symlink with -f" \ + "$BFC" extract -f -C s3 l.bfc +check " it is a symlink now" test -L s3/l/tree/link + +mkdir -p s4/l/tree/link +echo keep >s4/l/tree/link/inside.txt +check_fails "a non-empty directory is never replaced by a symlink, even with -f" \ + "$BFC" extract -f -C s4 l.bfc +check " the directory and its contents are intact" grep -qx keep s4/l/tree/link/inside.txt +leftover=$(find s1 s2 s3 s4 -name '*.bfc-tmp*' | wc -l | tr -d ' ') +check "no temporary symlinks are left behind" test "$leftover" = 0 + # --- #17: file modes and umask --------------------------------------------- # Permission bits of a path as octal, on both GNU and BSD stat