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 static struct hashcache *newhashcache(void)
136 struct hashcache *new;
138 new = smalloc(sizeof(*new));
139 memset(new, 0, sizeof(*new));
140 new->next = hashcache;
142 if(hashcache != NULL)
143 hashcache->prev = new;
148 static void freehashcache(struct hashcache *hc)
151 hc->next->prev = hc->prev;
153 hc->prev->next = hc->next;
155 hashcache = hc->next;
159 static char *findhashcachefile(int filldef)
161 static char ret[128];
164 if(getenv("HOME") != NULL)
166 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
167 if(!access(ret, R_OK))
170 if((hcname = icswcstombs(confgetstr("cli", "hashcache"), NULL, NULL)) == NULL)
172 flog(LOG_WARNING, "could not convert hash cache name into local charset: %s", strerror(errno));
175 if(strchr(hcname, '/') != NULL)
177 if(!access(hcname, R_OK))
183 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
184 if(!access(ret, R_OK))
186 snprintf(ret, sizeof(ret), "/usr/etc/%s", hcname);
187 if(!access(ret, R_OK))
189 snprintf(ret, sizeof(ret), "/usr/local/etc/%s", hcname);
190 if(!access(ret, R_OK))
195 if(getenv("HOME") != NULL)
196 snprintf(ret, sizeof(ret), "%s/.dc-hashcache", getenv("HOME"));
198 snprintf(ret, sizeof(ret), "/etc/%s", hcname);
205 static struct hashcache *findhashcache(dev_t dev, ino_t inode)
207 struct hashcache *hc;
209 for(hc = hashcache; hc != NULL; hc = hc->next)
211 if((hc->dev == dev) && (hc->inode == inode))
217 static void readhashcache(void)
223 char *p, *p2, *wv[32], *hash;
224 struct hashcache *hc;
227 if((hcname = findhashcachefile(0)) == NULL)
229 if((stream = fopen(hcname, "r")) == NULL)
231 flog(LOG_WARNING, "could not open hash cache %s: %s", hcname, strerror(errno));
234 while(hashcache != NULL)
235 freehashcache(hashcache);
239 fgets(linebuf, sizeof(linebuf), stream);
241 for(p = linebuf; *p; p++)
246 if(linebuf[0] == '#')
248 for(wc = 0, p = linebuf; (wc < 32) && ((p2 = strchr(p, ' ')) != NULL); p = p2 + 1)
258 hc->dev = strtoll(wv[0], NULL, 10);
259 hc->inode = strtoll(wv[1], NULL, 10);
260 hc->mtime = strtoll(wv[2], NULL, 10);
261 for(i = 3; i < wc; i++)
263 if(!strcmp(wv[i], "tth"))
267 hash = base64decode(wv[i], &len);
273 memcpy(hc->tth, hash, 24);
281 static void writehashcache(void)
286 struct hashcache *hc;
288 hcname = findhashcachefile(1);
289 if((stream = fopen(hcname, "w")) == NULL)
291 flog(LOG_WARNING, "could not write hash cache %s: %s", hcname, strerror(errno));
294 fprintf(stream, "# Dolda Connect hash cache file\n");
295 fprintf(stream, "# Generated automatically, do not edit\n");
296 fprintf(stream, "# Format: DEVICE INODE MTIME [HASH...]\n");
297 fprintf(stream, "# HASH := HASHTYPE HASHVAL\n");
298 fprintf(stream, "# HASHTYPE can currently only be `tth'\n");
299 for(hc = hashcache; hc != NULL; hc = hc->next)
301 buf = base64encode(hc->tth, 24);
302 fprintf(stream, "%lli %lli %li tth %s\n", hc->dev, (long long)hc->inode, hc->mtime, buf);
308 static void hashread(struct socket *sk, void *uudata)
310 static char *hashbuf;
311 static size_t hashbufsize = 0, hashbufdata = 0;
312 char *buf, *p, *p2, *lp;
319 struct hashcache *hc;
321 if((buf = sockgetinbuf(sk, &bufsize)) == NULL)
323 bufcat(hashbuf, buf, bufsize);
325 while((lp = memchr(hashbuf, '\n', hashbufdata)) != NULL)
332 while((p2 = strchr(p, ' ')) == p)
345 flog(LOG_ERR, "BUG: unexpected number of words (%i) arrived from hashing process", wc);
347 dev = strtoll(wv[0], NULL, 10);
348 inode = strtoll(wv[1], NULL, 10);
349 mtime = strtol(wv[2], NULL, 10);
350 if((hc = findhashcache(dev, inode)) == NULL)
357 buf = base64decode(wv[3], NULL);
358 memcpy(hc->tth, buf, 24);
362 memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
366 static void hashexit(pid_t pid, int status, void *uudata)
369 flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
371 flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
376 static int hashfile(char *path)
383 struct tigertreehash tth;
385 struct socket *outsock;
387 if((fd = open(path, O_RDONLY)) < 0)
389 flog(LOG_WARNING, "could not open %s for hashing: %s", path, strerror(errno));
392 if(fstat(fd, &sb) < 0)
394 flog(LOG_WARNING, "could not stat %s while hashing: %s", path, strerror(errno));
400 flog(LOG_WARNING, "could not create pipe(!): %s", strerror(errno));
407 flog(LOG_WARNING, "could not fork(!) hashing process: %s", strerror(errno));
416 signal(SIGHUP, SIG_DFL);
418 pfd[1] = dup2(pfd[1], 3);
421 for(i = 3; i < FD_SETSIZE; i++)
425 while((ret = read(0, buf, 4096)) > 0)
426 dotigertree(&tth, buf, ret);
429 flog(LOG_WARNING, "could not read from %s while hashing: %s", path, strerror(errno));
433 restigertree(&tth, digest);
434 ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
440 outsock = wrapsock(pfd[0]);
441 outsock->readcb = hashread;
442 childcallback(hashjob, hashexit, NULL);
447 * Call only when hashjob == 0
449 static void checkhashes(void)
451 struct sharecache *node;
452 struct hashcache *hc;
455 node = shareroot->child;
458 if(node->child != NULL)
463 if(!node->f.b.hastth)
465 if((hc = findhashcache(node->dev, node->inode)) != NULL)
467 memcpy(node->hashtth, hc->tth, 24);
468 node->f.b.hastth = 1;
469 GCBCHAINDOCB(sharechangecb, sharesize);
471 path = getfspath(node);
474 flog(LOG_WARNING, "could not hash %s, unsharing it", path);
481 while(node->next == NULL)
483 if((node = node->parent) == shareroot)
486 if(node == shareroot)
492 struct sharecache *nextscnode(struct sharecache *node)
494 if(node->child != NULL)
496 while(node->next == NULL)
499 if(node == shareroot)
505 static void freescan(struct scanstate *job)
512 /* No need for optimization; lookup isn't really that common */
513 struct sharecache *findcache(struct sharecache *parent, wchar_t *name)
515 struct sharecache *node;
517 for(node = parent->child; node != NULL; node = node->next)
519 if(!wcscmp(node->name, name))
525 static void attachcache(struct sharecache *parent, struct sharecache *node)
527 node->parent = parent;
528 node->next = parent->child;
529 if(parent->child != NULL)
530 parent->child->prev = node;
531 parent->child = node;
534 static void detachcache(struct sharecache *node)
536 if(node->next != NULL)
537 node->next->prev = node->prev;
538 if(node->prev != NULL)
539 node->prev->next = node->next;
540 if((node->parent != NULL) && (node->parent->child == node))
541 node->parent->child = node->next;
547 static void freecache(struct sharecache *node)
549 struct sharecache *cur, *next;
550 struct scanqueue *q, *nq, **fq;
554 for(q = scanqueue; q != NULL; q = nq)
557 if(q->state->node == node)
559 flog(LOG_DEBUG, "freed node %ls cancelled queued scan", node->name);
567 if(node->child != NULL)
569 for(cur = node->child; cur != NULL; cur = next)
575 CBCHAINDOCB(node, share_delete, node);
576 CBCHAINFREE(node, share_delete);
577 sharesize -= node->size;
578 if(node->path != NULL)
580 if(node->name != NULL)
585 static void freesharepoint(struct sharepoint *share)
587 struct sharecache *node;
589 if(share->next != NULL)
590 share->next->prev = share->prev;
591 if(share->prev != NULL)
592 share->prev->next = share->next;
594 shares = share->next;
595 if((node = findcache(shareroot, share->name)) != NULL)
602 static struct sharecache *newcache(void)
604 struct sharecache *new;
606 new = smalloc(sizeof(*new));
607 memset(new, 0, sizeof(*new));
608 CBCHAININIT(new, share_delete);
612 char *getfspath(struct sharecache *node)
617 buf = smalloc(bufsize = 64);
621 if(node->path != NULL)
623 if(bufsize < strlen(node->path) + strlen(buf) + 1)
624 buf = srealloc(buf, strlen(node->path) + strlen(buf) + 1);
625 memmove(buf + strlen(node->path), buf, strlen(buf) + 1);
626 memcpy(buf, node->path, strlen(node->path));
629 if((mbsname = icwcstombs(node->name, NULL)) == NULL)
631 flog(LOG_WARNING, "could not map unicode share name (%ls) into filesystem charset: %s", node->name, strerror(errno));
635 while(bufsize < strlen(mbsname) + 1 + strlen(buf) + 1)
636 buf = srealloc(buf, bufsize *= 2);
637 memmove(buf + strlen(mbsname) + 1, buf, strlen(buf) + 1);
638 memcpy(buf + 1, mbsname, strlen(mbsname));
643 buf = srealloc(buf, strlen(buf) + 1);
647 static int checknode(struct sharecache *node)
652 if(node->parent == NULL)
656 if(!checknode(node->parent))
658 path = getfspath(node);
659 if(stat(path, &sb) < 0)
661 flog(LOG_INFO, "%s was found to be broken (%s); scheduling rescan of parent", path, strerror(errno));
662 queuescan(node->parent);
670 int opensharecache(struct sharecache *node)
675 path = getfspath(node);
676 fd = open(path, O_RDONLY);
680 flog(LOG_WARNING, "could not open %s: %s", path, strerror(errbak));
688 static struct scanstate *newscan(struct sharecache *node)
690 struct scanstate *new;
692 new = smalloc(sizeof(*new));
699 void queuescan(struct sharecache *node)
701 struct scanqueue *new;
703 new = smalloc(sizeof(*new));
704 new->state = newscan(node);
705 new->next = scanqueue;
709 /* For internal use in doscan() */
710 static void removestale(struct sharecache *node)
712 struct sharecache *cur, *next;
714 for(cur = node->child; cur != NULL; cur = next)
722 /* For internal use in doscan() */
723 static void jobdone(void)
725 struct scanstate *jbuf;
728 scanjob = jbuf->next;
731 fchdir(dirfd(scanjob->dd));
734 int doscan(int quantum)
739 struct sharecache *n;
740 struct scanstate *jbuf;
741 struct scanqueue *qbuf;
744 struct hashcache *hc;
746 static int busybefore = 0;
748 dmask = confgetint("cli", "scandirmask");
749 fmask = confgetint("cli", "scanfilemask");
750 if((scanjob != NULL) && (scanjob->dd != NULL))
752 while(fchdir(dirfd(scanjob->dd)) < 0)
754 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
755 removestale(scanjob->node);
765 while(scanjob == NULL)
767 if(scanqueue == NULL)
771 flog(LOG_INFO, "sharing %lli bytes", sharesize);
773 GCBCHAINDOCB(sharechangecb, sharesize);
780 scanjob = scanqueue->state;
782 scanqueue = qbuf->next;
784 for(n = scanjob->node->child; n != NULL; n = n->next)
788 if(scanjob->dd == NULL)
790 path = getfspath(scanjob->node);
791 if((scanjob->dd = opendir(path)) == NULL)
793 flog(LOG_WARNING, "cannot open directory %s for scanning: %s, deleting from share", path, strerror(errno));
794 freecache(scanjob->node);
800 if(fchdir(dirfd(scanjob->dd)) < 0)
802 flog(LOG_WARNING, "could not fchdir to fd %i: %s", dirfd(scanjob->dd), strerror(errno));
807 if((de = readdir(scanjob->dd)) == NULL)
809 removestale(scanjob->node);
813 if(*de->d_name == '.')
815 if((wcs = icmbstowcs(de->d_name, NULL)) == NULL)
817 flog(LOG_WARNING, "file name %s has cannot be converted to wchar: %s", de->d_name, strerror(errno));
820 n = findcache(scanjob->node, wcs);
821 if(stat(de->d_name, &sb) < 0)
826 flog(LOG_WARNING, "could not stat %s: %s, deleting from share", de->d_name, strerror(errno));
829 flog(LOG_WARNING, "could not stat %s: %s", de->d_name, strerror(errno));
833 if(S_ISDIR(sb.st_mode))
835 if(~sb.st_mode & dmask)
841 } else if(S_ISREG(sb.st_mode)) {
842 if(~sb.st_mode & fmask)
849 flog(LOG_WARNING, "unhandled file type: %i", sb.st_mode);
855 if((n->f.b.type != type) || (n->mtime != sb.st_mtime) || ((type == FILE_REG) && (n->size != sb.st_size)))
865 if(S_ISREG(sb.st_mode))
867 sharesize += (n->size = sb.st_size);
871 n->mtime = sb.st_mtime;
873 n->inode = sb.st_ino;
875 attachcache(scanjob->node, n);
880 if(n->f.b.type == FILE_DIR)
883 jbuf->next = scanjob;
885 } else if(n->f.b.type == FILE_REG) {
886 if(n->f.b.hastth && (n->mtime != sb.st_mtime))
890 if((hc = findhashcache(sb.st_dev, sb.st_ino)) != NULL)
892 if(hc->mtime == n->mtime)
895 memcpy(n->hashtth, hc->tth, 24);
906 void scanshares(void)
908 struct sharepoint *cur;
909 struct sharecache *node;
912 for(cur = shares; cur != NULL; cur = cur->next)
914 if((node = findcache(shareroot, cur->name)) == NULL)
916 if(stat(cur->path, &sb))
918 flog(LOG_WARNING, "could not stat share \"%ls\": %s", cur->name, strerror(errno));
921 if(!S_ISDIR(sb.st_mode))
923 flog(LOG_WARNING, "%s is not a directory; won't share it", cur->path);
927 node->name = swcsdup(cur->name);
928 node->path = sstrdup(cur->path);
929 if(node->path[strlen(node->path) - 1] == '/')
930 node->path[strlen(node->path) - 1] = 0;
931 node->f.b.type = FILE_DIR;
932 attachcache(shareroot, node);
938 static void preinit(int hup)
940 struct sharepoint *cur;
944 for(cur = shares; cur != NULL; cur = cur->next)
947 shareroot = newcache();
948 shareroot->name = swcsdup(L"");
949 shareroot->f.b.type = FILE_DIR;
953 static int init(int hup)
955 struct sharepoint *cur, *next;
958 for(cur = shares; cur != NULL; cur = next)
975 static void terminate(void)
978 kill(hashjob, SIGHUP);
979 while(shares != NULL)
980 freesharepoint(shares);
981 freecache(shareroot);
984 static struct module me =
995 .terminate = terminate