Debian Codegraph @

src/ffindex_optimize.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
13 #define _GNU_SOURCE 1
14 #define _LARGEFILE64_SOURCE 1
15 #define _FILE_OFFSET_BITS 64
16
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25
26 #include "ffindex.h"
27 #include "ffutil.h"
28
29 #define MAX_FILENAME_LIST_FILES 4096
30
31
32 void usage(char *program_name)
33 {
34 fprintf(stderr, "USAGE: %s OUT_DATA_FILE OUT_INDEX_FILE -d IN_DATA_FILE -i IN_INDEX_FILE\n"
35 "\t-d FFDATA_FILE\tinput ffindex data file to be optimized\n"
36 "\t-i FFINDEX_FILE\tinput ffindex index file to be optimized\n"
37 "\t-v\t\tprint version and other info then exit\n"
38 FFINDEX_COPYRIGHT,
39 program_name, MAX_FILENAME_LIST_FILES, FFINDEX_MAX_ENTRY_NAME_LENTH, FFINDEX_MAX_INDEX_ENTRIES_DEFAULT);
40 }
41
42 int main(int argn, char **argv)
43 {
44 int version = 0;
45 int opt, err = EXIT_SUCCESS;
46 char* list_ffindex_data[MAX_FILENAME_LIST_FILES];
47 char* list_ffindex_index[MAX_FILENAME_LIST_FILES];
48 size_t list_ffindex_data_index = 0;
49 size_t list_ffindex_index_index = 0;
50 size_t list_filenames_index = 0;
51 while ((opt = getopt(argn, argv, "vd:i:")) != -1)
52 {
53 switch (opt)
54 {
55 case 'd':
56 list_ffindex_data[list_ffindex_data_index++] = optarg;
57 break;
58 case 'i':
59 list_ffindex_index[list_ffindex_index_index++] = optarg;
60 break;
61 case 'v':
62 version = 1;
63 break;
64 default:
65 usage(argv[0]);
66 return EXIT_FAILURE;
67 }
68 }
69
70 if(version == 1)
71 {
72 /* Don't you dare running it on a platform where byte != 8 bits */
73 printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8);
74 return EXIT_SUCCESS;
75 }
76
77 if(argn - optind < 2)
78 {
79 usage(argv[0]);
80 return EXIT_FAILURE;
81 }
82
83 if(list_ffindex_data_index != list_ffindex_index_index)
84 {
85 fprintf(stderr, "ERROR: -d and -i must be specified pairwise\n");
86 return EXIT_FAILURE;
87 }
88
89 char *data_filename = argv[optind++];
90 char *index_filename = argv[optind++];
91 FILE *data_file = NULL, *index_file = NULL;
92
93 size_t offset = 0;
94
95 /* open index and data file, seek to end if needed */
96 char *mode = "w";
97 if(append)
98 mode = "a";
99 err = ffindex_index_open(data_filename, index_filename, mode, &data_file, &index_file, &offset);
100 if(err != EXIT_SUCCESS)
101 return EXIT_FAILURE;
102
103 /* For each list_file insert */
104 if(list_filenames_index > 0)
105 for(int i = 0; i < list_filenames_index; i++)
106 {
107 FILE *list_file = fopen(list_filenames[i], "r");
108 if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; }
109 if(ffindex_insert_list_file(data_file, index_file, &offset, list_file) < 0)
110 {
111 perror(list_filenames[i]);
112 err = -1;
113 }
114 }
115
116 /* Append other ffindexes */
117 if(list_ffindex_data_index > 0)
118 {
119 for(int i = 0; i < list_ffindex_data_index; i++)
120 {
121 FILE* data_file_to_add = fopen(list_ffindex_data[i], "r"); if( data_file_to_add == NULL) { perror(list_ffindex_data[i]); return EXIT_FAILURE; }
122 FILE* index_file_to_add = fopen(list_ffindex_index[i], "r"); if( index_file_to_add == NULL) { perror(list_ffindex_index[i]); return EXIT_FAILURE; }
123 size_t data_size;
124 char *data_to_add = ffindex_mmap_data(data_file_to_add, &data_size);
125 ffindex_index_t* index_to_add = ffindex_index_parse(index_file_to_add, 0);
126 ffindex_insert_ffindex(data_file, index_file, &offset, data_to_add, index_to_add);
127 }
128 }
129
130
131 /* Insert files and directories into the index */
132 for(int i = optind; i < argn; i++)
133 {
134 char *path = argv[i];
135 struct stat sb;
136 if(stat(path, &sb) == -1)
137 {
138 fferror_print(__FILE__, __LINE__, __func__, path);
139 continue;
140 }
141
142 if(S_ISDIR(sb.st_mode))
143 {
144 err = ffindex_insert_dir(data_file, index_file, &offset, path);
145 if(err < 0)fferror_print(__FILE__, __LINE__, __func__, path);
146 }
147 else if(S_ISREG(sb.st_mode))
148 {
149 ffindex_insert_file(data_file, index_file, &offset, path, path);
150 }
151 }
152 fclose(data_file);
153
154 /* Sort the index entries and write back */
155 if(sort)
156 {
157 rewind(index_file);
158 ffindex_index_t* index = ffindex_index_parse(index_file, 0);
159 if(index == NULL)
160 {
161 fferror_print(__FILE__, __LINE__, __func__, index_filename);
162 exit(EXIT_FAILURE);
163 }
164 fclose(index_file);
165 ffindex_sort_index_file(index);
166 index_file = fopen(index_filename, "w");
167 if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; }
168 err += ffindex_write(index, index_file);
169 }
170
171 return err;
172 }
173
174 /* vim: ts=2 sw=2 et
175 */