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);
60 static struct configvar myvars[] =
62 {CONF_VAR_STRING, "defnick", {.str = L"DoldaConnect user"}},
63 {CONF_VAR_INT, "scanfilemask", {.num = 0004}},
64 {CONF_VAR_INT, "scandirmask", {.num = 0005}},
65 {CONF_VAR_STRING, "hashcache", {.str = L"dc-hashcache"}},
69 static struct configcmd mycmds[] =
71 {"share", conf_share},
75 static struct scanstate *scanjob = NULL;
76 static struct scanqueue *scanqueue = NULL;
77 static struct sharepoint *shares = NULL;
78 static struct hashcache *hashcache = NULL;
79 static pid_t hashjob = 0;
80 struct sharecache *shareroot = NULL;
81 unsigned long long sharesize = 0;
82 GCBCHAIN(sharechangecb, unsigned long long);
84 static int conf_share(int argc, wchar_t **argv)
86 struct sharepoint *share;
91 flog(LOG_WARNING, "not enough arguments given for share command");
94 if((b = icwcstombs(argv[2], NULL)) == NULL)
96 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
99 for(share = shares; share != NULL; share = share->next)
101 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
108 share = smalloc(sizeof(*share));
111 share->name = swcsdup(argv[1]);
112 share->next = shares;
115 shares->prev = share;
120 static void dumpsharecache(struct sharecache *node, int l)
124 for(; node != NULL; node = node->next)
126 for(i = 0; i < l; i++)
128 printf("%ls\n", node->name);
129 if(node->f.b.type == FILE_DIR)
130 dumpsharecache(node->child, l + 1);
134 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
138 ret = smalloc(sizeof(*ret));
139 memset(ret, 0, sizeof(*ret));
140 ret->algo = swcsdup(algo);
142 ret->buf = memcpy(smalloc(len), buf, len);
146 void freehash(struct hash *hash)
153 struct hash *duphash(struct hash *hash)
155 return(newhash(hash->algo, hash->len, hash->buf));
158 struct hash *parsehash(wchar_t *text)
161 char *mbsbuf, *decbuf;
165 if((p = wcschr(text, L':')) == NULL)
168 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
170 decbuf = base64decode(mbsbuf, &buflen);
174 ret = newhash(text, buflen, decbuf);
179 wchar_t *unparsehash(struct hash *hash)
181 static wchar_t *buf = NULL;
184 size_t bufsize, bufdata;
189 bufsize = bufdata = 0;
190 hbuf = base64encode(hash->buf, hash->len);
191 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
193 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
197 bufcat(buf, hash->algo, wcslen(hash->algo));
199 bufcat(buf, whbuf, wcslen(whbuf));
204 static struct hashcache *newhashcache(void)
206 struct hashcache *new;
208 new = smalloc(sizeof(*new));
209 memset(new, 0, sizeof(*new));
210 new->next = hashcache;
212 if(hashcache != NULL)
213 hashcache->prev = new;
218 static void freehashcache(struct hashcache *hc)
221 hc->next->prev = hc->prev;
223 hc->prev->next = hc->next;
225 hashcache = hc->next;
229 static char *findhashcachefile(int filldef)
231 static char ret[128];
234 if(getenv("HOME") != NULL)
236 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
237 if(!access(ret, R_OK))
240 if((hcname = icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL)) == NULL)
242 flog(LOG_WARNING, "could not convert hash cache name into local charset: %s", strerror(errno));
245 if(strchr(hcname, '/') != NULL)
247 if(!access(hcname, R_OK))
253 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
254 if(!access(ret, R_OK))
256 snprintf(ret, sizeof(ret), "/usr/etc/%s", hcname);
257 if(!access(ret, R_OK))
259 snprintf(ret, sizeof(ret), "/usr/local/etc/%s", hcname);
260 if(!access(ret, R_OK))
265 if(getenv("HOME") != NULL)
266 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
268 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
275 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
277 struct hashcache *hc;
279 for(hc = hashcache; hc != NULL; hc = hc->next)
281 if((hc->dev == dev) && (hc->inode == inode))
287 static void readhashcache(void)
293 char *p, *p2, *wv[32], *hash;
294 struct hashcache *hc;
297 if((hcname = findhashcachefile(0)) == NULL)
299 if((stream = fopen(hcname, "r")) == NULL)
301 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
304 while(hashcache != NULL)
305 freehashcache(hashcache);
309 fgets(linebuf, sizeof(linebuf), stream);
311 for(p = linebuf; *p; p++)
316 if(linebuf[0] == '#')
318 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
328 hc->dev = strtoll(wv[0], NULL, 10);
329 hc->inode = strtoll(wv[1], NULL, 10);
330 hc->mtime = strtoll(wv[2], NULL, 10);
331 for(i = 3; i < wc; i++)
333 if(!strcmp(wv[i], "tth"))
337 hash = base64decode(wv[i], &len);
343 memcpy(hc->tth, hash, 24);
351 static void writehashcache(void)
356 struct hashcache *hc;
358 hcname = findhashcachefile(1);
359 if((stream = fopen(hcname, "w")) == NULL)
361 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
364 fprintf(stream, "# Dolda Connect hash cache file\n");
365 fprintf(stream, "# Generated automatically, do not edit\n");
366 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
367 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
368 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
369 for(hc = hashcache; hc != NULL; hc = hc->next)
371 buf = base64encode(hc->tth, 24);
372 fprintf(stream, "%lli %lli %li tth %s\n", hc->dev, (long long)hc->inode, hc->mtime, buf);
378 static void hashread(struct socket *sk, void *uudata)
380 static char *hashbuf;
381 static size_t hashbufsize = 0, hashbufdata = 0;
382 char *buf, *p, *p2, *lp;
389 struct hashcache *hc;
391 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
393 bufcat(hashbuf, buf, bufsize);
395 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
402 while((p2 = strchr(p, ' ')) == p)
415 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
417 dev = strtoll(wv[0], NULL, 10);
418 inode = strtoll(wv[1], NULL, 10);
419 mtime = strtol(wv[2], NULL, 10);
420 if((hc = findhashcache(dev, inode)) == NULL)
427 buf = base64decode(wv[3], NULL);
428 memcpy(hc->tth, buf, 24);
432 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
436 static void hashexit(pid_t pid, int status, void *uudata)
439 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
441 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
446 static int hashfile(char *path)
453 struct tigertreehash tth;
455 struct socket *outsock;
457 if((fd = open(path, O_RDONLY)) < 0)
459 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
462 if(fstat(fd, &sb) < 0)
464 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
470 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
477 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
486 signal(SIGHUP, SIG_DFL);
488 pfd[1] = dup2(pfd[1], 3);
491 for(i = 3; i < FD_SETSIZE; i++)
495 while((ret = read(0, buf, 4096)) > 0)
496 dotigertree(&tth, buf, ret);
499 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
503 restigertree(&tth, digest);
504 ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
510 outsock = wrapsock(pfd[0]);
511 outsock->readcb = hashread;
512 childcallback(hashjob, hashexit, NULL);
517 * Call only when hashjob == 0
519 static void checkhashes(void)
521 struct sharecache *node;
522 struct hashcache *hc;
525 node = shareroot->child;
528 if(node->child != NULL)
533 if(!node->f.b.hastth)
535 if((hc = findhashcache(node->dev, node->inode)) != NULL)
537 memcpy(node->hashtth, hc->tth, 24);
538 node->f.b.hastth = 1;
539 GCBCHAINDOCB(sharechangecb, sharesize);
541 path = getfspath(node);
544 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
551 while(node->next == NULL)
553 if((node = node->parent) == shareroot)
556 if(node == shareroot)
562 struct sharecache *nextscnode(struct sharecache *node)
564 if(node->child != NULL)
566 while(node->next == NULL)
569 if(node == shareroot)
575 static void freescan(struct scanstate *job)
582 /* No need for optimization; lookup isn't really that common */
583 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
585 struct sharecache *node;
587 for(node = parent->child; node != NULL; node = node->next)
589 if(!wcscmp(node->name, name))
595 static void attachcache(struct sharecache *parent, struct sharecache *node)
597 node->parent = parent;
598 node->next = parent->child;
599 if(parent->child != NULL)
600 parent->child->prev = node;
601 parent->child = node;
604 static void detachcache(struct sharecache *node)
606 if(node->next != NULL)
607 node->next->prev = node->prev;
608 if(node->prev != NULL)
609 node->prev->next = node->next;
610 if((node->parent != NULL) && (node->parent->child == node))
611 node->parent->child = node->next;
617 static void freecache(struct sharecache *node)
619 struct sharecache *cur, *next;
620 struct scanqueue *q, *nq, **fq;
624 for(q = scanqueue; q != NULL; q = nq)
627 if(q->state->node == node)
629 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
637 if(node->child != NULL)
639 for(cur = node->child; cur != NULL; cur = next)
645 CBCHAINDOCB(node, share_delete, node);
646 CBCHAINFREE(node, share_delete);
647 sharesize -= node->size;
648 if(node->path != NULL)
650 if(node->name != NULL)
655 static void freesharepoint(struct sharepoint *share)
657 struct sharecache *node;
659 if(share->next != NULL)
660 share->next->prev = share->prev;
661 if(share->prev != NULL)
662 share->prev->next = share->next;
664 shares = share->next;
665 if((node = findcache(shareroot, share->name)) != NULL)
672 static struct sharecache *newcache(void)
674 struct sharecache *new;
676 new = smalloc(sizeof(*new));
677 memset(new, 0, sizeof(*new));
678 CBCHAININIT(new, share_delete);
682 char *getfspath(struct sharecache *node)
687 buf = smalloc(bufsize = 64);
691 if(node->path != NULL)
693 if(bufsize < strlen(node->path) + strlen(buf) + 1)
694 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
695 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
696 memcpy(buf, node->path, strlen(node->path));
699 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
701 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
705 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
706 buf = srealloc(buf, bufsize *= 2);
707 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
708 memcpy(buf + 1, mbsname, strlen(mbsname));
713 buf = srealloc(buf, strlen(buf) + 1);
717 static int checknode(struct sharecache *node)
722 if(node->parent == NULL)
726 if(!checknode(node->parent))
728 path = getfspath(node);
729 if(stat(path, &sb) < 0)
731 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
732 queuescan(node->parent);
740 int opensharecache(struct sharecache *node)
745 path = getfspath(node);
746 fd = open(path, O_RDONLY);
750 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
758 static struct scanstate *newscan(struct sharecache *node)
760 struct scanstate *new;
762 new = smalloc(sizeof(*new));
769 void queuescan(struct sharecache *node)
771 struct scanqueue *new;
773 new = smalloc(sizeof(*new));
774 new->state = newscan(node);
775 new->next = scanqueue;
779 /* For internal use in doscan() */
780 static void removestale(struct sharecache *node)
782 struct sharecache *cur, *next;
784 for(cur = node->child; cur != NULL; cur = next)
792 /* For internal use in doscan() */
793 static void jobdone(void)
795 struct scanstate *jbuf;
798 scanjob = jbuf->next;
801 fchdir(dirfd(scanjob->dd));
804 int doscan(int quantum)
809 struct sharecache *n;
810 struct scanstate *jbuf;
811 struct scanqueue *qbuf;
814 struct hashcache *hc;
816 static int busybefore = 0;
818 dmask = confgetint("cli", "scandirmask");
819 fmask = confgetint("cli", "scanfilemask");
820 if((scanjob != NULL) && (scanjob->dd != NULL))
822 while(fchdir(dirfd(scanjob->dd)) < 0)
824 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
825 removestale(scanjob->node);
835 while(scanjob == NULL)
837 if(scanqueue == NULL)
841 flog(LOG_INFO, "sharing %lli bytes", sharesize);
843 GCBCHAINDOCB(sharechangecb, sharesize);
850 scanjob = scanqueue->state;
852 scanqueue = qbuf->next;
854 for(n = scanjob->node->child; n != NULL; n = n->next)
858 if(scanjob->dd == NULL)
860 path = getfspath(scanjob->node);
861 if((scanjob->dd = opendir(path)) == NULL)
863 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
864 freecache(scanjob->node);
870 if(fchdir(dirfd(scanjob->dd)) < 0)
872 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
877 if((de = readdir(scanjob->dd)) == NULL)
879 removestale(scanjob->node);
883 if(*de->d_name == '.')
885 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
887 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
890 n = findcache(scanjob->node, wcs);
891 if(stat(de->d_name, &sb) < 0)
896 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
899 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
903 if(S_ISDIR(sb.st_mode))
905 if(~sb.st_mode & dmask)
911 } else if(S_ISREG(sb.st_mode)) {
912 if(~sb.st_mode & fmask)
919 flog(LOG_WARNING, "unhandled file type: %i", sb.st_mode);
925 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
935 if(S_ISREG(sb.st_mode))
937 sharesize += (n->size = sb.st_size);
941 n->mtime = sb.st_mtime;
943 n->inode = sb.st_ino;
945 attachcache(scanjob->node, n);
950 if(n->f.b.type == FILE_DIR)
953 jbuf->next = scanjob;
955 } else if(n->f.b.type == FILE_REG) {
956 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
960 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
962 if(hc->mtime == n->mtime)
965 memcpy(n->hashtth, hc->tth, 24);
976 void scanshares(void)
978 struct sharepoint *cur;
979 struct sharecache *node;
982 for(cur = shares; cur != NULL; cur = cur->next)
984 if((node = findcache(shareroot, cur->name)) == NULL)
986 if(stat(cur->path, &sb))
988 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
991 if(!S_ISDIR(sb.st_mode))
993 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
997 node->name = swcsdup(cur->name);
998 node->path = sstrdup(cur->path);
999 if(node->path[strlen(node->path) - 1] == '/')
1000 node->path[strlen(node->path) - 1] = 0;
1001 node->f.b.type = FILE_DIR;
1002 attachcache(shareroot, node);
1008 static void preinit(int hup)
1010 struct sharepoint *cur;
1014 for(cur = shares; cur != NULL; cur = cur->next)
1017 shareroot = newcache();
1018 shareroot->name = swcsdup(L"");
1019 shareroot->f.b.type = FILE_DIR;
1023 static int init(int hup)
1025 struct sharepoint *cur, *next;
1028 for(cur = shares; cur != NULL; cur = next)
1032 freesharepoint(cur);
1040 static int run(void)
1045 static void terminate(void)
1048 kill(hashjob, SIGHUP);
1049 while(shares != NULL)
1050 freesharepoint(shares);
1051 freecache(shareroot);
1054 static struct module me =
1065 .terminate = terminate