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));
299 while(hashcache != NULL)
300 freehashcache(hashcache);
304 fgets(linebuf, sizeof(linebuf), stream);
306 for(p = linebuf; *p; p++)
311 if(linebuf[0] == '#')
313 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
323 hc->dev = strtoll(wv[0], NULL, 10);
324 hc->inode = strtoll(wv[1], NULL, 10);
325 hc->mtime = strtoll(wv[2], NULL, 10);
326 for(i = 3; i < wc; i++)
328 if(!strcmp(wv[i], "tth"))
332 hash = base64decode(wv[i], &len);
338 memcpy(hc->tth, hash, 24);
346 static void hashtimercb(int cancelled, void *uudata)
348 hashwritetimer = NULL;
353 static void writehashcache(int now)
358 struct hashcache *hc;
362 if(hashwritetimer == NULL)
363 hashwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), (void (*)(int, void *))hashtimercb, NULL);
366 if(hashwritetimer != NULL)
367 canceltimer(hashwritetimer);
368 hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 1);
369 if((stream = fopen(hcname, "w")) == NULL)
371 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
374 fprintf(stream, "# Dolda Connect hash cache file\n");
375 fprintf(stream, "# Generated automatically, do not edit\n");
376 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
377 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
378 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
379 for(hc = hashcache; hc != NULL; hc = hc->next)
381 buf = base64encode(hc->tth, 24);
382 fprintf(stream, "%lli %lli %li tth %s\n", (long long)hc->dev, (long long)hc->inode, hc->mtime, buf);
388 static void hashread(struct socket *sk, void *uudata)
390 static char *hashbuf;
391 static size_t hashbufsize = 0, hashbufdata = 0;
392 char *buf, *p, *p2, *lp;
399 struct hashcache *hc;
401 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
403 bufcat(hashbuf, buf, bufsize);
405 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
412 while((p2 = strchr(p, ' ')) == p)
425 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
427 dev = strtoll(wv[0], NULL, 10);
428 inode = strtoll(wv[1], NULL, 10);
429 mtime = strtol(wv[2], NULL, 10);
430 if((hc = findhashcache(dev, inode)) == NULL)
437 buf = base64decode(wv[3], NULL);
438 memcpy(hc->tth, buf, 24);
442 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
446 static void hashexit(pid_t pid, int status, struct socket *outsock)
449 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
451 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
457 static int hashfile(char *path)
464 struct tigertreehash tth;
466 struct socket *outsock;
468 if((fd = open(path, O_RDONLY)) < 0)
470 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
473 if(fstat(fd, &sb) < 0)
475 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
481 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
488 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
497 signal(SIGHUP, SIG_DFL);
499 pfd[1] = dup2(pfd[1], 3);
502 for(i = 3; i < FD_SETSIZE; i++)
506 while((ret = read(0, buf, 4096)) > 0)
507 dotigertree(&tth, buf, ret);
510 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
514 restigertree(&tth, digest);
515 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));
521 outsock = wrapsock(pfd[0]);
522 outsock->readcb = hashread;
523 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
528 * Call only when hashjob == 0
530 static void checkhashes(void)
532 struct sharecache *node, *next;
533 struct hashcache *hc;
536 node = shareroot->child;
537 for(node = shareroot->child; node != NULL; node = next)
539 next = nextscnode(node);
540 if(node->f.b.type != FILE_REG)
542 if(!node->f.b.hastth)
544 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
546 memcpy(node->hashtth, hc->tth, 24);
547 node->f.b.hastth = 1;
548 GCBCHAINDOCB(sharechangecb, sharesize);
550 path = getfspath(node);
553 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
556 flog(LOG_INFO, "sharing %lli bytes", sharesize);
566 struct sharecache *nextscnode(struct sharecache *node)
568 if(node->child != NULL)
570 while(node->next == NULL)
573 if(node == shareroot)
579 static void freescan(struct scanstate *job)
586 /* No need for optimization; lookup isn't really that common */
587 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
589 struct sharecache *node;
591 for(node = parent->child; node != NULL; node = node->next)
593 if(!wcscmp(node->name, name))
599 static void attachcache(struct sharecache *parent, struct sharecache *node)
601 node->parent = parent;
602 node->next = parent->child;
603 if(parent->child != NULL)
604 parent->child->prev = node;
605 parent->child = node;
608 static void detachcache(struct sharecache *node)
610 if(node->next != NULL)
611 node->next->prev = node->prev;
612 if(node->prev != NULL)
613 node->prev->next = node->next;
614 if((node->parent != NULL) && (node->parent->child == node))
615 node->parent->child = node->next;
621 static void freecache(struct sharecache *node)
623 struct sharecache *cur, *next;
624 struct scanqueue *q, *nq, **fq;
628 for(q = scanqueue; q != NULL; q = nq)
631 if(q->state->node == node)
633 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
641 if(node->child != NULL)
643 for(cur = node->child; cur != NULL; cur = next)
649 CBCHAINDOCB(node, share_delete, node);
650 CBCHAINFREE(node, share_delete);
651 sharesize -= node->size;
652 if(node->f.b.type == FILE_REG)
654 if(node->path != NULL)
656 if(node->name != NULL)
661 static void freesharepoint(struct sharepoint *share)
663 struct sharecache *node;
665 if(share->next != NULL)
666 share->next->prev = share->prev;
667 if(share->prev != NULL)
668 share->prev->next = share->next;
670 shares = share->next;
671 if((node = findcache(shareroot, share->name)) != NULL)
678 static struct sharecache *newcache(void)
680 struct sharecache *new;
682 new = smalloc(sizeof(*new));
683 memset(new, 0, sizeof(*new));
684 CBCHAININIT(new, share_delete);
688 char *getfspath(struct sharecache *node)
693 buf = smalloc(bufsize = 64);
697 if(node->path != NULL)
699 if(bufsize < strlen(node->path) + strlen(buf) + 1)
700 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
701 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
702 memcpy(buf, node->path, strlen(node->path));
705 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
707 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
711 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
712 buf = srealloc(buf, bufsize *= 2);
713 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
714 memcpy(buf + 1, mbsname, strlen(mbsname));
719 buf = srealloc(buf, strlen(buf) + 1);
723 static int checknode(struct sharecache *node)
728 if(node->parent == NULL)
732 if(!checknode(node->parent))
734 path = getfspath(node);
735 if(stat(path, &sb) < 0)
737 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
738 queuescan(node->parent);
746 int opensharecache(struct sharecache *node)
751 path = getfspath(node);
752 fd = open(path, O_RDONLY);
756 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
764 static struct scanstate *newscan(struct sharecache *node)
766 struct scanstate *new;
768 new = smalloc(sizeof(*new));
775 void queuescan(struct sharecache *node)
777 struct scanqueue *new;
779 new = smalloc(sizeof(*new));
780 new->state = newscan(node);
781 new->next = scanqueue;
785 /* For internal use in doscan() */
786 static void removestale(struct sharecache *node)
788 struct sharecache *cur, *next;
790 for(cur = node->child; cur != NULL; cur = next)
798 /* For internal use in doscan() */
799 static void jobdone(void)
801 struct scanstate *jbuf;
804 scanjob = jbuf->next;
807 fchdir(dirfd(scanjob->dd));
810 int doscan(int quantum)
815 struct sharecache *n;
816 struct scanstate *jbuf;
817 struct scanqueue *qbuf;
820 struct hashcache *hc;
822 static int busybefore = 0;
824 dmask = confgetint("cli", "scandirmask");
825 fmask = confgetint("cli", "scanfilemask");
826 if((scanjob != NULL) && (scanjob->dd != NULL))
828 while(fchdir(dirfd(scanjob->dd)) < 0)
830 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
831 removestale(scanjob->node);
841 while(scanjob == NULL)
843 if(scanqueue == NULL)
847 flog(LOG_INFO, "sharing %lli bytes", sharesize);
849 GCBCHAINDOCB(sharechangecb, sharesize);
856 scanjob = scanqueue->state;
858 scanqueue = qbuf->next;
860 for(n = scanjob->node->child; n != NULL; n = n->next)
864 if(scanjob->dd == NULL)
866 path = getfspath(scanjob->node);
867 if((scanjob->dd = opendir(path)) == NULL)
869 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
870 freecache(scanjob->node);
876 if(fchdir(dirfd(scanjob->dd)) < 0)
878 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
883 if((de = readdir(scanjob->dd)) == NULL)
885 removestale(scanjob->node);
889 if(*de->d_name == '.')
891 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
893 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
896 n = findcache(scanjob->node, wcs);
897 if(stat(de->d_name, &sb) < 0)
902 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
905 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
909 if(S_ISDIR(sb.st_mode))
911 if(~sb.st_mode & dmask)
917 } else if(S_ISREG(sb.st_mode)) {
918 if(~sb.st_mode & fmask)
925 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
931 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
941 if(S_ISREG(sb.st_mode))
943 sharesize += (n->size = sb.st_size);
948 n->mtime = sb.st_mtime;
950 n->inode = sb.st_ino;
952 attachcache(scanjob->node, n);
957 if(n->f.b.type == FILE_DIR)
960 jbuf->next = scanjob;
962 } else if(n->f.b.type == FILE_REG) {
963 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
967 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
969 if(hc->mtime == n->mtime)
972 memcpy(n->hashtth, hc->tth, 24);
983 static void rescancb(int cancelled, void *uudata)
988 if(scanqueue == NULL)
990 else if(confgetint("cli", "rescandelay") > 0)
991 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
995 void scanshares(void)
997 struct sharepoint *cur;
998 struct sharecache *node;
1001 for(cur = shares; cur != NULL; cur = cur->next)
1003 if((node = findcache(shareroot, cur->name)) == NULL)
1005 if(stat(cur->path, &sb))
1007 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1010 if(!S_ISDIR(sb.st_mode))
1012 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1016 node->name = swcsdup(cur->name);
1017 node->path = sstrdup(cur->path);
1018 if(node->path[strlen(node->path) - 1] == '/')
1019 node->path[strlen(node->path) - 1] = 0;
1020 node->f.b.type = FILE_DIR;
1021 attachcache(shareroot, node);
1025 if(scantimer != NULL)
1026 canceltimer(scantimer);
1027 if(confgetint("cli", "rescandelay") > 0)
1028 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
1031 static void preinit(int hup)
1033 struct sharepoint *cur;
1037 for(cur = shares; cur != NULL; cur = cur->next)
1040 shareroot = newcache();
1041 shareroot->name = swcsdup(L"");
1042 shareroot->f.b.type = FILE_DIR;
1046 static int rsdelayupdate(struct configvar *var, void *uudata)
1048 if(scantimer != NULL)
1049 canceltimer(scantimer);
1050 if(confgetint("cli", "rescandelay") > 0)
1051 scantimer = timercallback(ntime() + var->val.num, (void (*)(int, void *))rescancb, NULL);
1055 static int init(int hup)
1057 struct sharepoint *cur, *next;
1060 for(cur = shares; cur != NULL; cur = next)
1064 freesharepoint(cur);
1070 CBREG(confgetvar("cli", "rescandelay"), conf_update, rsdelayupdate, NULL, NULL);
1075 static int run(void)
1085 static void terminate(void)
1088 kill(hashjob, SIGHUP);
1089 if(hashwritetimer != NULL)
1091 while(shares != NULL)
1092 freesharepoint(shares);
1093 freecache(shareroot);
1096 static struct module me =
1107 .terminate = terminate