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;
109 unsigned long long sharesize = 0;
110 GCBCHAIN(sharechangecb, unsigned long long);
112 static int conf_share(int argc, wchar_t **argv)
114 struct sharepoint *share;
119 flog(LOG_WARNING, "not enough arguments given for share command");
122 if((b = icwcstombs(argv[2], NULL)) == NULL)
124 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
127 for(share = shares; share != NULL; share = share->next)
129 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
136 share = smalloc(sizeof(*share));
139 share->name = swcsdup(argv[1]);
140 share->next = shares;
143 shares->prev = share;
148 static void dumpsharecache(struct sharecache *node, int l)
152 for(; node != NULL; node = node->next)
154 for(i = 0; i < l; i++)
156 printf("%ls\n", node->name);
157 if(node->f.b.type == FILE_DIR)
158 dumpsharecache(node->child, l + 1);
162 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
166 ret = smalloc(sizeof(*ret));
167 memset(ret, 0, sizeof(*ret));
168 ret->algo = swcsdup(algo);
170 ret->buf = memcpy(smalloc(len), buf, len);
174 void freehash(struct hash *hash)
181 struct hash *duphash(struct hash *hash)
183 return(newhash(hash->algo, hash->len, hash->buf));
186 struct hash *parsehash(wchar_t *text)
189 char *mbsbuf, *decbuf;
193 if((p = wcschr(text, L':')) == NULL)
196 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
198 decbuf = base64decode(mbsbuf, &buflen);
202 ret = newhash(text, buflen, decbuf);
207 wchar_t *unparsehash(struct hash *hash)
209 static wchar_t *buf = NULL;
212 size_t bufsize, bufdata;
217 bufsize = bufdata = 0;
218 hbuf = base64encode(hash->buf, hash->len);
219 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
221 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
225 bufcat(buf, hash->algo, wcslen(hash->algo));
227 bufcat(buf, whbuf, wcslen(whbuf));
233 int hashcmp(struct hash *h1, struct hash *h2)
235 if(wcscmp(h1->algo, h2->algo))
237 if(h1->len != h2->len)
239 if(memcmp(h1->buf, h2->buf, h1->len))
244 static struct hashcache *newhashcache(void)
246 struct hashcache *new;
248 new = smalloc(sizeof(*new));
249 memset(new, 0, sizeof(*new));
250 new->next = hashcache;
252 if(hashcache != NULL)
253 hashcache->prev = new;
258 static void freehashcache(struct hashcache *hc)
261 hc->next->prev = hc->prev;
263 hc->prev->next = hc->next;
265 hashcache = hc->next;
269 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
271 struct hashcache *hc;
273 for(hc = hashcache; hc != NULL; hc = hc->next)
275 if((hc->dev == dev) && (hc->inode == inode))
281 static void readhashcache(void)
287 char *p, *p2, *wv[32], *hash;
288 struct hashcache *hc;
291 if((hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 0)) == NULL)
293 if((stream = fopen(hcname, "r")) == NULL)
295 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
298 while(hashcache != NULL)
299 freehashcache(hashcache);
303 fgets(linebuf, sizeof(linebuf), stream);
305 for(p = linebuf; *p; p++)
310 if(linebuf[0] == '#')
312 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
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++)
327 if(!strcmp(wv[i], "tth"))
331 hash = base64decode(wv[i], &len);
337 memcpy(hc->tth, hash, 24);
345 static void hashtimercb(int cancelled, void *uudata)
347 hashwritetimer = NULL;
352 static void writehashcache(int now)
357 struct hashcache *hc;
361 if(hashwritetimer == NULL)
362 hashwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), (void (*)(int, void *))hashtimercb, NULL);
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)
370 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
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)
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);
387 static void hashread(struct socket *sk, void *uudata)
389 static char *hashbuf;
390 static size_t hashbufsize = 0, hashbufdata = 0;
391 char *buf, *p, *p2, *lp;
398 struct hashcache *hc;
400 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
402 bufcat(hashbuf, buf, bufsize);
404 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
411 while((p2 = strchr(p, ' ')) == p)
424 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
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)
436 buf = base64decode(wv[3], NULL);
437 memcpy(hc->tth, buf, 24);
441 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
445 static void hashexit(pid_t pid, int status, struct socket *outsock)
448 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
450 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
456 static int hashfile(char *path)
463 struct tigertreehash tth;
465 struct socket *outsock;
467 if((fd = open(path, O_RDONLY)) < 0)
469 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
472 if(fstat(fd, &sb) < 0)
474 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
480 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
487 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
496 signal(SIGHUP, SIG_DFL);
498 pfd[1] = dup2(pfd[1], 3);
501 for(i = 3; i < FD_SETSIZE; i++)
505 while((ret = read(0, buf, 4096)) > 0)
506 dotigertree(&tth, buf, ret);
509 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
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));
520 outsock = wrapsock(pfd[0]);
521 outsock->readcb = hashread;
522 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
527 * Call only when hashjob == 0
529 static void checkhashes(void)
531 struct sharecache *node, *next;
532 struct hashcache *hc;
535 node = shareroot->child;
536 for(node = shareroot->child; node != NULL; node = next)
538 next = nextscnode(node);
539 if(node->f.b.type != FILE_REG)
541 if(!node->f.b.hastth)
543 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
545 memcpy(node->hashtth, hc->tth, 24);
546 node->f.b.hastth = 1;
547 GCBCHAINDOCB(sharechangecb, sharesize);
549 path = getfspath(node);
552 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
555 flog(LOG_INFO, "sharing %lli bytes", sharesize);
565 struct sharecache *nextscnode(struct sharecache *node)
567 if(node->child != NULL)
569 while(node->next == NULL)
572 if(node == shareroot)
578 static void freescan(struct scanstate *job)
585 /* No need for optimization; lookup isn't really that common */
586 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
588 struct sharecache *node;
590 for(node = parent->child; node != NULL; node = node->next)
592 if(!wcscmp(node->name, name))
598 static void attachcache(struct sharecache *parent, struct sharecache *node)
600 node->parent = parent;
601 node->next = parent->child;
602 if(parent->child != NULL)
603 parent->child->prev = node;
604 parent->child = node;
607 static void detachcache(struct sharecache *node)
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;
620 static void freecache(struct sharecache *node)
622 struct sharecache *cur, *next;
623 struct scanqueue *q, *nq, **fq;
627 for(q = scanqueue; q != NULL; q = nq)
630 if(q->state->node == node)
632 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
640 if(node->child != NULL)
642 for(cur = node->child; cur != NULL; cur = next)
648 CBCHAINDOCB(node, share_delete, node);
649 CBCHAINFREE(node, share_delete);
650 sharesize -= node->size;
651 if(node->path != NULL)
653 if(node->name != NULL)
658 static void freesharepoint(struct sharepoint *share)
660 struct sharecache *node;
662 if(share->next != NULL)
663 share->next->prev = share->prev;
664 if(share->prev != NULL)
665 share->prev->next = share->next;
667 shares = share->next;
668 if((node = findcache(shareroot, share->name)) != NULL)
675 static struct sharecache *newcache(void)
677 struct sharecache *new;
679 new = smalloc(sizeof(*new));
680 memset(new, 0, sizeof(*new));
681 CBCHAININIT(new, share_delete);
685 char *getfspath(struct sharecache *node)
690 buf = smalloc(bufsize = 64);
694 if(node->path != NULL)
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));
702 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
704 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
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));
716 buf = srealloc(buf, strlen(buf) + 1);
720 static int checknode(struct sharecache *node)
725 if(node->parent == NULL)
729 if(!checknode(node->parent))
731 path = getfspath(node);
732 if(stat(path, &sb) < 0)
734 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
735 queuescan(node->parent);
743 int opensharecache(struct sharecache *node)
748 path = getfspath(node);
749 fd = open(path, O_RDONLY);
753 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
761 static struct scanstate *newscan(struct sharecache *node)
763 struct scanstate *new;
765 new = smalloc(sizeof(*new));
772 void queuescan(struct sharecache *node)
774 struct scanqueue *new;
776 new = smalloc(sizeof(*new));
777 new->state = newscan(node);
778 new->next = scanqueue;
782 /* For internal use in doscan() */
783 static void removestale(struct sharecache *node)
785 struct sharecache *cur, *next;
787 for(cur = node->child; cur != NULL; cur = next)
795 /* For internal use in doscan() */
796 static void jobdone(void)
798 struct scanstate *jbuf;
801 scanjob = jbuf->next;
804 fchdir(dirfd(scanjob->dd));
807 int doscan(int quantum)
812 struct sharecache *n;
813 struct scanstate *jbuf;
814 struct scanqueue *qbuf;
817 struct hashcache *hc;
819 static int busybefore = 0;
821 dmask = confgetint("cli", "scandirmask");
822 fmask = confgetint("cli", "scanfilemask");
823 if((scanjob != NULL) && (scanjob->dd != NULL))
825 while(fchdir(dirfd(scanjob->dd)) < 0)
827 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
828 removestale(scanjob->node);
838 while(scanjob == NULL)
840 if(scanqueue == NULL)
844 flog(LOG_INFO, "sharing %lli bytes", sharesize);
846 GCBCHAINDOCB(sharechangecb, sharesize);
853 scanjob = scanqueue->state;
855 scanqueue = qbuf->next;
857 for(n = scanjob->node->child; n != NULL; n = n->next)
861 if(scanjob->dd == NULL)
863 path = getfspath(scanjob->node);
864 if((scanjob->dd = opendir(path)) == NULL)
866 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
867 freecache(scanjob->node);
873 if(fchdir(dirfd(scanjob->dd)) < 0)
875 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
880 if((de = readdir(scanjob->dd)) == NULL)
882 removestale(scanjob->node);
886 if(*de->d_name == '.')
888 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
890 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
893 n = findcache(scanjob->node, wcs);
894 if(stat(de->d_name, &sb) < 0)
899 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
902 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
906 if(S_ISDIR(sb.st_mode))
908 if(~sb.st_mode & dmask)
914 } else if(S_ISREG(sb.st_mode)) {
915 if(~sb.st_mode & fmask)
922 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
928 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
938 if(S_ISREG(sb.st_mode))
940 sharesize += (n->size = sb.st_size);
944 n->mtime = sb.st_mtime;
946 n->inode = sb.st_ino;
948 attachcache(scanjob->node, n);
953 if(n->f.b.type == FILE_DIR)
956 jbuf->next = scanjob;
958 } else if(n->f.b.type == FILE_REG) {
959 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
963 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
965 if(hc->mtime == n->mtime)
968 memcpy(n->hashtth, hc->tth, 24);
979 static void rescancb(int cancelled, void *uudata)
984 if(scanqueue == NULL)
986 else if(confgetint("cli", "rescandelay") > 0)
987 scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
991 void scanshares(void)
993 struct sharepoint *cur;
994 struct sharecache *node;
997 for(cur = shares; cur != NULL; cur = cur->next)
999 if((node = findcache(shareroot, cur->name)) == NULL)
1001 if(stat(cur->path, &sb))
1003 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1006 if(!S_ISDIR(sb.st_mode))
1008 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
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);
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);
1027 static void preinit(int hup)
1029 struct sharepoint *cur;
1033 for(cur = shares; cur != NULL; cur = cur->next)
1036 shareroot = newcache();
1037 shareroot->name = swcsdup(L"");
1038 shareroot->f.b.type = FILE_DIR;
1042 static int rsdelayupdate(struct configvar *var, void *uudata)
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);
1051 static int init(int hup)
1053 struct sharepoint *cur, *next;
1056 for(cur = shares; cur != NULL; cur = next)
1060 freesharepoint(cur);
1066 CBREG(confgetvar("cli", "rescandelay"), conf_update, rsdelayupdate, NULL, NULL);
1071 static int run(void)
1081 static void terminate(void)
1084 kill(hashjob, SIGHUP);
1085 while(shares != NULL)
1086 freesharepoint(shares);
1087 freecache(shareroot);
1090 static struct module me =
1101 .terminate = terminate