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