Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 54 additions & 20 deletions src/cli/cmd_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -423,16 +459,14 @@ 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);
}

free(target);
return 0;
#endif
}

// Refuse entry paths that could land outside the output directory. The writer
Expand Down
35 changes: 35 additions & 0 deletions tests/cli/test_cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading