2 $Id: cmd_dbd.c,v 1.1 2009-05-14 13:46:08 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.
19 #endif /* HAVE_CONFIG_H */
22 #include <sys/types.h>
31 #include <atalk/logger.h>
32 #include <atalk/cnid_dbd_private.h>
33 #include <atalk/volinfo.h>
38 #define LOCKFILENAME "lock"
39 #define DBOPTIONS (DB_CREATE | DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN)
43 volatile sig_atomic_t alarmed;
44 static int verbose; /* Logging flag */
45 static int exclusive; /* Exclusive volume access */
46 static struct db_param db_param = {
47 NULL, /* Volume dirpath */
48 1, /* bdb logfile autoremove */
49 16384, /* bdb cachesize */
50 -1, /* not used ... */
61 void dbd_log(enum logtype lt, char *fmt, ...)
64 static char logbuffer[1024];
67 if ( (lt == LOGSTD) || (verbose == 1)) {
69 len = vsnprintf(logbuffer, 1023, fmt, args);
73 printf("%s\n", logbuffer);
79 catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
82 static void sig_handler(int signo)
92 sv.sa_handler = sig_handler;
93 sv.sa_flags = SA_RESTART;
94 sigemptyset(&sv.sa_mask);
95 if (sigaction(SIGTERM, &sv, NULL) < 0) {
96 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
99 if (sigaction(SIGINT, &sv, NULL) < 0) {
100 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
104 memset(&sv, 0, sizeof(struct sigaction));
105 sv.sa_handler = SIG_IGN;
106 sigemptyset(&sv.sa_mask);
108 if (sigaction(SIGABRT, &sv, NULL) < 0) {
109 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
112 if (sigaction(SIGHUP, &sv, NULL) < 0) {
113 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
116 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
117 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
127 if ((lockfd = open(LOCKFILENAME, O_RDWR | O_CREAT, 0644)) < 0) {
128 dbd_log( LOGSTD, "Error opening lockfile: %s", strerror(errno));
133 lock.l_whence = SEEK_SET;
135 lock.l_type = F_WRLCK;
137 if (fcntl(lockfd, F_SETLK, &lock) < 0) {
138 if (errno == EACCES || errno == EAGAIN) {
140 dbd_log( LOGSTD, "Database is in use and exlusive was requested", strerror(errno));
144 dbd_log( LOGSTD, "Error getting fcntl F_WRLCK on lockfile: %s", strerror(errno));
152 void free_lock(int lockfd)
157 lock.l_whence = SEEK_SET;
159 lock.l_type = F_UNLCK;
160 fcntl(lockfd, F_SETLK, &lock);
166 printf("Usage: dbd [-e|-v|-x] -d [-i] | -s | -r [-f] <path to netatalk volume>\n"
167 "dbd can dump, scan, reindex and rebuild Netatalk dbd CNID databases.\n"
168 "dbd must be run with appropiate permissions i.e. as root.\n\n"
169 "Main commands are:\n"
170 " -d Dump CNID database\n"
171 " Option: -i dump indexes too\n"
173 " 1. Compare database with volume\n"
174 " 2. Check if .AppleDouble dirs exist\n"
175 " 3. Check if AppleDouble file exist\n"
176 " 4. Report orphaned AppleDouble files\n"
177 " 5. Check for directories inside AppleDouble directories\n"
178 " 6. Check name encoding by roundtripping, log on error\n"
179 " -r Rebuild volume:\n"
180 " 1. Sync database with volume\n"
181 " 2. Make sure .AppleDouble dir exist, create if missing\n"
182 " 3. Make sure AppleDouble file exists, create if missing\n"
183 " 4. Delete orphaned AppleDouble files\n"
184 " 5. Check for directories inside AppleDouble directories\n"
185 " 6. Check name encoding by roundtripping, log on error\n"
186 " Option: -f wipe database and rebuild from IDs stored in AppleDouble files,\n"
187 " only available for volumes with 'cachecnid' option\n\n"
189 " -e only work on inactive volumes and lock them (exclusive)\n"
190 " -x rebuild indexes\n"
193 "05-05-2009: -s and -r already check/repair the AppleDouble stuff and encoding,\n"
194 " no CNID database maintanance is done yet\n"
198 int main(int argc, char **argv)
201 int dump=0, scan=0, rebuild=0, rebuildindexes=0, dumpindexes=0, force=0;
202 dbd_flags_t flags = 0;
204 struct volinfo volinfo;
206 if (geteuid() != 0) {
211 while ((c = getopt(argc, argv, ":dsrvxife")) != -1) {
221 flags = DBD_FLAGS_SCAN;
237 flags = DBD_FLAGS_FORCE;
247 if ((dump + scan + rebuild) != 1) {
252 if ( (optind + 1) != argc ) {
256 volpath = argv[optind];
258 /* Put "/.AppleDB" at end of volpath */
259 if ( (strlen(volpath) + strlen("/.AppleDB")) > (PATH_MAX - 1) ) {
260 dbd_log( LOGSTD, "Volume pathname too long");
263 char dbpath[PATH_MAX];
264 strncpy(dbpath, volpath, PATH_MAX - 1);
265 strcat(dbpath, "/.AppleDB");
269 if ((cdir = open(".", O_RDONLY)) < 0) {
270 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
275 Before we do anything else, check if there is an instance of cnid_dbd
276 running already and silently exit if yes.
280 /* Setup signal handling */
283 /* Setup logging. Should be portable among *NIXes */
285 setuplog("default log_info /dev/tty");
287 setuplog("default log_debug /dev/tty");
289 /* Load .volinfo file */
290 if (loadvolinfo(volpath, &volinfo) == -1) {
291 dbd_log( LOGSTD, "Unkown volume options!");
294 if (vol_load_charsets(&volinfo) == -1) {
295 dbd_log( LOGSTD, "Error loading charsets!");
300 Lets start with the BerkeleyDB stuff
302 if ((dbd = dbif_init(dbpath, "cnid2.db")) == NULL)
305 if (dbif_env_open(dbd, &db_param, exclusive ? (DBOPTIONS | DB_RECOVER) : DBOPTIONS) < 0) {
306 dbd_log( LOGSTD, "error opening database!");
311 dbd_log( LOGDEBUG, "Finished recovery.");
313 if (dbif_open(dbd, &db_param, rebuildindexes) < 0) {
319 if (dbif_dump(dbd, dumpindexes) < 0) {
320 dbd_log( LOGSTD, "Error dumping database");
322 } else if (rebuild || scan) {
323 if (cmd_dbd_scanvol(dbd, &volinfo, flags) < 0) {
324 dbd_log( LOGSTD, "Error repairing database.");
329 if (dbif_close(dbd) < 0) {
330 dbd_log( LOGSTD, "Error closing database");
336 if ((fchdir(cdir)) < 0)
337 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));