2 Copyright (c) 2009 Frank Lahm <franklahm@gmail.com>
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.
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.
17 #endif /* HAVE_CONFIG_H */
20 #include <sys/types.h>
29 #include <atalk/logger.h>
30 #include <atalk/globals.h>
31 #include <atalk/netatalk_conf.h>
32 #include <atalk/util.h>
33 #include <atalk/errchk.h>
37 enum dbd_cmd {dbd_scan, dbd_rebuild};
39 /* Global variables */
40 volatile sig_atomic_t alarmed; /* flags for signals */
43 static dbd_flags_t flags;
45 /***************************************************************************
47 ***************************************************************************/
51 * catch SIGINT and SIGTERM which cause clean exit. Ignore anything else.
53 static void sig_handler(int signo)
59 static void set_signal(void)
63 sv.sa_handler = sig_handler;
64 sv.sa_flags = SA_RESTART;
65 sigemptyset(&sv.sa_mask);
66 if (sigaction(SIGTERM, &sv, NULL) < 0) {
67 dbd_log( LOGSTD, "error in sigaction(SIGTERM): %s", strerror(errno));
70 if (sigaction(SIGINT, &sv, NULL) < 0) {
71 dbd_log( LOGSTD, "error in sigaction(SIGINT): %s", strerror(errno));
75 memset(&sv, 0, sizeof(struct sigaction));
76 sv.sa_handler = SIG_IGN;
77 sigemptyset(&sv.sa_mask);
79 if (sigaction(SIGABRT, &sv, NULL) < 0) {
80 dbd_log( LOGSTD, "error in sigaction(SIGABRT): %s", strerror(errno));
83 if (sigaction(SIGHUP, &sv, NULL) < 0) {
84 dbd_log( LOGSTD, "error in sigaction(SIGHUP): %s", strerror(errno));
87 if (sigaction(SIGQUIT, &sv, NULL) < 0) {
88 dbd_log( LOGSTD, "error in sigaction(SIGQUIT): %s", strerror(errno));
93 static void usage (void)
95 printf("Usage: dbd [-cfFstvV] <path to netatalk volume>\n"
96 "dbd scans or reindex Netatalk CNID databases of AFP volumes.\n"
97 "dbd must be run with appropiate permissions i.e. as root.\n"
98 "By default dbd rebuilds the CNID database of the volume.\n\n"
101 " -c convert from adouble:v2 to adouble:ea (use with -r)\n"
102 " -F location of the afp.conf config file\n"
103 " -f delete and recreate CNID database\n"
104 " -t show statistics while running\n"
106 " -V show version info\n\n"
110 /***************************************************************************
112 ***************************************************************************/
114 void dbd_log(enum logtype lt, char *fmt, ...)
117 static char logbuffer[1024];
120 if ( (lt == LOGSTD) || (flags & DBD_FLAGS_VERBOSE)) {
122 len = vsnprintf(logbuffer, 1023, fmt, args);
126 printf("%s\n", logbuffer);
130 int main(int argc, char **argv)
133 int dbd_cmd = dbd_rebuild;
136 struct vol *vol = NULL;
137 const char *volpath = NULL;
140 while ((c = getopt(argc, argv, ":cfF:stvV")) != -1) {
143 flags |= DBD_FLAGS_V2TOEA;
146 flags |= DBD_FLAGS_FORCE;
149 obj.cmdlineconfigfile = strdup(optarg);
153 flags |= DBD_FLAGS_SCAN;
156 flags |= DBD_FLAGS_STATS;
159 flags |= DBD_FLAGS_VERBOSE;
162 printf("dbd %s\n", VERSION);
172 if ( (optind + 1) != argc ) {
176 volpath = argv[optind];
178 if (geteuid() != 0) {
182 /* Inhereting perms in ad_mkdir etc requires this */
185 setvbuf(stdout, (char *) NULL, _IONBF, 0);
188 if ((cdir = open(".", O_RDONLY)) < 0) {
189 dbd_log( LOGSTD, "Can't open dir: %s", strerror(errno));
193 /* Setup signal handling */
197 if (afp_config_parse(&obj, "dbd") != 0) {
198 dbd_log( LOGSTD, "Couldn't load afp.conf");
202 /* Initialize CNID subsystem */
205 /* Setup logging. Should be portable among *NIXes */
206 if (flags & DBD_FLAGS_VERBOSE)
207 setuplog("default:note, cnid:debug", "/dev/tty");
209 setuplog("default:note", "/dev/tty");
211 if (load_volumes(&obj) != 0) {
212 dbd_log( LOGSTD, "Couldn't load volumes");
216 if ((vol = getvolbypath(&obj, volpath)) == NULL) {
217 dbd_log( LOGSTD, "Couldn't find volume for '%s'", volpath);
221 if (load_charset(vol) != 0) {
222 dbd_log( LOGSTD, "Couldn't load charsets for '%s'", volpath);
227 if (STRCMP(vol->v_cnidscheme, != , "dbd")) {
228 dbd_log(LOGSTD, "\"%s\" isn't a \"dbd\" CNID volume", vol->v_path);
231 if ((vol->v_cdb = cnid_open(vol->v_path,
236 vol->v_cnidport)) == NULL) {
237 dbd_log(LOGSTD, "Cant initialize CNID database connection for %s", vol->v_path);
241 if (vol->v_adouble == AD_VERSION_EA)
242 dbd_log( LOGDEBUG, "adouble:ea volume");
243 else if (vol->v_adouble == AD_VERSION2)
244 dbd_log( LOGDEBUG, "adouble:v2 volume");
246 dbd_log( LOGSTD, "unknown adouble volume");
250 /* -C v2 to ea conversion only on adouble:ea volumes */
251 if ((flags & DBD_FLAGS_V2TOEA) && (vol->v_adouble!= AD_VERSION_EA)) {
252 dbd_log( LOGSTD, "Can't run adouble:v2 to adouble:ea conversion because not an adouble:ea volume");
256 /* Sanity checks to ensure we can touch this volume */
257 if (vol->v_vfs_ea != AFPVOL_EA_AD && vol->v_vfs_ea != AFPVOL_EA_SYS) {
258 dbd_log( LOGSTD, "Unknown Extended Attributes option: %u", vol->v_vfs_ea);
262 if (flags & DBD_FLAGS_FORCE) {
263 if (cnid_wipe(vol->v_cdb) != 0) {
264 dbd_log( LOGSTD, "Failed to wipe CNID db");
269 /* Now execute given command scan|rebuild|dump */
273 if (cmd_dbd_scanvol(vol, flags) < 0) {
274 dbd_log( LOGSTD, "Error repairing database.");
281 cnid_close(vol->v_cdb);
283 if (cdir != -1 && (fchdir(cdir) < 0))
284 dbd_log(LOGSTD, "fchdir: %s", strerror(errno));