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;
26 /** The number of different uids found so far. */
27 uint32_t num_uids = 0;
30 * The table containing the directory names and statistics.
32 struct osl_table *dir_table = NULL;
34 static struct osl_column_description dir_table_cols[] = {
36 .storage_type = OSL_MAPPED_STORAGE,
41 .storage_type = OSL_MAPPED_STORAGE,
42 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
44 .compare_function = uint64_compare,
45 .data_size = sizeof(uint64_t)
48 .storage_type = OSL_MAPPED_STORAGE,
49 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
51 .compare_function = size_compare,
52 .data_size = sizeof(uint64_t)
55 .storage_type = OSL_MAPPED_STORAGE,
56 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
57 .compare_function = size_compare,
59 .data_size = sizeof(uint64_t)
62 .storage_type = OSL_MAPPED_STORAGE,
63 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
64 .compare_function = size_compare,
66 .data_size = sizeof(uint64_t)
70 static struct osl_table_description dir_table_desc = {
72 .num_columns = NUM_DT_COLUMNS,
74 .column_descriptions = dir_table_cols,
81 * \param fmt Usual format string.
83 * All XXX_LOG() macros use this function.
85 __printf_2_3 void __log(int ll, const char* fmt,...)
93 if (ll < conf.loglevel_arg)
98 strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
99 fprintf(outfd, "%s ", str);
101 vfprintf(outfd, fmt, argp);
105 static void close_dir_table(void)
111 ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
113 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
114 free((char *)dir_table_desc.dir);
118 void close_all_tables(void)
125 static void signal_handler(int s)
130 void check_signals(void)
134 EMERG_LOG("caught signal %d\n", signum);
139 static int init_signals(void)
141 if (signal(SIGINT, &signal_handler) == SIG_ERR)
142 return -E_SIGNAL_SIG_ERR;
143 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
144 return -E_SIGNAL_SIG_ERR;
145 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
146 return -E_SIGNAL_SIG_ERR;
150 int open_dir_table(int create)
152 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
155 int ret = osl(osl_create_table(&dir_table_desc));
157 free((char *)dir_table_desc.dir);
161 return osl(osl_open_table(&dir_table_desc, &dir_table));
164 static int check_args(void)
166 if (conf.create_given && !conf.base_dir_given)
169 /* remove trailing slashes from base-dir arg */
170 if (conf.base_dir_given) {
171 size_t len = strlen(conf.base_dir_arg);
173 if (!len) /* empty string */
174 return -ERRNO_TO_ERROR(EINVAL);
175 if (!--len) /* length 1 is always OK */
177 if (conf.base_dir_arg[len] != '/')
178 break; /* no trailing slash, also OK */
179 conf.base_dir_arg[len] = '\0';
185 static int print_complete_help_and_die(void)
188 select_cmdline_parser_init(&select_conf);
190 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
191 printf("%s\n\n", gengetopt_args_info_purpose);
192 printf("%s\n\n", gengetopt_args_info_usage);
195 line = gengetopt_args_info_help;
197 line = gengetopt_args_info_detailed_help;
198 for (; *line; line++)
199 printf("%s\n", *line);
202 line = select_args_info_help;
204 line = select_args_info_detailed_help;
205 printf("Select options:\n");
206 for (; *line; line++)
207 printf("%s\n", *line);
209 printf("Interactive commands:\n");
210 print_interactive_help();
214 int main(int argc, char **argv)
217 struct cmdline_parser_params params = {
221 .check_ambiguity = 0,
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 params.check_required = 1;
229 params.check_ambiguity = 1;
230 params.print_errors = 1;
231 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
238 ret = init_signals();
242 if (conf.select_given)
244 else if (conf.create_given)
247 ret = com_interactive();
252 ERROR_LOG("%s\n", adu_strerror(-ret));
253 return -EXIT_FAILURE;