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