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. (Broken shares are
85 * always rescanned upon detection, regardless of this
87 {CONF_VAR_INT, "rescandelay", {.num = 0}},
91 static struct configcmd mycmds[] =
93 {"share", conf_share},
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
106 static pid_t hashjob = -1;
107 struct sharecache *shareroot = NULL;
108 static struct timer *scantimer = NULL;
110 unsigned long long sharesize = 0;
111 GCBCHAIN(sharechangecb, unsigned long long);
113 static int conf_share(int argc, wchar_t **argv)
115 struct sharepoint *share;
120 flog(LOG_WARNING, "not enough arguments given for share command");
123 if((b = icwcstombs(argv[2], NULL)) == NULL)
125 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
128 for(share = shares; share != NULL; share = share->next)
130 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
137 share = smalloc(sizeof(*share));
140 share->name = swcsdup(argv[1]);
141 share->next = shares;
144 shares->prev = share;
149 static void dumpsharecache(struct sharecache *node, int l)
153 for(; node != NULL; node = node->next)
155 for(i = 0; i < l; i++)
157 printf("%ls\n", node->name);
158 if(node->f.b.type == FILE_DIR)
159 dumpsharecache(node->child, l + 1);
163 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
167 ret = smalloc(sizeof(*ret));
168 memset(ret, 0, sizeof(*ret));
169 ret->algo = swcsdup(algo);
171 ret->buf = memcpy(smalloc(len), buf, len);
175 void freehash(struct hash *hash)
182 struct hash *duphash(struct hash *hash)
184 return(newhash(hash->algo, hash->len, hash->buf));
187 struct hash *parsehash(wchar_t *text)
190 char *mbsbuf, *decbuf;
194 if((p = wcschr(text, L':')) == NULL)
197 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
199 decbuf = base64decode(mbsbuf, &buflen);
203 ret = newhash(text, buflen, decbuf);
208 wchar_t *unparsehash(struct hash *hash)
210 static wchar_t *buf = NULL;
213 size_t bufsize, bufdata;
218 bufsize = bufdata = 0;
219 hbuf = base64encode(hash->buf, hash->len);
220 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
222 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
226 bufcat(buf, hash->algo, wcslen(hash->algo));
228 bufcat(buf, whbuf, wcslen(whbuf));
234 int hashcmp(struct hash *h1, struct hash *h2)
236 if(wcscmp(h1->algo, h2->algo))
238 if(h1->len != h2->len)
240 if(memcmp(h1->buf, h2->buf, h1->len))
245 static struct hashcache *newhashcache(void)
247 struct hashcache *new;
249 new = smalloc(sizeof(*new));
250 memset(new, 0, sizeof(*new));
251 new->next = hashcache;
253 if(hashcache != NULL)
254 hashcache->prev = new;
259 static void freehashcache(struct hashcache *hc)
262 hc->next->prev = hc->prev;
264 hc->prev->next = hc->next;
266 hashcache = hc->next;
270 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
272 struct hashcache *hc;
274 for(hc = hashcache; hc != NULL; hc = hc->next)
276 if((hc->dev == dev) && (hc->inode == inode))
282 static void readhashcache(void)
288 char *p, *p2, *wv[32], *hash;
289 struct hashcache *hc;
292 if((hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 0)) == NULL)
294 if((stream = fopen(hcname, "r")) == NULL)
296 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
301 while(hashcache != NULL)
302 freehashcache(hashcache);
306 fgets(linebuf, sizeof(linebuf), stream);
308 for(p = linebuf; *p; p++)
313 if(linebuf[0] == '#')
315 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
325 hc->dev = strtoll(wv[0], NULL, 10);
326 hc->inode = strtoll(wv[1], NULL, 10);
327 hc->mtime = strtoll(wv[2], NULL, 10);
328 for(i = 3; i < wc; i++)
330 if(!strcmp(wv[i], "tth"))
334 hash = base64decode(wv[i], &len);
340 memcpy(hc->tth, hash, 24);
348 static void hashtimercb(int cancelled, void *uudata)
350 hashwritetimer = NULL;
355 static void writehashcache(int now)
360 struct hashcache *hc;
364 if(hashwritetimer == NULL)
365 hashwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), (void (*)(int, void *))hashtimercb, NULL);
368 if(hashwritetimer != NULL)
369 canceltimer(hashwritetimer);
370 hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 1);
371 if((stream = fopen(hcname, "w")) == NULL)
373 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
378 fprintf(stream, "# Dolda Connect hash cache file\n");
379 fprintf(stream, "# Generated automatically, do not edit\n");
380 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
381 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
382 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
383 for(hc = hashcache; hc != NULL; hc = hc->next)
385 buf = base64encode(hc->tth, 24);
386 fprintf(stream, "%lli %lli %li tth %s\n", (long long)hc->dev, (long long)hc->inode, hc->mtime, buf);
392 static void hashread(struct socket *sk, void *uudata)
394 static char *hashbuf;
395 static size_t hashbufsize = 0, hashbufdata = 0;
396 char *buf, *p, *p2, *lp;
403 struct hashcache *hc;
405 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
407 bufcat(hashbuf, buf, bufsize);
409 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
416 while((p2 = strchr(p, ' ')) == p)
429 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
431 dev = strtoll(wv[0], NULL, 10);
432 inode = strtoll(wv[1], NULL, 10);
433 mtime = strtol(wv[2], NULL, 10);
434 if((hc = findhashcache(dev, inode)) == NULL)
441 buf = base64decode(wv[3], NULL);
442 memcpy(hc->tth, buf, 24);
446 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
450 static void hashexit(pid_t pid, int status, struct socket *outsock)
453 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
455 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
461 static int hashfile(char *path)
468 struct tigertreehash tth;
470 struct socket *outsock;
472 if((fd = open(path, O_RDONLY)) < 0)
474 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
477 if(fstat(fd, &sb) < 0)
479 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
485 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
492 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
501 signal(SIGHUP, SIG_DFL);
503 pfd[1] = dup2(pfd[1], 3);
506 for(i = 3; i < FD_SETSIZE; i++)
510 while((ret = read(0, buf, 4096)) > 0)
511 dotigertree(&tth, buf, ret);
514 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
518 restigertree(&tth, digest);
519 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));
525 outsock = wrapsock(pfd[0]);
526 outsock->readcb = hashread;
527 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
532 * Call only when hashjob == 0
534 static void checkhashes(void)
536 struct sharecache *node, *next;
537 struct hashcache *hc;
540 node = shareroot->child;
541 for(node = shareroot->child; node != NULL; node = next)
543 next = nextscnode(node);
544 if(node->f.b.type != FILE_REG)
546 if(!node->f.b.hastth)
548 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
550 memcpy(node->hashtth, hc->tth, 24);
551 node->f.b.hastth = 1;
552 GCBCHAINDOCB(sharechangecb, sharesize);
554 path = getfspath(node);
557 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
560 flog(LOG_INFO, "sharing %lli bytes", sharesize);
570 struct sharecache *nextscnode(struct sharecache *node)
572 if(node->child != NULL)
574 while(node->next == NULL)
577 if(node == shareroot)
583 static void freescan(struct scanstate *job)
590 /* No need for optimization; lookup isn't really that common */
591 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
593 struct sharecache *node;
595 for(node = parent->child; node != NULL; node = node->next)
597 if(!wcscmp(node->name, name))
603 static void attachcache(struct sharecache *parent, struct sharecache *node)
605 node->parent = parent;
606 node->next = parent->child;
607 if(parent->child != NULL)
608 parent->child->prev = node;
609 parent->child = node;
612 static void detachcache(struct sharecache *node)
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;
625 static void freecache(struct sharecache *node)
627 struct sharecache *cur, *next;
628 struct scanqueue *q, *nq, **fq;
632 for(q = scanqueue; q != NULL; q = nq)
635 if(q->state->node == node)
637 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
645 if(node->child != NULL)
647 for(cur = node->child; cur != NULL; cur = next)
653 CBCHAINDOCB(node, share_delete, node);
654 CBCHAINFREE(node, share_delete);
655 sharesize -= node->size;
656 if(node->f.b.type == FILE_REG)
658 if(node->path != NULL)
660 if(node->name != NULL)
665 static void freesharepoint(struct sharepoint *share)
667 struct sharecache *node;
669 if(share->next != NULL)
670 share->next->prev = share->prev;
671 if(share->prev != NULL)
672 share->prev->next = share->next;
674 shares = share->next;
675 if((node = findcache(shareroot, share->name)) != NULL)
682 static struct sharecache *newcache(void)
684 struct sharecache *new;
686 new = smalloc(sizeof(*new));
687 memset(new, 0, sizeof(*new));
688 CBCHAININIT(new, share_delete);
692 char *getfspath(struct sharecache *node)
697 buf = smalloc(bufsize = 64);
701 if(node->path != NULL)
703 if(bufsize < strlen(node->path) + strlen(buf) + 1)
704 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
705 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
706 memcpy(buf, node->path, strlen(node->path));
709 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
711 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
715 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
716 buf = srealloc(buf, bufsize *= 2);
717 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
718 memcpy(buf + 1, mbsname, strlen(mbsname));
723 buf = srealloc(buf, strlen(buf) + 1);
727 static int checknode(struct sharecache *node)
732 if(node->parent == NULL)
736 if(!checknode(node->parent))
738 path = getfspath(node);
739 if(stat(path, &sb) < 0)
741 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
742 queuescan(node->parent);
750 int opensharecache(struct sharecache *node)
755 path = getfspath(node);
756 fd = open(path, O_RDONLY);
760 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
768 static struct scanstate *newscan(struct sharecache *node)
770 struct scanstate *new;
772 new = smalloc(sizeof(*new));
779 void queuescan(struct sharecache *node)
781 struct scanqueue *new;
783 new = smalloc(sizeof(*new));
784 new->state = newscan(node);
785 new->next = scanqueue;
789 /* For internal use in doscan() */
790 static void removestale(struct sharecache *node)
792 struct sharecache *cur, *next;
794 for(cur = node->child; cur != NULL; cur = next)
802 /* For internal use in doscan() */
803 static void jobdone(void)
805 struct scanstate *jbuf;
808 scanjob = jbuf->next;
811 fchdir(dirfd(scanjob->dd));
814 int doscan(int quantum)
819 struct sharecache *n;
820 struct scanstate *jbuf;
821 struct scanqueue *qbuf;
824 struct hashcache *hc;
826 static int busybefore = 0;
828 dmask = confgetint("cli", "scandirmask");
829 fmask = confgetint("cli", "scanfilemask");
830 if((scanjob != NULL) && (scanjob->dd != NULL))
832 while(fchdir(dirfd(scanjob->dd)) < 0)
834 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
835 removestale(scanjob->node);
845 while(scanjob == NULL)
847 if(scanqueue == NULL)
851 flog(LOG_INFO, "sharing %lli bytes", sharesize);
853 GCBCHAINDOCB(sharechangecb, sharesize);
860 scanjob = scanqueue->state;
862 scanqueue = qbuf->next;
864 for(n = scanjob->node->child; n != NULL; n = n->next)
868 if(scanjob->dd == NULL)
870 path = getfspath(scanjob->node);
871 if((scanjob->dd = opendir(path)) == NULL)
873 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
874 freecache(scanjob->node);
880 if(fchdir(dirfd(scanjob->dd)) < 0)
882 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
887 if((de = readdir(scanjob->dd)) == NULL)
889 removestale(scanjob->node);
893 if(*de->d_name == '.')
895 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
897 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
900 n = findcache(scanjob->node, wcs);
901 if(stat(de->d_name, &sb) < 0)
906 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
909 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
913 if(S_ISDIR(sb.st_mode))
915 if(~sb.st_mode & dmask)
921 } else if(S_ISREG(sb.st_mode)) {
922 if(~sb.st_mode & fmask)
929 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
935 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
945 if(S_ISREG(sb.st_mode))
947 sharesize += (n->size = sb.st_size);
952 n->mtime = sb.st_mtime;
954 n->inode = sb.st_ino;
956 attachcache(scanjob->node, n);
961 if(n->f.b.type == FILE_DIR)
964 jbuf->next = scanjob;
966 } else if(n->f.b.type == FILE_REG) {
967 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
971 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
973 if(hc->mtime == n->mtime)
976 memcpy(n->hashtth, hc->tth, 24);
987 static void rescancb(int cancelled, void *uudata)
992 if(scanqueue == NULL)
994 else if(confgetint("cli", "rescandelay") > 0)
995 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
999 void scanshares(void)
1001 struct sharepoint *cur;
1002 struct sharecache *node;
1005 for(cur = shares; cur != NULL; cur = cur->next)
1007 if((node = findcache(shareroot, cur->name)) == NULL)
1009 if(stat(cur->path, &sb))
1011 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1014 if(!S_ISDIR(sb.st_mode))
1016 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1020 node->name = swcsdup(cur->name);
1021 node->path = sstrdup(cur->path);
1022 if(node->path[strlen(node->path) - 1] == '/')
1023 node->path[strlen(node->path) - 1] = 0;
1024 node->f.b.type = FILE_DIR;
1025 attachcache(shareroot, node);
1029 if(scantimer != NULL)
1030 canceltimer(scantimer);
1031 if(confgetint("cli", "rescandelay") > 0)
1032 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
1035 static void preinit(int hup)
1037 struct sharepoint *cur;
1041 for(cur = shares; cur != NULL; cur = cur->next)
1044 shareroot = newcache();
1045 shareroot->name = swcsdup(L"");
1046 shareroot->f.b.type = FILE_DIR;
1050 static int rsdelayupdate(struct configvar *var, void *uudata)
1052 if(scantimer != NULL)
1053 canceltimer(scantimer);
1054 if(confgetint("cli", "rescandelay") > 0)
1055 scantimer = timercallback(ntime() + var->val.num, (void (*)(int, void *))rescancb, NULL);
1059 static int init(int hup)
1061 struct sharepoint *cur, *next;
1064 for(cur = shares; cur != NULL; cur = next)
1068 freesharepoint(cur);
1074 CBREG(confgetvar("cli", "rescandelay"), conf_update, rsdelayupdate, NULL, NULL);
1079 static int run(void)
1089 static void terminate(void)
1092 kill(hashjob, SIGHUP);
1093 if(hashwritetimer != NULL)
1095 while(shares != NULL)
1096 freesharepoint(shares);
1097 freecache(shareroot);
1100 static struct module me =
1111 .terminate = terminate