Debian Codegraph @

src/ffindex_unpack.c

raw
1 /*
2 * FFindex
3 * written by Andreas Hauser <[email protected]>.
4 * Please add your name here if you distribute modified versions.
5 *
6 * FFindex is provided under the Create Commons license "Attribution-ShareAlike
7 * 4.0", which basically captures the spirit of the Gnu Public License (GPL).
8 *
9 * See:
10 * http://creativecommons.org/licenses/by-sa/4.0/
11 *
12 * ffindex_apply
13 * apply a program to each FFindex entry
14 */
15
16 #define _GNU_SOURCE 1
17 #define _LARGEFILE64_SOURCE 1
18 #define _FILE_OFFSET_BITS 64
19
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/mman.h>
27
28
29 #include "ffindex.h"
30 #include "ffutil.h"
31
32
33 int main(int argn, char **argv)
34 {
35 if(argn < 4)
36 {
37 fprintf(stderr, "USAGE: %s DATA_FILENAME INDEX_FILENAME OUT_DIR\n"
38 "\n\tPlease note that a huge number of files might be created!\n"
39 FFINDEX_COPYRIGHT,
40 basename(argv[0]));
41 return -1;
42 }
43 char *data_filename = argv[1];
44 char *index_filename = argv[2];
45 char *out_dir = argv[3];
46
47 FILE *data_file = fopen(data_filename, "r");
48 FILE *index_file = fopen(index_filename, "r");
49
50 if( data_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], data_filename); exit(EXIT_FAILURE); }
51 if(index_file == NULL) { fferror_print(__FILE__, __LINE__, argv[0], index_filename); exit(EXIT_FAILURE); }
52
53 size_t data_size;
54 char *data = ffindex_mmap_data(data_file, &data_size);
55 madvise(data, data_size, MADV_SEQUENTIAL);
56
57 ffindex_index_t* index = ffindex_index_parse(index_file, 0);
58 if(index == NULL)
59 {
60 fferror_print(__FILE__, __LINE__, "ffindex_index_parse", index_filename);
61 exit(EXIT_FAILURE);
62 }
63
64 if(chdir(out_dir) < 0){ fferror_print(__FILE__, __LINE__, argv[0], out_dir); exit(EXIT_FAILURE); }
65
66 size_t range_start = 0;
67 size_t range_end = index->n_entries;
68
69 // Foreach entry
70 //#pragma omp parallel for
71 for(size_t entry_index = range_start; entry_index < range_end; entry_index++)
72 {
73 //fprintf(stderr, "index %ld\n", entry_index);
74
75 ffindex_entry_t* entry = ffindex_get_entry_by_index(index, entry_index);
76 if(entry == NULL) { perror(entry->name); continue; }
77
78 FILE *output_file = fopen(entry->name, "w");
79
80 // Write to file
81 char *filedata = ffindex_get_data_by_entry(data, entry);
82 size_t written = fwrite(filedata, entry->length - 1, 1, output_file);
83 if(written < 1) { perror(entry->name); }
84
85 fclose(output_file);
86 }
87
88 return 0;
89 }
90
91 /* vim: ts=2 sw=2 et
92 */