Add link to author homepage.
[adu.git] / adu.c
1 /*
2  * Copyright (C) 2008 Andre Noll <maan@tuebingen.mpg.de>
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                 if (!conf.base_dir_arg) {
301                         EMERG_LOG("fatal: neither database dir "
302                                 "nor base dir given\n");
303                         exit(EXIT_FAILURE);
304                 }
305                 tmp = make_message("%s%s",
306                         conf.database_root_arg, conf.base_dir_arg);
307         }
308         /*
309          * As we change the cwd during database creation, database_dir
310          * must be an absolute path.
311          */
312         database_dir = absolute_path(tmp);
313         free(tmp);
314         if (database_dir)
315                 return;
316         EMERG_LOG("failed to get absolute path of database dir\n");
317         exit(EXIT_FAILURE);
318 }
319
320 /**
321  * The main function of adu.
322  *
323  * \param argc Usual argument count.
324  * \param argv Usual argument vector.
325  *
326  * Check command line options, init the signal handlers and
327  * call the main function of the selected mode.
328  *
329  * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
330  */
331 int main(int argc, char **argv)
332 {
333         int ret;
334         struct cmdline_parser_params params = {
335                 .override = 1,
336                 .initialize = 1,
337                 .check_required = 0,
338                 .check_ambiguity = 0,
339                 .print_errors = 0
340         };
341         select_cmdline_parser_init(&select_conf);
342         cmdline_parser_init(&conf);
343         /* ignore errors and print complete help if --help was given */
344         cmdline_parser_ext(argc, argv, &conf, &params);
345         if (conf.help_given || conf.detailed_help_given)
346                 print_complete_help_and_die();
347         cmdline_parser_free(&conf);
348         params.check_required = 1;
349         params.check_ambiguity = 1;
350         params.print_errors = 1;
351         ret = cmdline_parser_ext(argc, argv, &conf, &params);
352         if (ret)
353                 exit(EXIT_FAILURE);
354
355         ret = check_args();
356         if (ret < 0)
357                 goto out;
358         ret = init_signals();
359         if (ret < 0)
360                 goto out;
361         get_database_dir_or_die();
362         if (conf.select_given)
363                 ret = com_select();
364         else if (conf.create_given)
365                 ret = com_create();
366         else
367                 ret = com_interactive();
368         if (ret < 0)
369                 goto out;
370 out:
371         close_all_tables();
372         if (ret < 0) {
373                 ERROR_LOG("%s\n", adu_strerror(-ret));
374                 return -EXIT_FAILURE;
375         }
376         free(database_dir);
377         cmdline_parser_free(&conf);
378         select_cmdline_parser_free(&select_conf);
379         return EXIT_SUCCESS;
380 }