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
40 #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 int 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));
470 static void hashexit(pid_t pid, int status, struct socket *outsock)
473 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
475 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
481 static int hashfile(char *path)
488 struct tigertreehash tth;
490 struct socket *outsock;
492 if((fd = open(path, O_RDONLY)) < 0)
494 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
497 if(fstat(fd, &sb) < 0)
499 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
505 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
512 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
521 signal(SIGHUP, SIG_DFL);
523 pfd[1] = dup2(pfd[1], 3);
526 for(i = 3; i < FD_SETSIZE; i++)
530 while((ret = read(0, buf, 4096)) > 0)
531 dotigertree(&tth, buf, ret);
534 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
538 restigertree(&tth, digest);
539 ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
545 outsock = wrapsock(pfd[0]);
546 CBREG(outsock, socket_read, hashread, NULL, NULL);
547 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
552 * Call only when hashjob == 0
554 static void checkhashes(void)
556 struct sharecache *node;
557 struct hashcache *hc;
560 node = shareroot->child;
561 for(node = shareroot->child; node != NULL; node = nextscnode(node))
563 if(node->f.b.type != FILE_REG)
565 if(!node->f.b.hastth)
567 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
569 memcpy(node->hashtth, hc->tth, 24);
570 node->f.b.hastth = 1;
571 GCBCHAINDOCB(sharechangecb, sharesize);
573 path = getfspath(node);
576 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
579 flog(LOG_INFO, "sharing %lli bytes", sharesize);
589 struct sharecache *nextscnode(struct sharecache *node)
591 if(node->child != NULL)
593 while(node->next == NULL)
596 if(node == shareroot)
602 static void freescan(struct scanstate *job)
609 /* No need for optimization; lookup isn't really that common */
610 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
612 struct sharecache *node;
614 for(node = parent->child; node != NULL; node = node->next)
616 if(!wcscmp(node->name, name))
622 static void attachcache(struct sharecache *parent, struct sharecache *node)
624 node->parent = parent;
625 node->next = parent->child;
626 if(parent->child != NULL)
627 parent->child->prev = node;
628 parent->child = node;
631 static void detachcache(struct sharecache *node)
633 if(node->next != NULL)
634 node->next->prev = node->prev;
635 if(node->prev != NULL)
636 node->prev->next = node->next;
637 if((node->parent != NULL) && (node->parent->child == node))
638 node->parent->child = node->next;
644 static void freecache(struct sharecache *node)
646 struct sharecache *cur, *next;
647 struct scanqueue *q, *nq, **fq;
651 for(q = scanqueue; q != NULL; q = nq)
654 if(q->state->node == node)
656 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
664 if(node->child != NULL)
666 for(cur = node->child; cur != NULL; cur = next)
672 CBCHAINDOCB(node, share_delete, node);
673 CBCHAINFREE(node, share_delete);
674 sharesize -= node->size;
675 if(node->path != NULL)
677 if(node->name != NULL)
682 static void freesharepoint(struct sharepoint *share)
684 struct sharecache *node;
686 if(share->next != NULL)
687 share->next->prev = share->prev;
688 if(share->prev != NULL)
689 share->prev->next = share->next;
691 shares = share->next;
692 if((node = findcache(shareroot, share->name)) != NULL)
699 static struct sharecache *newcache(void)
701 struct sharecache *new;
703 new = smalloc(sizeof(*new));
704 memset(new, 0, sizeof(*new));
705 CBCHAININIT(new, share_delete);
709 char *getfspath(struct sharecache *node)
714 buf = smalloc(bufsize = 64);
718 if(node->path != NULL)
720 if(bufsize < strlen(node->path) + strlen(buf) + 1)
721 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
722 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
723 memcpy(buf, node->path, strlen(node->path));
726 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
728 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
732 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
733 buf = srealloc(buf, bufsize *= 2);
734 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
735 memcpy(buf + 1, mbsname, strlen(mbsname));
740 buf = srealloc(buf, strlen(buf) + 1);
744 static int checknode(struct sharecache *node)
749 if(node->parent == NULL)
753 if(!checknode(node->parent))
755 path = getfspath(node);
756 if(stat(path, &sb) < 0)
758 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
759 queuescan(node->parent);
767 int opensharecache(struct sharecache *node)
772 path = getfspath(node);
773 fd = open(path, O_RDONLY);
777 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
785 static struct scanstate *newscan(struct sharecache *node)
787 struct scanstate *new;
789 new = smalloc(sizeof(*new));
796 void queuescan(struct sharecache *node)
798 struct scanqueue *new;
800 new = smalloc(sizeof(*new));
801 new->state = newscan(node);
802 new->next = scanqueue;
806 /* For internal use in doscan() */
807 static void removestale(struct sharecache *node)
809 struct sharecache *cur, *next;
811 for(cur = node->child; cur != NULL; cur = next)
819 /* For internal use in doscan() */
820 static void jobdone(void)
822 struct scanstate *jbuf;
825 scanjob = jbuf->next;
828 fchdir(dirfd(scanjob->dd));
831 int doscan(int quantum)
836 struct sharecache *n;
837 struct scanstate *jbuf;
838 struct scanqueue *qbuf;
841 struct hashcache *hc;
843 static int busybefore = 0;
845 dmask = confgetint("cli", "scandirmask");
846 fmask = confgetint("cli", "scanfilemask");
847 if((scanjob != NULL) && (scanjob->dd != NULL))
849 while(fchdir(dirfd(scanjob->dd)) < 0)
851 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
852 removestale(scanjob->node);
862 while(scanjob == NULL)
864 if(scanqueue == NULL)
868 flog(LOG_INFO, "sharing %lli bytes", sharesize);
870 GCBCHAINDOCB(sharechangecb, sharesize);
877 scanjob = scanqueue->state;
879 scanqueue = qbuf->next;
881 for(n = scanjob->node->child; n != NULL; n = n->next)
885 if(scanjob->dd == NULL)
887 path = getfspath(scanjob->node);
888 if((scanjob->dd = opendir(path)) == NULL)
890 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
891 freecache(scanjob->node);
897 if(fchdir(dirfd(scanjob->dd)) < 0)
899 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
904 if((de = readdir(scanjob->dd)) == NULL)
906 removestale(scanjob->node);
910 if(*de->d_name == '.')
912 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
914 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
917 n = findcache(scanjob->node, wcs);
918 if(stat(de->d_name, &sb) < 0)
923 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
926 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
930 if(S_ISDIR(sb.st_mode))
932 if(~sb.st_mode & dmask)
938 } else if(S_ISREG(sb.st_mode)) {
939 if(~sb.st_mode & fmask)
946 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
952 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
962 if(S_ISREG(sb.st_mode))
964 sharesize += (n->size = sb.st_size);
968 n->mtime = sb.st_mtime;
970 n->inode = sb.st_ino;
972 attachcache(scanjob->node, n);
977 if(n->f.b.type == FILE_DIR)
980 jbuf->next = scanjob;
982 } else if(n->f.b.type == FILE_REG) {
983 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
987 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
989 if(hc->mtime == n->mtime)
992 memcpy(n->hashtth, hc->tth, 24);
1003 void scanshares(void)
1005 struct sharepoint *cur;
1006 struct sharecache *node;
1009 for(cur = shares; cur != NULL; cur = cur->next)
1011 if((node = findcache(shareroot, cur->name)) == NULL)
1013 if(stat(cur->path, &sb))
1015 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1018 if(!S_ISDIR(sb.st_mode))
1020 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1024 node->name = swcsdup(cur->name);
1025 node->path = sstrdup(cur->path);
1026 if(node->path[strlen(node->path) - 1] == '/')
1027 node->path[strlen(node->path) - 1] = 0;
1028 node->f.b.type = FILE_DIR;
1029 attachcache(shareroot, node);
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 init(int hup)
1052 struct sharepoint *cur, *next;
1055 for(cur = shares; cur != NULL; cur = next)
1059 freesharepoint(cur);
1067 static int run(void)
1077 static void terminate(void)
1080 kill(hashjob, SIGHUP);
1081 while(shares != NULL)
1082 freesharepoint(shares);
1083 freecache(shareroot);
1086 static struct module me =
1097 .terminate = terminate