Write the hash cache back to disk whenever it is dirty when the daemon exits.
[doldaconnect.git] / daemon / client.c
index e6075dc..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
@@ -81,7 +81,9 @@ static struct configvar myvars[] =
     {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. */
+     * 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}
 };
@@ -104,6 +106,7 @@ static struct timer *hashwritetimer = NULL;
 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);
 
@@ -526,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)
@@ -645,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)
@@ -935,6 +941,7 @@ int doscan(int quantum)
            if(S_ISREG(sb.st_mode))
            {
                sharesize += (n->size = sb.st_size);
+               sharedfiles++;
            } else {
                n->size = 0;
            }
@@ -1079,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);