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
41 #include "sysevents.h"
45 struct scanstate *next;
46 struct sharecache *node;
52 struct scanqueue *next;
53 struct scanstate *state;
56 static int conf_share(int argc, wchar_t **argv);
57 static void freecache(struct sharecache *node);
58 static void checkhashes(void);
59 static void writehashcache(int now);
61 static struct configvar myvars[] =
63 {CONF_VAR_STRING, "defnick", {.str = L"DoldaConnect user"}},
64 {CONF_VAR_INT, "scanfilemask", {.num = 0004}},
65 {CONF_VAR_INT, "scandirmask", {.num = 0005}},
66 {CONF_VAR_STRING, "hashcache", {.str = L"dc-hashcache"}},
70 static struct configcmd mycmds[] =
72 {"share", conf_share},
76 static struct scanstate *scanjob = NULL;
77 static struct scanqueue *scanqueue = NULL;
78 static struct sharepoint *shares = NULL;
79 static struct hashcache *hashcache = NULL;
80 static struct timer *hashwritetimer = NULL;
81 /* Set initially to -1, but changed to 0 the first time run() is
82 * called. This is to avoid forking a hash job before daemonizing,
83 * since that would make the daemon unable to wait() for the hash
85 static pid_t hashjob = -1;
86 struct sharecache *shareroot = NULL;
87 unsigned long long sharesize = 0;
88 GCBCHAIN(sharechangecb, unsigned long long);
90 static int conf_share(int argc, wchar_t **argv)
92 struct sharepoint *share;
97 flog(LOG_WARNING, "not enough arguments given for share command");
100 if((b = icwcstombs(argv[2], NULL)) == NULL)
102 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
105 for(share = shares; share != NULL; share = share->next)
107 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
114 share = smalloc(sizeof(*share));
117 share->name = swcsdup(argv[1]);
118 share->next = shares;
121 shares->prev = share;
126 static void dumpsharecache(struct sharecache *node, int l)
130 for(; node != NULL; node = node->next)
132 for(i = 0; i < l; i++)
134 printf("%ls\n", node->name);
135 if(node->f.b.type == FILE_DIR)
136 dumpsharecache(node->child, l + 1);
140 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
144 ret = smalloc(sizeof(*ret));
145 memset(ret, 0, sizeof(*ret));
146 ret->algo = swcsdup(algo);
148 ret->buf = memcpy(smalloc(len), buf, len);
152 void freehash(struct hash *hash)
159 struct hash *duphash(struct hash *hash)
161 return(newhash(hash->algo, hash->len, hash->buf));
164 struct hash *parsehash(wchar_t *text)
167 char *mbsbuf, *decbuf;
171 if((p = wcschr(text, L':')) == NULL)
174 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
176 decbuf = base64decode(mbsbuf, &buflen);
180 ret = newhash(text, buflen, decbuf);
185 wchar_t *unparsehash(struct hash *hash)
187 static wchar_t *buf = NULL;
190 size_t bufsize, bufdata;
195 bufsize = bufdata = 0;
196 hbuf = base64encode(hash->buf, hash->len);
197 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
199 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
203 bufcat(buf, hash->algo, wcslen(hash->algo));
205 bufcat(buf, whbuf, wcslen(whbuf));
211 int hashcmp(struct hash *h1, struct hash *h2)
213 if(wcscmp(h1->algo, h2->algo))
215 if(h1->len != h2->len)
217 if(memcmp(h1->buf, h2->buf, h1->len))
222 static struct hashcache *newhashcache(void)
224 struct hashcache *new;
226 new = smalloc(sizeof(*new));
227 memset(new, 0, sizeof(*new));
228 new->next = hashcache;
230 if(hashcache != NULL)
231 hashcache->prev = new;
236 static void freehashcache(struct hashcache *hc)
239 hc->next->prev = hc->prev;
241 hc->prev->next = hc->next;
243 hashcache = hc->next;
247 static char *findhashcachefile(int filldef)
249 static char ret[128];
252 if(getenv("HOME") != NULL)
254 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
255 if(!access(ret, R_OK))
258 if((hcname = icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL)) == NULL)
260 flog(LOG_WARNING, "could not convert hash cache name into local charset: %s", strerror(errno));
263 if(strchr(hcname, '/') != NULL)
265 if(!access(hcname, R_OK))
271 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
272 if(!access(ret, R_OK))
274 snprintf(ret, sizeof(ret), "/usr/etc/%s", hcname);
275 if(!access(ret, R_OK))
277 snprintf(ret, sizeof(ret), "/usr/local/etc/%s", hcname);
278 if(!access(ret, R_OK))
283 if(getenv("HOME") != NULL)
284 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
286 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
293 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
295 struct hashcache *hc;
297 for(hc = hashcache; hc != NULL; hc = hc->next)
299 if((hc->dev == dev) && (hc->inode == inode))
305 static void readhashcache(void)
311 char *p, *p2, *wv[32], *hash;
312 struct hashcache *hc;
315 if((hcname = findhashcachefile(0)) == NULL)
317 if((stream = fopen(hcname, "r")) == NULL)
319 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
322 while(hashcache != NULL)
323 freehashcache(hashcache);
327 fgets(linebuf, sizeof(linebuf), stream);
329 for(p = linebuf; *p; p++)
334 if(linebuf[0] == '#')
336 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
346 hc->dev = strtoll(wv[0], NULL, 10);
347 hc->inode = strtoll(wv[1], NULL, 10);
348 hc->mtime = strtoll(wv[2], NULL, 10);
349 for(i = 3; i < wc; i++)
351 if(!strcmp(wv[i], "tth"))
355 hash = base64decode(wv[i], &len);
361 memcpy(hc->tth, hash, 24);
369 static void hashtimercb(int cancelled, void *uudata)
371 hashwritetimer = NULL;
376 static void writehashcache(int now)
381 struct hashcache *hc;
385 if(hashwritetimer == NULL)
386 hashwritetimer = timercallback(ntime() + 300, (void (*)(int, void *))hashtimercb, NULL);
389 if(hashwritetimer != NULL)
390 canceltimer(hashwritetimer);
391 hcname = findhashcachefile(1);
392 if((stream = fopen(hcname, "w")) == NULL)
394 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
397 fprintf(stream, "# Dolda Connect hash cache file\n");
398 fprintf(stream, "# Generated automatically, do not edit\n");
399 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
400 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
401 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
402 for(hc = hashcache; hc != NULL; hc = hc->next)
404 buf = base64encode(hc->tth, 24);
405 fprintf(stream, "%lli %lli %li tth %s\n", hc->dev, (long long)hc->inode, hc->mtime, buf);
411 static void hashread(struct socket *sk, void *uudata)
413 static char *hashbuf;
414 static size_t hashbufsize = 0, hashbufdata = 0;
415 char *buf, *p, *p2, *lp;
422 struct hashcache *hc;
424 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
426 bufcat(hashbuf, buf, bufsize);
428 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
435 while((p2 = strchr(p, ' ')) == p)
448 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
450 dev = strtoll(wv[0], NULL, 10);
451 inode = strtoll(wv[1], NULL, 10);
452 mtime = strtol(wv[2], NULL, 10);
453 if((hc = findhashcache(dev, inode)) == NULL)
460 buf = base64decode(wv[3], NULL);
461 memcpy(hc->tth, buf, 24);
465 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
469 static void hashexit(pid_t pid, int status, struct socket *outsock)
472 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
474 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
480 static int hashfile(char *path)
487 struct tigertreehash tth;
489 struct socket *outsock;
491 if((fd = open(path, O_RDONLY)) < 0)
493 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
496 if(fstat(fd, &sb) < 0)
498 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
504 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
511 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
520 signal(SIGHUP, SIG_DFL);
522 pfd[1] = dup2(pfd[1], 3);
525 for(i = 3; i < FD_SETSIZE; i++)
529 while((ret = read(0, buf, 4096)) > 0)
530 dotigertree(&tth, buf, ret);
533 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
537 restigertree(&tth, digest);
538 ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
544 outsock = wrapsock(pfd[0]);
545 outsock->readcb = hashread;
546 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
551 * Call only when hashjob == 0
553 static void checkhashes(void)
555 struct sharecache *node;
556 struct hashcache *hc;
559 node = shareroot->child;
560 for(node = shareroot->child; node != NULL; node = nextscnode(node))
562 if(node->f.b.type != FILE_REG)
564 if(!node->f.b.hastth)
566 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
568 memcpy(node->hashtth, hc->tth, 24);
569 node->f.b.hastth = 1;
570 GCBCHAINDOCB(sharechangecb, sharesize);
572 path = getfspath(node);
575 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
578 flog(LOG_INFO, "sharing %lli bytes", sharesize);
588 struct sharecache *nextscnode(struct sharecache *node)
590 if(node->child != NULL)
592 while(node->next == NULL)
595 if(node == shareroot)
601 static void freescan(struct scanstate *job)
608 /* No need for optimization; lookup isn't really that common */
609 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
611 struct sharecache *node;
613 for(node = parent->child; node != NULL; node = node->next)
615 if(!wcscmp(node->name, name))
621 static void attachcache(struct sharecache *parent, struct sharecache *node)
623 node->parent = parent;
624 node->next = parent->child;
625 if(parent->child != NULL)
626 parent->child->prev = node;
627 parent->child = node;
630 static void detachcache(struct sharecache *node)
632 if(node->next != NULL)
633 node->next->prev = node->prev;
634 if(node->prev != NULL)
635 node->prev->next = node->next;
636 if((node->parent != NULL) && (node->parent->child == node))
637 node->parent->child = node->next;
643 static void freecache(struct sharecache *node)
645 struct sharecache *cur, *next;
646 struct scanqueue *q, *nq, **fq;
650 for(q = scanqueue; q != NULL; q = nq)
653 if(q->state->node == node)
655 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
663 if(node->child != NULL)
665 for(cur = node->child; cur != NULL; cur = next)
671 CBCHAINDOCB(node, share_delete, node);
672 CBCHAINFREE(node, share_delete);
673 sharesize -= node->size;
674 if(node->path != NULL)
676 if(node->name != NULL)
681 static void freesharepoint(struct sharepoint *share)
683 struct sharecache *node;
685 if(share->next != NULL)
686 share->next->prev = share->prev;
687 if(share->prev != NULL)
688 share->prev->next = share->next;
690 shares = share->next;
691 if((node = findcache(shareroot, share->name)) != NULL)
698 static struct sharecache *newcache(void)
700 struct sharecache *new;
702 new = smalloc(sizeof(*new));
703 memset(new, 0, sizeof(*new));
704 CBCHAININIT(new, share_delete);
708 char *getfspath(struct sharecache *node)
713 buf = smalloc(bufsize = 64);
717 if(node->path != NULL)
719 if(bufsize < strlen(node->path) + strlen(buf) + 1)
720 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
721 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
722 memcpy(buf, node->path, strlen(node->path));
725 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
727 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
731 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
732 buf = srealloc(buf, bufsize *= 2);
733 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
734 memcpy(buf + 1, mbsname, strlen(mbsname));
739 buf = srealloc(buf, strlen(buf) + 1);
743 static int checknode(struct sharecache *node)
748 if(node->parent == NULL)
752 if(!checknode(node->parent))
754 path = getfspath(node);
755 if(stat(path, &sb) < 0)
757 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
758 queuescan(node->parent);
766 int opensharecache(struct sharecache *node)
771 path = getfspath(node);
772 fd = open(path, O_RDONLY);
776 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
784 static struct scanstate *newscan(struct sharecache *node)
786 struct scanstate *new;
788 new = smalloc(sizeof(*new));
795 void queuescan(struct sharecache *node)
797 struct scanqueue *new;
799 new = smalloc(sizeof(*new));
800 new->state = newscan(node);
801 new->next = scanqueue;
805 /* For internal use in doscan() */
806 static void removestale(struct sharecache *node)
808 struct sharecache *cur, *next;
810 for(cur = node->child; cur != NULL; cur = next)
818 /* For internal use in doscan() */
819 static void jobdone(void)
821 struct scanstate *jbuf;
824 scanjob = jbuf->next;
827 fchdir(dirfd(scanjob->dd));
830 int doscan(int quantum)
835 struct sharecache *n;
836 struct scanstate *jbuf;
837 struct scanqueue *qbuf;
840 struct hashcache *hc;
842 static int busybefore = 0;
844 dmask = confgetint("cli", "scandirmask");
845 fmask = confgetint("cli", "scanfilemask");
846 if((scanjob != NULL) && (scanjob->dd != NULL))
848 while(fchdir(dirfd(scanjob->dd)) < 0)
850 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
851 removestale(scanjob->node);
861 while(scanjob == NULL)
863 if(scanqueue == NULL)
867 flog(LOG_INFO, "sharing %lli bytes", sharesize);
869 GCBCHAINDOCB(sharechangecb, sharesize);
876 scanjob = scanqueue->state;
878 scanqueue = qbuf->next;
880 for(n = scanjob->node->child; n != NULL; n = n->next)
884 if(scanjob->dd == NULL)
886 path = getfspath(scanjob->node);
887 if((scanjob->dd = opendir(path)) == NULL)
889 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
890 freecache(scanjob->node);
896 if(fchdir(dirfd(scanjob->dd)) < 0)
898 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
903 if((de = readdir(scanjob->dd)) == NULL)
905 removestale(scanjob->node);
909 if(*de->d_name == '.')
911 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
913 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
916 n = findcache(scanjob->node, wcs);
917 if(stat(de->d_name, &sb) < 0)
922 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
925 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
929 if(S_ISDIR(sb.st_mode))
931 if(~sb.st_mode & dmask)
937 } else if(S_ISREG(sb.st_mode)) {
938 if(~sb.st_mode & fmask)
945 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
951 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
961 if(S_ISREG(sb.st_mode))
963 sharesize += (n->size = sb.st_size);
967 n->mtime = sb.st_mtime;
969 n->inode = sb.st_ino;
971 attachcache(scanjob->node, n);
976 if(n->f.b.type == FILE_DIR)
979 jbuf->next = scanjob;
981 } else if(n->f.b.type == FILE_REG) {
982 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
986 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
988 if(hc->mtime == n->mtime)
991 memcpy(n->hashtth, hc->tth, 24);
1002 void scanshares(void)
1004 struct sharepoint *cur;
1005 struct sharecache *node;
1008 for(cur = shares; cur != NULL; cur = cur->next)
1010 if((node = findcache(shareroot, cur->name)) == NULL)
1012 if(stat(cur->path, &sb))
1014 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1017 if(!S_ISDIR(sb.st_mode))
1019 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1023 node->name = swcsdup(cur->name);
1024 node->path = sstrdup(cur->path);
1025 if(node->path[strlen(node->path) - 1] == '/')
1026 node->path[strlen(node->path) - 1] = 0;
1027 node->f.b.type = FILE_DIR;
1028 attachcache(shareroot, node);
1034 static void preinit(int hup)
1036 struct sharepoint *cur;
1040 for(cur = shares; cur != NULL; cur = cur->next)
1043 shareroot = newcache();
1044 shareroot->name = swcsdup(L"");
1045 shareroot->f.b.type = FILE_DIR;
1049 static int init(int hup)
1051 struct sharepoint *cur, *next;
1054 for(cur = shares; cur != NULL; cur = next)
1058 freesharepoint(cur);
1066 static int run(void)
1076 static void terminate(void)
1079 kill(hashjob, SIGHUP);
1080 while(shares != NULL)
1081 freesharepoint(shares);
1082 freecache(shareroot);
1085 static struct module me =
1096 .terminate = terminate