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
49 changes: 29 additions & 20 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ jobs:
cd extract_test
../build/bin/bfc extract ../test.bfc

# Verify extracted files
[ -f hello.txt ] && echo "hello.txt extracted"
[ -f bye.txt ] && echo "bye.txt extracted"
[ -f subdir/nested.txt ] && echo "nested.txt extracted"
# Verify extracted files (directory paths are preserved)
[ -f test_data/hello.txt ] && echo "hello.txt extracted"
[ -f test_data/bye.txt ] && echo "bye.txt extracted"
[ -f test_data/subdir/nested.txt ] && echo "nested.txt extracted"

cd ..
rm -rf extract_test
Expand Down Expand Up @@ -153,12 +153,13 @@ jobs:
./build/bin/bfc info test_symlinks.bfc | grep "Symlinks:" && echo "Symlinks counted in container info" || echo "Symlink counting failed"

# Test symlink-specific info
./build/bin/bfc info test_symlinks.bfc simple_link.txt | grep "Type: Symlink" && echo "Symlink type displayed correctly" || echo "Symlink type display failed"
./build/bin/bfc info test_symlinks.bfc test_symlinks/simple_link.txt | grep "Type: Symlink" && echo "Symlink type displayed correctly" || echo "Symlink type display failed"

# Test symlink extraction
mkdir -p extract_symlinks
cd extract_symlinks
../build/bin/bfc extract ../test_symlinks.bfc
cd test_symlinks

# Verify symlinks were extracted correctly
[ -L simple_link.txt ] && echo "Simple symlink extracted as symlink" || echo "Simple symlink not extracted as symlink"
Expand All @@ -175,11 +176,11 @@ jobs:
[ "$TARGET" = "/tmp/absolute_target" ] && echo "Absolute symlink target correct" || echo "Absolute symlink target incorrect: $TARGET"
fi

cd ..
cd ../..
rm -rf extract_symlinks

# Test symlinks with compression and encryption
./build/bin/bfc create -c zstd -e testpass symlinks_comp_enc.bfc test_symlinks/simple_link.txt test_symlinks/absolute_link.txt
./build/bin/bfc create -c zstd -p testpass symlinks_comp_enc.bfc test_symlinks/simple_link.txt test_symlinks/absolute_link.txt
echo "Symlinks work with compression and encryption"

mkdir -p extract_comp_enc
Expand All @@ -194,7 +195,7 @@ jobs:

# Test encryption functionality
echo "Testing encryption features..."
./build/bin/bfc create -e testpassword123 test_encrypted.bfc test_data/
./build/bin/bfc create -p testpassword123 test_encrypted.bfc test_data/
./build/bin/bfc info test_encrypted.bfc
./build/bin/bfc info test_encrypted.bfc test_data/hello.txt | grep -i encrypt

Expand All @@ -204,9 +205,9 @@ jobs:
../build/bin/bfc extract -p testpassword123 ../test_encrypted.bfc

# Verify extracted files from encrypted container
[ -f hello.txt ] && echo "hello.txt extracted from encrypted container"
[ -f bye.txt ] && echo "bye.txt extracted from encrypted container"
[ -f subdir/nested.txt ] && echo "nested.txt extracted from encrypted container"
[ -f test_data/hello.txt ] && echo "hello.txt extracted from encrypted container"
[ -f test_data/bye.txt ] && echo "bye.txt extracted from encrypted container"
[ -f test_data/subdir/nested.txt ] && echo "nested.txt extracted from encrypted container"

cd ..
rm -rf extract_encrypted
Expand All @@ -220,19 +221,19 @@ jobs:

# Test key file encryption
echo -n "0123456789abcdef0123456789abcdef" > test.key
./build/bin/bfc create -k test.key test_keyfile.bfc test_data/
./build/bin/bfc create -K test.key test_keyfile.bfc test_data/
./build/bin/bfc info test_keyfile.bfc | grep -i encrypt

mkdir -p extract_keyfile
cd extract_keyfile
../build/bin/bfc extract -K ../test.key ../test_keyfile.bfc
[ -f hello.txt ] && echo "hello.txt extracted with key file"
[ -f test_data/hello.txt ] && echo "hello.txt extracted with key file"
cd ..
rm -rf extract_keyfile

# Test encryption with compression
echo "This is a larger test file with enough content to be compressed by zstd compression algorithm." > test_data/large.txt
./build/bin/bfc create -e testpass -c zstd test_enc_comp.bfc test_data/
./build/bin/bfc create -p testpass -c zstd test_enc_comp.bfc test_data/
./build/bin/bfc info test_enc_comp.bfc test_data/large.txt | grep -i encrypt
./build/bin/bfc info test_enc_comp.bfc test_data/large.txt | grep -i zstd

Expand Down Expand Up @@ -264,15 +265,15 @@ jobs:

mkdir extract_test && cd extract_test
"$OLDPWD/$BFC" extract ../test.bfc
[ -f hello.txt ] && echo "hello.txt extracted"
[ -f bye.txt ] && echo "bye.txt extracted"
[ -f subdir/nested.txt ] && echo "nested.txt extracted"
[ -f test_data/hello.txt ] && echo "hello.txt extracted"
[ -f test_data/bye.txt ] && echo "bye.txt extracted"
[ -f test_data/subdir/nested.txt ] && echo "nested.txt extracted"
cd ..

"$BFC" create -e testpassword123 test_encrypted.bfc test_data/
"$BFC" create -p testpassword123 test_encrypted.bfc test_data/
mkdir extract_enc && cd extract_enc
"$OLDPWD/$BFC" extract -p testpassword123 ../test_encrypted.bfc
[ -f hello.txt ] && echo "hello.txt extracted from encrypted container"
[ -f test_data/hello.txt ] && echo "hello.txt extracted from encrypted container"
cd ..

mkdir extract_fail && cd extract_fail
Expand Down Expand Up @@ -353,14 +354,22 @@ jobs:
lcov --capture --directory . --output-file coverage.info \
--rc branch_coverage=1 \
--ignore-errors deprecated,unsupported,unused
# Thresholds apply to the library. The CLI only shows up in coverage
# since tests/cli runs the bfc binary; it is reported separately
# below and does not count towards the thresholds.
lcov --remove coverage.info \
'*/tests/*' \
'*/tests/*' '*/src/cli/*' \
--output-file coverage_filtered.info \
--rc branch_coverage=1 \
--ignore-errors deprecated,unsupported,unused
lcov --list coverage_filtered.info \
--rc branch_coverage=1 \
--ignore-errors deprecated,unsupported,unused
echo "CLI coverage (informational, not checked):"
lcov --extract coverage.info '*/src/cli/*' \
--output-file coverage_cli.info \
--rc branch_coverage=1 \
--ignore-errors deprecated,unsupported,unused,empty || true

- name: Check coverage thresholds
run: |
Expand Down
68 changes: 44 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,10 @@ The `create` command builds new BFC containers from files and directories.
- `-c, --compression TYPE` - Compression: `none`, `zstd`, `auto` (default: none)
- `-l, --compression-level N` - Compression level for ZSTD (1-22, default: 3)
- `-t, --compression-threshold SIZE` - Minimum file size to compress (default: 64 bytes)
- `-e, --encrypt PASSWORD` - Encrypt files with password (requires libsodium)
- `-k, --keyfile FILE` - Encrypt with 32-byte key from file (requires libsodium)
- `-p, --password PASS` - Encrypt files with password (requires libsodium)
- `--password-file FILE` - Read the password from FILE, `-` for stdin (keeps it out of `ps` and shell history)
- `-K, --keyfile FILE` - Encrypt with 32-byte key from file (requires libsodium)
- `-e, --encrypt` and `-k` are deprecated spellings of `-p` and `-K`

**Examples:**
```bash
Expand All @@ -184,11 +186,12 @@ bfc create -c zstd archive.bfc /data/
bfc create -c zstd -l 9 archive.bfc /data/ # Maximum compression

# Create with encryption
bfc create -e mypassword secure.bfc /sensitive/data/
bfc create -k secret.key secure.bfc /sensitive/data/
bfc create -p mypassword secure.bfc /sensitive/data/
bfc create --password-file pass.txt secure.bfc /sensitive/data/
bfc create -K secret.key secure.bfc /sensitive/data/

# Combined compression and encryption
bfc create -c zstd -e secret -l 6 archive.bfc /data/
bfc create -c zstd -p secret -l 6 archive.bfc /data/
```

### Compression support
Expand Down Expand Up @@ -231,18 +234,22 @@ BFC supports optional file encryption using industry-standard ChaCha20-Poly1305

```bash
# Password-based encryption
bfc create -e mypassword secure.bfc /sensitive/data/
bfc create -p mypassword secure.bfc /sensitive/data/

# Key file encryption (32 bytes)
echo -n "0123456789abcdef0123456789abcdef" > secret.key
bfc create -k secret.key secure.bfc /sensitive/data/
bfc create -K secret.key secure.bfc /sensitive/data/

# Combine encryption with compression
bfc create -e mypassword -c zstd archive.bfc /data/
bfc create -p mypassword -c zstd archive.bfc /data/

# Extract encrypted container with password
bfc extract -p mypassword secure.bfc

# Without -p, --password-file or -K, extract asks for the password on the
# terminal (only when stdin is a terminal)
bfc extract secure.bfc

# Extract with key file
bfc extract -K secret.key secure.bfc

Expand Down Expand Up @@ -310,20 +317,24 @@ The `extract` command extracts files and directories from containers.
**Options:**
- `-C, --directory DIR` - Extract to specific directory (changes to DIR before extracting)
- `-f, --force` - Overwrite existing files without prompting
- `-k, --keep-paths` - Preserve full directory structure (default: flatten to basenames)
- `--flatten` - Drop directories and extract every file by its name only. Fails if two entries have the same name, even with `-f`
- `-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)
- `-k, --keep-paths` - Deprecated, no effect: directory paths are preserved by default

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.

**Examples:**
```bash
# Extract all files to current directory (flattened)
# Extract all files to the current directory, keeping directory paths
bfc extract archive.bfc

# Extract to specific directory
bfc extract -C /tmp/extracted archive.bfc

# Extract preserving directory structure
bfc extract -k archive.bfc
# Extract every file into one directory, without subdirectories
bfc extract --flatten archive.bfc

# Extract specific files/directories only
bfc extract archive.bfc docs/ README.txt
Expand All @@ -337,8 +348,8 @@ bfc extract -p mypassword secure.bfc
# Extract encrypted container with key file
bfc extract -K secret.key secure.bfc

# Combined: extract to directory, preserve paths, force overwrite
bfc extract -kf -C /tmp/output archive.bfc
# Combined: extract to directory, force overwrite
bfc extract -f -C /tmp/output archive.bfc
```

### Container information
Expand Down Expand Up @@ -373,7 +384,8 @@ The `verify` command checks container and file integrity.

**Options:**
- `--deep` - Perform deep verification (read and verify all file contents, slower but thorough)
- `-p, --progress` - Show progress bar during verification (useful for large containers)
- `--progress` - Show progress during verification (useful for large containers)
- `-p, --password PASS`, `--password-file FILE`, `-K, --keyfile FILE` - Credentials for encrypted containers, needed for `--deep`

**Examples:**
```bash
Expand All @@ -384,7 +396,10 @@ bfc verify archive.bfc
bfc verify --deep archive.bfc

# Deep verification with progress indicator
bfc verify -p --deep archive.bfc
bfc verify --progress --deep archive.bfc

# Deep verification of an encrypted container
bfc verify --deep -p mypassword secure.bfc
```

## Library Usage
Expand Down Expand Up @@ -473,8 +488,9 @@ See the [examples/](examples/) directory for complete examples.
-c, --compression TYPE none|zstd|auto (default: none)
-l, --compression-level N ZSTD level 1-22 (default: 3)
-t, --compression-threshold SIZE Min size to compress (default: 64)
-e, --encrypt PASSWORD Encrypt with password
-k, --keyfile FILE Encrypt with key file (32 bytes)
-p, --password PASS Encrypt with password
--password-file FILE Read the password from FILE (- for stdin)
-K, --keyfile FILE Encrypt with key file (32 bytes)
```

**`bfc list [options] <container.bfc> [path]`**
Expand All @@ -488,8 +504,9 @@ See the [examples/](examples/) directory for complete examples.
```
-C, --directory DIR Extract to specific directory
-f, --force Overwrite existing files
-k, --keep-paths Preserve directory structure
--flatten Drop directories, extract files by name only
-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
```

Expand All @@ -501,23 +518,26 @@ See the [examples/](examples/) directory for complete examples.
**`bfc verify [options] <container.bfc>`**
```
--deep Deep verification (check all content)
-p, --progress Show progress indicator
--progress Show progress indicator
-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
```

### Common Usage Patterns

```bash
# Create compressed encrypted archive
bfc create -c zstd -e password archive.bfc /data/
bfc create -c zstd -p password archive.bfc /data/

# List with full details
bfc list -lsc archive.bfc

# Extract preserving structure to specific location
bfc extract -k -C /tmp/restored archive.bfc
# Extract to a specific location
bfc extract -C /tmp/restored archive.bfc

# Verify with progress
bfc verify -p --deep archive.bfc
bfc verify --progress --deep archive.bfc

# Get detailed info about specific file
bfc info -d archive.bfc path/to/file.txt
Expand Down
1 change: 1 addition & 0 deletions src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(BFC_CLI_SOURCES
cmd_extract.c
cmd_verify.c
cli_util.c
cli_credentials.c
)

add_executable(bfc_cli ${BFC_CLI_SOURCES})
Expand Down
21 changes: 21 additions & 0 deletions src/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ int parse_global_options(int* argc, char*** argv, bfc_options_t* opts);
const char* bfc_error_string(int error_code);
int read_key_from_file(const char* keyfile, uint8_t key[32]);

// Password and key-file options, shared by create, extract and verify
typedef struct {
const char* password; // -p, --password
const char* password_file; // --password-file ("-" for stdin)
const char* keyfile; // -K, --keyfile
char* owned_password; // read from file or prompt; wiped on free
} cli_credentials_t;

void cli_credentials_init(cli_credentials_t* c);
void cli_credentials_free(cli_credentials_t* c);
// Returns 1 if argv[*i] was a credential option (and consumes its value),
// 0 if it was not, -1 on error.
int cli_parse_credential_option(int argc, char* argv[], int* i, cli_credentials_t* c);
int cli_credentials_given(const cli_credentials_t* c);
int cli_credentials_check(const cli_credentials_t* c);
// prompt_if_needed: ask on the terminal when the container is encrypted and
// no credentials were given (only when stdin is a terminal).
// Both return 1 if a password or key was set, 0 if none was given, -1 on error.
int cli_apply_reader_credentials(bfc_t* reader, cli_credentials_t* c, int prompt_if_needed);
int cli_apply_writer_credentials(bfc_t* writer, cli_credentials_t* c);

// Global options (extern declaration)
extern bfc_options_t g_options;

Expand Down
Loading
Loading