-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcmd_list.c
More file actions
263 lines (235 loc) · 7.6 KB
/
cmd_list.c
File metadata and controls
263 lines (235 loc) · 7.6 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
/*
* 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 <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
typedef struct {
int long_format;
int show_size;
int show_checksum;
const char* path_filter;
const char* container_file;
} list_options_t;
static void print_list_help(void) {
printf("Usage: bfc list [options] <container.bfc> [path]\n\n");
printf("List contents of a BFC container.\n\n");
printf("Options:\n");
printf(" -l, --long Use long listing format (like 'ls -l')\n");
printf(" -s, --size Show file sizes\n");
printf(" -c, --checksum Show CRC32C checksums\n");
printf(" -h, --help Show this help message\n\n");
printf("Arguments:\n");
printf(" container.bfc BFC container to list\n");
printf(" path Optional path to filter entries (directory or prefix)\n\n");
printf("Examples:\n");
printf(" bfc list archive.bfc # List all entries\n");
printf(" bfc list -l archive.bfc # Long format listing\n");
printf(" bfc list archive.bfc docs/ # List entries in docs/ directory\n");
printf(" bfc list -sc archive.bfc # Show sizes and checksums\n");
}
static int parse_list_options(int argc, char* argv[], list_options_t* opts) {
// Initialize options
opts->long_format = 0;
opts->show_size = 0;
opts->show_checksum = 0;
opts->path_filter = NULL;
opts->container_file = NULL;
int i;
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
print_list_help();
return 1;
} else if (strcmp(argv[i], "-l") == 0 || strcmp(argv[i], "--long") == 0) {
opts->long_format = 1;
} else if (strcmp(argv[i], "-s") == 0 || strcmp(argv[i], "--size") == 0) {
opts->show_size = 1;
} else if (strcmp(argv[i], "-c") == 0 || strcmp(argv[i], "--checksum") == 0) {
opts->show_checksum = 1;
} else if (argv[i][0] == '-') {
// Handle combined short options like -lsc
const char* opt = argv[i] + 1;
while (*opt) {
switch (*opt) {
case 'l':
opts->long_format = 1;
break;
case 's':
opts->show_size = 1;
break;
case 'c':
opts->show_checksum = 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 if (!opts->path_filter) {
opts->path_filter = argv[i];
} else {
print_error("Too many arguments");
return -1;
}
}
}
if (!opts->container_file) {
print_error("Container file not specified");
return -1;
}
return 0;
}
static void format_file_mode(uint32_t mode, char* buffer) {
strcpy(buffer, "----------");
if (S_ISDIR(mode))
buffer[0] = 'd';
else if (S_ISREG(mode))
buffer[0] = '-';
else if (S_ISLNK(mode))
buffer[0] = 'l';
else if (S_ISBLK(mode))
buffer[0] = 'b';
else if (S_ISCHR(mode))
buffer[0] = 'c';
else if (S_ISFIFO(mode))
buffer[0] = 'p';
else if (S_ISSOCK(mode))
buffer[0] = 's';
else
buffer[0] = '?';
if (mode & S_IRUSR)
buffer[1] = 'r';
if (mode & S_IWUSR)
buffer[2] = 'w';
if (mode & S_IXUSR)
buffer[3] = 'x';
if (mode & S_IRGRP)
buffer[4] = 'r';
if (mode & S_IWGRP)
buffer[5] = 'w';
if (mode & S_IXGRP)
buffer[6] = 'x';
if (mode & S_IROTH)
buffer[7] = 'r';
if (mode & S_IWOTH)
buffer[8] = 'w';
if (mode & S_IXOTH)
buffer[9] = 'x';
}
static void format_file_size(uint64_t size, char* buffer, size_t buffer_size) {
if (size < 1024) {
snprintf(buffer, buffer_size, "%" PRIu64 "B", size);
} else if (size < 1024 * 1024) {
snprintf(buffer, buffer_size, "%.1fK", size / 1024.0);
} else if (size < 1024 * 1024 * 1024) {
snprintf(buffer, buffer_size, "%.1fM", size / (1024.0 * 1024.0));
} else {
snprintf(buffer, buffer_size, "%.1fG", size / (1024.0 * 1024.0 * 1024.0));
}
}
static void format_timestamp(uint64_t mtime_ns, char* buffer, size_t buffer_size) {
time_t mtime = (time_t) (mtime_ns / 1000000000ULL);
struct tm* tm = localtime(&mtime);
if (tm) {
strftime(buffer, buffer_size, "%Y-%m-%d %H:%M", tm);
} else {
strcpy(buffer, "----?--?-- --?--");
}
}
// List callback structure
typedef struct {
list_options_t* opts;
int count;
} list_context_t;
static int list_entry_callback(const bfc_entry_t* entry, void* user) {
list_context_t* ctx = (list_context_t*) user;
list_options_t* opts = ctx->opts;
ctx->count++;
if (opts->long_format) {
char mode_str[11];
char size_str[16];
char time_str[32];
format_file_mode(entry->mode, mode_str);
format_file_size(entry->size, size_str, sizeof(size_str));
format_timestamp(entry->mtime_ns, time_str, sizeof(time_str));
if (opts->show_checksum && S_ISREG(entry->mode)) {
printf("%s %8s %s 0x%08x %s\n", mode_str, size_str, time_str, entry->crc32c, entry->path);
} else {
printf("%s %8s %s %s\n", mode_str, size_str, time_str, entry->path);
}
} else {
// Simple format
if (opts->show_size && opts->show_checksum && S_ISREG(entry->mode)) {
char size_str[16];
format_file_size(entry->size, size_str, sizeof(size_str));
printf("%8s 0x%08x %s\n", size_str, entry->crc32c, entry->path);
} else if (opts->show_size) {
char size_str[16];
format_file_size(entry->size, size_str, sizeof(size_str));
printf("%8s %s\n", size_str, entry->path);
} else if (opts->show_checksum && S_ISREG(entry->mode)) {
printf("0x%08x %s\n", entry->crc32c, entry->path);
} else {
printf("%s\n", entry->path);
}
}
return 0;
}
int cmd_list(int argc, char* argv[]) {
list_options_t opts;
int result = parse_list_options(argc, argv, &opts);
if (result != 0) {
return (result > 0) ? 0 : 1;
}
// Open container for reading
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;
}
// List entries
list_context_t ctx = {&opts, 0};
print_verbose("Listing entries%s%s", opts.path_filter ? " in path: " : "",
opts.path_filter ? opts.path_filter : "");
result = bfc_list(reader, opts.path_filter, list_entry_callback, &ctx);
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.path_filter) {
printf("No entries found matching '%s'\n", opts.path_filter);
} else {
printf("Container is empty\n");
}
} else if (!g_options.quiet) {
print_verbose("Listed %d entries", ctx.count);
}
return 0;
}