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