Line data Source code
1 : /*
2 : * Copyright (C) 2014 Andrea Mazzoleni
3 : *
4 : * This program is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 3 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * This program is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "portable.h"
19 :
20 : #include "support.h"
21 : #include "search.h"
22 :
23 : /****************************************************************************/
24 : /* search */
25 :
26 1844870 : static void search_file(struct snapraid_state* state, const char* path, data_off_t size, int64_t mtime_sec, int mtime_nsec)
27 : {
28 : struct snapraid_search_file* file;
29 : tommy_uint32_t file_hash;
30 :
31 1844870 : file = malloc_nofail(sizeof(struct snapraid_search_file));
32 1844870 : file->path = strdup_nofail(path);
33 1844870 : file->size = size;
34 1844870 : file->mtime_sec = mtime_sec;
35 1844870 : file->mtime_nsec = mtime_nsec;
36 :
37 1844870 : file_hash = file_stamp_hash(file->size, file->mtime_sec, file->mtime_nsec);
38 :
39 1844870 : tommy_hashdyn_insert(&state->searchset, &file->node, file, file_hash);
40 1844870 : }
41 :
42 1844870 : void search_file_free(struct snapraid_search_file* file)
43 : {
44 1844870 : free(file->path);
45 1844870 : free(file);
46 1844870 : }
47 :
48 : struct search_file_compare_arg {
49 : const struct snapraid_state* state;
50 : const struct snapraid_block* block;
51 : const struct snapraid_file* file;
52 : unsigned char* buffer;
53 : data_off_t offset;
54 : unsigned read_size;
55 : int prevhash;
56 : };
57 :
58 11468 : int search_file_compare(const void* void_arg, const void* void_data)
59 : {
60 11468 : const struct search_file_compare_arg* arg = void_arg;
61 11468 : const struct snapraid_search_file* file = void_data;
62 11468 : const struct snapraid_state* state = arg->state;
63 : unsigned char buffer_hash[HASH_SIZE];
64 11468 : const char* path = file->path;
65 : int f;
66 : ssize_t ret;
67 :
68 : /* compare file info */
69 11468 : if (arg->file->size != file->size)
70 0 : return -1;
71 :
72 11468 : if (arg->file->mtime_sec != file->mtime_sec)
73 0 : return -1;
74 :
75 11468 : if (arg->file->mtime_nsec != file->mtime_nsec)
76 0 : return -1;
77 :
78 : /* read the block and compare the hash */
79 11468 : f = open(path, O_RDONLY | O_BINARY);
80 11468 : if (f == -1) {
81 : /* LCOV_EXCL_START */
82 : if (errno == ENOENT) {
83 : log_fatal("DANGER! file '%s' disappeared.\n", path);
84 : log_fatal("If you moved it, please rerun the same command.\n");
85 : } else {
86 : log_fatal("Error opening file '%s'. %s.\n", path, strerror(errno));
87 : }
88 : exit(EXIT_FAILURE);
89 : /* LCOV_EXCL_STOP */
90 : }
91 :
92 11468 : if (lseek(f, arg->offset, SEEK_SET) != arg->offset) {
93 : /* LCOV_EXCL_START */
94 : log_fatal("Error seeking file '%s'. %s.\n", path, strerror(errno));
95 : exit(EXIT_FAILURE);
96 : /* LCOV_EXCL_STOP */
97 : }
98 :
99 11468 : ret = read(f, arg->buffer, arg->read_size);
100 11468 : if (ret < 0 || (unsigned)ret != arg->read_size) {
101 : /* LCOV_EXCL_START */
102 : log_fatal("Error reading file '%s'. %s.\n", path, strerror(errno));
103 : exit(EXIT_FAILURE);
104 : /* LCOV_EXCL_STOP */
105 : }
106 :
107 11468 : ret = close(f);
108 11468 : if (ret != 0) {
109 : /* LCOV_EXCL_START */
110 : log_fatal("Error closing file '%s'. %s.\n", path, strerror(errno));
111 : exit(EXIT_FAILURE);
112 : /* LCOV_EXCL_STOP */
113 : }
114 :
115 : /* compute the hash */
116 11468 : if (arg->prevhash)
117 4503 : memhash(state->prevhash, state->prevhashseed, buffer_hash, arg->buffer, arg->read_size);
118 : else
119 6965 : memhash(state->hash, state->hashseed, buffer_hash, arg->buffer, arg->read_size);
120 :
121 : /* check if the hash is matching */
122 11468 : if (memcmp(buffer_hash, arg->block->hash, HASH_SIZE) != 0)
123 2086 : return -1;
124 :
125 9382 : if (arg->read_size != state->block_size) {
126 : /* fill the remaining with 0 */
127 3922 : memset(arg->buffer + arg->read_size, 0, state->block_size - arg->read_size);
128 : }
129 :
130 9382 : return 0;
131 : }
132 :
133 625435 : int state_search_fetch(struct snapraid_state* state, int prevhash, struct snapraid_block* missing_block, unsigned char* buffer)
134 : {
135 : struct snapraid_search_file* file;
136 : tommy_uint32_t file_hash;
137 : struct search_file_compare_arg arg;
138 :
139 625435 : arg.state = state;
140 625435 : arg.block = missing_block;
141 625435 : arg.file = block_file_get(missing_block);
142 625435 : arg.buffer = buffer;
143 625435 : arg.offset = state->block_size * (data_off_t)block_file_pos(missing_block);
144 625435 : arg.read_size = block_file_size(missing_block, state->block_size);
145 625435 : arg.prevhash = prevhash;
146 :
147 625435 : file_hash = file_stamp_hash(arg.file->size, arg.file->mtime_sec, arg.file->mtime_nsec);
148 :
149 : /* search in the hashtable, and also check if the data matches the hash */
150 625435 : file = tommy_hashdyn_search(&state->searchset, search_file_compare, &arg, file_hash);
151 625435 : if (!file)
152 616053 : return -1;
153 :
154 : /* if found, buffer is already set with data */
155 9382 : return 0;
156 : }
157 :
158 2465 : static void search_dir(struct snapraid_state* state, struct snapraid_disk* disk, const char* dir, const char* sub)
159 : {
160 : DIR* d;
161 :
162 2465 : d = opendir(dir);
163 2465 : if (!d) {
164 : /* LCOV_EXCL_START */
165 : log_fatal("Error opening directory '%s'. %s.\n", dir, strerror(errno));
166 : exit(EXIT_FAILURE);
167 : /* LCOV_EXCL_STOP */
168 : }
169 :
170 : while (1) {
171 : char path_next[PATH_MAX];
172 : char sub_next[PATH_MAX];
173 : char out[PATH_MAX];
174 2041185 : struct snapraid_filter* reason = 0;
175 : struct stat st;
176 : const char* name;
177 : struct dirent* dd;
178 :
179 : /* clear errno to detect erroneous conditions */
180 2041185 : errno = 0;
181 2041185 : dd = readdir(d);
182 2041185 : if (dd == 0 && errno != 0) {
183 : /* LCOV_EXCL_START */
184 : log_fatal("Error reading directory '%s'. %s.\n", dir, strerror(errno));
185 : exit(EXIT_FAILURE);
186 : /* LCOV_EXCL_STOP */
187 : }
188 2041185 : if (dd == 0) {
189 2465 : break; /* finished */
190 : }
191 :
192 : /* skip "." and ".." files */
193 2038720 : name = dd->d_name;
194 2038720 : if (name[0] == '.' && (name[1] == 0 || (name[1] == '.' && name[2] == 0)))
195 9860 : continue;
196 :
197 2033790 : pathprint(path_next, sizeof(path_next), "%s%s", dir, name);
198 2033790 : pathprint(sub_next, sizeof(sub_next), "%s%s", sub, name);
199 :
200 : /* exclude hidden files even before calling lstat() */
201 2033790 : if (disk != 0 && filter_hidden(state->filter_hidden, dd) != 0) {
202 0 : msg_verbose("Excluding hidden '%s'\n", path_next);
203 0 : continue;
204 : }
205 :
206 : /* exclude content files even before calling lstat() */
207 2033790 : if (disk != 0 && filter_content(&state->contentlist, path_next) != 0) {
208 0 : msg_verbose("Excluding content '%s'\n", path_next);
209 0 : continue;
210 : }
211 :
212 : #if HAVE_STRUCT_DIRENT_D_STAT
213 : /* convert dirent to lstat result */
214 : dirent_lstat(dd, &st);
215 :
216 : /* if the st_mode field is missing, takes care to fill it using normal lstat() */
217 : /* at now this can happen only in Windows (with HAVE_STRUCT_DIRENT_D_STAT defined), */
218 : /* because we use a directory reading method that doesn't read info about ReparsePoint. */
219 : /* Note that here we cannot call here lstat_sync(), because we don't know what kind */
220 : /* of file is it, and lstat_sync() doesn't always work */
221 : if (st.st_mode == 0) {
222 : if (lstat(path_next, &st) != 0) {
223 : /* LCOV_EXCL_START */
224 : log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
225 : exit(EXIT_FAILURE);
226 : /* LCOV_EXCL_STOP */
227 : }
228 : }
229 : #else
230 : /* get lstat info about the file */
231 2033790 : if (lstat(path_next, &st) != 0) {
232 : /* LCOV_EXCL_START */
233 : log_fatal("Error in stat file/directory '%s'. %s.\n", path_next, strerror(errno));
234 : exit(EXIT_FAILURE);
235 : /* LCOV_EXCL_STOP */
236 : }
237 : #endif
238 :
239 2033790 : if (S_ISREG(st.st_mode)) {
240 1969797 : if (disk == 0 || filter_path(&state->filterlist, &reason, disk->name, sub_next) == 0) {
241 1844870 : search_file(state, path_next, st.st_size, st.st_mtime, STAT_NSEC(&st));
242 : } else {
243 124927 : msg_verbose("Excluding link '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
244 : }
245 63993 : } else if (S_ISDIR(st.st_mode)) {
246 1783 : if (disk == 0 || filter_dir(&state->filterlist, &reason, disk->name, sub_next) == 0) {
247 1783 : pathslash(path_next, sizeof(path_next));
248 1783 : pathslash(sub_next, sizeof(sub_next));
249 1783 : search_dir(state, disk, path_next, sub_next);
250 : } else {
251 0 : msg_verbose("Excluding directory '%s' for rule '%s'\n", path_next, filter_type(reason, out, sizeof(out)));
252 : }
253 : }
254 2038720 : }
255 :
256 2465 : if (closedir(d) != 0) {
257 : /* LCOV_EXCL_START */
258 : log_fatal("Error closing directory '%s'. %s.\n", dir, strerror(errno));
259 : exit(EXIT_FAILURE);
260 : /* LCOV_EXCL_STOP */
261 : }
262 2465 : }
263 :
264 1 : void state_search(struct snapraid_state* state, const char* dir)
265 : {
266 : char path[PATH_MAX];
267 :
268 1 : msg_progress("Importing...\n");
269 :
270 : /* add the final slash */
271 1 : pathimport(path, sizeof(path), dir);
272 1 : pathslash(path, sizeof(path));
273 :
274 1 : search_dir(state, 0, path, "");
275 1 : }
276 :
277 114 : void state_search_array(struct snapraid_state* state)
278 : {
279 : tommy_node* i;
280 :
281 : /* import from all the disks */
282 795 : for (i = state->disklist; i != 0; i = i->next) {
283 681 : struct snapraid_disk* disk = i->data;
284 :
285 681 : msg_progress("Scanning disk %s...\n", disk->name);
286 :
287 681 : search_dir(state, disk, disk->dir, "");
288 : }
289 114 : }
290 :
|