/
tcfiler.c
335 lines (257 loc) · 7.62 KB
/
tcfiler.c
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
/*
* File: tcfiler.c
* Author: evg
*
* Created on July 9, 2010, 1:32 AM
*/
#include <tcutil.h>
#include <tchdb.h>
#include <tcrdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <glob.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <string.h>
#include <sys/fcntl.h>
/*
*
*/
TCHDB *hdb;
TCRDB *tdb;
TCXSTR *db_file, *tdb_host;
int tdb_port;
bool use_cabinet_lib;
bool open_cabinet_db(int64_t bnum, bool optimize) {
int ecode;
if (optimize) fprintf(stdout, "Opening database optimized for %d items\n", bnum);
/* tune up the db*/
if (optimize && !tchdbtune(hdb, bnum, -1, -1, HDBTLARGE)) {
ecode = tchdbecode(hdb);
fprintf(stdout, "Failed to tune up database, error: %s\n", tchdberrmsg(ecode));
return false;
}
/* open the database */
if (!tchdbopen(hdb, tcxstrptr(db_file), HDBOWRITER | HDBOCREAT)) {
ecode = tchdbecode(hdb);
fprintf(stdout, "Failed to open database, error: %s\n", tchdberrmsg(ecode));
return false;
}
return true;
}
bool open_tyrant_db() {
int ecode;
/* open the database */
if (!tcrdbopen(tdb, tcxstrptr(tdb_host), tdb_port)) {
ecode = tchdbecode(tdb);
fprintf(stdout, "Failed to open database, error: %s\n", tcrdberrmsg(ecode));
return false;
}
return true;
}
void close_db(){
if(use_cabinet_lib){
tchdbclose(hdb);
tchdbdel(hdb);
} else{
tcrdbclose(tdb);
tcrdbdel(tdb);
}
}
bool key_exists(const char *key) {
if (use_cabinet_lib) return tchdbvsiz2(hdb, key) > 0;
else return tcrdbvsiz2(tdb, key) > 0;
}
char *get_file_name(char *path, size_t path_size) {
if (NULL == path || 0 == path_size) return 0;
char *file_name;
file_name = path;
char *c;
char *fend = path + path_size - 1;
for (c = fend; *c; c--) {
if ('/' == *c) {
file_name = c + 1;
break;
}
}
return file_name;
}
int unpack_files(char *root_dir, bool verbose, bool test) {
char *key;
void *value;
int fd, count, key_size, val_size;
/* traverse records */
count = 0;
tchdbiterinit(hdb);
while ((key = tchdbiternext2(hdb)) != NULL) {
key_size = (int) strlen(key);
if (verbose || test) fprintf(stdout, "Extracting file: %s\n", key);
value = tchdbget(hdb, key, key_size, &val_size);
if (value && !test) {
if (-1 == (fd = open(key, O_WRONLY | O_CREAT | O_TRUNC, 0644))) {
fprintf(stdout, "***Failed open file %s\n", key);
return 1;
}
write(fd, value, val_size);
close(fd);
free(value);
}
free(key);
count++;
}
return count;
}
int pack_file(char *file, TCXSTR *root_key, bool resume) {
struct stat st;
int ecode;
TCXSTR *key;
key = tcxstrdup(root_key);
size_t file_path_len;
file_path_len = strlen(file);
char *file_name;
file_name = get_file_name(file, file_path_len);
tcxstrcat2(key, file_name);
if (resume && key_exists(tcxstrptr(key))) {
fprintf(stdout, "already exists");
return;
}
if (-1 == stat(file, &st)) {
return 1;
}
if (!S_ISREG(st.st_mode)) {
fprintf(stdout, "Not regular file: %s", file);
return 2;
}
int fd;
if (-1 == (fd = open(file, O_RDONLY))) {
fprintf(stdout, "***Failed open file %s", file);
return 1;
}
void *fmap;
if (MAP_FAILED == (fmap = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))) {
fprintf(stdout, "mmaping failed for %s", file);
close(fd);
return -1;
}
/* store records */
if (use_cabinet_lib) {
if (!tchdbput(hdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) {
ecode = tchdbecode(hdb);
fprintf(stdout, "put error: %s", tchdberrmsg(ecode));
}
} else {
if (!tcrdbput(tdb, tcxstrptr(key), tcxstrsize(key), fmap, st.st_size)) {
ecode = tcrdbecode(tdb);
fprintf(stdout, "put error: %s", tcrdberrmsg(ecode));
}
}
fprintf(stdout, "%d bytes", st.st_size);
munmap(fmap, st.st_size);
close(fd);
tcxstrdel(key);
}
int main(int argc, char **argv) {
TCXSTR *pattern, *key_root;
int ecode;
bool verbose, test, create, extract, optimize, resume;
int o;
verbose = test = create = extract = optimize = resume = false;
pattern = tcxstrnew();
key_root = tcxstrnew();
db_file = tcxstrnew();
tdb_host = tcxstrnew();
tdb_port = 0;
while (-1 != (o = getopt(argc, argv, "cxtvrof:p:k:H:P:"))) {
switch (o) {
case 'f':
use_cabinet_lib = true;
tcxstrcat2(db_file, optarg);
/* create the object */
hdb = tchdbnew();
break;
case 'H':
use_cabinet_lib = false;
tcxstrcat2(tdb_host, optarg);
tdb = tcrdbnew();
break;
case 'P':
tdb_port = strtol(optarg, NULL, 0);
break;
case 'k':
tcxstrcat2(key_root, optarg);
// add trailing slash if needed
char *last_c;
last_c = tcxstrptr(key_root) + tcxstrsize(key_root) - 1;
if (*last_c != '/') {
tcxstrcat2(key_root, "/");
}
break;
case 'p':
tcxstrcat2(pattern, optarg);
break;
case 'v':
verbose = true;
break;
case 't':
test = true;
break;
case 'c':
create = true;
break;
case 'x':
extract = true;
break;
case 'r':
resume = true;
break;
case 'o':
optimize = true;
case '?':
default:
break;
}
}
if (!create && !extract) {
fprintf(stdout, "No action specifed. Use -c to create DB and -x to extract files from dbfile.tch\n");
return 1;
}
if ((use_cabinet_lib && NULL == hdb)
||
(!use_cabinet_lib && NULL == tdb)) {
fprintf(stdout, "No database specifed. Use -f dbfile.tch\n");
return 1;
}
if (0 == tcxstrsize(pattern)) {
fprintf(stdout, "No pattern is given. Using *. Use -p <glob_pattern> to override\n");
tcxstrcat2(pattern, "*");
}
if (create) {
glob_t gtree;
glob(tcxstrptr(pattern), GLOB_NOSORT, NULL, >ree);
size_t found = gtree.gl_pathc;
if (use_cabinet_lib && !open_cabinet_db((int64_t) found, optimize)) return 2;
if (!use_cabinet_lib && !open_tyrant_db()) return 3;
int i;
for (i = 0; i < found; i++) {
char * fname = gtree.gl_pathv[i];
if (verbose || test) fprintf(stdout, "\n%d of %d - packing file: %s ...", i, found, fname);
if (!test) pack_file(fname, key_root, resume);
}
fprintf(stdout, "Finished. Processed %d items\n", (int) found);
globfree(>ree);
} else if (extract) {
if (!open_cabinet_db(0, false)) return 2;
int count;
count = unpack_files(NULL, verbose, test);
fprintf(stdout, "Finished. Processed %d items\n", count);
}
/* close the database */
close_db();
/* delete the objects */
tcxstrdel(pattern);
tcxstrdel(key_root);
return 0;
}