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
16 * - Generated files: \ref cmdline.h, \ref select.cmdline.h
20 #include <dirent.h> /* readdir() */
24 #include "select.cmdline.h"
31 /** Define the array of error descriptions. */
35 * The error code of the last osl library function.
41 /** In case a signal is received, its number is stored here. */
44 /** Command line and config file options. */
45 struct gengetopt_args_info conf;
47 /** Options passed to --select-options. */
48 struct select_args_info select_conf;
52 * The table containing the directory names and statistics.
54 struct osl_table *dir_table = NULL;
56 static struct osl_column_description dir_table_cols[] = {
58 .storage_type = OSL_MAPPED_STORAGE,
63 .storage_type = OSL_MAPPED_STORAGE,
64 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
66 .compare_function = uint64_compare,
67 .data_size = sizeof(uint64_t)
70 .storage_type = OSL_MAPPED_STORAGE,
71 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
73 .compare_function = size_compare,
74 .data_size = sizeof(uint64_t)
77 .storage_type = OSL_MAPPED_STORAGE,
78 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
79 .compare_function = size_compare,
81 .data_size = sizeof(uint64_t)
84 .storage_type = OSL_MAPPED_STORAGE,
85 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
86 .compare_function = size_compare,
88 .data_size = sizeof(uint64_t)
92 static struct osl_table_description dir_table_desc = {
94 .num_columns = NUM_DT_COLUMNS,
96 .column_descriptions = dir_table_cols,
102 * \param ll Loglevel.
103 * \param fmt Usual format string.
105 * All XXX_LOG() macros use this function.
107 __printf_2_3 void __log(int ll, const char* fmt,...)
115 if (ll < conf.loglevel_arg)
120 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
121 fprintf(outfd, "%s ", str);
123 vfprintf(outfd, fmt, argp);
127 static void close_dir_table(void)
133 NOTICE_LOG("closing dir table\n");
134 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
136 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
137 free((char *)dir_table_desc.dir);
141 static void close_all_tables(void)
147 static void signal_handler(int s)
153 * Check whether to terminate adu.
155 * Check whether a signal was caught that should terminate the
156 * adu process. If yes, close all osl tables and exit gracefully.
158 void check_signals(void)
162 EMERG_LOG("caught signal %d\n", signum);
167 static int init_signals(void)
169 if (signal(SIGINT, &signal_handler) == SIG_ERR)
170 return -E_SIGNAL_SIG_ERR;
171 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
172 return -E_SIGNAL_SIG_ERR;
173 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
174 return -E_SIGNAL_SIG_ERR;
179 * Open the directory table.
181 * \param create If non-zero, create the table first.
185 int open_dir_table(int create)
190 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
193 NOTICE_LOG("creating dir table\n");
194 int ret = osl(osl_create_table(&dir_table_desc));
196 free((char *)dir_table_desc.dir);
200 INFO_LOG("opening dir table\n");
201 return osl(osl_open_table(&dir_table_desc, &dir_table));
204 static int check_args(void)
206 if (conf.create_given && !conf.base_dir_given)
209 /* remove trailing slashes from base-dir arg */
210 if (conf.base_dir_given) {
211 size_t len = strlen(conf.base_dir_arg);
213 if (!len) /* empty string */
214 return -ERRNO_TO_ERROR(EINVAL);
215 if (!--len) /* length 1 is always OK */
217 if (conf.base_dir_arg[len] != '/')
218 break; /* no trailing slash, also OK */
219 conf.base_dir_arg[len] = '\0';
225 static int print_complete_help_and_die(void)
229 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
230 printf("%s\n\n", gengetopt_args_info_purpose);
231 printf("%s\n\n", gengetopt_args_info_usage);
234 line = gengetopt_args_info_help;
236 line = gengetopt_args_info_detailed_help;
237 for (; *line; line++)
238 printf("%s\n", *line);
241 line = select_args_info_help;
243 line = select_args_info_detailed_help;
244 printf("Select options:\n");
245 for (; *line; line++)
246 printf("%s\n", *line);
248 printf("Interactive commands:\n");
249 print_interactive_help();
250 cmdline_parser_free(&conf);
251 select_cmdline_parser_free(&select_conf);
256 * The main function of adu.
258 * \param argc Usual argument count.
259 * \param argv Usual argument vector.
261 * Check command line options, init the signal handlers and
262 * call the main function of the selected mode.
264 * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
266 int main(int argc, char **argv)
269 struct cmdline_parser_params params = {
273 .check_ambiguity = 0,
276 select_cmdline_parser_init(&select_conf);
277 cmdline_parser_init(&conf);
278 /* ignore errors and print complete help if --help was given */
279 cmdline_parser_ext(argc, argv, &conf, ¶ms);
280 if (conf.help_given || conf.detailed_help_given)
281 print_complete_help_and_die();
282 cmdline_parser_free(&conf);
283 params.check_required = 1;
284 params.check_ambiguity = 1;
285 params.print_errors = 1;
286 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
293 ret = init_signals();
297 if (conf.select_given)
299 else if (conf.create_given)
302 ret = com_interactive();
308 ERROR_LOG("%s\n", adu_strerror(-ret));
309 return -EXIT_FAILURE;
311 cmdline_parser_free(&conf);
312 select_cmdline_parser_free(&select_conf);