]> git.tuebingen.mpg.de Git - adu.git/blob - adu.c
Print directory name on create errors.
[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 static void close_dir_table(void)
129 {
130         int ret;
131
132         if (!dir_table)
133                 return;
134         NOTICE_LOG("closing dir table\n");
135         ret = osl(osl_close_table(dir_table, OSL_MARK_CLEAN));
136         if (ret < 0)
137                 ERROR_LOG("failed to close dir table: %s\n", adu_strerror(-ret));
138         free((char *)dir_table_desc.dir);
139         dir_table = NULL;
140 }
141
142 static void close_all_tables(void)
143 {
144         close_dir_table();
145         close_user_tables();
146 }
147
148 static void signal_handler(int s)
149 {
150         signum = s;
151 }
152
153 /**
154  * Check whether to terminate adu.
155  *
156  * Check whether a signal was caught that should terminate the
157  * adu process. If yes, close all osl tables and exit gracefully.
158  */
159 void check_signals(void)
160 {
161         if (likely(!signum))
162                 return;
163         EMERG_LOG("caught signal %d\n", signum);
164         close_all_tables();
165         exit(EXIT_FAILURE);
166 }
167
168 static int catch_signal(int sig)
169 {
170         struct sigaction act;
171
172         act.sa_handler = signal_handler;
173         sigemptyset(&act.sa_mask);
174         act.sa_flags = 0;
175         return sigaction(sig, &act, NULL);
176 }
177
178 static int init_signals(void)
179 {
180         if (catch_signal(SIGINT) < 0)
181                 return -E_SIGACTION;
182         if (catch_signal(SIGTERM) < 0)
183                 return -E_SIGACTION;
184         if (catch_signal(SIGPIPE) < 0)
185                 return -E_SIGACTION;
186         return 1;
187 }
188
189 /**
190  * Open the directory table.
191  *
192  * \param create If non-zero, create the table first.
193  *
194  * \return Standard.
195  */
196 int open_dir_table(int create)
197 {
198         int ret;
199
200         if (dir_table)
201                 return 1;
202
203         dir_table_desc.dir = adu_strdup(database_dir);
204         if (create) {
205                 INFO_LOG("creating database directory structure\n");
206                 ret = mkpath(dir_table_desc.dir, 0777);
207                 if (ret < 0)
208                         goto out;
209                 NOTICE_LOG("creating dir table\n");
210                 ret = osl(osl_create_table(&dir_table_desc));
211                 if (ret < 0) {
212                         ERROR_LOG("could not create %s\n", dir_table_desc.dir);
213                         goto out;
214                 }
215         }
216         INFO_LOG("opening dir table\n");
217         return osl(osl_open_table(&dir_table_desc, &dir_table));
218 out:
219         free((char *)dir_table_desc.dir);
220         return ret;
221 }
222
223 static int check_args(void)
224 {
225         if (conf.create_given && !conf.base_dir_given)
226                 return -E_SYNTAX;
227
228         /* remove trailing slashes from base-dir arg */
229         if (conf.base_dir_given) {
230                 size_t len = strlen(conf.base_dir_arg);
231                 for (;;) {
232                         if (!len) /* empty string */
233                                 return -ERRNO_TO_ERROR(EINVAL);
234                         if (!--len) /* length 1 is always OK */
235                                 break;
236                         if (conf.base_dir_arg[len] != '/')
237                                 break; /* no trailing slash, also OK */
238                         conf.base_dir_arg[len] = '\0';
239                 }
240         }
241         return 1;
242 }
243
244 static void print_complete_help_and_die(void)
245 {
246         const char **line;
247
248         printf("%s-%s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
249         printf("%s\n\n", gengetopt_args_info_purpose);
250         printf("%s\n\n", gengetopt_args_info_usage);
251
252         if (conf.help_given)
253                 line = gengetopt_args_info_help;
254         else
255                 line = gengetopt_args_info_detailed_help;
256         for (; *line; line++)
257                 printf("%s\n", *line);
258
259         if (conf.help_given)
260                 line = select_args_info_help;
261         else
262                 line  = select_args_info_detailed_help;
263         printf("Select options:\n");
264         for (; *line; line++)
265                 printf("%s\n", *line);
266
267         printf("Interactive commands:\n");
268         print_interactive_help();
269         cmdline_parser_free(&conf);
270         select_cmdline_parser_free(&select_conf);
271         exit(EXIT_FAILURE);
272 }
273
274 static void get_database_dir_or_die(void)
275 {
276         char *tmp;
277
278         if (conf.database_dir_given)
279                 tmp = adu_strdup(conf.database_dir_arg);
280         else
281                 tmp = make_message("%s%s",
282                         conf.database_root_arg, conf.base_dir_arg);
283         /*
284          * As we change the cwd during database creation, database_dir
285          * must be an absolute path.
286          */
287         database_dir = absolute_path(tmp);
288         free(tmp);
289         if (database_dir)
290                 return;
291         EMERG_LOG("failed to get absolute path of database dir\n");
292         exit(EXIT_FAILURE);
293 }
294
295 /**
296  * The main function of adu.
297  *
298  * \param argc Usual argument count.
299  * \param argv Usual argument vector.
300  *
301  * Check command line options, init the signal handlers and
302  * call the main function of the selected mode.
303  *
304  * \return \p EXIT_SUCCESS on success, \p EXIT_FAILURE otherwise.
305  */
306 int main(int argc, char **argv)
307 {
308         int ret;
309         struct cmdline_parser_params params = {
310                 .override = 1,
311                 .initialize = 1,
312                 .check_required = 0,
313                 .check_ambiguity = 0,
314                 .print_errors = 0
315         };
316         select_cmdline_parser_init(&select_conf);
317         cmdline_parser_init(&conf);
318         /* ignore errors and print complete help if --help was given */
319         cmdline_parser_ext(argc, argv, &conf, &params);
320         if (conf.help_given || conf.detailed_help_given)
321                 print_complete_help_and_die();
322         cmdline_parser_free(&conf);
323         params.check_required = 1;
324         params.check_ambiguity = 1;
325         params.print_errors = 1;
326         ret = cmdline_parser_ext(argc, argv, &conf, &params);
327         if (ret)
328                 exit(EXIT_FAILURE);
329
330         ret = check_args();
331         if (ret < 0)
332                 goto out;
333         ret = init_signals();
334         if (ret < 0)
335                 goto out;
336         get_database_dir_or_die();
337         if (conf.select_given)
338                 ret = com_select();
339         else if (conf.create_given)
340                 ret = com_create();
341         else
342                 ret = com_interactive();
343         if (ret < 0)
344                 goto out;
345 out:
346         close_all_tables();
347         if (ret < 0) {
348                 ERROR_LOG("%s\n", adu_strerror(-ret));
349                 return -EXIT_FAILURE;
350         }
351         free(database_dir);
352         cmdline_parser_free(&conf);
353         select_cmdline_parser_free(&select_conf);
354         return EXIT_SUCCESS;
355 }