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;
51 * The table containing the directory names and statistics.
53 struct osl_table *dir_table = NULL;
55 static struct osl_column_description dir_table_cols[] = {
57 .storage_type = OSL_MAPPED_STORAGE,
62 .storage_type = OSL_MAPPED_STORAGE,
63 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
65 .compare_function = uint64_compare,
66 .data_size = sizeof(uint64_t)
69 .storage_type = OSL_MAPPED_STORAGE,
70 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
72 .compare_function = size_compare,
73 .data_size = sizeof(uint64_t)
76 .storage_type = OSL_MAPPED_STORAGE,
77 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
78 .compare_function = size_compare,
80 .data_size = sizeof(uint64_t)
83 .storage_type = OSL_MAPPED_STORAGE,
84 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
85 .compare_function = size_compare,
87 .data_size = sizeof(uint64_t)
91 static struct osl_table_description dir_table_desc = {
93 .num_columns = NUM_DT_COLUMNS,
95 .column_descriptions = dir_table_cols,
101 * \param ll Loglevel.
102 * \param fmt Usual format string.
104 * All XXX_LOG() macros use this function.
106 __printf_2_3 void __log(int ll, const char* fmt,...)
114 if (ll < conf.loglevel_arg)
119 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
120 fprintf(outfd, "%s ", str);
122 vfprintf(outfd, fmt, argp);
126 static void close_dir_table(void)
132 NOTICE_LOG("closing dir table\n");
133 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
135 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
136 free((char *)dir_table_desc.dir);
140 static void close_all_tables(void)
146 static void signal_handler(int s)
152 * Check whether to terminate adu.
154 * Check whether a signal was caught that should terminate the
155 * adu process. If yes, close all osl tables and exit gracefully.
157 void check_signals(void)
161 EMERG_LOG("caught signal %d\n", signum);
166 static int init_signals(void)
168 if (signal(SIGINT, &signal_handler) == SIG_ERR)
169 return -E_SIGNAL_SIG_ERR;
170 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
171 return -E_SIGNAL_SIG_ERR;
172 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
173 return -E_SIGNAL_SIG_ERR;
178 * Open the directory table.
180 * \param create If non-zero, create the table first.
184 int open_dir_table(int create)
189 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
192 NOTICE_LOG("creating dir table\n");
193 int ret = osl(osl_create_table(&dir_table_desc));
195 free((char *)dir_table_desc.dir);
199 INFO_LOG("opening dir table\n");
200 return osl(osl_open_table(&dir_table_desc, &dir_table));
203 static int check_args(void)
205 if (conf.create_given && !conf.base_dir_given)
208 /* remove trailing slashes from base-dir arg */
209 if (conf.base_dir_given) {
210 size_t len = strlen(conf.base_dir_arg);
212 if (!len) /* empty string */
213 return -ERRNO_TO_ERROR(EINVAL);
214 if (!--len) /* length 1 is always OK */
216 if (conf.base_dir_arg[len] != '/')
217 break; /* no trailing slash, also OK */
218 conf.base_dir_arg[len] = '\0';
224 static void print_complete_help_and_die(void)
228 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
229 printf("%s\n\n", gengetopt_args_info_purpose);
230 printf("%s\n\n", gengetopt_args_info_usage);
233 line = gengetopt_args_info_help;
235 line = gengetopt_args_info_detailed_help;
236 for (; *line; line++)
237 printf("%s\n", *line);
240 line = select_args_info_help;
242 line = select_args_info_detailed_help;
243 printf("Select options:\n");
244 for (; *line; line++)
245 printf("%s\n", *line);
247 printf("Interactive commands:\n");
248 print_interactive_help();
249 cmdline_parser_free(&conf);
250 select_cmdline_parser_free(&select_conf);
255 * The main function of adu.
257 * \param argc Usual argument count.
258 * \param argv Usual argument vector.
260 * Check command line options, init the signal handlers and
261 * call the main function of the selected mode.
263 * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
265 int main(int argc, char **argv)
268 struct cmdline_parser_params params = {
272 .check_ambiguity = 0,
275 select_cmdline_parser_init(&select_conf);
276 cmdline_parser_init(&conf);
277 /* ignore errors and print complete help if --help was given */
278 cmdline_parser_ext(argc, argv, &conf, ¶ms);
279 if (conf.help_given || conf.detailed_help_given)
280 print_complete_help_and_die();
281 cmdline_parser_free(&conf);
282 params.check_required = 1;
283 params.check_ambiguity = 1;
284 params.print_errors = 1;
285 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
292 ret = init_signals();
296 if (conf.select_given)
298 else if (conf.create_given)
301 ret = com_interactive();
307 ERROR_LOG("%s\n", adu_strerror(-ret));
308 return -EXIT_FAILURE;
310 cmdline_parser_free(&conf);
311 select_cmdline_parser_free(&select_conf);