Fix the user summary.
[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 The select mode of adu. */
8
9 #include <dirent.h> /* readdir() */
10 #include "format.h"
11 #include "adu.h"
12 #include "gcc-compat.h"
13 #include "cmdline.h"
14 #include "fd.h"
15 #include "string.h"
16 #include "error.h"
17 #include "portable_io.h"
18
19 /** The decimal representation of an uint64_t never exceeds that size. */
20 #define FORMATED_VALUE_SIZE 25
21
22 #define GLOBAL_LIST_ATOMS \
23 ATOM(size, SIZE) \
24 ATOM(files, COUNT) \
25 ATOM(dirname, STRING) \
26
27 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
28 struct atom global_list_atoms[] = {
29 GLOBAL_LIST_ATOMS
30 {.name = NULL}
31 };
32 #undef ATOM
33 #define ATOM(x, y) gla_ ## x,
34 enum global_list_atoms {GLOBAL_LIST_ATOMS};
35 #undef ATOM
36
37 #define GLOBAL_SUMMARY_ATOMS \
38 ATOM(dirs, COUNT) \
39 ATOM(files, COUNT) \
40 ATOM(size, SIZE)
41
42 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
43 struct atom global_summary_atoms[] = {
44 GLOBAL_SUMMARY_ATOMS
45 {.name = NULL}
46 };
47 #undef ATOM
48 #define ATOM(x, y) gsa_ ## x,
49 enum global_summary_atoms {GLOBAL_SUMMARY_ATOMS};
50 #undef ATOM
51
52 #define USER_SUMMARY_ATOMS \
53 ATOM(pw_name, STRING) \
54 ATOM(uid, ID) \
55 ATOM(dirs, COUNT) \
56 ATOM(files, COUNT) \
57 ATOM(size, SIZE)
58
59 #define ATOM(x, y) { .name = #x, .type = AT_ ## y},
60 struct atom user_summary_atoms[] = {
61 USER_SUMMARY_ATOMS
62 {.name = NULL}
63 };
64 #undef ATOM
65 #define ATOM(x, y) usa_ ## x,
66 enum user_summary_atoms {USER_SUMMARY_ATOMS};
67 #undef ATOM
68
69
70
71 /* these get filled in by the select command. */
72 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
73
74 struct global_list_info {
75 uint32_t count;
76 int ret;
77 int osl_errno;
78 struct format_info *fi;
79 };
80
81 struct global_summary_info {
82 /** Global dir count. */
83 uint64_t num_dirs;
84 /** Global files count. */
85 uint64_t num_files;
86 /** Global bytes count. */
87 uint64_t num_bytes;
88 int ret;
89 int osl_errno;
90 };
91
92 struct user_summary_info {
93 struct user_info *ui;
94 int ret;
95 int osl_errno;
96 };
97
98
99 enum user_stats_flags {
100 USF_PRINT_DIRNAME = 1,
101 USF_PRINT_BYTES = 2,
102 USF_PRINT_FILES = 4,
103 USF_COMPUTE_SUMMARY = 8,
104 };
105
106 struct user_stats_info {
107 uint32_t count;
108 enum user_stats_flags flags;
109 int ret;
110 int osl_errno;
111 struct user_info *ui;
112 };
113
114 static const uint64_t size_unit_divisors[] = {
115 [size_unit_arg_b] = 1ULL,
116 [size_unit_arg_k] = 1024ULL,
117 [size_unit_arg_m] = 1024ULL * 1024ULL,
118 [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
119 [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
120 };
121
122 static const uint64_t count_unit_divisors[] = {
123
124 [count_unit_arg_n] = 1ULL,
125 [count_unit_arg_k] = 1000ULL,
126 [count_unit_arg_m] = 1000ULL * 1000ULL,
127 [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
128 [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
129 };
130
131 static const char size_unit_abbrevs[] = " BKMGT";
132 static const char count_unit_abbrevs[] = " kmgt";
133 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
134 uint64_t value, int print_unit, char *result)
135 {
136 enum enum_size_unit u = unit;
137 char unit_buf[2] = "\0\0";
138
139 if (unit == size_unit_arg_h) /* human readable */
140 for (u = size_unit_arg_b; u < size_unit_arg_t &&
141 value > size_unit_divisors[u + 1]; u++)
142 ; /* nothing */
143 if (print_unit)
144 unit_buf[0] = size_unit_abbrevs[u];
145 sprintf(result, "%llu%s",
146 (long long unsigned)value / size_unit_divisors[u], unit_buf);
147 return u;
148 }
149
150 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
151 uint64_t value, int print_unit, char *result)
152 {
153 enum enum_count_unit u = unit;
154 char unit_buf[2] = "\0\0";
155
156 if (unit == count_unit_arg_h) /* human readable */
157 for (u = count_unit_arg_n; u < count_unit_arg_t &&
158 value > count_unit_divisors[u + 1]; u++)
159 ; /* nothing */
160 if (print_unit)
161 unit_buf[0] = count_unit_abbrevs[u];
162 sprintf(result, "%llu%s",
163 (long long unsigned)value / count_unit_divisors[u], unit_buf);
164 return u;
165 }
166
167 static FILE *output_file;
168
169 __printf_1_2 static int output(const char const *fmt, ...)
170 {
171 va_list argp;
172 int ret;
173
174 va_start(argp, fmt);
175 ret = vfprintf(output_file, fmt, argp);
176 va_end(argp);
177 return ret < 0? -E_OUTPUT : 1;
178 }
179
180 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
181 {
182 char *result = NULL, *tmp;
183 struct osl_row *row;
184 uint64_t val = *dirnum;
185 struct osl_object obj;
186 int ret;
187 char *pfx;
188
189 again:
190 obj.data = &val;
191 obj.size = sizeof(val);
192 ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
193 if (ret < 0)
194 goto out;
195 ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
196 if (ret < 0)
197 goto out;
198 val = *(uint64_t *)obj.data;
199 ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
200 if (ret < 0)
201 goto out;
202 pfx = (select_conf.print_base_dir_given || val)? (char *)obj.data : ".";
203 tmp = make_message("%s/%s", pfx, result? result : "");
204 free(result);
205 result = tmp;
206 if (val)
207 goto again;
208 out:
209 if (ret < 0) {
210 free(result);
211 *name = NULL;
212 } else {
213 assert(result);
214 *name = result;
215 }
216 return ret;
217 }
218
219 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
220 {
221 struct osl_object obj;
222 int ret;
223
224 *name = NULL;
225 ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
226 if (ret < 0)
227 return ret;
228 return get_dir_name_by_number((uint64_t *)obj.data, name);
229 }
230
231 static int get_num_files_of_row(struct osl_row *row, uint64_t *num_files)
232 {
233 struct osl_object obj;
234 int ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
235 if (ret < 0)
236 return ret;
237 *num_files = *(uint64_t *)obj.data;
238 return 1;
239 }
240
241 static int get_num_user_files(struct osl_row *row, struct user_info *ui,
242 uint64_t *num_files)
243 {
244 struct osl_object obj;
245 int ret = osl(osl_get_object(ui->table, row, UT_FILES, &obj));
246
247 if (ret < 0)
248 return ret;
249 *num_files = *(uint64_t *)obj.data;
250 return 1;
251 }
252
253 static int get_num_bytes_of_row(struct osl_row *row, uint64_t *num_bytes)
254 {
255 struct osl_object obj;
256 int ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
257 if (ret < 0)
258 return ret;
259 *num_bytes = *(uint64_t *)obj.data;
260 return 1;
261 }
262
263 static int get_num_user_bytes(struct osl_row *row, struct user_info *ui,
264 uint64_t *num_bytes)
265 {
266 struct osl_object obj;
267 int ret = osl(osl_get_object(ui->table, row, UT_BYTES, &obj));
268
269 if (ret < 0)
270 return ret;
271 *num_bytes = *(uint64_t *)obj.data;
272 return 1;
273 }
274
275
276 static int user_stats_loop_function(struct osl_row *row, void *data)
277 {
278 struct user_stats_info *usi = data;
279 struct osl_object obj;
280 int ret, summary = usi->flags & USF_COMPUTE_SUMMARY;
281 char formated_value[FORMATED_VALUE_SIZE];
282
283 check_signals();
284 if (!usi->count && !summary) {
285 ret = -E_LOOP_COMPLETE;
286 goto err;
287 }
288 if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
289 uint64_t files;
290 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
291 if (ret < 0)
292 goto err;
293 files = *(uint64_t *)obj.data;
294 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
295 format_count_value(select_conf.count_unit_arg, files,
296 select_conf.count_unit_arg == count_unit_arg_h,
297 formated_value);
298 ret = output("\t%s%s", formated_value,
299 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
300 "\t" : "\n");
301 if (ret < 0)
302 goto err;
303 }
304 if (summary)
305 usi->ui->files += files;
306 }
307 if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
308 uint64_t bytes;
309 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
310 if (ret < 0)
311 goto err;
312 bytes = *(uint64_t *)obj.data;
313 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
314 format_size_value(select_conf.size_unit_arg, bytes,
315 select_conf.size_unit_arg == size_unit_arg_h,
316 formated_value);
317 ret = output("%s%s%s",
318 (usi->flags & USF_PRINT_FILES)? "" : "\t",
319 formated_value,
320 usi->flags & USF_PRINT_DIRNAME? "\t" : "\n"
321 );
322 if (ret < 0)
323 goto err;
324 }
325 if (summary) {
326 usi->ui->bytes += bytes;
327 usi->ui->dirs++;
328 }
329
330 }
331 if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
332 char *dirname;
333 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
334 if (ret < 0)
335 goto err;
336 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
337 if (ret < 0)
338 goto err;
339 ret = output("%s%s\n",
340 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
341 dirname);
342 free(dirname);
343 if (ret < 0)
344 goto err;
345 }
346 if (usi->count > 0)
347 usi->count--;
348 return 1;
349 err:
350 usi->ret = ret;
351 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
352 return -1;
353 }
354
355 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
356 {
357 if (ret >= 0)
358 return ret;
359 assert(ret == -E_OSL);
360 if (osl_errno != E_OSL_LOOP)
361 /* error not caused by loop function returning negative. */
362 return ret;
363 assert(loop_ret < 0);
364 if (loop_ret == -E_LOOP_COMPLETE) /* no error */
365 return 1;
366 if (loop_ret == -E_OSL) { /* osl error in loop function */
367 assert(loop_osl_errno);
368 osl_errno = loop_osl_errno;
369 }
370 return loop_ret;
371 }
372
373 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
374 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
375 {
376 int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
377 return check_loop_return(ret, *loop_ret, *loop_osl_errno);
378 }
379
380 static int global_summary_loop_function(struct osl_row *row, void *data)
381 {
382 struct global_summary_info *gsi = data;
383 int ret;
384 uint64_t num;
385
386 ret = get_num_files_of_row(row, &num);
387 if (ret < 0)
388 goto err;
389 gsi->num_files += num;
390
391 ret = get_num_bytes_of_row(row, &num);
392 if (ret < 0)
393 goto err;
394 gsi->num_bytes += num;
395 gsi->num_dirs++;
396 return 1;
397 err:
398 gsi->ret = ret;
399 gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
400 return -1;
401 }
402
403 static int print_global_summary(struct format_info *fi)
404 {
405 int ret;
406 char *buf;
407 struct global_summary_info gsi = {.num_dirs = 0};
408
409 union atom_value values[] = {
410 [gsa_dirs] = {.num_value = 0ULL},
411 [gsa_files] = {.num_value = 0ULL},
412 [gsa_size] = {.num_value = 0ULL}
413 };
414
415 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
416 global_summary_loop_function, &gsi.ret, &gsi.osl_errno);
417 if (ret < 0)
418 return ret;
419 values[gsa_dirs].num_value = (long long unsigned)gsi.num_dirs;
420 values[gsa_files].num_value = (long long unsigned)gsi.num_files;
421 values[gsa_size].num_value = (long long unsigned)gsi.num_bytes;
422 if (!select_conf.no_headers_given) {
423 ret = output("Global summary\n");
424 if (ret < 0)
425 return ret;
426 }
427 buf = format_items(fi, values);
428 ret = output("%s", buf);
429 free(buf);
430 return ret;
431 }
432
433 static int user_summary_loop_function(struct osl_row *row, void *data)
434 {
435 struct user_summary_info *usi = data;
436 uint64_t num;
437 int ret;
438
439 ret = get_num_user_files(row, usi->ui, &num);
440 if (ret < 0)
441 goto err;
442 usi->ui->files += num;
443 ret = get_num_user_bytes(row, usi->ui, &num);
444 if (ret < 0)
445 goto err;
446 usi->ui->bytes += num;
447 usi->ui->dirs++;
448 return 1;
449 err:
450 usi->ret = ret;
451 usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
452 return -1;
453 }
454
455 static int compute_user_summary(struct user_info *ui, __a_unused void *data)
456 {
457 struct user_summary_info usi = {.ui = ui};
458
459 return adu_loop_reverse(ui->table, UT_BYTES, &usi, user_summary_loop_function,
460 &usi.ret, &usi.osl_errno);
461 }
462
463 static int print_user_summary_line(struct user_info *ui, void *data)
464 {
465 struct format_info *fi = data;
466 union atom_value values[] = {
467 [usa_pw_name] = {.string_value = ui->pw_name?
468 ui->pw_name : "?"},
469 [usa_uid] = {.num_value = (long long unsigned)ui->uid},
470 [usa_dirs] = {.num_value = (long long unsigned)ui->dirs},
471 [usa_files] = {.num_value = (long long unsigned)ui->files},
472 [usa_size] = {.num_value = (long long unsigned)ui->bytes}
473 };
474 char *buf;
475 int ret;
476
477 buf = format_items(fi, values);
478 ret = output("%s", buf);
479 free(buf);
480 return ret;
481 }
482
483 static int name_comp(const void *a, const void *b)
484 {
485 char *x = ((struct user_info *)a)->pw_name;
486 char *y = ((struct user_info *)b)->pw_name;
487
488 if (!x)
489 return 1;
490 if (!y)
491 return -1;
492 return strcmp(x, y);
493 }
494
495 static int uid_comp(const void *a, const void *b)
496 {
497 return -NUM_COMPARE(((struct user_info *)a)->uid,
498 ((struct user_info *)b)->uid);
499 }
500
501 static int dir_count_comp(const void *a, const void *b)
502 {
503 return NUM_COMPARE(((struct user_info *)a)->dirs,
504 ((struct user_info *)b)->dirs);
505 }
506
507 static int file_count_comp(const void *a, const void *b)
508 {
509 return NUM_COMPARE(((struct user_info *)a)->files,
510 ((struct user_info *)b)->files);
511 }
512
513 static int size_comp(const void *a, const void *b)
514 {
515 return NUM_COMPARE(((struct user_info *)a)->bytes,
516 ((struct user_info *)b)->bytes);
517 }
518
519 /*
520 * The comparators for sorting the user summary.
521 *
522 * This is an array of pointers to functions taking two constant void *
523 * pointers and returning an int.
524 */
525 static int (*summary_comparators[])(const void *, const void *) = {
526 [user_summary_sort_arg_name] = name_comp,
527 [user_summary_sort_arg_uid] = uid_comp,
528 [user_summary_sort_arg_dir_count] = dir_count_comp,
529 [user_summary_sort_arg_file_count] = file_count_comp,
530 [user_summary_sort_arg_size] = size_comp,
531 };
532
533 static int print_user_summary(struct format_info *fi)
534 {
535 if (!select_conf.no_headers_given) {
536 int ret = output("User summary\n");
537 if (ret < 0)
538 return ret;
539 }
540 int ret = for_each_admissible_user(compute_user_summary, fi);
541 if (ret < 0)
542 return ret;
543 sort_hash_table(summary_comparators[select_conf.user_summary_sort_arg]);
544 return for_each_admissible_user(print_user_summary_line, fi);
545 }
546
547 static int print_user_list(struct user_info *ui, __a_unused void *data)
548 {
549 int ret;
550 struct user_stats_info usi;
551 enum enum_user_list ula = select_conf.user_list_arg;
552 int print_size_list = (ula == user_list_arg_size
553 || ula == user_list_arg_both);
554
555 if (print_size_list) {
556 usi.count = select_conf.limit_arg;
557 usi.ui = ui;
558 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
559 if (!select_conf.no_headers_given) {
560 ret = output("%s (uid %u), by size%s:\n",
561 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
562 size_unit_buf);
563 if (ret < 0)
564 return ret;
565 }
566 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
567 &usi.ret, &usi.osl_errno);
568 if (ret < 0)
569 return ret;
570 ret = output("\n");
571 if (ret < 0)
572 return ret;
573 }
574 if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
575 if (!select_conf.no_headers_given) {
576 ret = output("%s (uid %u), by file count%s:\n",
577 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
578 count_unit_buf);
579 if (ret < 0)
580 return ret;
581 }
582 usi.count = select_conf.limit_arg,
583 usi.ui = ui;
584 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
585 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
586 &usi.ret, &usi.osl_errno);
587 if (ret < 0)
588 return ret;
589 ret = output("\n");
590 if (ret < 0)
591 return ret;
592 }
593 if (ula == user_list_arg_none && !select_conf.no_user_summary_given) {
594 usi.count = select_conf.limit_arg;
595 usi.ui = ui;
596 usi.flags = USF_COMPUTE_SUMMARY;
597 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
598 &usi.ret, &usi.osl_errno);
599 if (ret < 0)
600 return ret;
601 }
602 return 1;
603 }
604
605 static int print_user_lists(void)
606 {
607 return for_each_admissible_user(print_user_list, NULL);
608 }
609
610 static int global_list_loop_function(struct osl_row *row, void *data)
611 {
612 struct global_list_info *gli = data;
613 union atom_value values[] = {
614 [gla_size] = {.num_value = 0ULL},
615 [gla_files] = {.num_value = 0ULL},
616 [gla_dirname] = {.string_value = NULL}
617 };
618 uint64_t num_files, num_bytes;
619 char *dirname, *buf;
620 int ret;
621
622 check_signals();
623 ret = -E_LOOP_COMPLETE;
624 if (!gli->count)
625 goto err;
626
627 ret = get_num_files_of_row(row, &num_files);
628 if (ret < 0)
629 goto err;
630 values[gla_files].num_value = (long long unsigned)num_files;
631
632 ret = get_num_bytes_of_row(row, &num_bytes);
633 if (ret < 0)
634 goto err;
635 values[gla_size].num_value = (long long unsigned)num_bytes;
636
637 ret = get_dir_name_of_row(row, &dirname);
638 if (ret < 0)
639 goto err;
640 values[gla_dirname].string_value = dirname;
641
642 buf = format_items(gli->fi, values);
643 free(dirname);
644 ret = output("%s", buf);
645 free(buf);
646 if (gli->count > 0)
647 gli->count--;
648 return ret;
649 err:
650 gli->ret = ret;
651 gli->osl_errno = (ret == -E_OSL)? osl_errno : 0;
652 return -1;
653 }
654
655 static int print_global_list(struct format_info *fi)
656 {
657 int ret;
658 enum dir_table_columns sort_column = DT_BYTES;
659 struct global_list_info gli = {
660 .fi = fi,
661 .count = select_conf.limit_arg
662 };
663
664 if (!select_conf.no_headers_given) {
665 ret = output("Global list\n");
666 if (ret < 0)
667 return ret;
668 }
669 if (select_conf.list_sort_arg == list_sort_arg_file_count)
670 sort_column = DT_FILES;
671 return adu_loop_reverse(dir_table, sort_column, &gli,
672 global_list_loop_function, &gli.ret, &gli.osl_errno);
673 }
674
675 static int print_statistics(struct select_format_info *sli)
676 {
677 int ret;
678
679 switch (select_conf.select_mode_arg) {
680 case select_mode_arg_global_list:
681 ret = print_global_list(sli->global_list_fi);
682 free_format_info(sli->global_list_fi);
683 return ret;
684 case select_mode_arg_global_summary:
685 ret = print_global_summary(sli->global_summary_fi);
686 free_format_info(sli->global_summary_fi);
687 return ret;
688 case select_mode_arg_user_list:
689 ret = print_user_lists();
690 return ret;
691 case select_mode_arg_user_summary:
692 ret = print_user_summary(sli->user_summary_fi);
693 free_format_info(sli->user_summary_fi);
694 return ret;
695 };
696 ERROR_LOG("bad select mode\n");
697 return ERRNO_TO_ERROR(-EINVAL);
698 }
699
700 static int read_uid_file(struct uid_range *admissible_uids)
701 {
702 size_t size;
703 uint32_t n;
704 char *filename = get_uid_list_name(), *map;
705 int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
706 unsigned bits;
707
708 if (ret < 0) {
709 INFO_LOG("failed to map %s\n", filename);
710 free(filename);
711 return ret;
712 }
713 num_uids = size / 4;
714 INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
715 free(filename);
716 /*
717 * Compute number of hash table bits. The hash table size must be a
718 * power of two and larger than the number of uids.
719 */
720 bits = 2;
721 while (1 << bits < num_uids)
722 bits++;
723 create_hash_table(bits);
724 for (n = 0; n < num_uids; n++) {
725 uint32_t uid = read_u32(map + n * sizeof(uid));
726 ret = search_uid(uid, admissible_uids, OPEN_USER_TABLE, NULL);
727 if (ret < 0)
728 goto out;
729 }
730 out:
731 adu_munmap(map, size);
732 return ret;
733 }
734
735 int run_select_query(struct uid_range *admissible_uids,
736 struct select_format_info *sfi)
737 {
738 int ret;
739
740 if (select_conf.output_given && strcmp(select_conf.output_arg, "-")) {
741 output_file = fopen(select_conf.output_arg, "w");
742 if (!output_file)
743 return -ERRNO_TO_ERROR(errno);
744 } else
745 output_file = stdout;
746
747 if (select_conf.count_unit_arg != count_unit_arg_h)
748 count_unit_buf[1] = count_unit_abbrevs[select_conf.count_unit_arg];
749 else
750 count_unit_buf[0] = '\0';
751 if (select_conf.size_unit_arg != size_unit_arg_h)
752 size_unit_buf[1] = size_unit_abbrevs[select_conf.size_unit_arg];
753 else
754 size_unit_buf[0] = '\0';
755
756 ret = open_dir_table(0);
757 if (ret < 0)
758 goto out;
759 check_signals();
760 ret = read_uid_file(admissible_uids);
761 if (ret < 0)
762 goto out;
763 check_signals();
764 ret = print_statistics(sfi);
765 out:
766 close_all_tables();
767 if (output_file != stdout)
768 fclose(output_file);
769 return ret;
770 }
771
772 /* return: < 0: error, >0: OK, == 0: help given */
773 int parse_select_options(char *string, struct select_cmdline_parser_params *params,
774 struct uid_range **admissible_uids, struct select_format_info *sfi)
775 {
776 int ret;
777 const char **line;
778
779 if (conf.select_options_given) {
780 int argc;
781 char **argv;
782
783 ret = create_argv(string, &argv);
784 if (ret < 0)
785 return ret;
786 argc = ret;
787 ret = select_cmdline_parser_ext(argc, argv, &select_conf, params);
788 free_argv(argv);
789 if (ret)
790 return -E_SYNTAX;
791 if (select_conf.help_given || select_conf.detailed_help_given)
792 goto help;
793
794 }
795 ret = parse_uid_arg(select_conf.uid_arg, admissible_uids);
796 if (ret < 0)
797 return ret;
798 ret = parse_format_string(select_conf.user_summary_format_arg,
799 user_summary_atoms, &sfi->user_summary_fi);
800 if (ret < 0)
801 return ret;
802 ret = parse_format_string(select_conf.global_summary_format_arg,
803 global_summary_atoms, &sfi->global_summary_fi);
804 if (ret < 0)
805 goto global_summary_err;
806 ret = parse_format_string(select_conf.global_list_format_arg,
807 global_list_atoms, &sfi->global_list_fi);
808 if (ret < 0)
809 goto global_list_err;
810 return 1;
811 global_list_err:
812 free_format_info(sfi->global_summary_fi);
813 global_summary_err:
814 free_format_info(sfi->user_summary_fi);
815 return ret;
816 help:
817 line = select_conf.detailed_help_given?
818 select_args_info_detailed_help : select_args_info_help;
819 if (!output_file)
820 output_file = stdout;
821 for (; *line; line++) {
822 ret = output("%s\n", *line);
823 if (ret < 0)
824 return ret;
825 }
826 return 0;
827 }
828
829 int com_select(void)
830 {
831 struct uid_range *admissible_uids = NULL;
832 struct select_format_info sfi;
833 int ret;
834 struct select_cmdline_parser_params params = {
835 .override = 1,
836 .initialize = 1,
837 .check_required = 1,
838 .check_ambiguity = 1,
839 .print_errors = 1
840 };
841
842 select_cmdline_parser_init(&select_conf);
843 ret = parse_select_options(conf.select_options_arg, &params,
844 &admissible_uids, &sfi);
845 if (ret <= 0) /* do not run query if help was given */
846 return ret;
847 return run_select_query(admissible_uids, &sfi);
848 }