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