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)
124 static void signal_handler(int s)
129 void check_signals(void)
133 EMERG_LOG("caught signal %d\n", signum);
138 static int init_signals(void)
140 if (signal(SIGINT, &signal_handler) == SIG_ERR)
141 return -E_SIGNAL_SIG_ERR;
142 if (signal(SIGTERM, &signal_handler) == SIG_ERR)
143 return -E_SIGNAL_SIG_ERR;
144 if (signal(SIGPIPE, &signal_handler) == SIG_ERR)
145 return -E_SIGNAL_SIG_ERR;
149 int open_dir_table(int create)
151 dir_table_desc.dir = adu_strdup(conf.database_dir_arg);
154 int ret = osl(osl_create_table(&dir_table_desc));
156 free((char *)dir_table_desc.dir);
160 return osl(osl_open_table(&dir_table_desc, &dir_table));
163 static int check_args(void)
165 if (conf.create_given && !conf.base_dir_given)
168 /* remove trailing slashes from base-dir arg */
169 if (conf.base_dir_given) {
170 size_t len = strlen(conf.base_dir_arg);
172 if (!len) /* empty string */
173 return -ERRNO_TO_ERROR(EINVAL);
174 if (!--len) /* length 1 is always OK */
176 if (conf.base_dir_arg[len] != '/')
177 break; /* no trailing slash, also OK */
178 conf.base_dir_arg[len] = '\0';
184 static int print_complete_help_and_die(void)
187 select_cmdline_parser_init(&select_conf);
189 printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
190 printf("%s\n\n", gengetopt_args_info_purpose);
191 printf("%s\n\n", gengetopt_args_info_usage);
194 line = gengetopt_args_info_help;
196 line = gengetopt_args_info_detailed_help;
197 for (; *line; line++)
198 printf("%s\n", *line);
201 line = select_args_info_help;
203 line = select_args_info_detailed_help;
204 printf("Select options:\n");
205 for (; *line; line++)
206 printf("%s\n", *line);
208 printf("Interactive commands:\n");
209 print_interactive_help();
213 int main(int argc, char **argv)
216 struct cmdline_parser_params params = {
220 .check_ambiguity = 0,
223 /* ignore errors and print complete help if --help was given */
224 cmdline_parser_ext(argc, argv, &conf, ¶ms);
225 if (conf.help_given || conf.detailed_help_given)
226 print_complete_help_and_die();
227 params.check_required = 1;
228 params.check_ambiguity = 1;
229 params.print_errors = 1;
230 ret = cmdline_parser_ext(argc, argv, &conf, ¶ms);
237 ret = init_signals();
241 if (conf.select_given)
243 else if (conf.create_given)
246 ret = com_interactive();
251 ERROR_LOG("%s\n", adu_strerror(-ret));
252 return -EXIT_FAILURE;