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