2 * Dolda Connect - Modular multiuser Direct Connect-style client
3 * Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
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.
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.
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
39 #include "sysevents.h"
44 struct scanstate *next;
45 struct sharecache *node;
51 struct scanqueue *next;
52 struct scanstate *state;
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);
60 static struct configvar myvars[] =
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. */
85 {CONF_VAR_INT, "rescandelay", {.num = 0}},
89 static struct configcmd mycmds[] =
91 {"share", conf_share},
95 static struct scanstate *scanjob = NULL;
96 static struct scanqueue *scanqueue = NULL;
97 static struct sharepoint *shares = NULL;
98 static struct hashcache *hashcache = NULL;
99 static struct timer *hashwritetimer = NULL;
100 /* Set initially to -1, but changed to 0 the first time run() is
101 * called. This is to avoid forking a hash job before daemonizing,
102 * since that would make the daemon unable to wait() for the hash
104 static pid_t hashjob = -1;
105 struct sharecache *shareroot = NULL;
106 static struct timer *scantimer = NULL;
107 unsigned long long sharesize = 0;
108 GCBCHAIN(sharechangecb, unsigned long long);
110 static int conf_share(int argc, wchar_t **argv)
112 struct sharepoint *share;
117 flog(LOG_WARNING, "not enough arguments given for share command");
120 if((b = icwcstombs(argv[2], NULL)) == NULL)
122 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
125 for(share = shares; share != NULL; share = share->next)
127 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
134 share = smalloc(sizeof(*share));
137 share->name = swcsdup(argv[1]);
138 share->next = shares;
141 shares->prev = share;
146 static void dumpsharecache(struct sharecache *node, int l)
150 for(; node != NULL; node = node->next)
152 for(i = 0; i < l; i++)
154 printf("%ls\n", node->name);
155 if(node->f.b.type == FILE_DIR)
156 dumpsharecache(node->child, l + 1);
160 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
164 ret = smalloc(sizeof(*ret));
165 memset(ret, 0, sizeof(*ret));
166 ret->algo = swcsdup(algo);
168 ret->buf = memcpy(smalloc(len), buf, len);
172 void freehash(struct hash *hash)
179 struct hash *duphash(struct hash *hash)
181 return(newhash(hash->algo, hash->len, hash->buf));
184 struct hash *parsehash(wchar_t *text)
187 char *mbsbuf, *decbuf;
191 if((p = wcschr(text, L':')) == NULL)
194 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
196 decbuf = base64decode(mbsbuf, &buflen);
200 ret = newhash(text, buflen, decbuf);
205 wchar_t *unparsehash(struct hash *hash)
207 static wchar_t *buf = NULL;
210 size_t bufsize, bufdata;
215 bufsize = bufdata = 0;
216 hbuf = base64encode(hash->buf, hash->len);
217 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
219 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
223 bufcat(buf, hash->algo, wcslen(hash->algo));
225 bufcat(buf, whbuf, wcslen(whbuf));
231 int hashcmp(struct hash *h1, struct hash *h2)
233 if(wcscmp(h1->algo, h2->algo))
235 if(h1->len != h2->len)
237 if(memcmp(h1->buf, h2->buf, h1->len))
242 static struct hashcache *newhashcache(void)
244 struct hashcache *new;
246 new = smalloc(sizeof(*new));
247 memset(new, 0, sizeof(*new));
248 new->next = hashcache;
250 if(hashcache != NULL)
251 hashcache->prev = new;
256 static void freehashcache(struct hashcache *hc)
259 hc->next->prev = hc->prev;
261 hc->prev->next = hc->next;
263 hashcache = hc->next;
267 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
269 struct hashcache *hc;
271 for(hc = hashcache; hc != NULL; hc = hc->next)
273 if((hc->dev == dev) && (hc->inode == inode))
279 static void readhashcache(void)
285 char *p, *p2, *wv[32], *hash;
286 struct hashcache *hc;
289 if((hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 0)) == NULL)
291 if((stream = fopen(hcname, "r")) == NULL)
293 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
296 while(hashcache != NULL)
297 freehashcache(hashcache);
301 fgets(linebuf, sizeof(linebuf), stream);
303 for(p = linebuf; *p; p++)
308 if(linebuf[0] == '#')
310 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
320 hc->dev = strtoll(wv[0], NULL, 10);
321 hc->inode = strtoll(wv[1], NULL, 10);
322 hc->mtime = strtoll(wv[2], NULL, 10);
323 for(i = 3; i < wc; i++)
325 if(!strcmp(wv[i], "tth"))
329 hash = base64decode(wv[i], &len);
335 memcpy(hc->tth, hash, 24);
343 static void hashtimercb(int cancelled, void *uudata)
345 hashwritetimer = NULL;
350 static void writehashcache(int now)
355 struct hashcache *hc;
359 if(hashwritetimer == NULL)
360 hashwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), (void (*)(int, void *))hashtimercb, NULL);
363 if(hashwritetimer != NULL)
364 canceltimer(hashwritetimer);
365 hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 1);
366 if((stream = fopen(hcname, "w")) == NULL)
368 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
371 fprintf(stream, "# Dolda Connect hash cache file\n");
372 fprintf(stream, "# Generated automatically, do not edit\n");
373 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
374 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
375 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
376 for(hc = hashcache; hc != NULL; hc = hc->next)
378 buf = base64encode(hc->tth, 24);
379 fprintf(stream, "%lli %lli %li tth %s\n", (long long)hc->dev, (long long)hc->inode, hc->mtime, buf);
385 static void hashread(struct socket *sk, void *uudata)
387 static char *hashbuf;
388 static size_t hashbufsize = 0, hashbufdata = 0;
389 char *buf, *p, *p2, *lp;
396 struct hashcache *hc;
398 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
400 bufcat(hashbuf, buf, bufsize);
402 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
409 while((p2 = strchr(p, ' ')) == p)
422 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
424 dev = strtoll(wv[0], NULL, 10);
425 inode = strtoll(wv[1], NULL, 10);
426 mtime = strtol(wv[2], NULL, 10);
427 if((hc = findhashcache(dev, inode)) == NULL)
434 buf = base64decode(wv[3], NULL);
435 memcpy(hc->tth, buf, 24);
439 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
443 static void hashexit(pid_t pid, int status, struct socket *outsock)
446 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
448 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
454 static int hashfile(char *path)
461 struct tigertreehash tth;
463 struct socket *outsock;
465 if((fd = open(path, O_RDONLY)) < 0)
467 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
470 if(fstat(fd, &sb) < 0)
472 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
478 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
485 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
494 signal(SIGHUP, SIG_DFL);
496 pfd[1] = dup2(pfd[1], 3);
499 for(i = 3; i < FD_SETSIZE; i++)
503 while((ret = read(0, buf, 4096)) > 0)
504 dotigertree(&tth, buf, ret);
507 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
511 restigertree(&tth, digest);
512 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));
518 outsock = wrapsock(pfd[0]);
519 outsock->readcb = hashread;
520 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
525 * Call only when hashjob == 0
527 static void checkhashes(void)
529 struct sharecache *node;
530 struct hashcache *hc;
533 node = shareroot->child;
534 for(node = shareroot->child; node != NULL; node = nextscnode(node))
536 if(node->f.b.type != FILE_REG)
538 if(!node->f.b.hastth)
540 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
542 memcpy(node->hashtth, hc->tth, 24);
543 node->f.b.hastth = 1;
544 GCBCHAINDOCB(sharechangecb, sharesize);
546 path = getfspath(node);
549 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
552 flog(LOG_INFO, "sharing %lli bytes", sharesize);
562 struct sharecache *nextscnode(struct sharecache *node)
564 if(node->child != NULL)
566 while(node->next == NULL)
569 if(node == shareroot)
575 static void freescan(struct scanstate *job)
582 /* No need for optimization; lookup isn't really that common */
583 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
585 struct sharecache *node;
587 for(node = parent->child; node != NULL; node = node->next)
589 if(!wcscmp(node->name, name))
595 static void attachcache(struct sharecache *parent, struct sharecache *node)
597 node->parent = parent;
598 node->next = parent->child;
599 if(parent->child != NULL)
600 parent->child->prev = node;
601 parent->child = node;
604 static void detachcache(struct sharecache *node)
606 if(node->next != NULL)
607 node->next->prev = node->prev;
608 if(node->prev != NULL)
609 node->prev->next = node->next;
610 if((node->parent != NULL) && (node->parent->child == node))
611 node->parent->child = node->next;
617 static void freecache(struct sharecache *node)
619 struct sharecache *cur, *next;
620 struct scanqueue *q, *nq, **fq;
624 for(q = scanqueue; q != NULL; q = nq)
627 if(q->state->node == node)
629 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
637 if(node->child != NULL)
639 for(cur = node->child; cur != NULL; cur = next)
645 CBCHAINDOCB(node, share_delete, node);
646 CBCHAINFREE(node, share_delete);
647 sharesize -= node->size;
648 if(node->path != NULL)
650 if(node->name != NULL)
655 static void freesharepoint(struct sharepoint *share)
657 struct sharecache *node;
659 if(share->next != NULL)
660 share->next->prev = share->prev;
661 if(share->prev != NULL)
662 share->prev->next = share->next;
664 shares = share->next;
665 if((node = findcache(shareroot, share->name)) != NULL)
672 static struct sharecache *newcache(void)
674 struct sharecache *new;
676 new = smalloc(sizeof(*new));
677 memset(new, 0, sizeof(*new));
678 CBCHAININIT(new, share_delete);
682 char *getfspath(struct sharecache *node)
687 buf = smalloc(bufsize = 64);
691 if(node->path != NULL)
693 if(bufsize < strlen(node->path) + strlen(buf) + 1)
694 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
695 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
696 memcpy(buf, node->path, strlen(node->path));
699 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
701 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
705 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
706 buf = srealloc(buf, bufsize *= 2);
707 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
708 memcpy(buf + 1, mbsname, strlen(mbsname));
713 buf = srealloc(buf, strlen(buf) + 1);
717 static int checknode(struct sharecache *node)
722 if(node->parent == NULL)
726 if(!checknode(node->parent))
728 path = getfspath(node);
729 if(stat(path, &sb) < 0)
731 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
732 queuescan(node->parent);
740 int opensharecache(struct sharecache *node)
745 path = getfspath(node);
746 fd = open(path, O_RDONLY);
750 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
758 static struct scanstate *newscan(struct sharecache *node)
760 struct scanstate *new;
762 new = smalloc(sizeof(*new));
769 void queuescan(struct sharecache *node)
771 struct scanqueue *new;
773 new = smalloc(sizeof(*new));
774 new->state = newscan(node);
775 new->next = scanqueue;
779 /* For internal use in doscan() */
780 static void removestale(struct sharecache *node)
782 struct sharecache *cur, *next;
784 for(cur = node->child; cur != NULL; cur = next)
792 /* For internal use in doscan() */
793 static void jobdone(void)
795 struct scanstate *jbuf;
798 scanjob = jbuf->next;
801 fchdir(dirfd(scanjob->dd));
804 int doscan(int quantum)
809 struct sharecache *n;
810 struct scanstate *jbuf;
811 struct scanqueue *qbuf;
814 struct hashcache *hc;
816 static int busybefore = 0;
818 dmask = confgetint("cli", "scandirmask");
819 fmask = confgetint("cli", "scanfilemask");
820 if((scanjob != NULL) && (scanjob->dd != NULL))
822 while(fchdir(dirfd(scanjob->dd)) < 0)
824 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
825 removestale(scanjob->node);
835 while(scanjob == NULL)
837 if(scanqueue == NULL)
841 flog(LOG_INFO, "sharing %lli bytes", sharesize);
843 GCBCHAINDOCB(sharechangecb, sharesize);
850 scanjob = scanqueue->state;
852 scanqueue = qbuf->next;
854 for(n = scanjob->node->child; n != NULL; n = n->next)
858 if(scanjob->dd == NULL)
860 path = getfspath(scanjob->node);
861 if((scanjob->dd = opendir(path)) == NULL)
863 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
864 freecache(scanjob->node);
870 if(fchdir(dirfd(scanjob->dd)) < 0)
872 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
877 if((de = readdir(scanjob->dd)) == NULL)
879 removestale(scanjob->node);
883 if(*de->d_name == '.')
885 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
887 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
890 n = findcache(scanjob->node, wcs);
891 if(stat(de->d_name, &sb) < 0)
896 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
899 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
903 if(S_ISDIR(sb.st_mode))
905 if(~sb.st_mode & dmask)
911 } else if(S_ISREG(sb.st_mode)) {
912 if(~sb.st_mode & fmask)
919 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
925 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
935 if(S_ISREG(sb.st_mode))
937 sharesize += (n->size = sb.st_size);
941 n->mtime = sb.st_mtime;
943 n->inode = sb.st_ino;
945 attachcache(scanjob->node, n);
950 if(n->f.b.type == FILE_DIR)
953 jbuf->next = scanjob;
955 } else if(n->f.b.type == FILE_REG) {
956 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
960 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
962 if(hc->mtime == n->mtime)
965 memcpy(n->hashtth, hc->tth, 24);
976 static void rescancb(int cancelled, void *uudata)
981 if(scanqueue == NULL)
983 else if(confgetint("cli", "rescandelay") > 0)
984 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
988 void scanshares(void)
990 struct sharepoint *cur;
991 struct sharecache *node;
994 for(cur = shares; cur != NULL; cur = cur->next)
996 if((node = findcache(shareroot, cur->name)) == NULL)
998 if(stat(cur->path, &sb))
1000 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1003 if(!S_ISDIR(sb.st_mode))
1005 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1009 node->name = swcsdup(cur->name);
1010 node->path = sstrdup(cur->path);
1011 if(node->path[strlen(node->path) - 1] == '/')
1012 node->path[strlen(node->path) - 1] = 0;
1013 node->f.b.type = FILE_DIR;
1014 attachcache(shareroot, node);
1018 if(scantimer != NULL)
1019 canceltimer(scantimer);
1020 if(confgetint("cli", "rescandelay") > 0)
1021 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
1024 static void preinit(int hup)
1026 struct sharepoint *cur;
1030 for(cur = shares; cur != NULL; cur = cur->next)
1033 shareroot = newcache();
1034 shareroot->name = swcsdup(L"");
1035 shareroot->f.b.type = FILE_DIR;
1039 static int rsdelayupdate(struct configvar *var, void *uudata)
1041 if(scantimer != NULL)
1042 canceltimer(scantimer);
1043 if(confgetint("cli", "rescandelay") > 0)
1044 scantimer = timercallback(ntime() + var->val.num, (void (*)(int, void *))rescancb, NULL);
1048 static int init(int hup)
1050 struct sharepoint *cur, *next;
1053 for(cur = shares; cur != NULL; cur = next)
1057 freesharepoint(cur);
1063 CBREG(confgetvar("cli", "rescandelay"), conf_update, rsdelayupdate, NULL, NULL);
1068 static int run(void)
1078 static void terminate(void)
1081 kill(hashjob, SIGHUP);
1082 while(shares != NULL)
1083 freesharepoint(shares);
1084 freecache(shareroot);
1087 static struct module me =
1098 .terminate = terminate