]> git.tuebingen.mpg.de Git - adu.git/blob - adu.c
7d133d619836e3340e5bce2f6b2b3bf83b39d789
[adu.git] / adu.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file adu.c \brief The main functions used by all modes of operation. */
8
9 /**
10  * \mainpage adu API reference
11  *
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, \ref bloom.c
16  */
17
18 #include "adu.h"
19 #include <dirent.h> /* readdir() */
20 #include <pwd.h>
21 #include "format.h"
22 #include "user.h"
23 #include "select.cmdline.h"
24 #include "select.h"
25 #include "cmdline.h"
26 #include "fd.h"
27 #include "string.h"
28 #include "error.h"
29
30 /** Define the array of error descriptions. */
31 DEFINE_ERRLIST;
32
33 /**
34  * The error code of the last called osl library function.
35  *
36  * \sa osl().
37  */
38 int osl_errno;
39
40 /** In case a signal is received, its number is stored here. */
41 static int signum;
42
43 /** Command line and config file options. */
44 struct gengetopt_args_info conf;
45
46 /** Options passed to --select-options. */
47 struct select_args_info select_conf;
48
49 /** Computed database dir */
50 char *database_dir;
51
52 /**
53  * The table containing the directory names and statistics.
54  */
55 struct osl_table *dir_table = NULL;
56
57 static struct osl_column_description dir_table_cols[] = {
58         [DT_NAME] = {
59                 .storage_type = OSL_MAPPED_STORAGE,
60                 .storage_flags = 0,
61                 .name = "dir",
62         },
63         [DT_NUM] = {
64                 .storage_type = OSL_MAPPED_STORAGE,
65                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
66                 .name = "num",
67                 .compare_function = uint64_compare,
68                 .data_size = sizeof(uint64_t)
69         },
70         [DT_PARENT_NUM] = {
71                 .storage_type = OSL_MAPPED_STORAGE,
72                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
73                 .name = "parent_num",
74                 .compare_function = size_compare,
75                 .data_size = sizeof(uint64_t)
76         },
77         [DT_BYTES] = {
78                 .storage_type = OSL_MAPPED_STORAGE,
79                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
80                 .compare_function = size_compare,
81                 .name = "num_bytes",
82                 .data_size = sizeof(uint64_t)
83         },
84         [DT_FILES] = {
85                 .storage_type = OSL_MAPPED_STORAGE,
86                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
87                 .compare_function = size_compare,
88                 .name = "num_files",
89                 .data_size = sizeof(uint64_t)
90         }
91 };
92
93 static struct osl_table_description dir_table_desc = {
94         .name = "dir_table",
95         .num_columns = NUM_DT_COLUMNS,
96         .flags = 0,
97         .column_descriptions = dir_table_cols,
98 };
99
100 /**
101  * The log function.
102  *
103  * \param ll Loglevel.
104  * \param fmt Usual format string.
105  *
106  * All XXX_LOG() macros use this function.
107  */
108 __printf_2_3 void __log(int ll, const char* fmt,...)
109 {
110         va_list argp;
111         FILE *outfd;
112         struct tm *tm;
113         time_t t1;
114         char str[255] = "";
115
116         if (ll < conf.loglevel_arg)
117                 return;
118         outfd = stderr;
119         time(&t1);
120         tm = localtime(&t1);
121         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
122         fprintf(outfd, "%s ", str);
123         va_start(argp, fmt);
124         vfprintf(outfd, fmt, argp);
125         va_end(argp);
126 }
127
128 /**
129  * adu's version of strerror(3).
130  *
131  * \param num The error number.
132  *
133  * \return The error text of \a num.
134  */
135 const char *adu_strerror(int num)
136 {
137         assert(num > 0);
138         if (num == E_OSL) {
139                 assert(osl_errno > 0);
140                 return osl_strerror((osl_errno));
141         }
142         if (IS_SYSTEM_ERROR(num))
143                 return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1));
144         return adu_errlist[num];
145 }
146
147 static void close_dir_table(void)
148 {
149         int ret;
150
151         if (!dir_table)
152                 return;
153         NOTICE_LOG("closing dir table\n");
154         ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
155         if (ret < 0)
156                 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
157         free((char *)dir_table_desc.dir);
158         dir_table = NULL;
159 }
160
161 static void close_all_tables(void)
162 {
163         close_dir_table();
164         close_user_tables();
165 }
166
167 static void signal_handler(int s)
168 {
169         signum = s;
170 }
171
172 /**
173  * Check whether to terminate adu.
174  *
175  * Check whether a signal was caught that should terminate the
176  * adu process. If yes, close all osl tables and exit gracefully.
177  */
178 void check_signals(void)
179 {
180         if (likely(!signum))
181                 return;
182         EMERG_LOG("caught signal %d\n", signum);
183         close_all_tables();
184         exit(EXIT_FAILURE);
185 }
186
187 static int catch_signal(int sig)
188 {
189         struct sigaction act;
190
191         act.sa_handler = signal_handler;
192         sigemptyset(&act.sa_mask);
193         act.sa_flags = 0;
194         return sigaction(sig, &act, NULL);
195 }
196
197 static int init_signals(void)
198 {
199         if (catch_signal(SIGINT) < 0)
200                 return -E_SIGACTION;
201         if (catch_signal(SIGTERM) < 0)
202                 return -E_SIGACTION;
203         if (catch_signal(SIGPIPE) < 0)
204                 return -E_SIGACTION;
205         return 1;
206 }
207
208 /**
209  * Open the directory table.
210  *
211  * \param create If non-zero, create the table first.
212  *
213  * \return Standard.
214  */
215 int open_dir_table(int create)
216 {
217         int ret;
218
219         if (dir_table)
220                 return 1;
221
222         dir_table_desc.dir = adu_strdup(database_dir);
223         if (create) {
224                 INFO_LOG("creating database directory structure\n");
225                 ret = mkpath(dir_table_desc.dir, 0777);
226                 if (ret < 0)
227                         goto out;
228                 NOTICE_LOG("creating dir table\n");
229                 ret = osl(osl_create_table(&dir_table_desc));
230                 if (ret < 0) {
231                         ERROR_LOG("could not create %s\n", dir_table_desc.dir);
232                         goto out;
233                 }
234         }
235         INFO_LOG("opening dir table\n");
236         return osl(osl_open_table(&dir_table_desc, &dir_table));
237 out:
238         free((char *)dir_table_desc.dir);
239         return ret;
240 }
241
242 static int check_args(void)
243 {
244         if (conf.create_given && !conf.base_dir_given)
245                 return -E_SYNTAX;
246
247         /* remove trailing slashes from base-dir arg */
248         if (conf.base_dir_given) {
249                 size_t len = strlen(conf.base_dir_arg);
250                 for (;;) {
251                         if (!len) /* empty string */
252                                 return -ERRNO_TO_ERROR(EINVAL);
253                         if (!--len) /* length 1 is always OK */
254                                 break;
255                         if (conf.base_dir_arg[len] != '/')
256                                 break; /* no trailing slash, also OK */
257                         conf.base_dir_arg[len] = '\0';
258                 }
259         }
260         return 1;
261 }
262
263 static void print_complete_help_and_die(void)
264 {
265         const char **line;
266
267         printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
268         printf("%s\n\n", gengetopt_args_info_purpose);
269         printf("%s\n\n", gengetopt_args_info_usage);
270
271         if (conf.help_given)
272                 line = gengetopt_args_info_help;
273         else
274                 line = gengetopt_args_info_detailed_help;
275         for (; *line; line++)
276                 printf("%s\n", *line);
277
278         if (conf.help_given)
279                 line = select_args_info_help;
280         else
281                 line  = select_args_info_detailed_help;
282         printf("Select options:\n");
283         for (; *line; line++)
284                 printf("%s\n", *line);
285
286         printf("Interactive commands:\n");
287         print_interactive_help();
288         cmdline_parser_free(&conf);
289         select_cmdline_parser_free(&select_conf);
290         exit(EXIT_FAILURE);
291 }
292
293 static void get_database_dir_or_die(void)
294 {
295         char *tmp;
296
297         if (conf.database_dir_given)
298                 tmp = adu_strdup(conf.database_dir_arg);
299         else
300                 tmp = make_message("%s%s",
301                         conf.database_root_arg, conf.base_dir_arg);
302         /*
303          * As we change the cwd during database creation, database_dir
304          * must be an absolute path.
305          */
306         database_dir = absolute_path(tmp);
307         free(tmp);
308         if (database_dir)
309                 return;
310         EMERG_LOG("failed to get absolute path of database dir\n");
311         exit(EXIT_FAILURE);
312 }
313
314 /**
315  * The main function of adu.
316  *
317  * \param argc Usual argument count.
318  * \param argv Usual argument vector.
319  *
320  * Check command line options, init the signal handlers and
321  * call the main function of the selected mode.
322  *
323  * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
324  */
325 int main(int argc, char **argv)
326 {
327         int ret;
328         struct cmdline_parser_params params = {
329                 .override = 1,
330                 .initialize = 1,
331                 .check_required = 0,
332                 .check_ambiguity = 0,
333                 .print_errors = 0
334         };
335         select_cmdline_parser_init(&select_conf);
336         cmdline_parser_init(&conf);
337         /* ignore errors and print complete help if --help was given */
338         cmdline_parser_ext(argc, argv, &conf, &params);
339         if (conf.help_given || conf.detailed_help_given)
340                 print_complete_help_and_die();
341         cmdline_parser_free(&conf);
342         params.check_required = 1;
343         params.check_ambiguity = 1;
344         params.print_errors = 1;
345         ret = cmdline_parser_ext(argc, argv, &conf, &params);
346         if (ret)
347                 exit(EXIT_FAILURE);
348
349         ret = check_args();
350         if (ret < 0)
351                 goto out;
352         ret = init_signals();
353         if (ret < 0)
354                 goto out;
355         get_database_dir_or_die();
356         if (conf.select_given)
357                 ret = com_select();
358         else if (conf.create_given)
359                 ret = com_create();
360         else
361                 ret = com_interactive();
362         if (ret < 0)
363                 goto out;
364 out:
365         close_all_tables();
366         if (ret < 0) {
367                 ERROR_LOG("%s\n", adu_strerror(-ret));
368                 return -EXIT_FAILURE;
369         }
370         free(database_dir);
371         cmdline_parser_free(&conf);
372         select_cmdline_parser_free(&select_conf);
373         return EXIT_SUCCESS;
374 }