Avoid gcc warning.
[adu.git] / adu.c
diff --git a/adu.c b/adu.c
index b8c7b68f905b2448aa069eb2d6820e029ca42baf..6d4780e3bff9937eea053413ca6db82f2b14bd76 100644 (file)
--- a/adu.c
+++ b/adu.c
@@ -12,8 +12,7 @@
  * - Modes of operation: \ref select.c, \ref create.c, \ref interactive.c
  * - User handling: \ref user.c
  * - Error handling: \ref error.h
- * - Library-type functions: \ref fd.c, \ref format.c, \ref string.c, \ref portable_io.h
- * - Generated files: \ref cmdline.h, \ref select.cmdline.h
+ * - Library-type functions: \ref fd.c, \ref format.c, \ref string.c, \ref portable_io.h, \ref bloom.c
  */
 
 #include "adu.h"
@@ -32,7 +31,7 @@
 DEFINE_ERRLIST;
 
 /**
- * The error code of the last osl library function.
+ * The error code of the last called osl library function.
  *
  * \sa osl().
  */
@@ -47,6 +46,8 @@ struct gengetopt_args_info conf;
 /** Options passed to --select-options. */
 struct select_args_info select_conf;
 
+/** Computed database dir */
+char *database_dir;
 
 /**
  * The table containing the directory names and statistics.
@@ -164,14 +165,24 @@ void check_signals(void)
        exit(EXIT_FAILURE);
 }
 
+static int catch_signal(int sig)
+{
+       struct sigaction act;
+
+       act.sa_handler = signal_handler;
+       sigemptyset(&act.sa_mask);
+       act.sa_flags = 0;
+       return sigaction(sig, &act, NULL);
+}
+
 static int init_signals(void)
 {
-       if (signal(SIGINT, &signal_handler) == SIG_ERR)
-               return -E_SIGNAL_SIG_ERR;
-       if (signal(SIGTERM, &signal_handler) == SIG_ERR)
-               return -E_SIGNAL_SIG_ERR;
-       if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
-               return -E_SIGNAL_SIG_ERR;
+       if (catch_signal(SIGINT) < 0)
+               return -E_SIGACTION;
+       if (catch_signal(SIGTERM) < 0)
+               return -E_SIGACTION;
+       if (catch_signal(SIGPIPE) < 0)
+               return -E_SIGACTION;
        return 1;
 }
 
@@ -184,21 +195,27 @@ static int init_signals(void)
  */
 int open_dir_table(int create)
 {
+       int ret;
 
        if (dir_table)
                return 1;
-       dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
 
+       dir_table_desc.dir = adu_strdup(database_dir);
        if (create) {
+               INFO_LOG("creating database directory structure\n");
+               ret = mkpath(dir_table_desc.dir, 0777);
+               if (ret < 0)
+                       goto out;
                NOTICE_LOG("creating dir table\n");
-               int ret = osl(osl_create_table(&dir_table_desc));
-               if (ret < 0) {
-                       free((char *)dir_table_desc.dir);
-                       return ret;
-               }
+               ret = osl(osl_create_table(&dir_table_desc));
+               if (ret < 0)
+                       goto out;
        }
        INFO_LOG("opening dir table\n");
        return osl(osl_open_table(&dir_table_desc, &dir_table));
+out:
+       free((char *)dir_table_desc.dir);
+       return ret;
 }
 
 static int check_args(void)
@@ -222,7 +239,7 @@ static int check_args(void)
        return 1;
 }
 
-static int print_complete_help_and_die(void)
+static void print_complete_help_and_die(void)
 {
        const char **line;
 
@@ -252,6 +269,27 @@ static int print_complete_help_and_die(void)
        exit(EXIT_FAILURE);
 }
 
+static void get_database_dir_or_die(void)
+{
+       char *tmp;
+
+       if (conf.database_dir_given)
+               tmp = adu_strdup(conf.database_dir_arg);
+       else
+               tmp = make_message("%s%s",
+                       conf.database_root_arg, conf.base_dir_arg);
+       /*
+        * As we change the cwd during database creation, database_dir
+        * must be an absolute path.
+        */
+       database_dir = absolute_path(tmp);
+       free(tmp);
+       if (database_dir)
+               return;
+       EMERG_LOG("failed to get absolute path of database dir\n");
+       exit(EXIT_FAILURE);
+}
+
 /**
  * The main function of adu.
  *
@@ -293,7 +331,7 @@ int main(int argc, char **argv)
        ret = init_signals();
        if (ret < 0)
                goto out;
-       ret = -E_SYNTAX;
+       get_database_dir_or_die();
        if (conf.select_given)
                ret = com_select();
        else if (conf.create_given)
@@ -308,6 +346,7 @@ out:
                ERROR_LOG("%s\n", adu_strerror(-ret));
                return -EXIT_FAILURE;
        }
+       free(database_dir);
        cmdline_parser_free(&conf);
        select_cmdline_parser_free(&select_conf);
        return EXIT_SUCCESS;