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. */
9 #include <dirent.h> /* readdir() */
13 #include "select.cmdline.h"
23 * The error code of the last osl library function.
29 /** In case a signal is received, its number is stored here. */
32 /** Command line and config file options. */
33 struct gengetopt_args_info conf;
35 /** Options passed to --select-options. */
36 struct select_args_info select_conf;
40 * The table containing the directory names and statistics.
42 struct osl_table *dir_table = NULL;
44 static struct osl_column_description dir_table_cols[] = {
46 .storage_type = OSL_MAPPED_STORAGE,
51 .storage_type = OSL_MAPPED_STORAGE,
52 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
54 .compare_function = uint64_compare,
55 .data_size = sizeof(uint64_t)
58 .storage_type = OSL_MAPPED_STORAGE,
59 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
61 .compare_function = size_compare,
62 .data_size = sizeof(uint64_t)
65 .storage_type = OSL_MAPPED_STORAGE,
66 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
67 .compare_function = size_compare,
69 .data_size = sizeof(uint64_t)
72 .storage_type = OSL_MAPPED_STORAGE,
73 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
74 .compare_function = size_compare,
76 .data_size = sizeof(uint64_t)
80 static struct osl_table_description dir_table_desc = {
82 .num_columns = NUM_DT_COLUMNS,
84 .column_descriptions = dir_table_cols,
91 * \param fmt Usual format string.
93 * All XXX_LOG() macros use this function.
95 __printf_2_3 void __log(int ll, const char* fmt,...)
103 if (ll < conf.loglevel_arg)
108 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
109 fprintf(outfd, "%s ", str);
111 vfprintf(outfd, fmt, argp);
115 static void close_dir_table(void)
121 NOTICE_LOG("closing dir table\n");
122 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
124 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
125 free((char *)dir_table_desc.dir);
129 static void close_all_tables(void)
135 static void signal_handler(int s)
141 * Check whether to terminate adu.
143 * Check whether a signal was caught that should terminate the
144 * adu process. If yes, close all osl tables and exit gracefully.
146 void check_signals(void)
150 EMERG_LOG("caught signal %d\n", signum);
155 static int init_signals(void)
157 if (signal(SIGINT, &signal_handler) == SIG_ERR)
158 return -E_SIGNAL_SIG_ERR;
159 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
160 return -E_SIGNAL_SIG_ERR;
161 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
162 return -E_SIGNAL_SIG_ERR;
167 * Open the directory table.
169 * \param create If non-zero, create the table first.
173 int open_dir_table(int create)
178 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
181 NOTICE_LOG("creating dir table\n");
182 int ret = osl(osl_create_table(&dir_table_desc));
184 free((char *)dir_table_desc.dir);
188 INFO_LOG("opening dir table\n");
189 return osl(osl_open_table(&dir_table_desc, &dir_table));
192 static int check_args(void)
194 if (conf.create_given && !conf.base_dir_given)
197 /* remove trailing slashes from base-dir arg */
198 if (conf.base_dir_given) {
199 size_t len = strlen(conf.base_dir_arg);
201 if (!len) /* empty string */
202 return -ERRNO_TO_ERROR(EINVAL);
203 if (!--len) /* length 1 is always OK */
205 if (conf.base_dir_arg[len] != '/')
206 break; /* no trailing slash, also OK */
207 conf.base_dir_arg[len] = '\0';
213 static int print_complete_help_and_die(void)
217 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
218 printf("%s\n\n", gengetopt_args_info_purpose);
219 printf("%s\n\n", gengetopt_args_info_usage);
222 line = gengetopt_args_info_help;
224 line = gengetopt_args_info_detailed_help;
225 for (; *line; line++)
226 printf("%s\n", *line);
229 line = select_args_info_help;
231 line = select_args_info_detailed_help;
232 printf("Select options:\n");
233 for (; *line; line++)
234 printf("%s\n", *line);
236 printf("Interactive commands:\n");
237 print_interactive_help();
238 cmdline_parser_free(&conf);
239 select_cmdline_parser_free(&select_conf);
244 * The main function of adu.
246 * \param argc Usual argument count.
247 * \param argv Usual argument vector.
249 * Check command line options, init the signal handlers and
250 * call the main function of the selected mode.
252 * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
254 int main(int argc, char **argv)
257 struct cmdline_parser_params params = {
261 .check_ambiguity = 0,
264 select_cmdline_parser_init(&select_conf);
265 cmdline_parser_init(&conf);
266 /* ignore errors and print complete help if --help was given */
267 cmdline_parser_ext(argc, argv, &conf, ¶ms);
268 if (conf.help_given || conf.detailed_help_given)
269 print_complete_help_and_die();
270 cmdline_parser_free(&conf);
271 params.check_required = 1;
272 params.check_ambiguity = 1;
273 params.print_errors = 1;
274 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
281 ret = init_signals();
285 if (conf.select_given)
287 else if (conf.create_given)
290 ret = com_interactive();
296 ERROR_LOG("%s\n", adu_strerror(-ret));
297 return -EXIT_FAILURE;
299 cmdline_parser_free(&conf);
300 select_cmdline_parser_free(&select_conf);