]> arthur.barton.de Git - netatalk.git/blobdiff - etc/cnid_dbd/cmd_dbd.c
Change logging
[netatalk.git] / etc / cnid_dbd / cmd_dbd.c
index 33b0b72f325efc9d77368599b219ea391c1d6322..aa2930aa446bc4fda0b57fb6e2413b3e526ef31c 100644 (file)
@@ -1,5 +1,5 @@
 /* 
-   $Id: cmd_dbd.c,v 1.12 2009-10-13 22:55:37 didg Exp $
+   $Id: cmd_dbd.c,v 1.24 2009-12-21 17:00:17 franklahm Exp $
 
    Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
    
@@ -91,7 +91,7 @@ static int exclusive;           /* Exclusive volume access */
 static struct db_param db_param = {
     NULL,                       /* Volume dirpath */
     1,                          /* bdb logfile autoremove */
-    16384,                      /* bdb cachesize */
+    64 * 1024,                  /* bdb cachesize (64 MB) */
     -1,                         /* not used ... */
     -1,
     "",
@@ -131,7 +131,7 @@ static void sig_handler(int signo)
     return;
 }
 
-void set_signal(void)
+static void set_signal(void)
 {
     struct sigaction sv;
 
@@ -165,7 +165,7 @@ void set_signal(void)
     }        
 }
 
-int get_lock(const char *dbpath)
+static int get_lock(const char *dbpath)
 {
     int lockfd;
     char lockpath[PATH_MAX];
@@ -215,7 +215,7 @@ int get_lock(const char *dbpath)
     return lockfd;
 }
 
-void free_lock(int lockfd)
+static void free_lock(int lockfd)
 {
     struct flock lock;
 
@@ -266,9 +266,8 @@ static void usage (void)
            "   -x rebuild indexes (just for completeness, mostly useless!)\n"
            "   -v verbose\n\n"
            "WARNING:\n"
-           "If you want/need to run an -r -f rebuild after adding 'cachecnid' to a volume configuration,\n"
-           "you must run a rebuild with -r alone at first in order to sync all existing CNIDs from the db\n"
-           "to the AppleDouble files!\n"
+           "For -r -f restore of the CNID database from the adouble files, the CNID must of course\n"
+           "be synched to them files first with a plain -r rebuild !\n"
         );
 }
 
@@ -285,6 +284,8 @@ int main(int argc, char **argv)
         usage();
         exit(EXIT_FAILURE);
     }
+    /* Inhereting perms in ad_mkdir etc requires this */
+    ad_setfuid(0);
 
     while ((c = getopt(argc, argv, ":dsnruvxife")) != -1) {
         switch(c) {
@@ -341,6 +342,8 @@ int main(int argc, char **argv)
     }
     volpath = argv[optind];
 
+    setvbuf(stdout, (char *) NULL, _IONBF, 0);
+
     /* Remember cwd */
     if ((cdir = open(".", O_RDONLY)) < 0) {
         dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
@@ -366,6 +369,12 @@ int main(int argc, char **argv)
         exit(EXIT_FAILURE);
     }
 
+    /* Sanity checks to ensure we can touch this volume */
+    if (volinfo.v_vfs_ea != AFPVOL_EA_AD && volinfo.v_vfs_ea != AFPVOL_EA_SYS) {
+        dbd_log( LOGSTD, "Unknown Extended Attributes option: %u", volinfo.v_vfs_ea);
+        exit(EXIT_FAILURE);        
+    }
+
     /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
     if ( (strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
         dbd_log( LOGSTD, "Volume pathname too long");
@@ -374,6 +383,22 @@ int main(int argc, char **argv)
     strncpy(dbpath, volinfo.v_dbpath, PATH_MAX - 9 - 1);
     strcat(dbpath, "/.AppleDB");
 
+    /* Check or create dbpath */
+    int dbdirfd = open(dbpath, O_RDONLY);
+    if (dbdirfd == -1 && errno == ENOENT) {
+        if (errno == ENOENT) {
+            if ((mkdir(dbpath, 0755)) != 0) {
+                dbd_log( LOGSTD, "Can't create .AppleDB for \"%s\": %s", dbpath, strerror(errno));
+                exit(EXIT_FAILURE);
+            }
+        } else {
+            dbd_log( LOGSTD, "Somethings wrong with .AppleDB for \"%s\", giving up: %s", dbpath, strerror(errno));
+            exit(EXIT_FAILURE);
+        }
+    } else {
+        close(dbdirfd);
+    }
+
     /* 
        Before we do anything else, check if there is an instance of cnid_dbd
        running already and silently exit if yes.
@@ -411,7 +436,7 @@ int main(int argc, char **argv)
         if (exclusive)
             dbd_log( LOGDEBUG, "Finished recovery.");
 
-        if (dbif_open(dbd, &db_param, rebuildindexes) < 0) {
+        if (dbif_open(dbd, NULL, rebuildindexes) < 0) {
             dbif_close(dbd);
             goto exit_failure;
         }
@@ -427,7 +452,7 @@ int main(int argc, char **argv)
         if (dbif_dump(dbd, dumpindexes) < 0) {
             dbd_log( LOGSTD, "Error dumping database");
         }
-    } else if ((rebuild && ! nocniddb) || (scan && nocniddb)) {
+    } else if ((rebuild && ! nocniddb) || scan) {
         if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
             dbd_log( LOGSTD, "Error repairing database.");
         }