Add configvar documentation comments.
[doldaconnect.git] / daemon / client.c
index 1bf2420..47ac9de 100644 (file)
@@ -36,9 +36,9 @@
 #include "log.h"
 #include "utils.h"
 #include "module.h"
-#include "tiger.h"
 #include "net.h"
 #include "sysevents.h"
+#include <tiger.h>
 
 struct scanstate
 {
@@ -56,12 +56,24 @@ struct scanqueue
 static int conf_share(int argc, wchar_t **argv);
 static void freecache(struct sharecache *node);
 static void checkhashes(void);
+static void writehashcache(int now);
 
 static struct configvar myvars[] =
 {
+    /** The default nick name to use. The nickname can also be
+     * specified for individual hubs, overriding this setting. */
     {CONF_VAR_STRING, "defnick", {.str = L"DoldaConnect user"}},
+    /** When scanning shares, this bitmask is consulted for every
+     * regular file. Unless the file's mode has the bits specified by
+     * this mask set, it will not be shared. */
     {CONF_VAR_INT, "scanfilemask", {.num = 0004}},
+    /** When scanning shares, this bitmask is consulted for every
+     * directory encountered. Unless the directory's mode has the bits
+     * specified by this mask set, it will be ignored and any files
+     * under it will not be shared. */
     {CONF_VAR_INT, "scandirmask", {.num = 0005}},
+    /** The filename to use for the hash cache (see the FILES section
+     * for more information). */
     {CONF_VAR_STRING, "hashcache", {.str = L"dc-hashcache"}},
     {CONF_VAR_END}
 };
@@ -76,7 +88,12 @@ static struct scanstate *scanjob = NULL;
 static struct scanqueue *scanqueue = NULL;
 static struct sharepoint *shares = NULL;
 static struct hashcache *hashcache = NULL;
-static pid_t hashjob = 0;
+static struct timer *hashwritetimer = NULL;
+/* Set initially to -1, but changed to 0 the first time run() is
+ * called. This is to avoid forking a hash job before daemonizing,
+ * since that would make the daemon unable to wait() for the hash
+ * job. */
+static pid_t hashjob = -1;
 struct sharecache *shareroot = NULL;
 unsigned long long sharesize = 0;
 GCBCHAIN(sharechangecb, unsigned long long);
@@ -178,10 +195,13 @@ struct hash *parsehash(wchar_t *text)
 
 wchar_t *unparsehash(struct hash *hash)
 {
-    wchar_t *buf, *whbuf;
+    static wchar_t *buf = NULL;
+    wchar_t *whbuf;
     char *hbuf;
     size_t bufsize, bufdata;
     
+    if(buf != NULL)
+       free(buf);
     buf = NULL;
     bufsize = bufdata = 0;
     hbuf = base64encode(hash->buf, hash->len);
@@ -195,9 +215,21 @@ wchar_t *unparsehash(struct hash *hash)
     addtobuf(buf, ':');
     bufcat(buf, whbuf, wcslen(whbuf));
     free(whbuf);
+    addtobuf(buf, 0);
     return(buf);
 }
 
+int hashcmp(struct hash *h1, struct hash *h2)
+{
+    if(wcscmp(h1->algo, h2->algo))
+       return(0);
+    if(h1->len != h2->len)
+       return(0);
+    if(memcmp(h1->buf, h2->buf, h1->len))
+       return(0);
+    return(1);
+}
+
 static struct hashcache *newhashcache(void)
 {
     struct hashcache *new;
@@ -345,13 +377,28 @@ static void readhashcache(void)
     fclose(stream);
 }
 
-static void writehashcache(void)
+static void hashtimercb(int cancelled, void *uudata)
+{
+    hashwritetimer = NULL;
+    if(!cancelled)
+       writehashcache(1);
+}
+
+static void writehashcache(int now)
 {
     char *buf;
     char *hcname;
     FILE *stream;
     struct hashcache *hc;
     
+    if(!now)
+    {
+       if(hashwritetimer == NULL)
+           hashwritetimer = timercallback(ntime() + 300, (void (*)(int, void *))hashtimercb, NULL);
+       return;
+    }
+    if(hashwritetimer != NULL)
+       canceltimer(hashwritetimer);
     hcname = findhashcachefile(1);
     if((stream = fopen(hcname, "w")) == NULL)
     {
@@ -424,13 +471,13 @@ static void hashread(struct socket *sk, void *uudata)
            buf = base64decode(wv[3], NULL);
            memcpy(hc->tth, buf, 24);
            free(buf);
-           writehashcache();
+           writehashcache(0);
        }
        memmove(hashbuf, lp, hashbufdata -= (lp - hashbuf));
     }
 }
 
-static void hashexit(pid_t pid, int status, void *uudata)
+static void hashexit(pid_t pid, int status, struct socket *outsock)
 {
     if(pid != hashjob)
        flog(LOG_ERR, "BUG: hashing process changed PID?! old: %i new %i", hashjob, pid);
@@ -438,6 +485,7 @@ static void hashexit(pid_t pid, int status, void *uudata)
        flog(LOG_WARNING, "hashing process exited with non-zero status: %i", status);
     hashjob = 0;
     checkhashes();
+    putsock(outsock);
 }
 
 static int hashfile(char *path)
@@ -506,7 +554,7 @@ static int hashfile(char *path)
     close(pfd[1]);
     outsock = wrapsock(pfd[0]);
     outsock->readcb = hashread;
-    childcallback(hashjob, hashexit, NULL);
+    childcallback(hashjob, (void (*)(pid_t, int, void *))hashexit, outsock);
     return(0);
 }
 
@@ -520,16 +568,13 @@ static void checkhashes(void)
     char *path;
     
     node = shareroot->child;
-    while(1)
+    for(node = shareroot->child; node != NULL; node = nextscnode(node))
     {
-       if(node->child != NULL)
-       {
-           node = node->child;
+       if(node->f.b.type != FILE_REG)
            continue;
-       }
        if(!node->f.b.hastth)
        {
-           if((hc = findhashcache(node->dev, node->inode)) != NULL)
+           if(((hc = findhashcache(node->dev, node->inode)) != NULL) && (hc->mtime == node->mtime))
            {
                memcpy(node->hashtth, hc->tth, 24);
                node->f.b.hastth = 1;
@@ -540,19 +585,14 @@ static void checkhashes(void)
                {
                    flog(LOG_WARNING, "could not hash %s, unsharing it", path);
                    freecache(node);
+                   free(path);
+                   flog(LOG_INFO, "sharing %lli bytes", sharesize);
+                   continue;
                }
                free(path);
                return;
            }
        }
-       while(node->next == NULL)
-       {
-           if((node = node->parent) == shareroot)
-               break;
-       }
-       if(node == shareroot)
-           break;
-       node = node->next;
     }
 }
 
@@ -913,7 +953,7 @@ int doscan(int quantum)
            }
            type = FILE_REG;
        } else {
-           flog(LOG_WARNING, "unhandled file type: %i", sb.st_mode);
+           flog(LOG_WARNING, "unhandled file type: 0%o", sb.st_mode);
            free(wcs);
            continue;
        }
@@ -1036,6 +1076,11 @@ static int init(int hup)
 
 static int run(void)
 {
+    if(hashjob == -1)
+    {
+       hashjob = 0;
+       checkhashes();
+    }
     return(doscan(10));
 }