df68b67073bf41116859eaac633a2c5d0a835396
[adu.git] / select.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 select.c \brief The select mode of adu. */
8
9 #include <dirent.h> /* readdir() */
10 #include <sys/types.h>
11 #include <regex.h>
12
13 #include "format.h"
14 #include "adu.h"
15 #include "gcc-compat.h"
16 #include "cmdline.h"
17 #include "fd.h"
18 #include "string.h"
19 #include "error.h"
20 #include "user.h"
21 #include "select.cmdline.h"
22
23 /** \cond */
24 /* global list */
25 #define GLOBAL_LIST_ATOMS \
26 ATOM(size, SIZE) \
27 ATOM(files, COUNT) \
28 ATOM(dirname, STRING) \
29
30 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
31 struct atom global_list_atoms[] = {
32 GLOBAL_LIST_ATOMS
33 {.name = NULL}
34 };
35 #undef ATOM
36 #define ATOM(x, y) gla_ ## x,
37 enum global_list_atoms {GLOBAL_LIST_ATOMS};
38 #undef ATOM
39
40 /* global summary */
41 #define GLOBAL_SUMMARY_ATOMS \
42 ATOM(dirs, COUNT) \
43 ATOM(files, COUNT) \
44 ATOM(size, SIZE)
45
46 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
47 struct atom global_summary_atoms[] = {
48 GLOBAL_SUMMARY_ATOMS
49 {.name = NULL}
50 };
51 #undef ATOM
52 #define ATOM(x, y) gsa_ ## x,
53 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
54 #undef ATOM
55
56 /* user list */
57 #define USER_LIST_ATOMS \
58 ATOM(pw_name, STRING) \
59 ATOM(uid, ID) \
60 ATOM(size, SIZE) \
61 ATOM(files, COUNT) \
62 ATOM(dirname, STRING) \
63
64 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
65 struct atom user_list_atoms[] = {
66 USER_LIST_ATOMS
67 {.name = NULL}
68 };
69 #undef ATOM
70 #define ATOM(x, y) ula_ ## x,
71 enum user_list_atoms {USER_LIST_ATOMS};
72 #undef ATOM
73
74 /* user list header */
75 #define USER_LIST_HEADER_TRAILER_ATOMS \
76 ATOM(pw_name, STRING) \
77 ATOM(uid, ID)
78
79 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
80 struct atom user_list_header_trailer_atoms[] = {
81 USER_LIST_HEADER_TRAILER_ATOMS
82 {.name = NULL}
83 };
84 #undef ATOM
85 #define ATOM(x, y) ulha_ ## x,
86 enum user_list_header_trailer_atoms {USER_LIST_HEADER_TRAILER_ATOMS};
87 #undef ATOM
88
89 /* user summary */
90 #define USER_SUMMARY_ATOMS \
91 ATOM(pw_name, STRING) \
92 ATOM(uid, ID) \
93 ATOM(dirs, COUNT) \
94 ATOM(files, COUNT) \
95 ATOM(size, SIZE)
96
97 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
98 struct atom user_summary_atoms[] = {
99 USER_SUMMARY_ATOMS
100 {.name = NULL}
101 };
102 #undef ATOM
103 #define ATOM(x, y) usa_ ## x,
104 enum user_summary_atoms {USER_SUMMARY_ATOMS};
105 #undef ATOM
106
107 /** \endcond */
108
109 struct global_list_info {
110 uint32_t count;
111 int ret;
112 int osl_errno;
113 struct format_info *fi;
114 regex_t *preg;
115 int inverse_matching;
116 };
117
118 struct global_summary_info {
119 /** Global dir count. */
120 uint64_t num_dirs;
121 /** Global files count. */
122 uint64_t num_files;
123 /** Global bytes count. */
124 uint64_t num_bytes;
125 regex_t *preg;
126 int inverse_matching;
127 int ret;
128 int osl_errno;
129 };
130
131 struct user_list_info {
132 uint32_t count;
133 struct user_info *ui;
134 struct format_info *fi;
135 regex_t *preg;
136 int inverse_matching;
137 int ret;
138 int osl_errno;
139 };
140
141 struct user_list_format_info {
142 struct format_info *fi;
143 struct format_info *header_fi;
144 struct format_info *trailer_fi;
145 };
146
147 struct user_summary_info {
148 struct user_info *ui;
149 /** Total number of files owned by this user. */
150 uint64_t files;
151 /** Total number of bytes owned by this user. */
152 uint64_t bytes;
153 /** Total number of directories that contain at least one file */
154 uint64_t dirs;
155 int ret;
156 int osl_errno;
157 regex_t *preg;
158 int inverse_matching;
159 };
160
161 struct user_summary_loop_data {
162 unsigned num_admissible_users;
163 struct user_summary_info *usis;
164 struct user_summary_info *current;
165 struct format_info *fi;
166 };
167
168 static FILE *output_file;
169
170 __printf_1_2 static int output(const char const *fmt, ...)
171 {
172 va_list argp;
173 int ret;
174
175 va_start(argp, fmt);
176 ret = vfprintf(output_file, fmt, argp);
177 va_end(argp);
178 return ret < 0? -E_OUTPUT : 1;
179 }
180
181 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
182 {
183 char *result = NULL, *tmp;
184 struct osl_row *row;
185 uint64_t val = *dirnum;
186 struct osl_object obj;
187 int ret;
188 char *pfx;
189
190 again:
191 obj.data = &val;
192 obj.size = sizeof(val);
193 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
194 if (ret < 0)
195 goto out;
196 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
197 if (ret < 0)
198 goto out;
199 val = *(uint64_t *)obj.data;
200 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
201 if (ret < 0)
202 goto out;
203 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
204 tmp = make_message("%s/%s", pfx, result? result : "");
205 free(result);
206 result = tmp;
207 if (val)
208 goto again;
209 out:
210 if (ret < 0) {
211 free(result);
212 *name = NULL;
213 } else {
214 assert(result);
215 *name = result;
216 }
217 return ret;
218 }
219
220 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
221 {
222 struct osl_object obj;
223 int ret;
224
225 *name = NULL;
226 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
227 if (ret < 0)
228 return ret;
229 return get_dir_name_by_number((uint64_t *)obj.data, name);
230 }
231
232 static int get_dir_name_of_user_row(struct osl_row *user_table_row,
233 struct user_info *ui, char **dirname)
234 {
235 struct osl_object obj;
236 int ret = osl(osl_get_object(ui->table, user_table_row,
237 UT_DIR_NUM, &obj));
238
239 if (ret < 0)
240 return ret;
241 return get_dir_name_by_number((uint64_t *)obj.data, dirname);
242 }
243
244 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
245 {
246 struct osl_object obj;
247 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
248 if (ret < 0)
249 return ret;
250 *num_files = *(uint64_t *)obj.data;
251 return 1;
252 }
253
254 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
255 uint64_t *num_files)
256 {
257 struct osl_object obj;
258 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
259
260 if (ret < 0)
261 return ret;
262 *num_files = *(uint64_t *)obj.data;
263 return 1;
264 }
265
266 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
267 {
268 struct osl_object obj;
269 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
270 if (ret < 0)
271 return ret;
272 *num_bytes = *(uint64_t *)obj.data;
273 return 1;
274 }
275
276 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
277 uint64_t *num_bytes)
278 {
279 struct osl_object obj;
280 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
281
282 if (ret < 0)
283 return ret;
284 *num_bytes = *(uint64_t *)obj.data;
285 return 1;
286 }
287
288 static void free_regex(regex_t *preg)
289 {
290 if (!preg)
291 return;
292 regfree(preg);
293 free(preg);
294 }
295
296 static int compile_regex(regex_t **preg, int *invert)
297 {
298 int ret;
299 size_t size;
300 char *buf, *p = select_conf.pattern_arg;
301
302 if (!select_conf.pattern_given || !p[0]) {
303 *preg = NULL;
304 return 0;
305 }
306 if (p[0] == '!') {
307 if (!p[1]) {
308 *preg = NULL;
309 return -E_REGEX;
310 }
311 *invert = 1;
312 p++;
313 } else
314 *invert = 0;
315 *preg = adu_malloc(sizeof(regex_t));
316 ret = regcomp(*preg, p, 0);
317 if (!ret)
318 return 1;
319 size = regerror(ret, *preg, NULL, 0);
320 buf = adu_malloc(size);
321 regerror(ret, *preg, buf, size);
322 ERROR_LOG("%s\n", buf);
323 free(buf);
324 free_regex(*preg);
325 *preg = NULL;
326 return -E_REGEX;
327 }
328
329 static int dir_is_admissible(char *dirname, regex_t *preg, int inverse_matching)
330 {
331 int ret;
332
333 if (!preg)
334 return 1;
335 ret = regexec(preg, dirname, 0, NULL, 0);
336 if (ret == REG_NOMATCH && !inverse_matching)
337 return 0;
338 if (ret != REG_NOMATCH && inverse_matching)
339 return 0;
340 return 1;
341 }
342
343 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
344 {
345 if (ret >= 0)
346 return ret;
347 assert(ret == -E_OSL);
348 if (osl_errno != E_OSL_LOOP)
349 /* error not caused by loop function returning negative. */
350 return ret;
351 assert(loop_ret < 0);
352 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
353 return 1;
354 if (loop_ret == -E_OSL) { /* osl error in loop function */
355 assert(loop_osl_errno);
356 osl_errno = loop_osl_errno;
357 }
358 return loop_ret;
359 }
360
361 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
362 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
363 {
364 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
365 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
366 }
367
368 static int global_summary_loop_function(struct osl_row *row, void *data)
369 {
370 struct global_summary_info *gsi = data;
371 int ret;
372 uint64_t num;
373
374 if (gsi->preg) {
375 char *dirname;
376 ret = get_dir_name_of_row(row, &dirname);
377 if (ret < 0)
378 goto err;
379 ret = dir_is_admissible(dirname, gsi->preg, gsi->inverse_matching);
380 free(dirname);
381 if (!ret)
382 return 1;
383 }
384
385 ret = get_num_files_of_row(row, &num);
386 if (ret < 0)
387 goto err;
388 gsi->num_files += num;
389
390 ret = get_num_bytes_of_row(row, &num);
391 if (ret < 0)
392 goto err;
393 gsi->num_bytes += num;
394 gsi->num_dirs++;
395 return 1;
396 err:
397 gsi->ret = ret;
398 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
399 return ret;
400 }
401
402 static int print_global_summary(struct format_info *fi)
403 {
404 int ret;
405 char *buf;
406 struct global_summary_info gsi = {.num_dirs = 0};
407 char *header = select_conf.header_given? select_conf.header_arg :
408 "Global summary\n";
409
410 union atom_value values[] = {
411 [gsa_dirs] = {.num_value = 0ULL},
412 [gsa_files] = {.num_value = 0ULL},
413 [gsa_size] = {.num_value = 0ULL}
414 };
415
416 ret = compile_regex(&gsi.preg, &gsi.inverse_matching);
417 if (ret < 0)
418 return ret;
419 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
420 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
421 free_regex(gsi.preg);
422 if (ret < 0)
423 return ret;
424 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
425 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
426 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
427
428 ret = output("%s", header);
429 if (ret < 0)
430 return ret;
431 buf = format_items(fi, values);
432 ret = output("%s", buf);
433 free(buf);
434 if (ret < 0)
435 return ret;
436 return output("%s", select_conf.trailer_arg);
437 }
438
439 static int user_summary_loop_function(struct osl_row *row, void *data)
440 {
441 struct user_summary_info *usi = data;
442 uint64_t num;
443 int ret;
444
445 if (usi->preg) {
446 char *dirname;
447 ret = get_dir_name_of_row(row, &dirname);
448 if (ret < 0)
449 goto err;
450 ret = dir_is_admissible(dirname, usi->preg, usi->inverse_matching);
451 free(dirname);
452 if (!ret)
453 return 1;
454 }
455 ret = get_num_user_files(row, usi->ui, &num);
456 if (ret < 0)
457 goto err;
458 usi->files += num;
459 ret = get_num_user_bytes(row, usi->ui, &num);
460 if (ret < 0)
461 goto err;
462 usi->bytes += num;
463 usi->dirs++;
464 return 1;
465 err:
466 usi->ret = ret;
467 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
468 return ret;
469 }
470
471 static int compute_user_summary(struct user_info *ui, void *data)
472 {
473 struct user_summary_loop_data *usld = data;
474 struct user_summary_info *usi = usld->current++;
475 int ret = compile_regex(&usi->preg, &usi->inverse_matching);
476
477 if (ret < 0)
478 return ret;
479 usi->ui = ui;
480 ret = adu_loop_reverse(ui->table, UT_BYTES, usi, user_summary_loop_function,
481 &usi->ret, &usi->osl_errno);
482 free_regex(usi->preg);
483 return ret;
484 }
485
486 static int print_user_summary_line(struct user_summary_info *usi,
487 struct format_info *fi)
488 {
489 struct user_info *ui = usi->ui;
490 union atom_value values[] = {
491 [usa_pw_name] = {.string_value = ui->pw_name?
492 ui->pw_name : "?"},
493 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
494 [usa_dirs] = {.num_value = (long long unsigned)usi->dirs},
495 [usa_files] = {.num_value = (long long unsigned)usi->files},
496 [usa_size] = {.num_value = (long long unsigned)usi->bytes}
497 };
498 char *buf = format_items(fi, values);
499 int ret = output("%s", buf);
500
501 free(buf);
502 return ret;
503 }
504
505 static int name_comp(const void *a, const void *b)
506 {
507 const struct user_summary_info *x = a, *y = b;
508 char *n1 = x->ui->pw_name;
509 char *n2 = y->ui->pw_name;
510
511 if (!n1)
512 return 1;
513 if (!n2)
514 return -1;
515 return strcmp(n1, n2);
516 }
517
518 static int uid_comp(const void *a, const void *b)
519 {
520 const struct user_summary_info *x = a, *y = b;
521 return -NUM_COMPARE(x->ui->uid, y->ui->uid);
522 }
523
524 static int dir_count_comp(const void *a, const void *b)
525 {
526 const struct user_summary_info *x = a, *y = b;
527 return NUM_COMPARE(x->dirs, y->dirs);
528 }
529
530 static int file_count_comp(const void *a, const void *b)
531 {
532 const struct user_summary_info *x = a, *y = b;
533 return NUM_COMPARE(x->files, y->files);
534 }
535
536 static int size_comp(const void *a, const void *b)
537 {
538 const struct user_summary_info *x = a, *y = b;
539 return NUM_COMPARE(x->bytes, y->bytes);
540 }
541
542 static int count_admissible_users(__a_unused struct user_info *ui, void *data)
543 {
544 struct user_summary_loop_data *usld = data;
545 usld->num_admissible_users++;
546 return 1;
547 }
548
549 static int print_user_summary(struct format_info *fi)
550 {
551 int i, ret;
552 int (*comp)(const void *a, const void *b);
553 struct user_summary_loop_data usld = { .fi = fi};
554 char *header = select_conf.header_given? select_conf.header_arg :
555 "User summary\n";
556
557 ret = output("%s", header);
558 if (ret < 0)
559 return ret;
560 ret = for_each_admissible_user(count_admissible_users, &usld);
561 if (ret < 0)
562 return ret;
563 if (usld.num_admissible_users == 0)
564 return 1;
565 usld.usis = adu_calloc(usld.num_admissible_users
566 * sizeof(struct user_summary_info));
567 usld.current = usld.usis;
568 ret = for_each_admissible_user(compute_user_summary, &usld);
569 if (ret < 0)
570 goto out;
571 switch (select_conf.user_summary_sort_arg) {
572 case user_summary_sort_arg_name:
573 comp = name_comp;
574 break;
575 case user_summary_sort_arg_uid:
576 comp = uid_comp;
577 break;
578 case user_summary_sort_arg_dir_count:
579 comp = dir_count_comp;
580 break;
581 case user_summary_sort_arg_file_count:
582 comp = file_count_comp;
583 break;
584 case user_summary_sort_arg_size:
585 comp = size_comp;
586 break;
587 default: /* this should never happen, but anyway */
588 comp = size_comp;
589 break;
590 }
591 qsort(usld.usis, usld.num_admissible_users,
592 sizeof(struct user_summary_info), comp);
593 for (i = 0; i < usld.num_admissible_users; i++) {
594 if (select_conf.limit_arg >= 0 && i > select_conf.limit_arg)
595 break;
596 ret = print_user_summary_line(usld.usis + i, usld.fi);
597 if (ret < 0)
598 goto out;
599 }
600 ret = output("%s", select_conf.trailer_arg);
601 out:
602 free(usld.usis);
603 return ret;
604 }
605
606 static int user_list_loop_function(struct osl_row *row, void *data)
607 {
608 struct user_list_info *uli = data;
609 union atom_value values[] = {
610 [ula_pw_name] = {.string_value = uli->ui->pw_name?
611 uli->ui->pw_name : "?"},
612 [ula_uid] = {.num_value = (long long unsigned)uli->ui->uid},
613 [ula_files] = {.num_value = 0ULL},
614 [ula_size] = {.num_value = 0ULL},
615 [ula_dirname] = {.string_value = NULL}
616 };
617 uint64_t num;
618 int ret;
619 char *dirname = NULL, *buf;
620
621 check_signals();
622 ret = -E_LOOP_COMPLETE;
623 if (!uli->count)
624 goto err;
625
626 ret = get_dir_name_of_user_row(row, uli->ui, &dirname);
627 if (ret < 0)
628 goto err;
629 if (!dir_is_admissible(dirname, uli->preg, uli->inverse_matching)) {
630 free(dirname);
631 return 1;
632 }
633 values[ula_dirname].string_value = dirname;
634
635 ret = get_num_user_files(row, uli->ui, &num);
636 if (ret < 0)
637 goto err;
638 values[ula_files].num_value = num;
639
640 ret = get_num_user_bytes(row, uli->ui, &num);
641 if (ret < 0)
642 goto err;
643 values[ula_size].num_value = num;
644
645 buf = format_items(uli->fi, values);
646 free(dirname);
647 dirname = NULL;
648 ret = output("%s", buf);
649 free(buf);
650 if (ret < 0)
651 goto err;
652 uli->count--;
653 return ret;
654 err:
655 free(dirname);
656 uli->ret = ret;
657 uli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
658 return ret;
659 }
660
661 static int print_user_list(struct user_info *ui, void *data)
662 {
663 struct user_list_format_info *ulfi = data;
664 int ret;
665 enum user_table_columns sort_column;
666 struct user_list_info uli = {
667 .ui = ui,
668 .fi = ulfi->fi,
669 .count = select_conf.limit_arg
670 };
671 union atom_value header_trailer_values[] = {
672 [ulha_uid] = {.num_value = (long long unsigned)ui->uid},
673 [ulha_pw_name] = {.string_value = ui->pw_name?
674 ui->pw_name : "?"}
675 };
676 char *buf = format_items(ulfi->header_fi, header_trailer_values);
677
678 ret = output("%s", buf);
679 free(buf);
680 if (ret < 0)
681 return ret;
682 if (select_conf.list_sort_arg == list_sort_arg_file_count)
683 sort_column = UT_FILES;
684 else
685 sort_column = UT_BYTES;
686
687 ret = compile_regex(&uli.preg, &uli.inverse_matching);
688 if (ret < 0)
689 return ret;
690 ret = adu_loop_reverse(ui->table, sort_column, &uli,
691 user_list_loop_function, &uli.ret, &uli.osl_errno);
692 free_regex(uli.preg);
693 if (ret < 0)
694 return ret;
695 buf = format_items(ulfi->trailer_fi, header_trailer_values);
696 ret = output("%s", buf);
697 free(buf);
698 return ret;
699 }
700
701 static int print_user_lists(struct format_info *fi)
702 {
703 struct user_list_format_info ulfi = {.fi = fi};
704 char *header_fmt = select_conf.header_given?
705 select_conf.header_arg : "uid %(uid)(%(pw_name)):\n";
706 char *trailer_fmt = select_conf.trailer_arg;
707 int ret = parse_format_string(header_fmt,
708 user_list_header_trailer_atoms, &ulfi.header_fi);
709 if (ret < 0)
710 return ret;
711 ret = parse_format_string(trailer_fmt,
712 user_list_header_trailer_atoms, &ulfi.trailer_fi);
713 if (ret < 0)
714 return ret;
715 ret = for_each_admissible_user(print_user_list, &ulfi);
716 free_format_info(ulfi.header_fi);
717 free_format_info(ulfi.trailer_fi);
718 return ret;
719 }
720
721 static int global_list_loop_function(struct osl_row *row, void *data)
722 {
723 struct global_list_info *gli = data;
724 union atom_value values[] = {
725 [gla_size] = {.num_value = 0ULL},
726 [gla_files] = {.num_value = 0ULL},
727 [gla_dirname] = {.string_value = NULL}
728 };
729 uint64_t num_files, num_bytes;
730 char *dirname = NULL, *buf;
731 int ret;
732
733 check_signals();
734 ret = -E_LOOP_COMPLETE;
735 if (!gli->count)
736 goto err;
737
738 ret = get_dir_name_of_row(row, &dirname);
739 if (ret < 0)
740 goto err;
741 if (!dir_is_admissible(dirname, gli->preg, gli->inverse_matching)) {
742 free(dirname);
743 return 1;
744 }
745 values[gla_dirname].string_value = dirname;
746
747 ret = get_num_files_of_row(row, &num_files);
748 if (ret < 0)
749 goto err;
750 values[gla_files].num_value = (long long unsigned)num_files;
751
752 ret = get_num_bytes_of_row(row, &num_bytes);
753 if (ret < 0)
754 goto err;
755 values[gla_size].num_value = (long long unsigned)num_bytes;
756
757 buf = format_items(gli->fi, values);
758 free(dirname);
759 dirname = NULL;
760 ret = output("%s", buf);
761 free(buf);
762 if (ret < 0)
763 goto err;
764 if (gli->count > 0)
765 gli->count--;
766 return ret;
767 err:
768 free(dirname);
769 gli->ret = ret;
770 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
771 return -1;
772 }
773
774 static int print_global_list(struct format_info *fi)
775 {
776 int ret;
777 enum dir_table_columns sort_column;
778 struct global_list_info gli = {
779 .fi = fi,
780 .count = select_conf.limit_arg
781 };
782 char *header = select_conf.header_given?
783 select_conf.header_arg : "Global list\n";
784
785 ret = output("%s", header);
786 if (ret < 0)
787 return ret;
788 if (select_conf.list_sort_arg == list_sort_arg_file_count)
789 sort_column = DT_FILES;
790 else
791 sort_column = DT_BYTES;
792 ret = compile_regex(&gli.preg, &gli.inverse_matching);
793 if (ret < 0)
794 return ret;
795 ret = adu_loop_reverse(dir_table, sort_column, &gli,
796 global_list_loop_function, &gli.ret, &gli.osl_errno);
797 free_regex(gli.preg);
798 if (ret < 0)
799 return ret;
800 return output("%s", select_conf.trailer_arg);
801 }
802
803 static int print_statistics(struct format_info *fi)
804 {
805 switch (select_conf.select_mode_arg) {
806 case select_mode_arg_global_list:
807 return print_global_list(fi);
808 case select_mode_arg_global_summary:
809 return print_global_summary(fi);
810 case select_mode_arg_user_list:
811 return print_user_lists(fi);
812 case select_mode_arg_user_summary:
813 return print_user_summary(fi);
814 default:
815 ERROR_LOG("bad select mode\n");
816 return -ERRNO_TO_ERROR(EINVAL);
817 };
818 }
819
820 static int open_pipe(char *path)
821 {
822 int p[2], ret;
823 char **argv;
824
825 ret = pipe(p);
826 if (ret < 0)
827 return ERRNO_TO_ERROR(errno);
828 ret = fork();
829 if (ret < 0)
830 return ERRNO_TO_ERROR(errno);
831 if (ret) { /* parent */
832 DEBUG_LOG("created process %d\n", ret);
833 close(p[0]);
834 output_file = fdopen(p[1], "w");
835 if (!output_file)
836 return ERRNO_TO_ERROR(errno);
837 return 1;
838 }
839 close(p[1]);
840 if (p[0] != STDIN_FILENO)
841 dup2(p[0], STDIN_FILENO);
842 DEBUG_LOG("executing %s\n", path);
843 split_args(path, &argv, " \t");
844 execvp(argv[0], argv);
845 ERROR_LOG("error executing %s: %s\n", path,
846 adu_strerror(ERRNO_TO_ERROR(errno)));
847 _exit(EXIT_FAILURE);
848 }
849
850 static int open_output_stream(void)
851 {
852 char *p;
853 int ret, flags = O_WRONLY | O_CREAT;
854
855 if (!select_conf.output_given)
856 goto use_stdout;
857 p = select_conf.output_arg;
858 switch (p[0]) {
859 case '\0': /* empty string */
860 goto bad_output_arg;
861 case '-':
862 if (!p[1]) /* "-" means stdout */
863 goto use_stdout;
864 /* string starting with a dash */
865 flags |= O_EXCL;
866 goto open_file;
867 case '>':
868 if (!p[1]) /* ">" is invalid */
869 goto bad_output_arg;
870 if (p[1] != '>') {
871 p++;
872 flags |= O_TRUNC;
873 goto open_file;
874 }
875 /* string starting with ">>" */
876 if (!p[2]) /* ">>" is invalid */
877 goto bad_output_arg;
878 flags |= O_APPEND;
879 p += 2;
880 goto open_file;
881 case '|':
882 if (!p[1]) /* "|" is invalid */
883 goto bad_output_arg;
884 p++;
885 return open_pipe(p);
886 default: /* args starts with no magic character */
887 flags |= O_EXCL;
888 goto open_file;
889 }
890 use_stdout:
891 output_file = stdout;
892 return 1;
893 bad_output_arg:
894 output_file = NULL;
895 return -E_BAD_OUTPUT_ARG;
896 open_file:
897 /*
898 * glibc's 'x' mode to fopen is not portable, so use open() and
899 * fdopen().
900 */
901 ret = open(p, flags, 0644);
902 if (ret < 0)
903 return -ERRNO_TO_ERROR(errno);
904 output_file = fdopen(ret, "w");
905 if (!output_file)
906 return -ERRNO_TO_ERROR(errno);
907 return 1;
908 }
909
910 /**
911 * Execute a select query.
912 *
913 * \param admissible_uids User IDs to take into account.
914 * \param fi Format information.
915 *
916 * Called once in select mode or for each \a run command in interactive mode.
917 *
918 * Open the output stream and the dir table if not already open. For each
919 * admissible uid, the user table is opened if necessary. After these
920 * preparations, the output according to \a select_mode and \a fi is written to
921 * the output stream.
922 *
923 * \return Standard.
924 */
925 int run_select_query(struct uid_range *admissible_uids, struct format_info *fi)
926 {
927 int ret = open_output_stream();
928
929 if (ret < 0)
930 goto out;
931 ret = open_dir_table(0);
932 if (ret < 0)
933 goto out;
934 check_signals();
935 ret = open_admissible_user_tables(admissible_uids);
936 if (ret < 0)
937 goto out;
938 check_signals();
939 ret = print_statistics(fi);
940 out:
941 if (output_file && output_file != stdout) {
942 fclose(output_file);
943 output_file = NULL;
944 }
945 return ret;
946 }
947
948 /** Default format string for global_list mode. */
949 #define GLOBAL_LIST_DFLT_FMT "%(size:r:8) %(files:r:8) %(dirname)\n"
950 /** Default format string for global_summary mode. */
951 #define GLOBAL_SUMMARY_DFLT_FMT "#directories: %(dirs), #files: %(files), size: %(size)\n"
952 /** Default format string for user_list mode. */
953 #define USER_LIST_DFLT_FMT "%(size:r:5) %(files:r:5) %(dirname)\n"
954 /** Default format string for user_summary mode. */
955 #define USER_SUMMARY_DFLT_FMT "%(pw_name:l:16) %(uid:r:6) %(dirs:r:5) %(files:r:5) %(size:r:5)\n"
956
957 static int setup_format_string(char *fmt, struct format_info **fi)
958 {
959 struct atom *atoms;
960
961 if (!fmt)
962 INFO_LOG("using default format string\n");
963 switch (select_conf.select_mode_arg) {
964 case select_mode_arg_global_list:
965 if (!fmt)
966 fmt = GLOBAL_LIST_DFLT_FMT;
967 atoms = global_list_atoms;
968 break;
969 case select_mode_arg_global_summary:
970 if (!fmt)
971 fmt = GLOBAL_SUMMARY_DFLT_FMT;
972 atoms = global_summary_atoms;
973 break;
974 case select_mode_arg_user_list:
975 if (!fmt)
976 fmt = USER_LIST_DFLT_FMT;
977 atoms = user_list_atoms;
978 break;
979 case select_mode_arg_user_summary:
980 if (!fmt)
981 fmt = USER_SUMMARY_DFLT_FMT;
982 atoms = user_summary_atoms;
983 break;
984 default:
985 ERROR_LOG("bad select mode\n");
986 return -ERRNO_TO_ERROR(EINVAL);
987 };
988 INFO_LOG("format string: %s\n", fmt);
989 return parse_format_string(fmt, atoms, fi);
990 }
991
992 /**
993 * Parse a given format string.
994 *
995 * \param string The format string to parse.
996 * \param params gengetopt parameters.
997 * \param admissible_uids The array of admissible uid ranges.
998 * \param fi The format info to be used with format_items().
999 *
1000 * If \a string is not \p NULL, it is broken down into its components using
1001 * \ref create_argv() and the resulting argument vector is passed together with
1002 * \a params to gengetopt's command line parser. If --help or --detailed-help
1003 * was specified in \a string, the corresponding help text is printed and the
1004 * function returns zero.
1005 *
1006 * Otherwise, any --uid or --user options are parsed and transformed into an
1007 * array of admissible uids which is returned via \a admissible_uids.
1008 *
1009 * Finally, the format string given by --format (or the default format string
1010 * for the given select mode if no --format option was given in \a string) is
1011 * parsed as well resulting in a format_info structure which is returned via
1012 * \a fi. The caller uses the \a fi pointer later to format each output line.
1013 *
1014 * \return Negative on errors, zero if --help or --detailed-help was given,
1015 * positive otherwise.
1016 *
1017 * \sa format_items().
1018 */
1019 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
1020 struct uid_range **admissible_uids, struct format_info **fi)
1021 {
1022 int ret, num_uid_ranges;
1023 const char **line;
1024 char *fmt = NULL;
1025
1026 if (string) {
1027 int argc;
1028 char **argv;
1029
1030 ret = create_argv(string, &argv);
1031 if (ret < 0)
1032 return ret;
1033 argc = ret;
1034 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
1035 free_argv(argv);
1036 if (ret)
1037 return -E_SYNTAX;
1038 if (select_conf.help_given || select_conf.detailed_help_given)
1039 goto help;
1040 fmt = select_conf.format_arg;
1041 }
1042 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
1043 if (ret < 0)
1044 return ret;
1045 num_uid_ranges = ret;
1046 ret = append_users(select_conf.user_arg, select_conf.user_given,
1047 admissible_uids, num_uid_ranges);
1048 if (ret < 0)
1049 return ret;
1050 return setup_format_string(fmt, fi);
1051 help:
1052 line = select_conf.detailed_help_given?
1053 select_args_info_detailed_help : select_args_info_help;
1054 if (!output_file)
1055 output_file = stdout;
1056 for (; *line; line++) {
1057 ret = output("%s\n", *line);
1058 if (ret < 0)
1059 return ret;
1060 }
1061 return 0;
1062 }
1063
1064 /**
1065 * Main function for select mode.
1066 *
1067 * \return Standard.
1068 */
1069 int com_select(void)
1070 {
1071 struct uid_range *admissible_uids = NULL;
1072 int ret;
1073 struct format_info *fi;
1074 struct select_cmdline_parser_params params = {
1075 .override = 1,
1076 .initialize = 1,
1077 .check_required = 1,
1078 .check_ambiguity = 1,
1079 .print_errors = 1
1080 };
1081
1082 ret = parse_select_options(conf.select_options_arg, &params,
1083 &admissible_uids, &fi);
1084 if (ret > 0) {
1085 ret = read_uid_file();
1086 if (ret < 0)
1087 goto out;
1088 ret = run_select_query(admissible_uids, fi);
1089 free_format_info(fi);
1090 }
1091 out:
1092 select_cmdline_parser_free(&select_conf);
1093 return ret;
1094 }