d3372da9 |
1 | /* |
2 | * Dolda Connect - Modular multiuser Direct Connect-style client |
3 | * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com) |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 2 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program; if not, write to the Free Software |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
19 | #include <stdlib.h> |
20 | #include <stdio.h> |
21 | #include <malloc.h> |
22 | #include <wchar.h> |
23 | #include <string.h> |
24 | #include <errno.h> |
25 | #include <dirent.h> |
26 | #include <sys/stat.h> |
27 | #include <unistd.h> |
28 | #include <fcntl.h> |
29 | #include <signal.h> |
30 | |
31 | #ifdef HAVE_CONFIG_H |
32 | #include <config.h> |
33 | #endif |
34 | #include "client.h" |
35 | #include "conf.h" |
36 | #include "log.h" |
37 | #include "utils.h" |
38 | #include "module.h" |
d3372da9 |
39 | #include "net.h" |
40 | #include "sysevents.h" |
8b17e919 |
41 | #include <tiger.h> |
d3372da9 |
42 | |
43 | struct scanstate |
44 | { |
45 | struct scanstate *next; |
46 | struct sharecache *node; |
47 | DIR *dd; |
48 | }; |
49 | |
50 | struct scanqueue |
51 | { |
52 | struct scanqueue *next; |
53 | struct scanstate *state; |
54 | }; |
55 | |
56 | static int conf_share(int argc, wchar_t **argv); |
57 | static void freecache(struct sharecache *node); |
58 | static void checkhashes(void); |
b00ef4d1 |
59 | static void writehashcache(int now); |
d3372da9 |
60 | |
61 | static struct configvar myvars[] = |
62 | { |
d9f89ef5 |
63 | /** The default nick name to use. The nickname can also be |
64 | * specified for individual hubs, overriding this setting. */ |
d3372da9 |
65 | {CONF_VAR_STRING, "defnick", {.str = L"DoldaConnect user"}}, |
d9f89ef5 |
66 | /** When scanning shares, this bitmask is consulted for every |
67 | * regular file. Unless the file's mode has the bits specified by |
68 | * this mask set, it will not be shared. */ |
d3372da9 |
69 | {CONF_VAR_INT, "scanfilemask", {.num = 0004}}, |
d9f89ef5 |
70 | /** When scanning shares, this bitmask is consulted for every |
71 | * directory encountered. Unless the directory's mode has the bits |
72 | * specified by this mask set, it will be ignored and any files |
73 | * under it will not be shared. */ |
d3372da9 |
74 | {CONF_VAR_INT, "scandirmask", {.num = 0005}}, |
d9f89ef5 |
75 | /** The filename to use for the hash cache (see the FILES section |
76 | * for more information). */ |
d3372da9 |
77 | {CONF_VAR_STRING, "hashcache", {.str = L"dc-hashcache"}}, |
78 | {CONF_VAR_END} |
79 | }; |
80 | |
81 | static struct configcmd mycmds[] = |
82 | { |
83 | {"share", conf_share}, |
84 | {NULL} |
85 | }; |
86 | |
87 | static struct scanstate *scanjob = NULL; |
88 | static struct scanqueue *scanqueue = NULL; |
89 | static struct sharepoint *shares = NULL; |
90 | static struct hashcache *hashcache = NULL; |
b00ef4d1 |
91 | static struct timer *hashwritetimer = NULL; |
cd8a934e |
92 | /* Set initially to -1, but changed to 0 the first time run() is |
93 | * called. This is to avoid forking a hash job before daemonizing, |
94 | * since that would make the daemon unable to wait() for the hash |
95 | * job. */ |
96 | static pid_t hashjob = -1; |
d3372da9 |
97 | struct sharecache *shareroot = NULL; |
98 | unsigned long long sharesize = 0; |
99 | GCBCHAIN(sharechangecb, unsigned long long); |
100 | |
101 | static int conf_share(int argc, wchar_t **argv) |
102 | { |
103 | struct sharepoint *share; |
104 | char *b; |
105 | |
106 | if(argc < 3) |
107 | { |
108 | flog(LOG_WARNING, "not enough arguments given for share command"); |
109 | return(1); |
110 | } |
111 | if((b = icwcstombs(argv[2], NULL)) == NULL) |
112 | { |
113 | flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno)); |
114 | return(1); |
115 | } |
116 | for(share = shares; share != NULL; share = share->next) |
117 | { |
118 | if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1])) |
119 | { |
120 | share->delete = 0; |
121 | free(b); |
122 | return(0); |
123 | } |
124 | } |
125 | share = smalloc(sizeof(*share)); |
126 | share->path = b; |
127 | share->delete = 0; |
128 | share->name = swcsdup(argv[1]); |
129 | share->next = shares; |
130 | share->prev = NULL; |
131 | if(shares != NULL) |
132 | shares->prev = share; |
133 | shares = share; |
134 | return(0); |
135 | } |
136 | |
137 | static void dumpsharecache(struct sharecache *node, int l) |
138 | { |
139 | int i; |
140 | |
141 | for(; node != NULL; node = node->next) |
142 | { |
143 | for(i = 0; i < l; i++) |
144 | putc('\t', stdout); |
145 | printf("%ls\n", node->name); |
146 | if(node->f.b.type == FILE_DIR) |
147 | dumpsharecache(node->child, l + 1); |
148 | } |
149 | } |
150 | |
a55f78c6 |
151 | struct hash *newhash(wchar_t *algo, size_t len, char *buf) |
152 | { |
153 | struct hash *ret; |
154 | |
155 | ret = smalloc(sizeof(*ret)); |
156 | memset(ret, 0, sizeof(*ret)); |
157 | ret->algo = swcsdup(algo); |
158 | ret->len = len; |
159 | ret->buf = memcpy(smalloc(len), buf, len); |
160 | return(ret); |
161 | } |
162 | |
163 | void freehash(struct hash *hash) |
164 | { |
165 | free(hash->algo); |
166 | free(hash->buf); |
167 | free(hash); |
168 | } |
169 | |
170 | struct hash *duphash(struct hash *hash) |
171 | { |
172 | return(newhash(hash->algo, hash->len, hash->buf)); |
173 | } |
174 | |
175 | struct hash *parsehash(wchar_t *text) |
176 | { |
177 | wchar_t *p; |
178 | char *mbsbuf, *decbuf; |
179 | size_t buflen; |
180 | struct hash *ret; |
181 | |
182 | if((p = wcschr(text, L':')) == NULL) |
183 | return(NULL); |
184 | *(p++) = L'\0'; |
185 | if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL) |
186 | return(NULL); |
187 | decbuf = base64decode(mbsbuf, &buflen); |
188 | free(mbsbuf); |
189 | if(decbuf == NULL) |
190 | return(NULL); |
191 | ret = newhash(text, buflen, decbuf); |
192 | free(decbuf); |
193 | return(ret); |
194 | } |
195 | |
196 | wchar_t *unparsehash(struct hash *hash) |
197 | { |
2eaefd31 |
198 | static wchar_t *buf = NULL; |
199 | wchar_t *whbuf; |
a55f78c6 |
200 | char *hbuf; |
201 | size_t bufsize, bufdata; |
202 | |
2eaefd31 |
203 | if(buf != NULL) |
204 | free(buf); |
a55f78c6 |
205 | buf = NULL; |
206 | bufsize = bufdata = 0; |
207 | hbuf = base64encode(hash->buf, hash->len); |
208 | if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL) |
209 | { |
210 | flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno)); |
211 | abort(); |
212 | } |
213 | free(hbuf); |
214 | bufcat(buf, hash->algo, wcslen(hash->algo)); |
215 | addtobuf(buf, ':'); |
216 | bufcat(buf, whbuf, wcslen(whbuf)); |
217 | free(whbuf); |
2b77e3c4 |
218 | addtobuf(buf, 0); |
a55f78c6 |
219 | return(buf); |
220 | } |
221 | |
4f8fc795 |
222 | int hashcmp(struct hash *h1, struct hash *h2) |
223 | { |
224 | if(wcscmp(h1->algo, h2->algo)) |
225 | return(0); |
226 | if(h1->len != h2->len) |
227 | return(0); |
228 | if(memcmp(h1->buf, h2->buf, h1->len)) |
229 | return(0); |
230 | return(1); |
231 | } |
232 | |
d3372da9 |
233 | static struct hashcache *newhashcache(void) |
234 | { |
235 | struct hashcache *new; |
236 | |
237 | new = smalloc(sizeof(*new)); |
238 | memset(new, 0, sizeof(*new)); |
239 | new->next = hashcache; |
240 | new->prev = NULL; |
241 | if(hashcache != NULL) |
242 | hashcache->prev = new; |
243 | hashcache = new; |
244 | return(new); |
245 | } |
246 | |
247 | static void freehashcache(struct hashcache *hc) |
248 | { |
249 | if(hc->next != NULL) |
250 | hc->next->prev = hc->prev; |
251 | if(hc->prev != NULL) |
252 | hc->prev->next = hc->next; |
253 | if(hc == hashcache) |
254 | hashcache = hc->next; |
255 | free(hc); |
256 | } |
257 | |
d3372da9 |
258 | static struct hashcache *findhashcache(dev_t dev, ino_t inode) |
259 | { |
260 | struct hashcache *hc; |
261 | |
262 | for(hc = hashcache; hc != NULL; hc = hc->next) |
263 | { |
264 | if((hc->dev == dev) && (hc->inode == inode)) |
265 | return(hc); |
266 | } |
267 | return(NULL); |
268 | } |
269 | |
270 | static void readhashcache(void) |
271 | { |
272 | int i, wc, line; |
273 | char *hcname; |
274 | FILE *stream; |
275 | char linebuf[256]; |
276 | char *p, *p2, *wv[32], *hash; |
277 | struct hashcache *hc; |
278 | size_t len; |
279 | |
ed1ab8e1 |
280 | if((hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 0)) == NULL) |
d3372da9 |
281 | return; |
282 | if((stream = fopen(hcname, "r")) == NULL) |
283 | { |
284 | flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno)); |
285 | return; |
286 | } |
287 | while(hashcache != NULL) |
288 | freehashcache(hashcache); |
289 | line = 0; |
290 | while(!feof(stream)) |
291 | { |
292 | fgets(linebuf, sizeof(linebuf), stream); |
293 | line++; |
294 | for(p = linebuf; *p; p++) |
295 | { |
296 | if(*p == '\n') |
297 | *p = ' '; |
298 | } |
299 | if(linebuf[0] == '#') |
300 | continue; |
301 | for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1) |
302 | { |
303 | if(p2 == p) |
304 | continue; |
305 | *p2 = 0; |
306 | wv[wc++] = p; |
307 | } |
308 | if(wc < 3) |
309 | continue; |
310 | hc = newhashcache(); |
311 | hc->dev = strtoll(wv[0], NULL, 10); |
312 | hc->inode = strtoll(wv[1], NULL, 10); |
313 | hc->mtime = strtoll(wv[2], NULL, 10); |
314 | for(i = 3; i < wc; i++) |
315 | { |
316 | if(!strcmp(wv[i], "tth")) |
317 | { |
318 | if(++i >= wc) |
319 | continue; |
320 | hash = base64decode(wv[i], &len); |
321 | if(len != 24) |
322 | { |
323 | free(hash); |
324 | continue; |
325 | } |
326 | memcpy(hc->tth, hash, 24); |
327 | free(hash); |
328 | } |
329 | } |
330 | } |
331 | fclose(stream); |
332 | } |
333 | |
b00ef4d1 |
334 | static void hashtimercb(int cancelled, void *uudata) |
335 | { |
336 | hashwritetimer = NULL; |
337 | if(!cancelled) |
338 | writehashcache(1); |
339 | } |
340 | |
341 | static void writehashcache(int now) |
d3372da9 |
342 | { |
343 | char *buf; |
344 | char *hcname; |
345 | FILE *stream; |
346 | struct hashcache *hc; |
347 | |
b00ef4d1 |
348 | if(!now) |
349 | { |
350 | if(hashwritetimer == NULL) |
351 | hashwritetimer = timercallback(ntime() + 300, (void (*)(int, void *))hashtimercb, NULL); |
352 | return; |
353 | } |
354 | if(hashwritetimer != NULL) |
355 | canceltimer(hashwritetimer); |
ed1ab8e1 |
356 | hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 1); |
d3372da9 |
357 | if((stream = fopen(hcname, "w")) == NULL) |
358 | { |
359 | flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno)); |
360 | return; |
361 | } |
362 | fprintf(stream, "# Dolda Connect hash cache file\n"); |
363 | fprintf(stream, "# Generated automatically, do not edit\n"); |
364 | fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n"); |
365 | fprintf(stream, "# HASH := HASHTYPE HASHVAL\n"); |
366 | fprintf(stream, "# HASHTYPE can currently only be `tth'\n"); |
367 | for(hc = hashcache; hc != NULL; hc = hc->next) |
368 | { |
369 | buf = base64encode(hc->tth, 24); |
370 | fprintf(stream, "%lli %lli %li tth %s\n", hc->dev, (long long)hc->inode, hc->mtime, buf); |
371 | free(buf); |
372 | } |
373 | fclose(stream); |
374 | } |
375 | |
cab0b442 |
376 | static void hashread(struct socket *sk, void *uudata) |
d3372da9 |
377 | { |
378 | static char *hashbuf; |
379 | static size_t hashbufsize = 0, hashbufdata = 0; |
380 | char *buf, *p, *p2, *lp; |
381 | size_t bufsize; |
382 | char *wv[32]; |
383 | int wc; |
384 | dev_t dev; |
385 | ino_t inode; |
386 | time_t mtime; |
387 | struct hashcache *hc; |
388 | |
389 | if((buf = sockgetinbuf(sk, &bufsize)) == NULL) |
cab0b442 |
390 | return; |
d3372da9 |
391 | bufcat(hashbuf, buf, bufsize); |
392 | free(buf); |
393 | while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL) |
394 | { |
395 | *(lp++) = 0; |
396 | wc = 0; |
397 | p = hashbuf; |
398 | while(1) |
399 | { |
400 | while((p2 = strchr(p, ' ')) == p) |
401 | p++; |
402 | wv[wc++] = p; |
403 | if(p2 == NULL) |
404 | { |
405 | break; |
406 | } else { |
407 | *p2 = 0; |
408 | p = p2 + 1; |
409 | } |
410 | } |
411 | if(wc != 4) |
412 | { |
413 | flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc); |
414 | } else { |
415 | dev = strtoll(wv[0], NULL, 10); |
416 | inode = strtoll(wv[1], NULL, 10); |
417 | mtime = strtol(wv[2], NULL, 10); |
418 | if((hc = findhashcache(dev, inode)) == NULL) |
419 | { |
420 | hc = newhashcache(); |
421 | hc->dev = dev; |
422 | hc->inode = inode; |
423 | } |
424 | hc->mtime = mtime; |
425 | buf = base64decode(wv[3], NULL); |
426 | memcpy(hc->tth, buf, 24); |
427 | free(buf); |
b00ef4d1 |
428 | writehashcache(0); |
d3372da9 |
429 | } |
430 | memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf)); |
431 | } |
432 | } |
433 | |
6a050bf5 |
434 | static void hashexit(pid_t pid, int status, struct socket *outsock) |
d3372da9 |
435 | { |
436 | if(pid != hashjob) |
437 | flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid); |
438 | if(status) |
439 | flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status); |
440 | hashjob = 0; |
441 | checkhashes(); |
6a050bf5 |
442 | putsock(outsock); |
d3372da9 |
443 | } |
444 | |
445 | static int hashfile(char *path) |
446 | { |
447 | int i, ret; |
448 | int fd; |
449 | int pfd[2]; |
450 | char buf[4096]; |
451 | struct stat sb; |
452 | struct tigertreehash tth; |
453 | char digest[24]; |
454 | struct socket *outsock; |
455 | |
456 | if((fd = open(path, O_RDONLY)) < 0) |
457 | { |
458 | flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno)); |
459 | return(1); |
460 | } |
461 | if(fstat(fd, &sb) < 0) |
462 | { |
463 | flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno)); |
464 | close(fd); |
465 | return(1); |
466 | } |
467 | if(pipe(pfd) < 0) |
468 | { |
469 | flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno)); |
470 | close(fd); |
471 | return(1); |
472 | } |
473 | hashjob = fork(); |
474 | if(hashjob < 0) |
475 | { |
476 | flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno)); |
477 | close(fd); |
478 | close(pfd[0]); |
479 | close(pfd[1]); |
480 | return(1); |
481 | } |
482 | if(hashjob == 0) |
483 | { |
484 | nice(10); |
485 | signal(SIGHUP, SIG_DFL); |
486 | fd = dup2(fd, 4); |
487 | pfd[1] = dup2(pfd[1], 3); |
488 | dup2(fd, 0); |
489 | dup2(pfd[1], 1); |
490 | for(i = 3; i < FD_SETSIZE; i++) |
491 | close(i); |
492 | initlog(); |
493 | inittigertree(&tth); |
494 | while((ret = read(0, buf, 4096)) > 0) |
495 | dotigertree(&tth, buf, ret); |
496 | if(ret < 0) |
497 | { |
498 | flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno)); |
499 | exit(1); |
500 | } |
501 | synctigertree(&tth); |
502 | restigertree(&tth, digest); |
503 | ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24)); |
504 | write(1, buf, ret); |
505 | exit(0); |
506 | } |
507 | close(fd); |
508 | close(pfd[1]); |
509 | outsock = wrapsock(pfd[0]); |
cab0b442 |
510 | outsock->readcb = hashread; |
6a050bf5 |
511 | childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock); |
d3372da9 |
512 | return(0); |
513 | } |
514 | |
515 | /* |
516 | * Call only when hashjob == 0 |
517 | */ |
518 | static void checkhashes(void) |
519 | { |
520 | struct sharecache *node; |
521 | struct hashcache *hc; |
522 | char *path; |
523 | |
524 | node = shareroot->child; |
e194f2fd |
525 | for(node = shareroot->child; node != NULL; node = nextscnode(node)) |
d3372da9 |
526 | { |
e194f2fd |
527 | if(node->f.b.type != FILE_REG) |
d3372da9 |
528 | continue; |
d3372da9 |
529 | if(!node->f.b.hastth) |
530 | { |
72a06ab4 |
531 | if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime)) |
d3372da9 |
532 | { |
533 | memcpy(node->hashtth, hc->tth, 24); |
534 | node->f.b.hastth = 1; |
535 | GCBCHAINDOCB(sharechangecb, sharesize); |
536 | } else { |
537 | path = getfspath(node); |
538 | if(hashfile(path)) |
539 | { |
540 | flog(LOG_WARNING, "could not hash %s, unsharing it", path); |
541 | freecache(node); |
e194f2fd |
542 | free(path); |
543 | flog(LOG_INFO, "sharing %lli bytes", sharesize); |
544 | continue; |
d3372da9 |
545 | } |
546 | free(path); |
547 | return; |
548 | } |
549 | } |
d3372da9 |
550 | } |
551 | } |
552 | |
553 | struct sharecache *nextscnode(struct sharecache *node) |
554 | { |
555 | if(node->child != NULL) |
556 | return(node->child); |
557 | while(node->next == NULL) |
558 | { |
559 | node = node->parent; |
560 | if(node == shareroot) |
561 | return(NULL); |
562 | } |
563 | return(node->next); |
564 | } |
565 | |
566 | static void freescan(struct scanstate *job) |
567 | { |
568 | if(job->dd != NULL) |
569 | closedir(job->dd); |
570 | free(job); |
571 | } |
572 | |
573 | /* No need for optimization; lookup isn't really that common */ |
574 | struct sharecache *findcache(struct sharecache *parent, wchar_t *name) |
575 | { |
576 | struct sharecache *node; |
577 | |
578 | for(node = parent->child; node != NULL; node = node->next) |
579 | { |
580 | if(!wcscmp(node->name, name)) |
581 | return(node); |
582 | } |
583 | return(NULL); |
584 | } |
585 | |
586 | static void attachcache(struct sharecache *parent, struct sharecache *node) |
587 | { |
588 | node->parent = parent; |
589 | node->next = parent->child; |
590 | if(parent->child != NULL) |
591 | parent->child->prev = node; |
592 | parent->child = node; |
593 | } |
594 | |
595 | static void detachcache(struct sharecache *node) |
596 | { |
597 | if(node->next != NULL) |
598 | node->next->prev = node->prev; |
599 | if(node->prev != NULL) |
600 | node->prev->next = node->next; |
601 | if((node->parent != NULL) && (node->parent->child == node)) |
602 | node->parent->child = node->next; |
603 | node->parent = NULL; |
604 | node->next = NULL; |
605 | node->prev = NULL; |
606 | } |
607 | |
608 | static void freecache(struct sharecache *node) |
609 | { |
610 | struct sharecache *cur, *next; |
611 | struct scanqueue *q, *nq, **fq; |
612 | |
613 | detachcache(node); |
614 | fq = &scanqueue; |
615 | for(q = scanqueue; q != NULL; q = nq) |
616 | { |
617 | nq = q->next; |
618 | if(q->state->node == node) |
619 | { |
620 | flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name); |
621 | freescan(q->state); |
622 | *fq = q->next; |
623 | free(q); |
624 | continue; |
625 | } |
626 | fq = &q->next; |
627 | } |
628 | if(node->child != NULL) |
629 | { |
630 | for(cur = node->child; cur != NULL; cur = next) |
631 | { |
632 | next = cur->next; |
633 | freecache(cur); |
634 | } |
635 | } |
636 | CBCHAINDOCB(node, share_delete, node); |
637 | CBCHAINFREE(node, share_delete); |
638 | sharesize -= node->size; |
639 | if(node->path != NULL) |
640 | free(node->path); |
641 | if(node->name != NULL) |
642 | free(node->name); |
643 | free(node); |
644 | } |
645 | |
646 | static void freesharepoint(struct sharepoint *share) |
647 | { |
648 | struct sharecache *node; |
649 | |
650 | if(share->next != NULL) |
651 | share->next->prev = share->prev; |
652 | if(share->prev != NULL) |
653 | share->prev->next = share->next; |
654 | if(share == shares) |
655 | shares = share->next; |
656 | if((node = findcache(shareroot, share->name)) != NULL) |
657 | freecache(node); |
658 | free(share->path); |
659 | free(share->name); |
660 | free(share); |
661 | } |
662 | |
663 | static struct sharecache *newcache(void) |
664 | { |
665 | struct sharecache *new; |
666 | |
667 | new = smalloc(sizeof(*new)); |
668 | memset(new, 0, sizeof(*new)); |
669 | CBCHAININIT(new, share_delete); |
670 | return(new); |
671 | } |
672 | |
673 | char *getfspath(struct sharecache *node) |
674 | { |
675 | char *buf, *mbsname; |
676 | size_t bufsize; |
677 | |
678 | buf = smalloc(bufsize = 64); |
679 | *buf = 0; |
680 | while(node != NULL) |
681 | { |
682 | if(node->path != NULL) |
683 | { |
684 | if(bufsize < strlen(node->path) + strlen(buf) + 1) |
685 | buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1); |
686 | memmove(buf + strlen(node->path), buf, strlen(buf) + 1); |
687 | memcpy(buf, node->path, strlen(node->path)); |
688 | return(buf); |
689 | } |
690 | if((mbsname = icwcstombs(node->name, NULL)) == NULL) |
691 | { |
692 | flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno)); |
693 | free(buf); |
694 | return(NULL); |
695 | } |
696 | while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1) |
697 | buf = srealloc(buf, bufsize *= 2); |
698 | memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1); |
699 | memcpy(buf + 1, mbsname, strlen(mbsname)); |
700 | *buf = '/'; |
701 | free(mbsname); |
702 | node = node->parent; |
703 | } |
704 | buf = srealloc(buf, strlen(buf) + 1); |
705 | return(buf); |
706 | } |
707 | |
708 | static int checknode(struct sharecache *node) |
709 | { |
710 | char *path; |
711 | struct stat sb; |
712 | |
713 | if(node->parent == NULL) |
714 | { |
715 | return(1); |
716 | } else { |
717 | if(!checknode(node->parent)) |
718 | return(0); |
719 | path = getfspath(node); |
720 | if(stat(path, &sb) < 0) |
721 | { |
722 | flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno)); |
723 | queuescan(node->parent); |
724 | return(0); |
725 | } else { |
726 | return(1); |
727 | } |
728 | } |
729 | } |
730 | |
731 | int opensharecache(struct sharecache *node) |
732 | { |
733 | char *path; |
734 | int fd, errbak; |
735 | |
736 | path = getfspath(node); |
737 | fd = open(path, O_RDONLY); |
738 | errbak = errno; |
739 | if(fd < 0) |
740 | { |
741 | flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak)); |
742 | checknode(node); |
743 | } |
744 | free(path); |
745 | errno = errbak; |
746 | return(fd); |
747 | } |
748 | |
749 | static struct scanstate *newscan(struct sharecache *node) |
750 | { |
751 | struct scanstate *new; |
752 | |
753 | new = smalloc(sizeof(*new)); |
754 | new->next = NULL; |
755 | new->node = node; |
756 | new->dd = NULL; |
757 | return(new); |
758 | } |
759 | |
760 | void queuescan(struct sharecache *node) |
761 | { |
762 | struct scanqueue *new; |
763 | |
764 | new = smalloc(sizeof(*new)); |
765 | new->state = newscan(node); |
766 | new->next = scanqueue; |
767 | scanqueue = new; |
768 | } |
769 | |
770 | /* For internal use in doscan() */ |
771 | static void removestale(struct sharecache *node) |
772 | { |
773 | struct sharecache *cur, *next; |
774 | |
775 | for(cur = node->child; cur != NULL; cur = next) |
776 | { |
777 | next = cur->next; |
778 | if(!cur->f.b.found) |
779 | freecache(cur); |
780 | } |
781 | } |
782 | |
783 | /* For internal use in doscan() */ |
784 | static void jobdone(void) |
785 | { |
786 | struct scanstate *jbuf; |
787 | |
788 | jbuf = scanjob; |
789 | scanjob = jbuf->next; |
790 | freescan(jbuf); |
791 | if(scanjob != NULL) |
792 | fchdir(dirfd(scanjob->dd)); |
793 | } |
794 | |
795 | int doscan(int quantum) |
796 | { |
797 | char *path; |
798 | wchar_t *wcs; |
799 | int type; |
800 | struct sharecache *n; |
801 | struct scanstate *jbuf; |
802 | struct scanqueue *qbuf; |
803 | struct dirent *de; |
804 | struct stat sb; |
805 | struct hashcache *hc; |
806 | int dmask, fmask; |
807 | static int busybefore = 0; |
808 | |
809 | dmask = confgetint("cli", "scandirmask"); |
810 | fmask = confgetint("cli", "scanfilemask"); |
811 | if((scanjob != NULL) && (scanjob->dd != NULL)) |
812 | { |
813 | while(fchdir(dirfd(scanjob->dd)) < 0) |
814 | { |
815 | flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno)); |
816 | removestale(scanjob->node); |
817 | jobdone(); |
818 | } |
819 | } |
820 | while(quantum-- > 0) |
821 | { |
822 | if(scanjob != NULL) |
823 | { |
824 | busybefore = 1; |
825 | } else { |
826 | while(scanjob == NULL) |
827 | { |
828 | if(scanqueue == NULL) |
829 | { |
830 | if(busybefore) |
831 | { |
832 | flog(LOG_INFO, "sharing %lli bytes", sharesize); |
833 | busybefore = 0; |
834 | GCBCHAINDOCB(sharechangecb, sharesize); |
835 | if(hashjob == 0) |
836 | checkhashes(); |
837 | } |
838 | return(0); |
839 | } |
840 | busybefore = 1; |
841 | scanjob = scanqueue->state; |
842 | qbuf = scanqueue; |
843 | scanqueue = qbuf->next; |
844 | free(qbuf); |
845 | for(n = scanjob->node->child; n != NULL; n = n->next) |
846 | n->f.b.found = 0; |
847 | } |
848 | } |
849 | if(scanjob->dd == NULL) |
850 | { |
851 | path = getfspath(scanjob->node); |
852 | if((scanjob->dd = opendir(path)) == NULL) |
853 | { |
854 | flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno)); |
855 | freecache(scanjob->node); |
856 | free(path); |
857 | jobdone(); |
858 | continue; |
859 | } |
860 | free(path); |
861 | if(fchdir(dirfd(scanjob->dd)) < 0) |
862 | { |
863 | flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno)); |
864 | jobdone(); |
865 | continue; |
866 | } |
867 | } |
868 | if((de = readdir(scanjob->dd)) == NULL) |
869 | { |
870 | removestale(scanjob->node); |
871 | jobdone(); |
872 | continue; |
873 | } |
874 | if(*de->d_name == '.') |
875 | continue; |
876 | if((wcs = icmbstowcs(de->d_name, NULL)) == NULL) |
877 | { |
878 | flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno)); |
879 | continue; |
880 | } |
881 | n = findcache(scanjob->node, wcs); |
882 | if(stat(de->d_name, &sb) < 0) |
883 | { |
884 | free(wcs); |
885 | if(n != NULL) |
886 | { |
887 | flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno)); |
888 | freecache(n); |
889 | } else { |
890 | flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno)); |
891 | } |
892 | continue; |
893 | } |
894 | if(S_ISDIR(sb.st_mode)) |
895 | { |
896 | if(~sb.st_mode & dmask) |
897 | { |
898 | free(wcs); |
899 | continue; |
900 | } |
901 | type = FILE_DIR; |
902 | } else if(S_ISREG(sb.st_mode)) { |
903 | if(~sb.st_mode & fmask) |
904 | { |
905 | free(wcs); |
906 | continue; |
907 | } |
908 | type = FILE_REG; |
909 | } else { |
6c21fc1d |
910 | flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode); |
d3372da9 |
911 | free(wcs); |
912 | continue; |
913 | } |
914 | if(n != NULL) |
915 | { |
916 | if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size))) |
917 | { |
918 | freecache(n); |
919 | n = NULL; |
920 | } |
921 | } |
922 | if(n == NULL) |
923 | { |
924 | n = newcache(); |
925 | n->name = wcs; |
926 | if(S_ISREG(sb.st_mode)) |
927 | { |
928 | sharesize += (n->size = sb.st_size); |
929 | } else { |
930 | n->size = 0; |
931 | } |
932 | n->mtime = sb.st_mtime; |
933 | n->dev = sb.st_dev; |
934 | n->inode = sb.st_ino; |
935 | n->f.b.type = type; |
936 | attachcache(scanjob->node, n); |
937 | } else { |
938 | free(wcs); |
939 | } |
940 | n->f.b.found = 1; |
941 | if(n->f.b.type == FILE_DIR) |
942 | { |
943 | jbuf = newscan(n); |
944 | jbuf->next = scanjob; |
945 | scanjob = jbuf; |
946 | } else if(n->f.b.type == FILE_REG) { |
947 | if(n->f.b.hastth && (n->mtime != sb.st_mtime)) |
948 | n->f.b.hastth = 0; |
949 | if(!n->f.b.hastth) |
950 | { |
951 | if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL) |
952 | { |
953 | if(hc->mtime == n->mtime) |
954 | { |
955 | n->f.b.hastth = 1; |
956 | memcpy(n->hashtth, hc->tth, 24); |
957 | } else { |
958 | freehashcache(hc); |
959 | } |
960 | } |
961 | } |
962 | } |
963 | } |
964 | return(1); |
965 | } |
966 | |
967 | void scanshares(void) |
968 | { |
969 | struct sharepoint *cur; |
970 | struct sharecache *node; |
971 | struct stat sb; |
972 | |
973 | for(cur = shares; cur != NULL; cur = cur->next) |
974 | { |
975 | if((node = findcache(shareroot, cur->name)) == NULL) |
976 | { |
977 | if(stat(cur->path, &sb)) |
978 | { |
979 | flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno)); |
980 | continue; |
981 | } |
982 | if(!S_ISDIR(sb.st_mode)) |
983 | { |
984 | flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path); |
985 | continue; |
986 | } |
987 | node = newcache(); |
988 | node->name = swcsdup(cur->name); |
989 | node->path = sstrdup(cur->path); |
990 | if(node->path[strlen(node->path) - 1] == '/') |
991 | node->path[strlen(node->path) - 1] = 0; |
992 | node->f.b.type = FILE_DIR; |
993 | attachcache(shareroot, node); |
994 | } |
995 | queuescan(node); |
996 | } |
997 | } |
998 | |
999 | static void preinit(int hup) |
1000 | { |
1001 | struct sharepoint *cur; |
1002 | |
1003 | if(hup) |
1004 | { |
1005 | for(cur = shares; cur != NULL; cur = cur->next) |
1006 | cur->delete = 1; |
1007 | } else { |
1008 | shareroot = newcache(); |
1009 | shareroot->name = swcsdup(L""); |
1010 | shareroot->f.b.type = FILE_DIR; |
1011 | } |
1012 | } |
1013 | |
1014 | static int init(int hup) |
1015 | { |
1016 | struct sharepoint *cur, *next; |
1017 | |
1018 | readhashcache(); |
1019 | for(cur = shares; cur != NULL; cur = next) |
1020 | { |
1021 | next = cur->next; |
1022 | if(cur->delete) |
1023 | freesharepoint(cur); |
1024 | } |
1025 | scanshares(); |
1026 | if(!hup) |
1027 | while(doscan(100)); |
1028 | return(0); |
1029 | } |
1030 | |
1031 | static int run(void) |
1032 | { |
cd8a934e |
1033 | if(hashjob == -1) |
1034 | { |
1035 | hashjob = 0; |
1036 | checkhashes(); |
1037 | } |
d3372da9 |
1038 | return(doscan(10)); |
1039 | } |
1040 | |
1041 | static void terminate(void) |
1042 | { |
1043 | if(hashjob != 0) |
1044 | kill(hashjob, SIGHUP); |
1045 | while(shares != NULL) |
1046 | freesharepoint(shares); |
1047 | freecache(shareroot); |
1048 | } |
1049 | |
1050 | static struct module me = |
1051 | { |
1052 | .name = "cli", |
1053 | .conf = |
1054 | { |
1055 | .vars = myvars, |
1056 | .cmds = mycmds |
1057 | }, |
1058 | .preinit = preinit, |
1059 | .init = init, |
1060 | .run = run, |
1061 | .terminate = terminate |
1062 | }; |
1063 | |
1064 | MODULE(me) |