1 /** \file adu.c The main functions used by all modes of operation. */
3 #include <dirent.h> /* readdir() */
7 #include "select.cmdline.h"
17 /** In case a signal is received, its number is stored here. */
20 /** Command line and config file options. */
21 struct gengetopt_args_info conf;
23 /** Options passed to --select-options. */
24 struct select_args_info select_conf;
28 * The table containing the directory names and statistics.
30 struct osl_table *dir_table = NULL;
32 static struct osl_column_description dir_table_cols[] = {
34 .storage_type = OSL_MAPPED_STORAGE,
39 .storage_type = OSL_MAPPED_STORAGE,
40 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
42 .compare_function = uint64_compare,
43 .data_size = sizeof(uint64_t)
46 .storage_type = OSL_MAPPED_STORAGE,
47 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
49 .compare_function = size_compare,
50 .data_size = sizeof(uint64_t)
53 .storage_type = OSL_MAPPED_STORAGE,
54 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
55 .compare_function = size_compare,
57 .data_size = sizeof(uint64_t)
60 .storage_type = OSL_MAPPED_STORAGE,
61 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
62 .compare_function = size_compare,
64 .data_size = sizeof(uint64_t)
68 static struct osl_table_description dir_table_desc = {
70 .num_columns = NUM_DT_COLUMNS,
72 .column_descriptions = dir_table_cols,
79 * \param fmt Usual format string.
81 * All XXX_LOG() macros use this function.
83 __printf_2_3 void __log(int ll, const char* fmt,...)
91 if (ll < conf.loglevel_arg)
96 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
97 fprintf(outfd, "%s ", str);
99 vfprintf(outfd, fmt, argp);
103 static void close_dir_table(void)
109 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
111 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
112 free((char *)dir_table_desc.dir);
116 void close_all_tables(void)
122 static void signal_handler(int s)
127 void check_signals(void)
131 EMERG_LOG("caught signal %d\n", signum);
136 static int init_signals(void)
138 if (signal(SIGINT, &signal_handler) == SIG_ERR)
139 return -E_SIGNAL_SIG_ERR;
140 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
141 return -E_SIGNAL_SIG_ERR;
142 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
143 return -E_SIGNAL_SIG_ERR;
147 int open_dir_table(int create)
149 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
152 int ret = osl(osl_create_table(&dir_table_desc));
154 free((char *)dir_table_desc.dir);
158 return osl(osl_open_table(&dir_table_desc, &dir_table));
161 static int check_args(void)
163 if (conf.create_given && !conf.base_dir_given)
166 /* remove trailing slashes from base-dir arg */
167 if (conf.base_dir_given) {
168 size_t len = strlen(conf.base_dir_arg);
170 if (!len) /* empty string */
171 return -ERRNO_TO_ERROR(EINVAL);
172 if (!--len) /* length 1 is always OK */
174 if (conf.base_dir_arg[len] != '/')
175 break; /* no trailing slash, also OK */
176 conf.base_dir_arg[len] = '\0';
182 static int print_complete_help_and_die(void)
186 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
187 printf("%s\n\n", gengetopt_args_info_purpose);
188 printf("%s\n\n", gengetopt_args_info_usage);
191 line = gengetopt_args_info_help;
193 line = gengetopt_args_info_detailed_help;
194 for (; *line; line++)
195 printf("%s\n", *line);
198 line = select_args_info_help;
200 line = select_args_info_detailed_help;
201 printf("Select options:\n");
202 for (; *line; line++)
203 printf("%s\n", *line);
205 printf("Interactive commands:\n");
206 print_interactive_help();
207 cmdline_parser_free(&conf);
208 select_cmdline_parser_free(&select_conf);
212 int main(int argc, char **argv)
215 struct cmdline_parser_params params = {
219 .check_ambiguity = 0,
222 select_cmdline_parser_init(&select_conf);
223 cmdline_parser_init(&conf);
224 /* ignore errors and print complete help if --help was given */
225 cmdline_parser_ext(argc, argv, &conf, ¶ms);
226 if (conf.help_given || conf.detailed_help_given)
227 print_complete_help_and_die();
228 cmdline_parser_free(&conf);
229 params.check_required = 1;
230 params.check_ambiguity = 1;
231 params.print_errors = 1;
232 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
239 ret = init_signals();
243 if (conf.select_given)
245 else if (conf.create_given)
248 ret = com_interactive();
253 ERROR_LOG("%s\n", adu_strerror(-ret));
254 return -EXIT_FAILURE;
256 cmdline_parser_free(&conf);
257 select_cmdline_parser_free(&select_conf);