Write the hash cache back to disk whenever it is dirty when the daemon exits.
[doldaconnect.git] / daemon / client.c
index 5b080fb..46766e9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  Dolda Connect - Modular multiuser Direct Connect-style client
- *  Copyright (C) 2004 Fredrik Tolf (fredrik@dolda2000.com)
+ *  Copyright (C) 2004 Fredrik Tolf <fredrik@dolda2000.com>
  *  
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -18,7 +18,6 @@
 */
 #include <stdlib.h>
 #include <stdio.h>
-#include <malloc.h>
 #include <wchar.h>
 #include <string.h>
 #include <errno.h>
@@ -75,6 +74,17 @@ static struct configvar myvars[] =
     /** The filename to use for the hash cache (see the FILES section
      * for more information). */
     {CONF_VAR_STRING, "hashcache", {.str = L"dc-hashcache"}},
+    /** Writes of the hash cache and file lists are delayed for an
+     * amount of time, in order to minimize the time spent on I/O wait
+     * while hashing many small files. This variable sets the amount
+     * of time, in seconds. */
+    {CONF_VAR_INT, "hashwritedelay", {.num = 300}},
+    /** The amount of time, in seconds, to wait before automatically
+     * rescanning the shared directories for changes. Set to zero (the
+     * default) to disable automatic rescanning. (Broken shares are
+     * always rescanned upon detection, regardless of this
+     * setting.) */
+    {CONF_VAR_INT, "rescandelay", {.num = 0}},
     {CONF_VAR_END}
 };
 
@@ -95,6 +105,8 @@ static struct timer *hashwritetimer = NULL;
  * job. */
 static pid_t hashjob = -1;
 struct sharecache *shareroot = NULL;
+static struct timer *scantimer = NULL;
+int sharedfiles = 0;
 unsigned long long sharesize = 0;
 GCBCHAIN(sharechangecb, unsigned long long);
 
@@ -348,7 +360,7 @@ static void writehashcache(int now)
     if(!now)
     {
        if(hashwritetimer == NULL)
-           hashwritetimer = timercallback(ntime() + 300, (void (*)(int, void *))hashtimercb, NULL);
+           hashwritetimer = timercallback(ntime() + confgetint("cli", "hashwritedelay"), (void (*)(int, void *))hashtimercb, NULL);
        return;
     }
     if(hashwritetimer != NULL)
@@ -367,7 +379,7 @@ static void writehashcache(int now)
     for(hc = hashcache; hc != NULL; hc = hc->next)
     {
        buf = base64encode(hc->tth, 24);
-       fprintf(stream, "%lli %lli %li tth %s\n", hc->dev, (long long)hc->inode, hc->mtime, buf);
+       fprintf(stream, "%lli %lli %li tth %s\n", (long long)hc->dev, (long long)hc->inode, hc->mtime, buf);
        free(buf);
     }
     fclose(stream);
@@ -500,7 +512,7 @@ static int hashfile(char *path)
        }
        synctigertree(&tth);
        restigertree(&tth, digest);
-       ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
+       ret = snprintf(buf, sizeof(buf), "%lli %lli %li %s\n", (long long)sb.st_dev, (long long)sb.st_ino, sb.st_mtime, base64encode(digest, 24));
        write(1, buf, ret);
        exit(0);
     }
@@ -517,13 +529,14 @@ static int hashfile(char *path)
  */
 static void checkhashes(void)
 {
-    struct sharecache *node;
+    struct sharecache *node, *next;
     struct hashcache *hc;
     char *path;
     
     node = shareroot->child;
-    for(node = shareroot->child; node != NULL; node = nextscnode(node))
+    for(node = shareroot->child; node != NULL; node = next)
     {
+       next = nextscnode(node);
        if(node->f.b.type != FILE_REG)
            continue;
        if(!node->f.b.hastth)
@@ -636,6 +649,8 @@ static void freecache(struct sharecache *node)
     CBCHAINDOCB(node, share_delete, node);
     CBCHAINFREE(node, share_delete);
     sharesize -= node->size;
+    if(node->f.b.type == FILE_REG)
+       sharedfiles--;
     if(node->path != NULL)
        free(node->path);
     if(node->name != NULL)
@@ -926,6 +941,7 @@ int doscan(int quantum)
            if(S_ISREG(sb.st_mode))
            {
                sharesize += (n->size = sb.st_size);
+               sharedfiles++;
            } else {
                n->size = 0;
            }
@@ -964,6 +980,18 @@ int doscan(int quantum)
     return(1);
 }
 
+static void rescancb(int cancelled, void *uudata)
+{
+    scantimer = NULL;
+    if(!cancelled)
+    {
+       if(scanqueue == NULL)
+           scanshares();
+       else if(confgetint("cli", "rescandelay") > 0)
+           scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
+    }
+}
+
 void scanshares(void)
 {
     struct sharepoint *cur;
@@ -994,6 +1022,10 @@ void scanshares(void)
        }
        queuescan(node);
     }
+    if(scantimer != NULL)
+       canceltimer(scantimer);
+    if(confgetint("cli", "rescandelay") > 0)
+       scantimer = timercallback(ntime() + confgetint("cli", "rescandelay"), (void (*)(int, void *))rescancb, NULL);
 }
 
 static void preinit(int hup)
@@ -1011,6 +1043,15 @@ static void preinit(int hup)
     }
 }
 
+static int rsdelayupdate(struct configvar *var, void *uudata)
+{
+    if(scantimer != NULL)
+       canceltimer(scantimer);
+    if(confgetint("cli", "rescandelay") > 0)
+       scantimer = timercallback(ntime() + var->val.num, (void (*)(int, void *))rescancb, NULL);
+    return(0);
+}
+
 static int init(int hup)
 {
     struct sharepoint *cur, *next;
@@ -1024,7 +1065,10 @@ static int init(int hup)
     }
     scanshares();
     if(!hup)
+    {
        while(doscan(100));
+       CBREG(confgetvar("cli", "rescandelay"), conf_update, rsdelayupdate, NULL, NULL);
+    }
     return(0);
 }
 
@@ -1042,6 +1086,8 @@ static void terminate(void)
 {
     if(hashjob != 0)
        kill(hashjob, SIGHUP);
+    if(hashwritetimer != NULL)
+       writehashcache(1);
     while(shares != NULL)
        freesharepoint(shares);
     freecache(shareroot);