Mention PAM file.
[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;
cd8a934e 79/* Set initially to -1, but changed to 0 the first time run() is
80 * called. This is to avoid forking a hash job before daemonizing,
81 * since that would make the daemon unable to wait() for the hash
82 * job. */
83static pid_t hashjob = -1;
d3372da9 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
a55f78c6 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{
2eaefd31 185 static wchar_t *buf = NULL;
186 wchar_t *whbuf;
a55f78c6 187 char *hbuf;
188 size_t bufsize, bufdata;
189
2eaefd31 190 if(buf != NULL)
191 free(buf);
a55f78c6 192 buf = NULL;
193 bufsize = bufdata = 0;
194 hbuf = base64encode(hash->buf, hash->len);
195 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
196 {
197 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
198 abort();
199 }
200 free(hbuf);
201 bufcat(buf, hash->algo, wcslen(hash->algo));
202 addtobuf(buf, ':');
203 bufcat(buf, whbuf, wcslen(whbuf));
204 free(whbuf);
2b77e3c4 205 addtobuf(buf, 0);
a55f78c6 206 return(buf);
207}
208
4f8fc795 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
d3372da9 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, void *uudata)
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}
461
462static int hashfile(char *path)
463{
464 int i, ret;
465 int fd;
466 int pfd[2];
467 char buf[4096];
468 struct stat sb;
469 struct tigertreehash tth;
470 char digest[24];
471 struct socket *outsock;
472
473 if((fd = open(path, O_RDONLY)) < 0)
474 {
475 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
476 return(1);
477 }
478 if(fstat(fd, &sb) < 0)
479 {
480 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
481 close(fd);
482 return(1);
483 }
484 if(pipe(pfd) < 0)
485 {
486 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
487 close(fd);
488 return(1);
489 }
490 hashjob = fork();
491 if(hashjob < 0)
492 {
493 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
494 close(fd);
495 close(pfd[0]);
496 close(pfd[1]);
497 return(1);
498 }
499 if(hashjob == 0)
500 {
501 nice(10);
502 signal(SIGHUP, SIG_DFL);
503 fd = dup2(fd, 4);
504 pfd[1] = dup2(pfd[1], 3);
505 dup2(fd, 0);
506 dup2(pfd[1], 1);
507 for(i = 3; i < FD_SETSIZE; i++)
508 close(i);
509 initlog();
510 inittigertree(&tth);
511 while((ret = read(0, buf, 4096)) > 0)
512 dotigertree(&tth, buf, ret);
513 if(ret < 0)
514 {
515 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
516 exit(1);
517 }
518 synctigertree(&tth);
519 restigertree(&tth, digest);
520 ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
521 write(1, buf, ret);
522 exit(0);
523 }
524 close(fd);
525 close(pfd[1]);
526 outsock = wrapsock(pfd[0]);
527 outsock->readcb = hashread;
528 childcallback(hashjob, hashexit, NULL);
529 return(0);
530}
531
532/*
533 * Call only when hashjob == 0
534 */
535static void checkhashes(void)
536{
537 struct sharecache *node;
538 struct hashcache *hc;
539 char *path;
540
541 node = shareroot->child;
e194f2fd 542 for(node = shareroot->child; node != NULL; node = nextscnode(node))
d3372da9 543 {
e194f2fd 544 if(node->f.b.type != FILE_REG)
d3372da9 545 continue;
d3372da9 546 if(!node->f.b.hastth)
547 {
548 if((hc = findhashcache(node->dev, node->inode)) != NULL)
549 {
550 memcpy(node->hashtth, hc->tth, 24);
551 node->f.b.hastth = 1;
552 GCBCHAINDOCB(sharechangecb, sharesize);
553 } else {
554 path = getfspath(node);
555 if(hashfile(path))
556 {
557 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
558 freecache(node);
e194f2fd 559 free(path);
560 flog(LOG_INFO, "sharing %lli bytes", sharesize);
561 continue;
d3372da9 562 }
563 free(path);
564 return;
565 }
566 }
d3372da9 567 }
568}
569
570struct sharecache *nextscnode(struct sharecache *node)
571{
572 if(node->child != NULL)
573 return(node->child);
574 while(node->next == NULL)
575 {
576 node = node->parent;
577 if(node == shareroot)
578 return(NULL);
579 }
580 return(node->next);
581}
582
583static void freescan(struct scanstate *job)
584{
585 if(job->dd != NULL)
586 closedir(job->dd);
587 free(job);
588}
589
590/* No need for optimization; lookup isn't really that common */
591struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
592{
593 struct sharecache *node;
594
595 for(node = parent->child; node != NULL; node = node->next)
596 {
597 if(!wcscmp(node->name, name))
598 return(node);
599 }
600 return(NULL);
601}
602
603static void attachcache(struct sharecache *parent, struct sharecache *node)
604{
605 node->parent = parent;
606 node->next = parent->child;
607 if(parent->child != NULL)
608 parent->child->prev = node;
609 parent->child = node;
610}
611
612static void detachcache(struct sharecache *node)
613{
614 if(node->next != NULL)
615 node->next->prev = node->prev;
616 if(node->prev != NULL)
617 node->prev->next = node->next;
618 if((node->parent != NULL) && (node->parent->child == node))
619 node->parent->child = node->next;
620 node->parent = NULL;
621 node->next = NULL;
622 node->prev = NULL;
623}
624
625static void freecache(struct sharecache *node)
626{
627 struct sharecache *cur, *next;
628 struct scanqueue *q, *nq, **fq;
629
630 detachcache(node);
631 fq = &scanqueue;
632 for(q = scanqueue; q != NULL; q = nq)
633 {
634 nq = q->next;
635 if(q->state->node == node)
636 {
637 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
638 freescan(q->state);
639 *fq = q->next;
640 free(q);
641 continue;
642 }
643 fq = &q->next;
644 }
645 if(node->child != NULL)
646 {
647 for(cur = node->child; cur != NULL; cur = next)
648 {
649 next = cur->next;
650 freecache(cur);
651 }
652 }
653 CBCHAINDOCB(node, share_delete, node);
654 CBCHAINFREE(node, share_delete);
655 sharesize -= node->size;
656 if(node->path != NULL)
657 free(node->path);
658 if(node->name != NULL)
659 free(node->name);
660 free(node);
661}
662
663static void freesharepoint(struct sharepoint *share)
664{
665 struct sharecache *node;
666
667 if(share->next != NULL)
668 share->next->prev = share->prev;
669 if(share->prev != NULL)
670 share->prev->next = share->next;
671 if(share == shares)
672 shares = share->next;
673 if((node = findcache(shareroot, share->name)) != NULL)
674 freecache(node);
675 free(share->path);
676 free(share->name);
677 free(share);
678}
679
680static struct sharecache *newcache(void)
681{
682 struct sharecache *new;
683
684 new = smalloc(sizeof(*new));
685 memset(new, 0, sizeof(*new));
686 CBCHAININIT(new, share_delete);
687 return(new);
688}
689
690char *getfspath(struct sharecache *node)
691{
692 char *buf, *mbsname;
693 size_t bufsize;
694
695 buf = smalloc(bufsize = 64);
696 *buf = 0;
697 while(node != NULL)
698 {
699 if(node->path != NULL)
700 {
701 if(bufsize < strlen(node->path) + strlen(buf) + 1)
702 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
703 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
704 memcpy(buf, node->path, strlen(node->path));
705 return(buf);
706 }
707 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
708 {
709 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
710 free(buf);
711 return(NULL);
712 }
713 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
714 buf = srealloc(buf, bufsize *= 2);
715 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
716 memcpy(buf + 1, mbsname, strlen(mbsname));
717 *buf = '/';
718 free(mbsname);
719 node = node->parent;
720 }
721 buf = srealloc(buf, strlen(buf) + 1);
722 return(buf);
723}
724
725static int checknode(struct sharecache *node)
726{
727 char *path;
728 struct stat sb;
729
730 if(node->parent == NULL)
731 {
732 return(1);
733 } else {
734 if(!checknode(node->parent))
735 return(0);
736 path = getfspath(node);
737 if(stat(path, &sb) < 0)
738 {
739 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
740 queuescan(node->parent);
741 return(0);
742 } else {
743 return(1);
744 }
745 }
746}
747
748int opensharecache(struct sharecache *node)
749{
750 char *path;
751 int fd, errbak;
752
753 path = getfspath(node);
754 fd = open(path, O_RDONLY);
755 errbak = errno;
756 if(fd < 0)
757 {
758 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
759 checknode(node);
760 }
761 free(path);
762 errno = errbak;
763 return(fd);
764}
765
766static struct scanstate *newscan(struct sharecache *node)
767{
768 struct scanstate *new;
769
770 new = smalloc(sizeof(*new));
771 new->next = NULL;
772 new->node = node;
773 new->dd = NULL;
774 return(new);
775}
776
777void queuescan(struct sharecache *node)
778{
779 struct scanqueue *new;
780
781 new = smalloc(sizeof(*new));
782 new->state = newscan(node);
783 new->next = scanqueue;
784 scanqueue = new;
785}
786
787/* For internal use in doscan() */
788static void removestale(struct sharecache *node)
789{
790 struct sharecache *cur, *next;
791
792 for(cur = node->child; cur != NULL; cur = next)
793 {
794 next = cur->next;
795 if(!cur->f.b.found)
796 freecache(cur);
797 }
798}
799
800/* For internal use in doscan() */
801static void jobdone(void)
802{
803 struct scanstate *jbuf;
804
805 jbuf = scanjob;
806 scanjob = jbuf->next;
807 freescan(jbuf);
808 if(scanjob != NULL)
809 fchdir(dirfd(scanjob->dd));
810}
811
812int doscan(int quantum)
813{
814 char *path;
815 wchar_t *wcs;
816 int type;
817 struct sharecache *n;
818 struct scanstate *jbuf;
819 struct scanqueue *qbuf;
820 struct dirent *de;
821 struct stat sb;
822 struct hashcache *hc;
823 int dmask, fmask;
824 static int busybefore = 0;
825
826 dmask = confgetint("cli", "scandirmask");
827 fmask = confgetint("cli", "scanfilemask");
828 if((scanjob != NULL) && (scanjob->dd != NULL))
829 {
830 while(fchdir(dirfd(scanjob->dd)) < 0)
831 {
832 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
833 removestale(scanjob->node);
834 jobdone();
835 }
836 }
837 while(quantum-- > 0)
838 {
839 if(scanjob != NULL)
840 {
841 busybefore = 1;
842 } else {
843 while(scanjob == NULL)
844 {
845 if(scanqueue == NULL)
846 {
847 if(busybefore)
848 {
849 flog(LOG_INFO, "sharing %lli bytes", sharesize);
850 busybefore = 0;
851 GCBCHAINDOCB(sharechangecb, sharesize);
852 if(hashjob == 0)
853 checkhashes();
854 }
855 return(0);
856 }
857 busybefore = 1;
858 scanjob = scanqueue->state;
859 qbuf = scanqueue;
860 scanqueue = qbuf->next;
861 free(qbuf);
862 for(n = scanjob->node->child; n != NULL; n = n->next)
863 n->f.b.found = 0;
864 }
865 }
866 if(scanjob->dd == NULL)
867 {
868 path = getfspath(scanjob->node);
869 if((scanjob->dd = opendir(path)) == NULL)
870 {
871 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
872 freecache(scanjob->node);
873 free(path);
874 jobdone();
875 continue;
876 }
877 free(path);
878 if(fchdir(dirfd(scanjob->dd)) < 0)
879 {
880 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
881 jobdone();
882 continue;
883 }
884 }
885 if((de = readdir(scanjob->dd)) == NULL)
886 {
887 removestale(scanjob->node);
888 jobdone();
889 continue;
890 }
891 if(*de->d_name == '.')
892 continue;
893 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
894 {
895 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
896 continue;
897 }
898 n = findcache(scanjob->node, wcs);
899 if(stat(de->d_name, &sb) < 0)
900 {
901 free(wcs);
902 if(n != NULL)
903 {
904 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
905 freecache(n);
906 } else {
907 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
908 }
909 continue;
910 }
911 if(S_ISDIR(sb.st_mode))
912 {
913 if(~sb.st_mode & dmask)
914 {
915 free(wcs);
916 continue;
917 }
918 type = FILE_DIR;
919 } else if(S_ISREG(sb.st_mode)) {
920 if(~sb.st_mode & fmask)
921 {
922 free(wcs);
923 continue;
924 }
925 type = FILE_REG;
926 } else {
6c21fc1d 927 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
d3372da9 928 free(wcs);
929 continue;
930 }
931 if(n != NULL)
932 {
933 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
934 {
935 freecache(n);
936 n = NULL;
937 }
938 }
939 if(n == NULL)
940 {
941 n = newcache();
942 n->name = wcs;
943 if(S_ISREG(sb.st_mode))
944 {
945 sharesize += (n->size = sb.st_size);
946 } else {
947 n->size = 0;
948 }
949 n->mtime = sb.st_mtime;
950 n->dev = sb.st_dev;
951 n->inode = sb.st_ino;
952 n->f.b.type = type;
953 attachcache(scanjob->node, n);
954 } else {
955 free(wcs);
956 }
957 n->f.b.found = 1;
958 if(n->f.b.type == FILE_DIR)
959 {
960 jbuf = newscan(n);
961 jbuf->next = scanjob;
962 scanjob = jbuf;
963 } else if(n->f.b.type == FILE_REG) {
964 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
965 n->f.b.hastth = 0;
966 if(!n->f.b.hastth)
967 {
968 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
969 {
970 if(hc->mtime == n->mtime)
971 {
972 n->f.b.hastth = 1;
973 memcpy(n->hashtth, hc->tth, 24);
974 } else {
975 freehashcache(hc);
976 }
977 }
978 }
979 }
980 }
981 return(1);
982}
983
984void scanshares(void)
985{
986 struct sharepoint *cur;
987 struct sharecache *node;
988 struct stat sb;
989
990 for(cur = shares; cur != NULL; cur = cur->next)
991 {
992 if((node = findcache(shareroot, cur->name)) == NULL)
993 {
994 if(stat(cur->path, &sb))
995 {
996 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
997 continue;
998 }
999 if(!S_ISDIR(sb.st_mode))
1000 {
1001 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1002 continue;
1003 }
1004 node = newcache();
1005 node->name = swcsdup(cur->name);
1006 node->path = sstrdup(cur->path);
1007 if(node->path[strlen(node->path) - 1] == '/')
1008 node->path[strlen(node->path) - 1] = 0;
1009 node->f.b.type = FILE_DIR;
1010 attachcache(shareroot, node);
1011 }
1012 queuescan(node);
1013 }
1014}
1015
1016static void preinit(int hup)
1017{
1018 struct sharepoint *cur;
1019
1020 if(hup)
1021 {
1022 for(cur = shares; cur != NULL; cur = cur->next)
1023 cur->delete = 1;
1024 } else {
1025 shareroot = newcache();
1026 shareroot->name = swcsdup(L"");
1027 shareroot->f.b.type = FILE_DIR;
1028 }
1029}
1030
1031static int init(int hup)
1032{
1033 struct sharepoint *cur, *next;
1034
1035 readhashcache();
1036 for(cur = shares; cur != NULL; cur = next)
1037 {
1038 next = cur->next;
1039 if(cur->delete)
1040 freesharepoint(cur);
1041 }
1042 scanshares();
1043 if(!hup)
1044 while(doscan(100));
1045 return(0);
1046}
1047
1048static int run(void)
1049{
cd8a934e 1050 if(hashjob == -1)
1051 {
1052 hashjob = 0;
1053 checkhashes();
1054 }
d3372da9 1055 return(doscan(10));
1056}
1057
1058static void terminate(void)
1059{
1060 if(hashjob != 0)
1061 kill(hashjob, SIGHUP);
1062 while(shares != NULL)
1063 freesharepoint(shares);
1064 freecache(shareroot);
1065}
1066
1067static struct module me =
1068{
1069 .name = "cli",
1070 .conf =
1071 {
1072 .vars = myvars,
1073 .cmds = mycmds
1074 },
1075 .preinit = preinit,
1076 .init = init,
1077 .run = run,
1078 .terminate = terminate
1079};
1080
1081MODULE(me)