From 471a8fb5db0127156fbc9bbad7acd539f07715f3 Mon Sep 17 00:00:00 2001 From: zombocoder Date: Sat, 24 Oct 2026 14:53:08 +0300 Subject: [PATCH 1/2] feat(extract): add --preserve-permissions option to maintain original file modes --- .github/workflows/ci.yml | 8 ++++++ README.md | 6 +++++ src/cli/cmd_create.c | 21 +++++++++++++-- src/cli/cmd_extract.c | 57 ++++++++++++++++++++++++++++++++-------- tests/cli/test_cli.sh | 51 +++++++++++++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d4e1f86..4cf1bc8 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -255,6 +255,14 @@ jobs: "$BFC" create test.bfc test_data/ "$BFC" list test.bfc + + # Windows has no POSIX permissions; create must record 0644/0755, + # not the CRT's emulated 0666/0777 (#17) + "$BFC" list -l test.bfc + "$BFC" list -l test.bfc | grep -q -- '^-rw-r--r-- .*test_data/hello.txt' || + { echo "ERROR: hello.txt not recorded as 0644"; exit 1; } + "$BFC" list -l test.bfc | grep -q -- '^drwxr-xr-x .*test_data/subdir' || + { echo "ERROR: subdir not recorded as 0755"; exit 1; } "$BFC" info test.bfc "$BFC" verify test.bfc "$BFC" verify --deep test.bfc diff --git a/README.md b/README.md index 541f792..de126b5 100644 --- a/README.md +++ b/README.md @@ -318,6 +318,7 @@ The `extract` command extracts files and directories from containers. - `-C, --directory DIR` - Extract to specific directory (changes to DIR before extracting) - `-f, --force` - Overwrite existing files without prompting - `--flatten` - Drop directories and extract every file by its name only. Fails if two entries have the same name, even with `-f` +- `--preserve-permissions` - Use the exact permission bits from the container instead of applying the umask, also for directories that already exist - `-p, --password PASS` - Provide password for encrypted containers - `--password-file FILE` - Read the password from FILE (`-` for stdin) - `-K, --keyfile FILE` - Use key file for encrypted containers (32 bytes) @@ -325,6 +326,10 @@ The `extract` command extracts files and directories from containers. Directory paths are recreated under the output directory. Entries with absolute paths or `..` components, and entries that would be written through a symlink, are refused. +File and new directory permissions are the container's permissions with the umask applied, as `tar` does for ordinary users. A file archived as `0666` extracts as `0644` with the usual umask `022`. Directories that already exist keep their permissions. Setuid, setgid and sticky bits are never restored. Use `--preserve-permissions` to get the archived permissions exactly. + +Windows has no POSIX permissions. `bfc create` on Windows records `0644` for files and `0755` for directories and executables, without the write bits for read-only files. + **Examples:** ```bash # Extract all files to the current directory, keeping directory paths @@ -505,6 +510,7 @@ See the [examples/](examples/) directory for complete examples. -C, --directory DIR Extract to specific directory -f, --force Overwrite existing files --flatten Drop directories, extract files by name only + --preserve-permissions Exact archived modes instead of applying the umask -p, --password PASS Password for encrypted containers --password-file FILE Read the password from FILE (- for stdin) -K, --keyfile FILE Key file for encrypted containers diff --git a/src/cli/cmd_create.c b/src/cli/cmd_create.c index 55801d2..f0b92d2 100644 --- a/src/cli/cmd_create.c +++ b/src/cli/cmd_create.c @@ -180,6 +180,23 @@ static int parse_create_options(int argc, char* argv[], create_options_t* opts) return 0; } +// Permission bits to record for a file or directory. On Windows, stat() has +// no POSIX permissions: the CRT reports 0666 or 0777 (or 0444/0555 for +// read-only files), so record the conventional 0644/0755 instead, keeping the +// executable bit the CRT derives from the file type and the read-only +// attribute. +static uint32_t archive_mode(const struct stat* st) { +#ifdef _WIN32 + uint32_t mode = (S_ISDIR(st->st_mode) || (st->st_mode & S_IXUSR)) ? 0755 : 0644; + if (!(st->st_mode & S_IWUSR)) { + mode &= ~0222u; + } + return mode; +#else + return st->st_mode & 0777; +#endif +} + static int add_file_to_container(bfc_t* writer, const char* file_path, const char* container_path) { print_verbose("Adding file: %s -> %s", file_path, container_path); @@ -200,7 +217,7 @@ static int add_file_to_container(bfc_t* writer, const char* file_path, const cha // Add file to container uint64_t mtime_ns = (uint64_t) st.st_mtime * 1000000000ULL; uint32_t crc; - int result = bfc_add_file(writer, container_path, file, st.st_mode & 0777, mtime_ns, &crc); + int result = bfc_add_file(writer, container_path, file, archive_mode(&st), mtime_ns, &crc); fclose(file); @@ -306,7 +323,7 @@ static int add_directory_to_container(bfc_t* writer, const char* dir_path, // Add directory to container uint64_t mtime_ns = (uint64_t) st.st_mtime * 1000000000ULL; - int result = bfc_add_dir(writer, container_path, st.st_mode & 0777, mtime_ns); + int result = bfc_add_dir(writer, container_path, archive_mode(&st), mtime_ns); if (result != BFC_OK) { print_error("Failed to add directory '%s': %s", container_path, bfc_error_string(result)); diff --git a/src/cli/cmd_extract.c b/src/cli/cmd_extract.c index e0b16c0..df5eda4 100644 --- a/src/cli/cmd_extract.c +++ b/src/cli/cmd_extract.c @@ -31,6 +31,7 @@ typedef struct { int force; int flatten; + int preserve_perms; const char* output_dir; const char* container_file; const char** extract_paths; @@ -45,6 +46,8 @@ static void print_extract_help(void) { printf(" -C, --directory DIR Change to directory DIR before extracting\n"); printf(" -f, --force Overwrite existing files\n"); printf(" --flatten Drop directories: extract every file by its name only\n"); + printf(" --preserve-permissions Use the exact modes from the container instead\n"); + printf(" of applying the umask, also for existing directories\n"); printf(" -p, --password PASS Password for encrypted container\n"); printf(" --password-file FILE Read the password from FILE (- for stdin)\n"); printf(" -K, --keyfile FILE Key file for encrypted container (32 bytes)\n"); @@ -68,6 +71,7 @@ static int parse_extract_options(int argc, char* argv[], extract_options_t* opts // Initialize options opts->force = 0; opts->flatten = 0; + opts->preserve_perms = 0; opts->output_dir = NULL; opts->container_file = NULL; opts->extract_paths = NULL; @@ -87,6 +91,8 @@ static int parse_extract_options(int argc, char* argv[], extract_options_t* opts "Warning: %s is deprecated and has no effect: paths are preserved by " "default (use -K for a key file)\n", argv[i]); + } else if (strcmp(argv[i], "--preserve-permissions") == 0) { + opts->preserve_perms = 1; } else if (strcmp(argv[i], "--flatten") == 0) { opts->flatten = 1; } else if (strcmp(argv[i], "-C") == 0 || strcmp(argv[i], "--directory") == 0) { @@ -178,8 +184,10 @@ static int create_parent_directories(const char* path, int force) { return 0; } -static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* output_path, - int force) { +// mode is the permission set to apply: the archived mode with the umask taken +// out, or the exact archived mode with --preserve-permissions. +static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* output_path, int force, + uint32_t mode) { // Create parent directories if (create_parent_directories(output_path, force) != 0) { return -1; @@ -194,7 +202,7 @@ static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* out int fd = open(output_path, oflags, _S_IREAD | _S_IWRITE); #else int oflags = O_WRONLY | O_CREAT | O_NOFOLLOW | (force ? O_TRUNC : O_EXCL); - int fd = open(output_path, oflags, entry->mode & 0777); + int fd = open(output_path, oflags, mode); #endif if (fd < 0) { if (!force && errno == EEXIST) { @@ -217,7 +225,10 @@ static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* out // Set file permissions and timestamps using file descriptor to avoid TOCTOU race conditions #ifndef _WIN32 - if (fchmod(fd, entry->mode & 0777) != 0) { + // Needed even for new files: open() already applied the umask, but with + // --preserve-permissions mode is wider than that, and an overwritten file + // (-f) still has its old mode. + if (fchmod(fd, mode) != 0) { print_verbose("Warning: cannot set permissions on '%s': %s", output_path, strerror(errno)); } #endif @@ -244,9 +255,12 @@ static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* out return 0; } -static int extract_directory(const char* output_path, const bfc_entry_t* entry, int force) { +static int extract_directory(const char* output_path, const bfc_entry_t* entry, int force, + uint32_t mode, int preserve_perms) { #ifdef _WIN32 (void) entry; // permissions and timestamps are POSIX-only + (void) mode; + (void) preserve_perms; struct stat st; if (stat(output_path, &st) == 0) { if (!S_ISDIR(st.st_mode)) { @@ -283,8 +297,10 @@ static int extract_directory(const char* output_path, const bfc_entry_t* entry, // POSIX: use open() as the atomic probe — no preceding stat() to avoid TOCTOU. int dirfd = open(output_path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); if (dirfd >= 0) { - // Directory exists — update permissions and timestamps via fd. - if (fchmod(dirfd, entry->mode & 0777) != 0) { + // Directory exists — update timestamps via fd. Its mode is only changed + // with --preserve-permissions, so extracting into an existing tree does + // not loosen it (an archive made on Windows records 0777). + if (preserve_perms && fchmod(dirfd, mode) != 0) { print_verbose("Warning: cannot set permissions on '%s': %s", output_path, strerror(errno)); } struct timespec times[2] = { @@ -322,7 +338,7 @@ static int extract_directory(const char* output_path, const bfc_entry_t* entry, print_verbose("Creating directory: %s", output_path); - if (mkdir(output_path, entry->mode & 0777) != 0) { + if (mkdir(output_path, mode) != 0) { print_error("Cannot create directory '%s': %s", output_path, strerror(errno)); return -1; } @@ -331,6 +347,10 @@ static int extract_directory(const char* output_path, const bfc_entry_t* entry, { int dirfd_new = open(output_path, O_RDONLY | O_DIRECTORY | O_NOFOLLOW); if (dirfd_new >= 0) { + // mkdir() applied the umask; --preserve-permissions wants the exact mode + if (preserve_perms && fchmod(dirfd_new, mode) != 0) { + print_verbose("Warning: cannot set permissions on '%s': %s", output_path, strerror(errno)); + } struct timespec times[2] = { {.tv_sec = entry->mtime_ns / 1000000000ULL, .tv_nsec = entry->mtime_ns % 1000000000ULL}, {.tv_sec = entry->mtime_ns / 1000000000ULL, .tv_nsec = entry->mtime_ns % 1000000000ULL}}; @@ -526,6 +546,7 @@ typedef struct { int count; int errors; name_set_t flat_names; + uint32_t umask; // process umask, applied unless --preserve-permissions } extract_context_t; static int extract_entry_callback(const bfc_entry_t* entry, void* user) { @@ -584,12 +605,21 @@ static int extract_entry_callback(const bfc_entry_t* entry, void* user) { return 0; } + // Permission bits to apply. Setuid, setgid and sticky bits are never + // restored. Without --preserve-permissions the umask is taken out, as tar + // does for ordinary users, so an archive made on Windows (0666/0777) does + // not produce world-writable files. + uint32_t mode = entry->mode & 0777; + if (!opts->preserve_perms) { + mode &= ~ctx->umask; + } + // Extract based on entry type int result; if (S_ISREG(entry->mode)) { - result = extract_file(ctx->reader, entry, output_path, opts->force); + result = extract_file(ctx->reader, entry, output_path, opts->force, mode); } else if (S_ISDIR(entry->mode)) { - result = extract_directory(output_path, entry, opts->force); + result = extract_directory(output_path, entry, opts->force, mode, opts->preserve_perms); } else if (S_ISLNK(entry->mode)) { result = extract_symlink(ctx->reader, entry, output_path, opts->force); } else { @@ -641,7 +671,12 @@ int cmd_extract(int argc, char* argv[]) { } // Extract entries - extract_context_t ctx = {&opts, reader, NULL, 0, 0, {NULL, 0, 0}}; + extract_context_t ctx = {&opts, reader, NULL, 0, 0, {NULL, 0, 0}, 0}; +#ifndef _WIN32 + mode_t old_umask = umask(0); + umask(old_umask); + ctx.umask = (uint32_t) old_umask; +#endif if (opts.num_paths == 0) { // Extract all entries diff --git a/tests/cli/test_cli.sh b/tests/cli/test_cli.sh index baba63c..df85412 100644 --- a/tests/cli/test_cli.sh +++ b/tests/cli/test_cli.sh @@ -120,6 +120,57 @@ 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 +# --- #17: file modes and umask --------------------------------------------- + +# Permission bits of a path as octal, on both GNU and BSD stat +perm() { + stat -c %a "$1" 2>/dev/null || stat -f %Lp "$1" 2>/dev/null +} + +check_perm() { + # check_perm + got=$(perm "$3") + if [ "$got" = "$2" ]; then ok "$1"; else not_ok "$1 (got $got, expected $2)"; fi +} + +mkdir -p m/opendir +echo w >m/world.txt +echo x >m/run.sh +echo r >m/readonly.txt +chmod 666 m/world.txt +chmod 755 m/run.sh +chmod 444 m/readonly.txt +chmod 777 m/opendir +"$BFC" create m.bfc m >/dev/null +chmod 755 m/opendir + +mkdir p1 +(umask 022 && "$BFC" extract -C p1 m.bfc >/dev/null 2>&1) +check_perm "extract applies the umask to a 0666 file" 644 p1/m/world.txt +check_perm " executable bits are kept" 755 p1/m/run.sh +check_perm " a read-only file stays read-only" 444 p1/m/readonly.txt +check_perm " a new 0777 directory gets the umask" 755 p1/m/opendir + +mkdir p2 +(umask 077 && "$BFC" extract -C p2 m.bfc >/dev/null 2>&1) +check_perm "a stricter umask (077) is honoured for files" 600 p2/m/world.txt +check_perm " and for directories" 700 p2/m/opendir + +mkdir -p p3/m/opendir +chmod 750 p3/m/opendir +(umask 022 && "$BFC" extract -f -C p3 m.bfc >/dev/null 2>&1) +check_perm "an existing directory keeps its mode" 750 p3/m/opendir + +mkdir p4 +(umask 022 && "$BFC" extract --preserve-permissions -C p4 m.bfc >/dev/null 2>&1) +check_perm "--preserve-permissions keeps the archived file mode" 666 p4/m/world.txt +check_perm " and the archived directory mode" 777 p4/m/opendir + +mkdir -p p5/m/opendir +chmod 750 p5/m/opendir +(umask 022 && "$BFC" extract -f --preserve-permissions -C p5 m.bfc >/dev/null 2>&1) +check_perm "--preserve-permissions also applies to an existing directory" 777 p5/m/opendir + # --- #19: password and key-file options -------------------------------------- if "$BFC" create -p x probe.bfc src/top.txt 2>&1 | grep -q "Encryption support not available"; then From d25f44be402e048f709e1b3f878ff6f1ebe8afc8 Mon Sep 17 00:00:00 2001 From: zombocoder Date: Sat, 24 Oct 2026 15:01:38 +0300 Subject: [PATCH 2/2] fix(extract): handle mode parameter for Windows in extract_file function --- src/cli/cmd_extract.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cli/cmd_extract.c b/src/cli/cmd_extract.c index df5eda4..ed3078d 100644 --- a/src/cli/cmd_extract.c +++ b/src/cli/cmd_extract.c @@ -198,6 +198,7 @@ static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* out // Open output file atomically: O_EXCL rejects existing files when !force, // O_NOFOLLOW prevents symlink attacks (POSIX). No separate stat() needed. #ifdef _WIN32 + (void) mode; // Windows has no POSIX permissions to apply int oflags = O_WRONLY | O_CREAT | O_BINARY | (force ? O_TRUNC : O_EXCL); int fd = open(output_path, oflags, _S_IREAD | _S_IWRITE); #else