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