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 /* Set initially to -1, but changed to 0 the first time run() is
80 * called. This is to avoid forking a hash job before daemonizing,
81 * since that would make the daemon unable to wait() for the hash
83 static pid_t hashjob = -1;
84 struct sharecache *shareroot = NULL;
85 unsigned long long sharesize = 0;
86 GCBCHAIN(sharechangecb, unsigned long long);
88 static int conf_share(int argc, wchar_t **argv)
90 struct sharepoint *share;
95 flog(LOG_WARNING, "not enough arguments given for share command");
98 if((b = icwcstombs(argv[2], NULL)) == NULL)
100 flog(LOG_WARNING, "could not convert wcs path (%ls) to current locale's charset: %s", argv[2], strerror(errno));
103 for(share = shares; share != NULL; share = share->next)
105 if(!strcmp(share->path, b) && !wcscmp(share->name, argv[1]))
112 share = smalloc(sizeof(*share));
115 share->name = swcsdup(argv[1]);
116 share->next = shares;
119 shares->prev = share;
124 static void dumpsharecache(struct sharecache *node, int l)
128 for(; node != NULL; node = node->next)
130 for(i = 0; i < l; i++)
132 printf("%ls\n", node->name);
133 if(node->f.b.type == FILE_DIR)
134 dumpsharecache(node->child, l + 1);
138 struct hash *newhash(wchar_t *algo, size_t len, char *buf)
142 ret = smalloc(sizeof(*ret));
143 memset(ret, 0, sizeof(*ret));
144 ret->algo = swcsdup(algo);
146 ret->buf = memcpy(smalloc(len), buf, len);
150 void freehash(struct hash *hash)
157 struct hash *duphash(struct hash *hash)
159 return(newhash(hash->algo, hash->len, hash->buf));
162 struct hash *parsehash(wchar_t *text)
165 char *mbsbuf, *decbuf;
169 if((p = wcschr(text, L':')) == NULL)
172 if((mbsbuf = icwcstombs(p, "US-ASCII")) == NULL)
174 decbuf = base64decode(mbsbuf, &buflen);
178 ret = newhash(text, buflen, decbuf);
183 wchar_t *unparsehash(struct hash *hash)
185 static wchar_t *buf = NULL;
188 size_t bufsize, bufdata;
193 bufsize = bufdata = 0;
194 hbuf = base64encode(hash->buf, hash->len);
195 if((whbuf = icmbstowcs(hbuf, "US-ASCII")) == NULL)
197 flog(LOG_CRIT, "bug! could not convert base64 from us-ascii: %s", strerror(errno));
201 bufcat(buf, hash->algo, wcslen(hash->algo));
203 bufcat(buf, whbuf, wcslen(whbuf));
209 int hashcmp(struct hash *h1, struct hash *h2)
211 if(wcscmp(h1->algo, h2->algo))
213 if(h1->len != h2->len)
215 if(memcmp(h1->buf, h2->buf, h1->len))
220 static struct hashcache *newhashcache(void)
222 struct hashcache *new;
224 new = smalloc(sizeof(*new));
225 memset(new, 0, sizeof(*new));
226 new->next = hashcache;
228 if(hashcache != NULL)
229 hashcache->prev = new;
234 static void freehashcache(struct hashcache *hc)
237 hc->next->prev = hc->prev;
239 hc->prev->next = hc->next;
241 hashcache = hc->next;
245 static char *findhashcachefile(int filldef)
247 static char ret[128];
250 if(getenv("HOME") != NULL)
252 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
253 if(!access(ret, R_OK))
256 if((hcname = icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL)) == NULL)
258 flog(LOG_WARNING, "could not convert hash cache name into local charset: %s", strerror(errno));
261 if(strchr(hcname, '/') != NULL)
263 if(!access(hcname, R_OK))
269 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
270 if(!access(ret, R_OK))
272 snprintf(ret, sizeof(ret), "/usr/etc/%s", hcname);
273 if(!access(ret, R_OK))
275 snprintf(ret, sizeof(ret), "/usr/local/etc/%s", hcname);
276 if(!access(ret, R_OK))
281 if(getenv("HOME") != NULL)
282 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
284 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
291 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
293 struct hashcache *hc;
295 for(hc = hashcache; hc != NULL; hc = hc->next)
297 if((hc->dev == dev) && (hc->inode == inode))
303 static void readhashcache(void)
309 char *p, *p2, *wv[32], *hash;
310 struct hashcache *hc;
313 if((hcname = findhashcachefile(0)) == NULL)
315 if((stream = fopen(hcname, "r")) == NULL)
317 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
320 while(hashcache != NULL)
321 freehashcache(hashcache);
325 fgets(linebuf, sizeof(linebuf), stream);
327 for(p = linebuf; *p; p++)
332 if(linebuf[0] == '#')
334 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
344 hc->dev = strtoll(wv[0], NULL, 10);
345 hc->inode = strtoll(wv[1], NULL, 10);
346 hc->mtime = strtoll(wv[2], NULL, 10);
347 for(i = 3; i < wc; i++)
349 if(!strcmp(wv[i], "tth"))
353 hash = base64decode(wv[i], &len);
359 memcpy(hc->tth, hash, 24);
367 static void writehashcache(void)
372 struct hashcache *hc;
374 hcname = findhashcachefile(1);
375 if((stream = fopen(hcname, "w")) == NULL)
377 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
380 fprintf(stream, "# Dolda Connect hash cache file\n");
381 fprintf(stream, "# Generated automatically, do not edit\n");
382 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
383 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
384 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
385 for(hc = hashcache; hc != NULL; hc = hc->next)
387 buf = base64encode(hc->tth, 24);
388 fprintf(stream, "%lli %lli %li tth %s\n", hc->dev, (long long)hc->inode, hc->mtime, buf);
394 static void hashread(struct socket *sk, void *uudata)
396 static char *hashbuf;
397 static size_t hashbufsize = 0, hashbufdata = 0;
398 char *buf, *p, *p2, *lp;
405 struct hashcache *hc;
407 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
409 bufcat(hashbuf, buf, bufsize);
411 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
418 while((p2 = strchr(p, ' ')) == p)
431 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
433 dev = strtoll(wv[0], NULL, 10);
434 inode = strtoll(wv[1], NULL, 10);
435 mtime = strtol(wv[2], NULL, 10);
436 if((hc = findhashcache(dev, inode)) == NULL)
443 buf = base64decode(wv[3], NULL);
444 memcpy(hc->tth, buf, 24);
448 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
452 static void hashexit(pid_t pid, int status, struct socket *outsock)
455 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
457 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
463 static int hashfile(char *path)
470 struct tigertreehash tth;
472 struct socket *outsock;
474 if((fd = open(path, O_RDONLY)) < 0)
476 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
479 if(fstat(fd, &sb) < 0)
481 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
487 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
494 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
503 signal(SIGHUP, SIG_DFL);
505 pfd[1] = dup2(pfd[1], 3);
508 for(i = 3; i < FD_SETSIZE; i++)
512 while((ret = read(0, buf, 4096)) > 0)
513 dotigertree(&tth, buf, ret);
516 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
520 restigertree(&tth, digest);
521 ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
527 outsock = wrapsock(pfd[0]);
528 outsock->readcb = hashread;
529 childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
534 * Call only when hashjob == 0
536 static void checkhashes(void)
538 struct sharecache *node;
539 struct hashcache *hc;
542 node = shareroot->child;
543 for(node = shareroot->child; node != NULL; node = nextscnode(node))
545 if(node->f.b.type != FILE_REG)
547 if(!node->f.b.hastth)
549 if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
551 memcpy(node->hashtth, hc->tth, 24);
552 node->f.b.hastth = 1;
553 GCBCHAINDOCB(sharechangecb, sharesize);
555 path = getfspath(node);
558 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
561 flog(LOG_INFO, "sharing %lli bytes", sharesize);
571 struct sharecache *nextscnode(struct sharecache *node)
573 if(node->child != NULL)
575 while(node->next == NULL)
578 if(node == shareroot)
584 static void freescan(struct scanstate *job)
591 /* No need for optimization; lookup isn't really that common */
592 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
594 struct sharecache *node;
596 for(node = parent->child; node != NULL; node = node->next)
598 if(!wcscmp(node->name, name))
604 static void attachcache(struct sharecache *parent, struct sharecache *node)
606 node->parent = parent;
607 node->next = parent->child;
608 if(parent->child != NULL)
609 parent->child->prev = node;
610 parent->child = node;
613 static void detachcache(struct sharecache *node)
615 if(node->next != NULL)
616 node->next->prev = node->prev;
617 if(node->prev != NULL)
618 node->prev->next = node->next;
619 if((node->parent != NULL) && (node->parent->child == node))
620 node->parent->child = node->next;
626 static void freecache(struct sharecache *node)
628 struct sharecache *cur, *next;
629 struct scanqueue *q, *nq, **fq;
633 for(q = scanqueue; q != NULL; q = nq)
636 if(q->state->node == node)
638 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
646 if(node->child != NULL)
648 for(cur = node->child; cur != NULL; cur = next)
654 CBCHAINDOCB(node, share_delete, node);
655 CBCHAINFREE(node, share_delete);
656 sharesize -= node->size;
657 if(node->path != NULL)
659 if(node->name != NULL)
664 static void freesharepoint(struct sharepoint *share)
666 struct sharecache *node;
668 if(share->next != NULL)
669 share->next->prev = share->prev;
670 if(share->prev != NULL)
671 share->prev->next = share->next;
673 shares = share->next;
674 if((node = findcache(shareroot, share->name)) != NULL)
681 static struct sharecache *newcache(void)
683 struct sharecache *new;
685 new = smalloc(sizeof(*new));
686 memset(new, 0, sizeof(*new));
687 CBCHAININIT(new, share_delete);
691 char *getfspath(struct sharecache *node)
696 buf = smalloc(bufsize = 64);
700 if(node->path != NULL)
702 if(bufsize < strlen(node->path) + strlen(buf) + 1)
703 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
704 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
705 memcpy(buf, node->path, strlen(node->path));
708 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
710 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
714 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
715 buf = srealloc(buf, bufsize *= 2);
716 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
717 memcpy(buf + 1, mbsname, strlen(mbsname));
722 buf = srealloc(buf, strlen(buf) + 1);
726 static int checknode(struct sharecache *node)
731 if(node->parent == NULL)
735 if(!checknode(node->parent))
737 path = getfspath(node);
738 if(stat(path, &sb) < 0)
740 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
741 queuescan(node->parent);
749 int opensharecache(struct sharecache *node)
754 path = getfspath(node);
755 fd = open(path, O_RDONLY);
759 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
767 static struct scanstate *newscan(struct sharecache *node)
769 struct scanstate *new;
771 new = smalloc(sizeof(*new));
778 void queuescan(struct sharecache *node)
780 struct scanqueue *new;
782 new = smalloc(sizeof(*new));
783 new->state = newscan(node);
784 new->next = scanqueue;
788 /* For internal use in doscan() */
789 static void removestale(struct sharecache *node)
791 struct sharecache *cur, *next;
793 for(cur = node->child; cur != NULL; cur = next)
801 /* For internal use in doscan() */
802 static void jobdone(void)
804 struct scanstate *jbuf;
807 scanjob = jbuf->next;
810 fchdir(dirfd(scanjob->dd));
813 int doscan(int quantum)
818 struct sharecache *n;
819 struct scanstate *jbuf;
820 struct scanqueue *qbuf;
823 struct hashcache *hc;
825 static int busybefore = 0;
827 dmask = confgetint("cli", "scandirmask");
828 fmask = confgetint("cli", "scanfilemask");
829 if((scanjob != NULL) && (scanjob->dd != NULL))
831 while(fchdir(dirfd(scanjob->dd)) < 0)
833 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
834 removestale(scanjob->node);
844 while(scanjob == NULL)
846 if(scanqueue == NULL)
850 flog(LOG_INFO, "sharing %lli bytes", sharesize);
852 GCBCHAINDOCB(sharechangecb, sharesize);
859 scanjob = scanqueue->state;
861 scanqueue = qbuf->next;
863 for(n = scanjob->node->child; n != NULL; n = n->next)
867 if(scanjob->dd == NULL)
869 path = getfspath(scanjob->node);
870 if((scanjob->dd = opendir(path)) == NULL)
872 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
873 freecache(scanjob->node);
879 if(fchdir(dirfd(scanjob->dd)) < 0)
881 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
886 if((de = readdir(scanjob->dd)) == NULL)
888 removestale(scanjob->node);
892 if(*de->d_name == '.')
894 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
896 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
899 n = findcache(scanjob->node, wcs);
900 if(stat(de->d_name, &sb) < 0)
905 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
908 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
912 if(S_ISDIR(sb.st_mode))
914 if(~sb.st_mode & dmask)
920 } else if(S_ISREG(sb.st_mode)) {
921 if(~sb.st_mode & fmask)
928 flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
934 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
944 if(S_ISREG(sb.st_mode))
946 sharesize += (n->size = sb.st_size);
950 n->mtime = sb.st_mtime;
952 n->inode = sb.st_ino;
954 attachcache(scanjob->node, n);
959 if(n->f.b.type == FILE_DIR)
962 jbuf->next = scanjob;
964 } else if(n->f.b.type == FILE_REG) {
965 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
969 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
971 if(hc->mtime == n->mtime)
974 memcpy(n->hashtth, hc->tth, 24);
985 void scanshares(void)
987 struct sharepoint *cur;
988 struct sharecache *node;
991 for(cur = shares; cur != NULL; cur = cur->next)
993 if((node = findcache(shareroot, cur->name)) == NULL)
995 if(stat(cur->path, &sb))
997 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
1000 if(!S_ISDIR(sb.st_mode))
1002 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
1006 node->name = swcsdup(cur->name);
1007 node->path = sstrdup(cur->path);
1008 if(node->path[strlen(node->path) - 1] == '/')
1009 node->path[strlen(node->path) - 1] = 0;
1010 node->f.b.type = FILE_DIR;
1011 attachcache(shareroot, node);
1017 static void preinit(int hup)
1019 struct sharepoint *cur;
1023 for(cur = shares; cur != NULL; cur = cur->next)
1026 shareroot = newcache();
1027 shareroot->name = swcsdup(L"");
1028 shareroot->f.b.type = FILE_DIR;
1032 static int init(int hup)
1034 struct sharepoint *cur, *next;
1037 for(cur = shares; cur != NULL; cur = next)
1041 freesharepoint(cur);
1049 static int run(void)
1059 static void terminate(void)
1062 kill(hashjob, SIGHUP);
1063 while(shares != NULL)
1064 freesharepoint(shares);
1065 freecache(shareroot);
1068 static struct module me =
1079 .terminate = terminate