2 * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file adu.c \brief The main functions used by all modes of operation. */
10 * \mainpage adu API reference
12 * - Modes of operation: \ref select.c, \ref create.c, \ref interactive.c
13 * - User handling: \ref user.c
14 * - Error handling: \ref error.h
15 * - Library-type functions: \ref fd.c, \ref format.c, \ref string.c, \ref portable_io.h, \ref bloom.c
19 #include <dirent.h> /* readdir() */
23 #include "select.cmdline.h"
30 /** Define the array of error descriptions. */
34 * The error code of the last called osl library function.
40 /** In case a signal is received, its number is stored here. */
43 /** Command line and config file options. */
44 struct gengetopt_args_info conf;
46 /** Options passed to --select-options. */
47 struct select_args_info select_conf;
49 /** Computed database dir */
53 * The table containing the directory names and statistics.
55 struct osl_table *dir_table = NULL;
57 static struct osl_column_description dir_table_cols[] = {
59 .storage_type = OSL_MAPPED_STORAGE,
64 .storage_type = OSL_MAPPED_STORAGE,
65 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
67 .compare_function = uint64_compare,
68 .data_size = sizeof(uint64_t)
71 .storage_type = OSL_MAPPED_STORAGE,
72 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
74 .compare_function = size_compare,
75 .data_size = sizeof(uint64_t)
78 .storage_type = OSL_MAPPED_STORAGE,
79 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
80 .compare_function = size_compare,
82 .data_size = sizeof(uint64_t)
85 .storage_type = OSL_MAPPED_STORAGE,
86 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
87 .compare_function = size_compare,
89 .data_size = sizeof(uint64_t)
93 static struct osl_table_description dir_table_desc = {
95 .num_columns = NUM_DT_COLUMNS,
97 .column_descriptions = dir_table_cols,
103 * \param ll Loglevel.
104 * \param fmt Usual format string.
106 * All XXX_LOG() macros use this function.
108 __printf_2_3 void __log(int ll, const char* fmt,...)
116 if (ll < conf.loglevel_arg)
121 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
122 fprintf(outfd, "%s ", str);
124 vfprintf(outfd, fmt, argp);
129 * adu's version of strerror(3).
131 * \param num The error number.
133 * \return The error text of \a num.
135 const char *adu_strerror(int num)
139 assert(osl_errno > 0);
140 return osl_strerror((osl_errno));
142 if (IS_SYSTEM_ERROR(num))
143 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
144 return adu_errlist[num];
147 static void close_dir_table(void)
153 NOTICE_LOG("closing dir table\n");
154 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
156 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
157 free((char *)dir_table_desc.dir);
161 static void close_all_tables(void)
167 static void signal_handler(int s)
173 * Check whether to terminate adu.
175 * Check whether a signal was caught that should terminate the
176 * adu process. If yes, close all osl tables and exit gracefully.
178 void check_signals(void)
182 EMERG_LOG("caught signal %d\n", signum);
187 static int catch_signal(int sig)
189 struct sigaction act;
191 act.sa_handler = signal_handler;
192 sigemptyset(&act.sa_mask);
194 return sigaction(sig, &act, NULL);
197 static int init_signals(void)
199 if (catch_signal(SIGINT) < 0)
201 if (catch_signal(SIGTERM) < 0)
203 if (catch_signal(SIGPIPE) < 0)
209 * Open the directory table.
211 * \param create If non-zero, create the table first.
215 int open_dir_table(int create)
222 dir_table_desc.dir = adu_strdup(database_dir);
224 INFO_LOG("creating database directory structure\n");
225 ret = mkpath(dir_table_desc.dir, 0777);
228 NOTICE_LOG("creating dir table\n");
229 ret = osl(osl_create_table(&dir_table_desc));
231 ERROR_LOG("could not create %s\n", dir_table_desc.dir);
235 INFO_LOG("opening dir table\n");
236 return osl(osl_open_table(&dir_table_desc, &dir_table));
238 free((char *)dir_table_desc.dir);
242 static int check_args(void)
244 if (conf.create_given && !conf.base_dir_given)
247 /* remove trailing slashes from base-dir arg */
248 if (conf.base_dir_given) {
249 size_t len = strlen(conf.base_dir_arg);
251 if (!len) /* empty string */
252 return -ERRNO_TO_ERROR(EINVAL);
253 if (!--len) /* length 1 is always OK */
255 if (conf.base_dir_arg[len] != '/')
256 break; /* no trailing slash, also OK */
257 conf.base_dir_arg[len] = '\0';
263 static void print_complete_help_and_die(void)
267 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
268 printf("%s\n\n", gengetopt_args_info_purpose);
269 printf("%s\n\n", gengetopt_args_info_usage);
272 line = gengetopt_args_info_help;
274 line = gengetopt_args_info_detailed_help;
275 for (; *line; line++)
276 printf("%s\n", *line);
279 line = select_args_info_help;
281 line = select_args_info_detailed_help;
282 printf("Select options:\n");
283 for (; *line; line++)
284 printf("%s\n", *line);
286 printf("Interactive commands:\n");
287 print_interactive_help();
288 cmdline_parser_free(&conf);
289 select_cmdline_parser_free(&select_conf);
293 static void get_database_dir_or_die(void)
297 if (conf.database_dir_given)
298 tmp = adu_strdup(conf.database_dir_arg);
300 tmp = make_message("%s%s",
301 conf.database_root_arg, conf.base_dir_arg);
303 * As we change the cwd during database creation, database_dir
304 * must be an absolute path.
306 database_dir = absolute_path(tmp);
310 EMERG_LOG("failed to get absolute path of database dir\n");
315 * The main function of adu.
317 * \param argc Usual argument count.
318 * \param argv Usual argument vector.
320 * Check command line options, init the signal handlers and
321 * call the main function of the selected mode.
323 * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
325 int main(int argc, char **argv)
328 struct cmdline_parser_params params = {
332 .check_ambiguity = 0,
335 select_cmdline_parser_init(&select_conf);
336 cmdline_parser_init(&conf);
337 /* ignore errors and print complete help if --help was given */
338 cmdline_parser_ext(argc, argv, &conf, ¶ms);
339 if (conf.help_given || conf.detailed_help_given)
340 print_complete_help_and_die();
341 cmdline_parser_free(&conf);
342 params.check_required = 1;
343 params.check_ambiguity = 1;
344 params.print_errors = 1;
345 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
352 ret = init_signals();
355 get_database_dir_or_die();
356 if (conf.select_given)
358 else if (conf.create_given)
361 ret = com_interactive();
367 ERROR_LOG("%s\n", adu_strerror(-ret));
368 return -EXIT_FAILURE;
371 cmdline_parser_free(&conf);
372 select_cmdline_parser_free(&select_conf);