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 /** In case a signal is received, its number is stored here. */
26 /** Command line and config file options. */
27 struct gengetopt_args_info conf;
29 /** Options passed to --select-options. */
30 struct select_args_info select_conf;
34 * The table containing the directory names and statistics.
36 struct osl_table *dir_table = NULL;
38 static struct osl_column_description dir_table_cols[] = {
40 .storage_type = OSL_MAPPED_STORAGE,
45 .storage_type = OSL_MAPPED_STORAGE,
46 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
48 .compare_function = uint64_compare,
49 .data_size = sizeof(uint64_t)
52 .storage_type = OSL_MAPPED_STORAGE,
53 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
55 .compare_function = size_compare,
56 .data_size = sizeof(uint64_t)
59 .storage_type = OSL_MAPPED_STORAGE,
60 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
61 .compare_function = size_compare,
63 .data_size = sizeof(uint64_t)
66 .storage_type = OSL_MAPPED_STORAGE,
67 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
68 .compare_function = size_compare,
70 .data_size = sizeof(uint64_t)
74 static struct osl_table_description dir_table_desc = {
76 .num_columns = NUM_DT_COLUMNS,
78 .column_descriptions = dir_table_cols,
85 * \param fmt Usual format string.
87 * All XXX_LOG() macros use this function.
89 __printf_2_3 void __log(int ll, const char* fmt,...)
97 if (ll < conf.loglevel_arg)
102 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
103 fprintf(outfd, "%s ", str);
105 vfprintf(outfd, fmt, argp);
109 static void close_dir_table(void)
115 NOTICE_LOG("closing dir table\n");
116 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
118 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
119 free((char *)dir_table_desc.dir);
123 static void close_all_tables(void)
129 static void signal_handler(int s)
134 void check_signals(void)
138 EMERG_LOG("caught signal %d\n", signum);
143 static int init_signals(void)
145 if (signal(SIGINT, &signal_handler) == SIG_ERR)
146 return -E_SIGNAL_SIG_ERR;
147 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
148 return -E_SIGNAL_SIG_ERR;
149 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
150 return -E_SIGNAL_SIG_ERR;
154 int open_dir_table(int create)
159 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
162 NOTICE_LOG("creating dir table\n");
163 int ret = osl(osl_create_table(&dir_table_desc));
165 free((char *)dir_table_desc.dir);
169 INFO_LOG("opening dir table\n");
170 return osl(osl_open_table(&dir_table_desc, &dir_table));
173 static int check_args(void)
175 if (conf.create_given && !conf.base_dir_given)
178 /* remove trailing slashes from base-dir arg */
179 if (conf.base_dir_given) {
180 size_t len = strlen(conf.base_dir_arg);
182 if (!len) /* empty string */
183 return -ERRNO_TO_ERROR(EINVAL);
184 if (!--len) /* length 1 is always OK */
186 if (conf.base_dir_arg[len] != '/')
187 break; /* no trailing slash, also OK */
188 conf.base_dir_arg[len] = '\0';
194 static int print_complete_help_and_die(void)
198 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
199 printf("%s\n\n", gengetopt_args_info_purpose);
200 printf("%s\n\n", gengetopt_args_info_usage);
203 line = gengetopt_args_info_help;
205 line = gengetopt_args_info_detailed_help;
206 for (; *line; line++)
207 printf("%s\n", *line);
210 line = select_args_info_help;
212 line = select_args_info_detailed_help;
213 printf("Select options:\n");
214 for (; *line; line++)
215 printf("%s\n", *line);
217 printf("Interactive commands:\n");
218 print_interactive_help();
219 cmdline_parser_free(&conf);
220 select_cmdline_parser_free(&select_conf);
224 int main(int argc, char **argv)
227 struct cmdline_parser_params params = {
231 .check_ambiguity = 0,
234 select_cmdline_parser_init(&select_conf);
235 cmdline_parser_init(&conf);
236 /* ignore errors and print complete help if --help was given */
237 cmdline_parser_ext(argc, argv, &conf, ¶ms);
238 if (conf.help_given || conf.detailed_help_given)
239 print_complete_help_and_die();
240 cmdline_parser_free(&conf);
241 params.check_required = 1;
242 params.check_ambiguity = 1;
243 params.print_errors = 1;
244 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
251 ret = init_signals();
255 if (conf.select_given)
257 else if (conf.create_given)
260 ret = com_interactive();
266 ERROR_LOG("%s\n", adu_strerror(-ret));
267 return -EXIT_FAILURE;
269 cmdline_parser_free(&conf);
270 select_cmdline_parser_free(&select_conf);