-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_extract.c
More file actions
481 lines (418 loc) · 14.1 KB
/
cmd_extract.c
File metadata and controls
481 lines (418 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
* Copyright 2021 zombocoder (Taras Havryliak)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define _GNU_SOURCE
#include "cli.h"
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#ifdef BFC_WITH_SODIUM
static int read_key_from_file(const char* filename, uint8_t key[32]) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
print_error("Cannot open key file '%s': %s", filename, strerror(errno));
return -1;
}
ssize_t bytes_read = read(fd, key, 32);
close(fd);
if (bytes_read != 32) {
print_error("Key file '%s' must be exactly 32 bytes, got %zd bytes", filename, bytes_read);
return -1;
}
return 0;
}
#endif
typedef struct {
int force;
int preserve_paths;
const char* output_dir;
const char* container_file;
const char** extract_paths;
int num_paths;
const char* encryption_password;
const char* encryption_keyfile;
} extract_options_t;
static void print_extract_help(void) {
printf("Usage: bfc extract [options] <container.bfc> [paths...]\n\n");
printf("Extract files and directories from a BFC container.\n\n");
printf("Options:\n");
printf(" -C, --directory DIR Change to directory DIR before extracting\n");
printf(" -f, --force Overwrite existing files\n");
printf(" -k, --keep-paths Preserve full directory paths when extracting\n");
printf(" -p, --password PASS Password for encrypted container\n");
printf(" -K, --keyfile FILE Key file for encrypted container (32 bytes)\n");
printf(" -h, --help Show this help message\n\n");
printf("Arguments:\n");
printf(" container.bfc BFC container to extract from\n");
printf(" paths Optional paths to extract (default: all)\n\n");
printf("Examples:\n");
printf(" bfc extract archive.bfc # Extract all files\n");
printf(" bfc extract -C /tmp archive.bfc # Extract to /tmp\n");
printf(" bfc extract archive.bfc docs/ # Extract docs/ directory\n");
printf(" bfc extract -k archive.bfc file.txt # Extract preserving path\n");
printf(" bfc extract -p secret archive.bfc # Extract encrypted container\n");
printf(" bfc extract -K key.bin archive.bfc # Extract with key file\n");
}
static int parse_extract_options(int argc, char* argv[], extract_options_t* opts) {
// Initialize options
opts->force = 0;
opts->preserve_paths = 0;
opts->output_dir = NULL;
opts->container_file = NULL;
opts->extract_paths = NULL;
opts->num_paths = 0;
opts->encryption_password = NULL;
opts->encryption_keyfile = NULL;
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
print_extract_help();
return 1;
} else if (strcmp(argv[i], "-f") == 0 || strcmp(argv[i], "--force") == 0) {
opts->force = 1;
} else if (strcmp(argv[i], "-k") == 0 || strcmp(argv[i], "--keep-paths") == 0) {
opts->preserve_paths = 1;
} else if (strcmp(argv[i], "-C") == 0 || strcmp(argv[i], "--directory") == 0) {
if (i + 1 >= argc) {
print_error("--directory requires an argument");
return -1;
}
opts->output_dir = argv[++i];
} else if (strcmp(argv[i], "-p") == 0 || strcmp(argv[i], "--password") == 0) {
if (i + 1 >= argc) {
print_error("--password requires an argument");
return -1;
}
opts->encryption_password = argv[++i];
} else if (strcmp(argv[i], "-K") == 0 || strcmp(argv[i], "--keyfile") == 0) {
if (i + 1 >= argc) {
print_error("--keyfile requires an argument");
return -1;
}
opts->encryption_keyfile = argv[++i];
} else if (argv[i][0] == '-') {
// Handle combined short options like -fk
const char* opt = argv[i] + 1;
while (*opt) {
switch (*opt) {
case 'f':
opts->force = 1;
break;
case 'k':
opts->preserve_paths = 1;
break;
default:
print_error("Unknown option: -%c", *opt);
return -1;
}
opt++;
}
} else {
// First non-option argument is the container file
if (!opts->container_file) {
opts->container_file = argv[i];
} else {
// Remaining arguments are paths to extract
opts->extract_paths = (const char**) (argv + i);
opts->num_paths = argc - i;
break;
}
}
}
if (!opts->container_file) {
print_error("Container file not specified");
return -1;
}
return 0;
}
static int create_parent_directories(const char* path, int force) {
char* path_copy = strdup(path);
if (!path_copy) {
return -1;
}
char* dir = dirname(path_copy);
if (strcmp(dir, ".") == 0 || strcmp(dir, "/") == 0) {
free(path_copy);
return 0;
}
struct stat st;
if (stat(dir, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
print_error("'%s' exists but is not a directory", dir);
free(path_copy);
return -1;
}
free(path_copy);
return 0;
}
// Recursively create parent directories
if (create_parent_directories(dir, force) != 0) {
free(path_copy);
return -1;
}
print_verbose("Creating directory: %s", dir);
if (mkdir(dir, 0755) != 0) {
print_error("Cannot create directory '%s': %s", dir, strerror(errno));
free(path_copy);
return -1;
}
free(path_copy);
return 0;
}
static int extract_file(bfc_t* reader, const bfc_entry_t* entry, const char* output_path,
int force) {
// Check if file exists
struct stat st;
if (stat(output_path, &st) == 0 && !force) {
print_error("File '%s' already exists. Use -f to overwrite.", output_path);
return -1;
}
// Create parent directories
if (create_parent_directories(output_path, force) != 0) {
return -1;
}
print_verbose("Extracting file: %s -> %s", entry->path, output_path);
// Open output file
int fd = open(output_path, O_WRONLY | O_CREAT | O_TRUNC, entry->mode & 0777);
if (fd < 0) {
print_error("Cannot create file '%s': %s", output_path, strerror(errno));
return -1;
}
// Extract file content
int result = bfc_extract_to_fd(reader, entry->path, fd);
if (result != BFC_OK) {
close(fd);
print_error("Failed to extract file '%s': %s", entry->path, bfc_error_string(result));
unlink(output_path); // Clean up partial file
return -1;
}
// Set file permissions and timestamps using file descriptor to avoid TOCTOU race conditions
if (fchmod(fd, entry->mode & 0777) != 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}, // atime = mtime
{.tv_sec = entry->mtime_ns / 1000000000ULL, .tv_nsec = entry->mtime_ns % 1000000000ULL}
// mtime
};
if (futimens(fd, times) != 0) {
print_verbose("Warning: cannot set timestamps on '%s': %s", output_path, strerror(errno));
}
// Close file descriptor after setting metadata
close(fd);
if (!g_options.quiet) {
printf("Extracted: %s\n", output_path);
}
return 0;
}
static int extract_directory(const char* output_path, const bfc_entry_t* entry, int force) {
struct stat st;
if (stat(output_path, &st) == 0) {
if (!S_ISDIR(st.st_mode)) {
if (!force) {
print_error("'%s' exists but is not a directory. Use -f to overwrite.", output_path);
return -1;
}
if (unlink(output_path) != 0) {
print_error("Cannot remove file '%s': %s", output_path, strerror(errno));
return -1;
}
} else {
// Directory already exists, just update permissions and timestamps
if (chmod(output_path, entry->mode & 0777) != 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}, // atime = mtime
{.tv_sec = entry->mtime_ns / 1000000000ULL, .tv_nsec = entry->mtime_ns % 1000000000ULL}
// mtime
};
if (utimensat(AT_FDCWD, output_path, times, 0) != 0) {
print_verbose("Warning: cannot set timestamps on '%s': %s", output_path, strerror(errno));
}
return 0;
}
}
// Create parent directories
if (create_parent_directories(output_path, force) != 0) {
return -1;
}
print_verbose("Creating directory: %s", output_path);
// Create directory
if (mkdir(output_path, entry->mode & 0777) != 0) {
print_error("Cannot create directory '%s': %s", output_path, strerror(errno));
return -1;
}
// Set timestamps
struct timespec times[2] = {
{.tv_sec = entry->mtime_ns / 1000000000ULL,
.tv_nsec = entry->mtime_ns % 1000000000ULL}, // atime = mtime
{.tv_sec = entry->mtime_ns / 1000000000ULL, .tv_nsec = entry->mtime_ns % 1000000000ULL}
// mtime
};
if (utimensat(AT_FDCWD, output_path, times, 0) != 0) {
print_verbose("Warning: cannot set timestamps on '%s': %s", output_path, strerror(errno));
}
if (!g_options.quiet) {
printf("Created: %s/\n", output_path);
}
return 0;
}
// Extract callback structure
typedef struct {
extract_options_t* opts;
bfc_t* reader;
const char* output_dir;
int count;
int errors;
} extract_context_t;
static int extract_entry_callback(const bfc_entry_t* entry, void* user) {
extract_context_t* ctx = (extract_context_t*) user;
extract_options_t* opts = ctx->opts;
ctx->count++;
// Determine output path
char output_path[1024];
const char* extract_name;
if (opts->preserve_paths) {
extract_name = entry->path;
} else {
// Use basename only
extract_name = strrchr(entry->path, '/');
extract_name = extract_name ? extract_name + 1 : entry->path;
// Skip if basename is empty (root directory)
if (strlen(extract_name) == 0) {
return 0;
}
}
if (ctx->output_dir) {
snprintf(output_path, sizeof(output_path), "%s/%s", ctx->output_dir, extract_name);
} else {
snprintf(output_path, sizeof(output_path), "%s", extract_name);
}
// Extract based on entry type
int result;
if (S_ISREG(entry->mode)) {
result = extract_file(ctx->reader, entry, output_path, opts->force);
} else if (S_ISDIR(entry->mode)) {
result = extract_directory(output_path, entry, opts->force);
} else {
print_verbose("Skipping special file: %s", entry->path);
return 0;
}
if (result != 0) {
ctx->errors++;
}
return 0;
}
int cmd_extract(int argc, char* argv[]) {
extract_options_t opts;
int result = parse_extract_options(argc, argv, &opts);
if (result != 0) {
return (result > 0) ? 0 : 1;
}
// Open container for reading BEFORE changing directories
print_verbose("Opening container: %s", opts.container_file);
bfc_t* reader = NULL;
result = bfc_open(opts.container_file, &reader);
if (result != BFC_OK) {
print_error("Failed to open container '%s': %s", opts.container_file, bfc_error_string(result));
return 1;
}
// Change to output directory if specified (after opening container)
if (opts.output_dir) {
print_verbose("Changing to directory: %s", opts.output_dir);
if (chdir(opts.output_dir) != 0) {
print_error("Cannot change to directory '%s': %s", opts.output_dir, strerror(errno));
bfc_close_read(reader);
return 1;
}
}
// Configure encryption if needed
#ifdef BFC_WITH_SODIUM
if (opts.encryption_password) {
result = bfc_reader_set_encryption_password(reader, opts.encryption_password,
strlen(opts.encryption_password));
if (result != BFC_OK) {
print_error("Failed to set encryption password: %s", bfc_error_string(result));
bfc_close_read(reader);
return 1;
}
} else if (opts.encryption_keyfile) {
uint8_t key[32];
if (read_key_from_file(opts.encryption_keyfile, key) != 0) {
bfc_close_read(reader);
return 1;
}
result = bfc_reader_set_encryption_key(reader, key);
if (result != BFC_OK) {
print_error("Failed to set encryption key: %s", bfc_error_string(result));
bfc_close_read(reader);
return 1;
}
// Clear key from memory
memset(key, 0, sizeof(key));
}
#else
if (opts.encryption_password || opts.encryption_keyfile) {
print_error("Encryption support not available. Please build with BFC_WITH_SODIUM=ON");
bfc_close_read(reader);
return 1;
}
#endif
// Extract entries
extract_context_t ctx = {&opts, reader, NULL, 0, 0};
if (opts.num_paths == 0) {
// Extract all entries
print_verbose("Extracting all entries");
result = bfc_list(reader, NULL, extract_entry_callback, &ctx);
} else {
// Extract specific paths
for (int i = 0; i < opts.num_paths; i++) {
print_verbose("Extracting entries matching: %s", opts.extract_paths[i]);
result = bfc_list(reader, opts.extract_paths[i], extract_entry_callback, &ctx);
if (result != BFC_OK) {
break;
}
}
}
if (result != BFC_OK) {
print_error("Failed to list container contents: %s", bfc_error_string(result));
bfc_close_read(reader);
return 1;
}
bfc_close_read(reader);
if (ctx.count == 0 && !g_options.quiet) {
if (opts.num_paths > 0) {
printf("No entries found matching specified paths\n");
} else {
printf("Container is empty\n");
}
} else if (!g_options.quiet) {
if (ctx.errors > 0) {
printf("Extracted %d entries with %d errors\n", ctx.count, ctx.errors);
} else {
printf("Successfully extracted %d entries\n", ctx.count);
}
}
return (ctx.errors > 0) ? 1 : 0;
}