]> arthur.barton.de Git - netatalk.git/blob - etc/cnid_dbd/cmd_dbd.c
cnid_metad, cnid_dbd, dbd: show version
[netatalk.git] / etc / cnid_dbd / cmd_dbd.c
1 /* 
2    Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
3    
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2 of the License, or
7    (at your option) any later version.
8  
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 */
14
15 /*
16   dbd specs and implementation progress
17   =====================================
18
19   St := Status
20
21   Force option
22   ------------
23   
24   St Spec
25   -- ----
26   OK If -f is requested, ensure -e is too.
27      Check if volumes is using AFPVOL_CACHE, then wipe db from disk. Rebuild from ad-files.
28
29   1st pass: Scan volume
30   --------------------
31
32   St Type Check
33   -- ---- -----
34   OK F/D  Make sure ad file exists
35   OK D    Make sure .AppleDouble dir exist, create if missing. Error creating
36           it is fatal as that shouldn't happen as root.
37   OK F/D  Delete orphaned ad-files, log dirs in ad-dir
38   OK F/D  Check name encoding by roundtripping, log on error
39   OK F/D  try: read CNID from ad file (if cnid caching is on)
40           try: fetch CNID from database
41           -> on mismatch: use CNID from file, update database (deleting both found CNIDs first)
42           -> if no CNID in ad file: write CNID from database to ad file
43           -> if no CNID in database: add CNID from ad file to database
44           -> on no CNID at all: create one and store in both places
45   OK F/D  Add found CNID, DID, filename, dev/inode, stamp to rebuild database
46   OK F/D  Check/update stamp (implicitly done while checking CNIDs)
47
48
49   2nd pass: Delete unused CNIDs
50   -----------------------------
51
52   St Spec
53   -- ----
54   OK Step through dbd (the one on disk) and rebuild-db from pass 1 and delete any CNID from
55      dbd not in rebuild db. This in only done in exclusive mode.
56 */
57
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif /* HAVE_CONFIG_H */
61
62 #include <unistd.h>
63 #include <sys/types.h>
64 #include <stdlib.h>
65 #include <stdio.h>
66 #include <stdarg.h>
67 #include <limits.h>
68 #include <signal.h>
69 #include <string.h>
70 #include <errno.h>
71
72 #include <atalk/logger.h>
73 #include <atalk/cnid_dbd_private.h>
74 #include <atalk/volinfo.h>
75 #include <atalk/util.h>
76
77 #include "cmd_dbd.h"
78 #include "dbd.h"
79 #include "dbif.h"
80 #include "db_param.h"
81
82 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
83
84 int nocniddb = 0;               /* Dont open CNID database, only scan filesystem */
85 struct volinfo volinfo; /* needed by pack.c:idxname() */
86 volatile sig_atomic_t alarmed;  /* flags for signals */
87 int db_locked;                  /* have we got the fcntl lock on lockfile ? */
88
89 static DBD *dbd;
90 static int verbose;             /* Logging flag */
91 static int exclusive;           /* Exclusive volume access */
92 static struct db_param db_param = {
93     NULL,                       /* Volume dirpath */
94     1,                          /* bdb logfile autoremove */
95     64 * 1024,                  /* bdb cachesize (64 MB) */
96     DEFAULT_MAXLOCKS,           /* maxlocks */
97     DEFAULT_MAXLOCKOBJS,        /* maxlockobjs */
98     0,                          /* flush_interval */
99     0,                          /* flush_frequency */
100     0,                          /* usock_file */
101     -1,                         /* fd_table_size */
102     -1,                         /* idle_timeout */
103     -1                          /* max_vols */
104 };
105 static char dbpath[MAXPATHLEN+1];   /* Path to the dbd database */
106
107 /* 
108    Provide some logging
109  */
110 void dbd_log(enum logtype lt, char *fmt, ...)
111 {
112     int len;
113     static char logbuffer[1024];
114     va_list args;
115
116     if ( (lt == LOGSTD) || (verbose == 1)) {
117         va_start(args, fmt);
118         len = vsnprintf(logbuffer, 1023, fmt, args);
119         va_end(args);
120         logbuffer[1023] = 0;
121
122         printf("%s\n", logbuffer);
123     }
124 }
125
126 /* 
127    SIGNAL handling:
128    catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
129  */
130
131 static void sig_handler(int signo)
132 {
133     alarmed = 1;
134     return;
135 }
136
137 static void set_signal(void)
138 {
139     struct sigaction sv;
140
141     sv.sa_handler = sig_handler;
142     sv.sa_flags = SA_RESTART;
143     sigemptyset(&sv.sa_mask);
144     if (sigaction(SIGTERM, &sv, NULL) < 0) {
145         dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
146         exit(EXIT_FAILURE);
147     }        
148     if (sigaction(SIGINT, &sv, NULL) < 0) {
149         dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
150         exit(EXIT_FAILURE);
151     }        
152
153     memset(&sv, 0, sizeof(struct sigaction));
154     sv.sa_handler = SIG_IGN;
155     sigemptyset(&sv.sa_mask);
156
157     if (sigaction(SIGABRT, &sv, NULL) < 0) {
158         dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
159         exit(EXIT_FAILURE);
160     }        
161     if (sigaction(SIGHUP, &sv, NULL) < 0) {
162         dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
163         exit(EXIT_FAILURE);
164     }        
165     if (sigaction(SIGQUIT, &sv, NULL) < 0) {
166         dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
167         exit(EXIT_FAILURE);
168     }        
169 }
170
171 static void usage (void)
172 {
173     printf("dbd (Netatalk %s)\n"
174            "Usage: dbd [-e|-t|-v|-x] -d [-i] | -s [-c|-n]| -r [-c|-f] | -u <path to netatalk volume>\n"
175            "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
176            "dbd must be run with appropiate permissions i.e. as root.\n\n"
177            "Main commands are:\n"
178            "   -d Dump CNID database\n"
179            "      Option: -i dump indexes too\n\n"
180            "   -s Scan volume:\n"
181            "      1. Compare CNIDs in database with volume\n"
182            "      2. Check if .AppleDouble dirs exist\n"
183            "      3. Check if  AppleDouble file exist\n"
184            "      4. Report orphaned AppleDouble files\n"
185            "      5. Check for directories inside AppleDouble directories\n"
186            "      6. Check name encoding by roundtripping, log on error\n"
187            "      7. Check for orphaned CNIDs in database (requires -e)\n"
188            "      8. Open and close adouble files\n"
189            "      Options: -c Don't check .AppleDouble stuff, only ckeck orphaned.\n"
190            "               -n Don't open CNID database, skip CNID checks.\n\n"
191            "   -r Rebuild volume:\n"
192            "      1. Sync CNIDSs in database with volume\n"
193            "      2. Make sure .AppleDouble dir exist, create if missing\n"
194            "      3. Make sure AppleDouble file exists, create if missing\n"
195            "      4. Delete orphaned AppleDouble files\n"
196            "      5. Check for directories inside AppleDouble directories\n"
197            "      6. Check name encoding by roundtripping, log on error\n"
198            "      7. Check for orphaned CNIDs in database (requires -e)\n"
199            "      8. Open and close adouble files\n"
200            "      Options: -c Don't create .AppleDouble stuff, only cleanup orphaned.\n"
201            "               -f wipe database and rebuild from IDs stored in AppleDouble\n"
202            "                  files, only available for volumes without 'nocnidcache'\n"
203            "                  option. Implies -e.\n\n"
204            "   -u Upgrade:\n"
205            "      Opens the database which triggers any necessary upgrades,\n"
206            "      then closes and exits.\n\n"
207            "General options:\n"
208            "   -e only work on inactive volumes and lock them (exclusive)\n"
209            "   -x rebuild indexes (just for completeness, mostly useless!)\n"
210            "   -t show statistics while running\n"
211            "   -v verbose\n\n"
212            "WARNING:\n"
213            "For -r -f restore of the CNID database from the adouble files,\n"
214            "the CNID must of course be synched to them files first with a plain -r rebuild!\n"
215            , VERSION
216         );
217 }
218
219 int main(int argc, char **argv)
220 {
221     int c, lockfd, ret = -1;
222     int dump=0, scan=0, rebuild=0, prep_upgrade=0, rebuildindexes=0, dumpindexes=0, force=0;
223     dbd_flags_t flags = 0;
224     char *volpath;
225     int cdir;
226
227     if (geteuid() != 0) {
228         usage();
229         exit(EXIT_FAILURE);
230     }
231     /* Inhereting perms in ad_mkdir etc requires this */
232     ad_setfuid(0);
233
234     while ((c = getopt(argc, argv, ":cdefinrstuvx")) != -1) {
235         switch(c) {
236         case 'c':
237             flags |= DBD_FLAGS_CLEANUP;
238             break;
239         case 'd':
240             dump = 1;
241             break;
242         case 'i':
243             dumpindexes = 1;
244             break;
245         case 's':
246             scan = 1;
247             flags |= DBD_FLAGS_SCAN;
248             break;
249         case 'n':
250             nocniddb = 1; /* FIXME: this could/should be a flag too for consistency */
251             break;
252         case 'r':
253             rebuild = 1;
254             break;
255         case 't':
256             flags |= DBD_FLAGS_STATS;
257             break;
258         case 'u':
259             prep_upgrade = 1;
260             break;
261         case 'v':
262             verbose = 1;
263             break;
264         case 'e':
265             exclusive = 1;
266             flags |= DBD_FLAGS_EXCL;
267             break;
268         case 'x':
269             rebuildindexes = 1;
270             break;
271         case 'f':
272             force = 1;
273             exclusive = 1;
274             flags |= DBD_FLAGS_FORCE | DBD_FLAGS_EXCL;
275             break;
276         case ':':
277         case '?':
278             usage();
279             exit(EXIT_FAILURE);
280             break;
281         }
282     }
283
284     if ((dump + scan + rebuild + prep_upgrade) != 1) {
285         usage();
286         exit(EXIT_FAILURE);
287     }
288
289     if ( (optind + 1) != argc ) {
290         usage();
291         exit(EXIT_FAILURE);
292     }
293     volpath = argv[optind];
294
295     setvbuf(stdout, (char *) NULL, _IONBF, 0);
296
297     /* Remember cwd */
298     if ((cdir = open(".", O_RDONLY)) < 0) {
299         dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
300         exit(EXIT_FAILURE);
301     }
302         
303     /* Setup signal handling */
304     set_signal();
305
306     /* Setup logging. Should be portable among *NIXes */
307     if (!verbose)
308         setuplog("default log_info /dev/tty");
309     else
310         setuplog("default log_debug /dev/tty");
311
312     /* Load .volinfo file */
313     if (loadvolinfo(volpath, &volinfo) == -1) {
314         dbd_log( LOGSTD, "Not a Netatalk volume at '%s', no .volinfo file at '%s/.AppleDesktop/.volinfo' or unknown volume options", volpath, volpath);
315         exit(EXIT_FAILURE);
316     }
317     if (vol_load_charsets(&volinfo) == -1) {
318         dbd_log( LOGSTD, "Error loading charsets!");
319         exit(EXIT_FAILURE);
320     }
321
322     /* Sanity checks to ensure we can touch this volume */
323     if (volinfo.v_vfs_ea != AFPVOL_EA_AD && volinfo.v_vfs_ea != AFPVOL_EA_SYS) {
324         dbd_log( LOGSTD, "Unknown Extended Attributes option: %u", volinfo.v_vfs_ea);
325         exit(EXIT_FAILURE);        
326     }
327
328     /* Enuser dbpath is there, create if necessary */
329     struct stat st;
330     if (stat(volinfo.v_dbpath, &st) != 0) {
331         if (errno != ENOENT) {
332             dbd_log( LOGSTD, "Can't stat dbpath \"%s\": %s", volinfo.v_dbpath, strerror(errno));
333             exit(EXIT_FAILURE);        
334         }
335         if ((mkdir(volinfo.v_dbpath, 0755)) != 0) {
336             dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
337             exit(EXIT_FAILURE);
338         }        
339     }
340
341     /* Put "/.AppleDB" at end of volpath, get path from volinfo file */
342     if ( (strlen(volinfo.v_dbpath) + strlen("/.AppleDB")) > MAXPATHLEN ) {
343         dbd_log( LOGSTD, "Volume pathname too long");
344         exit(EXIT_FAILURE);        
345     }
346     strncpy(dbpath, volinfo.v_dbpath, MAXPATHLEN - strlen("/.AppleDB"));
347     strcat(dbpath, "/.AppleDB");
348
349     /* Check or create dbpath */
350     int dbdirfd = open(dbpath, O_RDONLY);
351     if (dbdirfd == -1 && errno == ENOENT) {
352         if (errno == ENOENT) {
353             if ((mkdir(dbpath, 0755)) != 0) {
354                 dbd_log( LOGSTD, "Can't create .AppleDB for \"%s\": %s", dbpath, strerror(errno));
355                 exit(EXIT_FAILURE);
356             }
357         } else {
358             dbd_log( LOGSTD, "Somethings wrong with .AppleDB for \"%s\", giving up: %s", dbpath, strerror(errno));
359             exit(EXIT_FAILURE);
360         }
361     } else {
362         close(dbdirfd);
363     }
364
365     /* Get db lock */
366     if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
367         goto exit_failure;
368     if (db_locked != LOCK_EXCL) {
369         /* Couldn't get exclusive lock, try shared lock if -e wasn't requested */
370         if (exclusive) {
371             dbd_log(LOGSTD, "Database is in use and exlusive was requested");
372             goto exit_failure;
373         }
374         if ((db_locked = get_lock(LOCK_SHRD, NULL)) != LOCK_SHRD)
375             goto exit_failure;
376     }
377
378     /* Check if -f is requested and wipe db if yes */
379     if ((flags & DBD_FLAGS_FORCE) && rebuild && (volinfo.v_flags & AFPVOL_CACHE)) {
380         char cmd[8 + MAXPATHLEN];
381         if ((db_locked = get_lock(LOCK_FREE, NULL)) != 0)
382             goto exit_failure;
383
384         snprintf(cmd, 8 + MAXPATHLEN, "rm -rf \"%s\"", dbpath);
385         dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
386         system(cmd);
387         if ((mkdir(dbpath, 0755)) != 0) {
388             dbd_log( LOGSTD, "Can't create dbpath \"%s\": %s", dbpath, strerror(errno));
389             exit(EXIT_FAILURE);
390         }
391         dbd_log( LOGDEBUG, "Removed old database.");
392         if ((db_locked = get_lock(LOCK_EXCL, dbpath)) == -1)
393             goto exit_failure;
394     }
395
396     /* 
397        Lets start with the BerkeleyDB stuff
398     */
399     if ( ! nocniddb) {
400         if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
401             goto exit_failure;
402         
403         if (dbif_env_open(dbd,
404                           &db_param,
405                           (db_locked == LOCK_EXCL) ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
406             dbd_log( LOGSTD, "error opening database!");
407             goto exit_failure;
408         }
409
410         if (db_locked == LOCK_EXCL)
411             dbd_log( LOGDEBUG, "Finished recovery.");
412
413         if (dbif_open(dbd, NULL, rebuildindexes) < 0) {
414             dbif_close(dbd);
415             goto exit_failure;
416         }
417
418         /* Prepare upgrade ? We're done */
419         if (prep_upgrade) {
420             (void)dbif_txn_close(dbd, 1);
421             goto cleanup;
422         }
423     }
424
425     /* Downgrade db lock if not running exclusive */
426     if (!exclusive && (db_locked == LOCK_EXCL)) {
427         if (get_lock(LOCK_UNLOCK, NULL) != 0)
428             goto exit_failure;
429         if (get_lock(LOCK_SHRD, NULL) != LOCK_SHRD)
430             goto exit_failure;
431     }
432
433     /* Now execute given command scan|rebuild|dump */
434     if (dump && ! nocniddb) {
435         if (dbif_dump(dbd, dumpindexes) < 0) {
436             dbd_log( LOGSTD, "Error dumping database");
437         }
438     } else if ((rebuild && ! nocniddb) || scan) {
439         if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
440             dbd_log( LOGSTD, "Error repairing database.");
441         }
442     }
443
444 cleanup:
445     /* Cleanup */
446     dbd_log(LOGDEBUG, "Closing db");
447     if (! nocniddb) {
448         if (dbif_close(dbd) < 0) {
449             dbd_log( LOGSTD, "Error closing database");
450             goto exit_failure;
451         }
452     }
453
454 exit_success:
455     ret = 0;
456
457 exit_failure:
458     get_lock(0, NULL);
459     
460     if ((fchdir(cdir)) < 0)
461         dbd_log(LOGSTD, "fchdir: %s", strerror(errno));
462
463     if (ret == 0)
464         exit(EXIT_SUCCESS);
465     else
466         exit(EXIT_FAILURE);
467 }