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"}},
80 static struct configcmd mycmds[] =
82 {"share", conf_share},
86 static struct scanstate *scanjob = NULL;
87 static struct scanqueue *scanqueue = NULL;
88 static struct sharepoint *shares = NULL;
89 static struct hashcache *hashcache = NULL;
90 static struct timer *hashwritetimer = NULL;
91 /* Set initially to -1, but changed to 0 the first time run() is
92 * called. This is to avoid forking a hash job before daemonizing,
93 * since that would make the daemon unable to wait() for the hash
95 static pid_t hashjob = -1;
96 struct sharecache *shareroot = NULL;
97 unsigned long long sharesize = 0;
98 GCBCHAIN(sharechangecb, unsigned long long);
100 static int conf_share(int argc, wchar_t **argv)
102 struct sharepoint *share;
107 flog(LOG_WARNING, "not enough arguments given for share command");
110 if((b = icwcstombs(argv[2], NULL)) == NULL)
112 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
115 for(share = shares; share != NULL; share = share->next)
117 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
124 share = smalloc(sizeof(*share));
127 share->name = swcsdup(argv[1]);
128 share->next = shares;
131 shares->prev = share;
136 static void dumpsharecache(struct sharecache *node, int l)
140 for(; node != NULL; node = node->next)
142 for(i = 0; i < l; i++)
144 printf("%ls\n", node->name);
145 if(node->f.b.type == FILE_DIR)
146 dumpsharecache(node->child, l + 1);
150 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
154 ret = smalloc(sizeof(*ret));
155 memset(ret, 0, sizeof(*ret));
156 ret->algo = swcsdup(algo);
158 ret->buf = memcpy(smalloc(len), buf, len);
162 void freehash(struct hash *hash)
169 struct hash *duphash(struct hash *hash)
171 return(newhash(hash->algo, hash->len, hash->buf));
174 struct hash *parsehash(wchar_t *text)
177 char *mbsbuf, *decbuf;
181 if((p = wcschr(text, L':')) == NULL)
184 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
186 decbuf = base64decode(mbsbuf, &buflen);
190 ret = newhash(text, buflen, decbuf);
195 wchar_t *unparsehash(struct hash *hash)
197 static wchar_t *buf = NULL;
200 size_t bufsize, bufdata;
205 bufsize = bufdata = 0;
206 hbuf = base64encode(hash->buf, hash->len);
207 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
209 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
213 bufcat(buf, hash->algo, wcslen(hash->algo));
215 bufcat(buf, whbuf, wcslen(whbuf));
221 int hashcmp(struct hash *h1, struct hash *h2)
223 if(wcscmp(h1->algo, h2->algo))
225 if(h1->len != h2->len)
227 if(memcmp(h1->buf, h2->buf, h1->len))
232 static struct hashcache *newhashcache(void)
234 struct hashcache *new;
236 new = smalloc(sizeof(*new));
237 memset(new, 0, sizeof(*new));
238 new->next = hashcache;
240 if(hashcache != NULL)
241 hashcache->prev = new;
246 static void freehashcache(struct hashcache *hc)
249 hc->next->prev = hc->prev;
251 hc->prev->next = hc->next;
253 hashcache = hc->next;
257 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
259 struct hashcache *hc;
261 for(hc = hashcache; hc != NULL; hc = hc->next)
263 if((hc->dev == dev) && (hc->inode == inode))
269 static void readhashcache(void)
275 char *p, *p2, *wv[32], *hash;
276 struct hashcache *hc;
279 if((hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 0)) == NULL)
281 if((stream = fopen(hcname, "r")) == NULL)
283 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
286 while(hashcache != NULL)
287 freehashcache(hashcache);
291 fgets(linebuf, sizeof(linebuf), stream);
293 for(p = linebuf; *p; p++)
298 if(linebuf[0] == '#')
300 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
310 hc->dev = strtoll(wv[0], NULL, 10);
311 hc->inode = strtoll(wv[1], NULL, 10);
312 hc->mtime = strtoll(wv[2], NULL, 10);
313 for(i = 3; i < wc; i++)
315 if(!strcmp(wv[i], "tth"))
319 hash = base64decode(wv[i], &len);
325 memcpy(hc->tth, hash, 24);
333 static void hashtimercb(int cancelled, void *uudata)
335 hashwritetimer = NULL;
340 static void writehashcache(int now)
345 struct hashcache *hc;
349 if(hashwritetimer == NULL)
350 hashwritetimer = timercallback(ntime() + 300, (void (*)(int, void *))hashtimercb, NULL);
353 if(hashwritetimer != NULL)
354 canceltimer(hashwritetimer);
355 hcname = findfile(icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL), NULL, 1);
356 if((stream = fopen(hcname, "w")) == NULL)
358 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
361 fprintf(stream, "# Dolda Connect hash cache file\n");
362 fprintf(stream, "# Generated automatically, do not edit\n");
363 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
364 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
365 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
366 for(hc = hashcache; hc != NULL; hc = hc->next)
368 buf = base64encode(hc->tth, 24);
369 fprintf(stream, "%lli %lli %li tth %s\n", (long long)hc->dev, (long long)hc->inode, hc->mtime, buf);
375 static void hashread(struct socket *sk, void *uudata)
377 static char *hashbuf;
378 static size_t hashbufsize = 0, hashbufdata = 0;
379 char *buf, *p, *p2, *lp;
386 struct hashcache *hc;
388 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
390 bufcat(hashbuf, buf, bufsize);
392 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
399 while((p2 = strchr(p, ' ')) == p)
412 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
414 dev = strtoll(wv[0], NULL, 10);
415 inode = strtoll(wv[1], NULL, 10);
416 mtime = strtol(wv[2], NULL, 10);
417 if((hc = findhashcache(dev, inode)) == NULL)
424 buf = base64decode(wv[3], NULL);
425 memcpy(hc->tth, buf, 24);
429 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
433 static void hashexit(pid_t pid, int status, struct socket *outsock)
436 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
438 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
444 static int hashfile(char *path)
451 struct tigertreehash tth;
453 struct socket *outsock;
455 if((fd = open(path, O_RDONLY)) < 0)
457 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
460 if(fstat(fd, &sb) < 0)
462 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
468 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
475 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
484 signal(SIGHUP, SIG_DFL);
486 pfd[1] = dup2(pfd[1], 3);
489 for(i = 3; i < FD_SETSIZE; i++)
493 while((ret = read(0, buf, 4096)) > 0)
494 dotigertree(&tth, buf, ret);
497 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
501 restigertree(&tth, digest);
502 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));
508 outsock = wrapsock(pfd[0]);
509 outsock->readcb = hashread;
510 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
515 * Call only when hashjob == 0
517 static void checkhashes(void)
519 struct sharecache *node;
520 struct hashcache *hc;
523 node = shareroot->child;
524 for(node = shareroot->child; node != NULL; node = nextscnode(node))
526 if(node->f.b.type != FILE_REG)
528 if(!node->f.b.hastth)
530 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
532 memcpy(node->hashtth, hc->tth, 24);
533 node->f.b.hastth = 1;
534 GCBCHAINDOCB(sharechangecb, sharesize);
536 path = getfspath(node);
539 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
542 flog(LOG_INFO, "sharing %lli bytes", sharesize);
552 struct sharecache *nextscnode(struct sharecache *node)
554 if(node->child != NULL)
556 while(node->next == NULL)
559 if(node == shareroot)
565 static void freescan(struct scanstate *job)
572 /* No need for optimization; lookup isn't really that common */
573 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
575 struct sharecache *node;
577 for(node = parent->child; node != NULL; node = node->next)
579 if(!wcscmp(node->name, name))
585 static void attachcache(struct sharecache *parent, struct sharecache *node)
587 node->parent = parent;
588 node->next = parent->child;
589 if(parent->child != NULL)
590 parent->child->prev = node;
591 parent->child = node;
594 static void detachcache(struct sharecache *node)
596 if(node->next != NULL)
597 node->next->prev = node->prev;
598 if(node->prev != NULL)
599 node->prev->next = node->next;
600 if((node->parent != NULL) && (node->parent->child == node))
601 node->parent->child = node->next;
607 static void freecache(struct sharecache *node)
609 struct sharecache *cur, *next;
610 struct scanqueue *q, *nq, **fq;
614 for(q = scanqueue; q != NULL; q = nq)
617 if(q->state->node == node)
619 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
627 if(node->child != NULL)
629 for(cur = node->child; cur != NULL; cur = next)
635 CBCHAINDOCB(node, share_delete, node);
636 CBCHAINFREE(node, share_delete);
637 sharesize -= node->size;
638 if(node->path != NULL)
640 if(node->name != NULL)
645 static void freesharepoint(struct sharepoint *share)
647 struct sharecache *node;
649 if(share->next != NULL)
650 share->next->prev = share->prev;
651 if(share->prev != NULL)
652 share->prev->next = share->next;
654 shares = share->next;
655 if((node = findcache(shareroot, share->name)) != NULL)
662 static struct sharecache *newcache(void)
664 struct sharecache *new;
666 new = smalloc(sizeof(*new));
667 memset(new, 0, sizeof(*new));
668 CBCHAININIT(new, share_delete);
672 char *getfspath(struct sharecache *node)
677 buf = smalloc(bufsize = 64);
681 if(node->path != NULL)
683 if(bufsize < strlen(node->path) + strlen(buf) + 1)
684 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
685 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
686 memcpy(buf, node->path, strlen(node->path));
689 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
691 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
695 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
696 buf = srealloc(buf, bufsize *= 2);
697 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
698 memcpy(buf + 1, mbsname, strlen(mbsname));
703 buf = srealloc(buf, strlen(buf) + 1);
707 static int checknode(struct sharecache *node)
712 if(node->parent == NULL)
716 if(!checknode(node->parent))
718 path = getfspath(node);
719 if(stat(path, &sb) < 0)
721 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
722 queuescan(node->parent);
730 int opensharecache(struct sharecache *node)
735 path = getfspath(node);
736 fd = open(path, O_RDONLY);
740 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
748 static struct scanstate *newscan(struct sharecache *node)
750 struct scanstate *new;
752 new = smalloc(sizeof(*new));
759 void queuescan(struct sharecache *node)
761 struct scanqueue *new;
763 new = smalloc(sizeof(*new));
764 new->state = newscan(node);
765 new->next = scanqueue;
769 /* For internal use in doscan() */
770 static void removestale(struct sharecache *node)
772 struct sharecache *cur, *next;
774 for(cur = node->child; cur != NULL; cur = next)
782 /* For internal use in doscan() */
783 static void jobdone(void)
785 struct scanstate *jbuf;
788 scanjob = jbuf->next;
791 fchdir(dirfd(scanjob->dd));
794 int doscan(int quantum)
799 struct sharecache *n;
800 struct scanstate *jbuf;
801 struct scanqueue *qbuf;
804 struct hashcache *hc;
806 static int busybefore = 0;
808 dmask = confgetint("cli", "scandirmask");
809 fmask = confgetint("cli", "scanfilemask");
810 if((scanjob != NULL) && (scanjob->dd != NULL))
812 while(fchdir(dirfd(scanjob->dd)) < 0)
814 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
815 removestale(scanjob->node);
825 while(scanjob == NULL)
827 if(scanqueue == NULL)
831 flog(LOG_INFO, "sharing %lli bytes", sharesize);
833 GCBCHAINDOCB(sharechangecb, sharesize);
840 scanjob = scanqueue->state;
842 scanqueue = qbuf->next;
844 for(n = scanjob->node->child; n != NULL; n = n->next)
848 if(scanjob->dd == NULL)
850 path = getfspath(scanjob->node);
851 if((scanjob->dd = opendir(path)) == NULL)
853 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
854 freecache(scanjob->node);
860 if(fchdir(dirfd(scanjob->dd)) < 0)
862 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
867 if((de = readdir(scanjob->dd)) == NULL)
869 removestale(scanjob->node);
873 if(*de->d_name == '.')
875 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
877 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
880 n = findcache(scanjob->node, wcs);
881 if(stat(de->d_name, &sb) < 0)
886 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
889 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
893 if(S_ISDIR(sb.st_mode))
895 if(~sb.st_mode & dmask)
901 } else if(S_ISREG(sb.st_mode)) {
902 if(~sb.st_mode & fmask)
909 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
915 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
925 if(S_ISREG(sb.st_mode))
927 sharesize += (n->size = sb.st_size);
931 n->mtime = sb.st_mtime;
933 n->inode = sb.st_ino;
935 attachcache(scanjob->node, n);
940 if(n->f.b.type == FILE_DIR)
943 jbuf->next = scanjob;
945 } else if(n->f.b.type == FILE_REG) {
946 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
950 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
952 if(hc->mtime == n->mtime)
955 memcpy(n->hashtth, hc->tth, 24);
966 void scanshares(void)
968 struct sharepoint *cur;
969 struct sharecache *node;
972 for(cur = shares; cur != NULL; cur = cur->next)
974 if((node = findcache(shareroot, cur->name)) == NULL)
976 if(stat(cur->path, &sb))
978 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
981 if(!S_ISDIR(sb.st_mode))
983 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
987 node->name = swcsdup(cur->name);
988 node->path = sstrdup(cur->path);
989 if(node->path[strlen(node->path) - 1] == '/')
990 node->path[strlen(node->path) - 1] = 0;
991 node->f.b.type = FILE_DIR;
992 attachcache(shareroot, node);
998 static void preinit(int hup)
1000 struct sharepoint *cur;
1004 for(cur = shares; cur != NULL; cur = cur->next)
1007 shareroot = newcache();
1008 shareroot->name = swcsdup(L"");
1009 shareroot->f.b.type = FILE_DIR;
1013 static int init(int hup)
1015 struct sharepoint *cur, *next;
1018 for(cur = shares; cur != NULL; cur = next)
1022 freesharepoint(cur);
1030 static int run(void)
1040 static void terminate(void)
1043 kill(hashjob, SIGHUP);
1044 while(shares != NULL)
1045 freesharepoint(shares);
1046 freecache(shareroot);
1049 static struct module me =
1060 .terminate = terminate