2 $Id: cmd_dbd.c,v 1.4 2009-05-25 13:52:14 franklahm Exp $
4 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
18 dbd specs and implementation progress
19 =====================================
28 OK If -f is requested, ensure -e is too.
29 Check if volumes is using AFPVOL_CACHE, then wipe db from disk. Rebuild from ad-files.
36 OK F/D Make sure ad file exists
37 OK D Make sure .AppleDouble dir exist, create if missing. Error creating
38 it is fatal as that shouldn't happen as root.
39 OK F/D Delete orphaned ad-files, log dirs in ad-dir
40 OK F/D Check name encoding by roundtripping, log on error
41 OK F/D try: read CNID from ad file (if cnid caching is on)
42 try: fetch CNID from database
43 -> on mismatch: use CNID from file, update database (deleting both found CNIDs first)
44 -> if no CNID in ad file: write CNID from database to ad file
45 -> if no CNID in database: add CNID from ad file to database
46 -> on no CNID at all: create one and store in both places
47 OK F/D Add found CNID, DID, filename, dev/inode, stamp to rebuild database
48 OK F/D Check/update stamp (implicitly done while checking CNIDs)
51 2nd pass: Delete unused CNIDs
52 -----------------------------
56 OK Step through dbd (the one on disk) and rebuild-db from pass 1 and delete any CNID from
57 dbd not in rebuild db. This in only done in exclusive mode.
62 #endif /* HAVE_CONFIG_H */
65 #include <sys/types.h>
74 #include <atalk/logger.h>
75 #include <atalk/cnid_dbd_private.h>
76 #include <atalk/volinfo.h>
82 #define LOCKFILENAME "lock"
83 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
87 volatile sig_atomic_t alarmed;
88 static int verbose; /* Logging flag */
89 static int exclusive; /* Exclusive volume access */
90 static struct db_param db_param = {
91 NULL, /* Volume dirpath */
92 1, /* bdb logfile autoremove */
93 16384, /* bdb cachesize */
94 -1, /* not used ... */
105 void dbd_log(enum logtype lt, char *fmt, ...)
108 static char logbuffer[1024];
111 if ( (lt == LOGSTD) || (verbose == 1)) {
113 len = vsnprintf(logbuffer, 1023, fmt, args);
117 printf("%s\n", logbuffer);
123 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
126 static void sig_handler(int signo)
132 void set_signal(void)
136 sv.sa_handler = sig_handler;
137 sv.sa_flags = SA_RESTART;
138 sigemptyset(&sv.sa_mask);
139 if (sigaction(SIGTERM, &sv, NULL) < 0) {
140 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
143 if (sigaction(SIGINT, &sv, NULL) < 0) {
144 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
148 memset(&sv, 0, sizeof(struct sigaction));
149 sv.sa_handler = SIG_IGN;
150 sigemptyset(&sv.sa_mask);
152 if (sigaction(SIGABRT, &sv, NULL) < 0) {
153 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
156 if (sigaction(SIGHUP, &sv, NULL) < 0) {
157 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
160 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
161 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
166 int get_lock(const char *dbpath)
169 char lockpath[PATH_MAX];
173 if ( (strlen(dbpath) + strlen(LOCKFILENAME+1)) > (PATH_MAX - 1) ) {
174 dbd_log( LOGSTD, ".AppleDB pathname too long");
177 strncpy(lockpath, dbpath, PATH_MAX - 1);
178 strcat(lockpath, "/");
179 strcat(lockpath, LOCKFILENAME);
181 if ((lockfd = open(lockpath, O_RDWR | O_CREAT, 0644)) < 0) {
182 dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
186 if ((stat(dbpath, &st)) != 0) {
187 dbd_log( LOGSTD, "Error statting lockfile: %s", strerror(errno));
191 if ((chown(lockpath, st.st_uid, st.st_gid)) != 0) {
192 dbd_log( LOGSTD, "Error inheriting lockfile permissions: %s", strerror(errno));
197 lock.l_whence = SEEK_SET;
199 lock.l_type = F_WRLCK;
201 if (fcntl(lockfd, F_SETLK, &lock) < 0) {
202 if (errno == EACCES || errno == EAGAIN) {
204 dbd_log( LOGSTD, "Database is in use and exlusive was requested", strerror(errno));
208 dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
216 void free_lock(int lockfd)
221 lock.l_whence = SEEK_SET;
223 lock.l_type = F_UNLCK;
224 fcntl(lockfd, F_SETLK, &lock);
230 printf("Usage: dbd [-e|-v|-x] -d [-i] | -s | -r [-f] <path to netatalk volume>\n"
231 "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
232 "dbd must be run with appropiate permissions i.e. as root.\n\n"
233 "Main commands are:\n"
234 " -d Dump CNID database\n"
235 " Option: -i dump indexes too\n"
237 " 1. Compare CNIDs in database with volume\n"
238 " 2. Check if .AppleDouble dirs exist\n"
239 " 3. Check if AppleDouble file exist\n"
240 " 4. Report orphaned AppleDouble files\n"
241 " 5. Check for directories inside AppleDouble directories\n"
242 " 6. Check name encoding by roundtripping, log on error\n"
243 " 7. Check for orphaned CNIDs in database (requires -e)\n"
244 " -r Rebuild volume:\n"
245 " 1. Sync CNIDSs in database with volume\n"
246 " 2. Make sure .AppleDouble dir exist, create if missing\n"
247 " 3. Make sure AppleDouble file exists, create if missing\n"
248 " 4. Delete orphaned AppleDouble files\n"
249 " 5. Check for directories inside AppleDouble directories\n"
250 " 6. Check name encoding by roundtripping, log on error\n"
251 " 7. Check for orphaned CNIDs in database (requires -e)\n"
252 " Option: -f wipe database and rebuild from IDs stored in AppleDouble files,\n"
253 " only available for volumes with 'cachecnid' option. Implies -e.\n"
255 " -e only work on inactive volumes and lock them (exclusive)\n"
256 " -x rebuild indexes (just for completeness, mostly useless!)\n"
259 "If you want/need to run an -r -f rebuild after adding 'cachecnid' to a volume configuration,\n"
260 "you must run a rebuild with -r alone at first in order to sync all existing CNIDs from the db\n"
261 "to the AppleDouble files!\n"
265 int main(int argc, char **argv)
268 int dump=0, scan=0, rebuild=0, rebuildindexes=0, dumpindexes=0, force=0;
269 dbd_flags_t flags = 0;
271 struct volinfo volinfo;
273 if (geteuid() != 0) {
278 while ((c = getopt(argc, argv, ":dsrvxife")) != -1) {
288 flags |= DBD_FLAGS_SCAN;
298 flags |= DBD_FLAGS_EXCL;
306 flags |= DBD_FLAGS_FORCE | DBD_FLAGS_EXCL;
316 if ((dump + scan + rebuild) != 1) {
321 if ( (optind + 1) != argc ) {
325 volpath = argv[optind];
327 /* Put "/.AppleDB" at end of volpath */
328 if ( (strlen(volpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
329 dbd_log( LOGSTD, "Volume pathname too long");
332 char dbpath[PATH_MAX];
333 strncpy(dbpath, volpath, PATH_MAX - 1);
334 strcat(dbpath, "/.AppleDB");
338 if ((cdir = open(".", O_RDONLY)) < 0) {
339 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
344 Before we do anything else, check if there is an instance of cnid_dbd
345 running already and silently exit if yes.
347 lockfd = get_lock(dbpath);
349 /* Setup signal handling */
352 /* Setup logging. Should be portable among *NIXes */
354 setuplog("default log_info /dev/tty");
356 setuplog("default log_debug /dev/tty");
358 /* Load .volinfo file */
359 if (loadvolinfo(volpath, &volinfo) == -1) {
360 dbd_log( LOGSTD, "Unkown volume options!");
363 if (vol_load_charsets(&volinfo) == -1) {
364 dbd_log( LOGSTD, "Error loading charsets!");
368 /* Check if -f is requested and wipe db if yes */
369 if ((flags & DBD_FLAGS_FORCE) && (volinfo.v_flags & AFPVOL_CACHE)) {
370 char cmd[8 + MAXPATHLEN];
371 snprintf(cmd, 8 + MAXPATHLEN, "rm -f %s/*", dbpath);
372 dbd_log( LOGDEBUG, "Removing old database of volume: '%s'", volpath);
374 dbd_log( LOGDEBUG, "Removed old database.");
378 Lets start with the BerkeleyDB stuff
380 if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
383 if (dbif_env_open(dbd, &db_param, exclusive ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
384 dbd_log( LOGSTD, "error opening database!");
389 dbd_log( LOGDEBUG, "Finished recovery.");
391 if (dbif_open(dbd, &db_param, rebuildindexes) < 0) {
396 if (dbd_stamp(dbd) < 0) {
402 if (dbif_dump(dbd, dumpindexes) < 0) {
403 dbd_log( LOGSTD, "Error dumping database");
405 } else if (rebuild || scan) {
406 if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
407 dbd_log( LOGSTD, "Error repairing database.");
411 if (dbif_close(dbd) < 0) {
412 dbd_log( LOGSTD, "Error closing database");
418 if ((fchdir(cdir)) < 0)
419 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));