Add large file support.
[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 "adu.h"
11 #include "gcc-compat.h"
12 #include "cmdline.h"
13 #include "fd.h"
14 #include "string.h"
15 #include "error.h"
16 #include "portable_io.h"
17
18 /** Global dir count. */
19 static uint64_t num_dirs;
20 /** Global files count. */
21 static uint64_t num_files;
22 /** Global bytes count. */
23 static uint64_t num_bytes;
24
25 /** The decimal representation of an uint64_t never exceeds that size. */
26 #define FORMATED_VALUE_SIZE 25
27
28 /* these get filled in by the select command. */
29 static char count_unit_buf[4] = "( )", size_unit_buf[4] = "( )";
30
31 enum global_stats_flags {
32         GSF_PRINT_DIRNAME = 1,
33         GSF_PRINT_BYTES = 2,
34         GSF_PRINT_FILES = 4,
35         GSF_COMPUTE_SUMMARY = 8,
36 };
37
38 struct global_stats_info {
39         uint32_t count;
40         int ret;
41         int osl_errno;
42         enum global_stats_flags flags;
43 };
44
45 enum user_stats_flags {
46         USF_PRINT_DIRNAME = 1,
47         USF_PRINT_BYTES = 2,
48         USF_PRINT_FILES = 4,
49         USF_COMPUTE_SUMMARY = 8,
50 };
51
52 struct user_stats_info {
53         uint32_t count;
54         enum user_stats_flags flags;
55         int ret;
56         int osl_errno;
57         struct user_info *ui;
58 };
59
60 static const uint64_t size_unit_divisors[] = {
61         [size_unit_arg_b] = 1ULL,
62         [size_unit_arg_k] = 1024ULL,
63         [size_unit_arg_m] = 1024ULL * 1024ULL,
64         [size_unit_arg_g] = 1024ULL * 1024ULL * 1024ULL,
65         [size_unit_arg_t] = 1024ULL * 1024ULL * 1024ULL * 1024ULL,
66 };
67
68 static const uint64_t count_unit_divisors[] = {
69
70         [count_unit_arg_n] = 1ULL,
71         [count_unit_arg_k] = 1000ULL,
72         [count_unit_arg_m] = 1000ULL * 1000ULL,
73         [count_unit_arg_g] = 1000ULL * 1000ULL * 1000ULL,
74         [count_unit_arg_t] = 1000ULL * 1000ULL * 1000ULL * 1000ULL,
75 };
76
77 static const char size_unit_abbrevs[] = " BKMGT";
78 static const char count_unit_abbrevs[] = "  kmgt";
79
80 static enum enum_size_unit format_size_value(enum enum_size_unit unit,
81                 uint64_t value, int print_unit, char *result)
82 {
83         enum enum_size_unit u = unit;
84         char unit_buf[2] = "\0\0";
85
86         if (unit == size_unit_arg_h) /* human readable */
87                 for (u = size_unit_arg_b; u < size_unit_arg_t &&
88                                 value > size_unit_divisors[u + 1]; u++)
89                         ; /* nothing */
90         if (print_unit)
91                 unit_buf[0] = size_unit_abbrevs[u];
92         sprintf(result, "%llu%s",
93                 (long long unsigned)value / size_unit_divisors[u], unit_buf);
94         return u;
95 }
96
97 static enum enum_count_unit format_count_value(enum enum_count_unit unit,
98                 uint64_t value, int print_unit, char *result)
99 {
100         enum enum_count_unit u = unit;
101         char unit_buf[2] = "\0\0";
102
103         if (unit == count_unit_arg_h) /* human readable */
104                 for (u = count_unit_arg_n; u < count_unit_arg_t &&
105                                 value > count_unit_divisors[u + 1]; u++)
106                         ; /* nothing */
107         if (print_unit)
108                 unit_buf[0] = count_unit_abbrevs[u];
109         sprintf(result, "%llu%s",
110                 (long long unsigned)value / count_unit_divisors[u], unit_buf);
111         return u;
112 }
113
114 static int get_dir_name_by_number(uint64_t *dirnum, char **name)
115 {
116         char *result = NULL, *tmp;
117         struct osl_row *row;
118         uint64_t val = *dirnum;
119         struct osl_object obj;
120         int ret;
121         char *pfx;
122
123 again:
124         obj.data = &val;
125         obj.size = sizeof(val);
126         ret = osl(osl_get_row(dir_table, DT_NUM, &obj, &row));
127         if (ret < 0)
128                 goto out;
129         ret = osl(osl_get_object(dir_table, row, DT_PARENT_NUM, &obj));
130         if (ret < 0)
131                 goto out;
132         val = *(uint64_t *)obj.data;
133         ret = osl(osl_get_object(dir_table, row, DT_NAME, &obj));
134         if (ret < 0)
135                 goto out;
136         pfx = (conf.print_base_dir_given || val)? (char *)obj.data :  ".";
137         tmp = make_message("%s/%s", pfx, result? result : "");
138         free(result);
139         result = tmp;
140         if (val)
141                 goto again;
142 out:
143         if (ret < 0) {
144                 free(result);
145                 *name = NULL;
146         } else {
147                 assert(result);
148                 *name = result;
149         }
150         return ret;
151 }
152
153 static int get_dir_name_of_row(struct osl_row *dir_table_row, char **name)
154 {
155         struct osl_object obj;
156         int ret;
157
158         *name = NULL;
159         ret = osl(osl_get_object(dir_table, dir_table_row, DT_NUM, &obj));
160         if (ret < 0)
161                 return ret;
162         return get_dir_name_by_number((uint64_t *)obj.data, name);
163 }
164
165 static int user_stats_loop_function(struct osl_row *row, void *data)
166 {
167         struct user_stats_info *usi = data;
168         struct osl_object obj;
169         int ret, summary = usi->flags & GSF_COMPUTE_SUMMARY;
170         char formated_value[FORMATED_VALUE_SIZE];
171
172         check_signals();
173         if (!usi->count && !summary) {
174                 ret = -E_LOOP_COMPLETE;
175                 goto err;
176         }
177         if (summary || (usi->count && (usi->flags & USF_PRINT_FILES))) {
178                 uint64_t files;
179                 ret = osl(osl_get_object(usi->ui->table, row, UT_FILES, &obj));
180                 if (ret < 0)
181                         goto err;
182                 files = *(uint64_t *)obj.data;
183                 if (usi->count && (usi->flags & USF_PRINT_FILES)) {
184                         format_count_value(conf.count_unit_arg, files,
185                                 conf.count_unit_arg == count_unit_arg_h,
186                                 formated_value);
187                         printf("\t%s%s", formated_value,
188                                 (usi->flags & (USF_PRINT_BYTES | USF_PRINT_DIRNAME))?
189                                         "\t" : "\n"
190                         );
191                 }
192                 if (summary)
193                         usi->ui->files += files;
194         }
195         if (summary || (usi->count && (usi->flags & USF_PRINT_BYTES))) {
196                 uint64_t bytes;
197                 ret = osl(osl_get_object(usi->ui->table, row, UT_BYTES, &obj));
198                 if (ret < 0)
199                         goto err;
200                 bytes = *(uint64_t *)obj.data;
201                 if (usi->count && (usi->flags & USF_PRINT_BYTES)) {
202                         format_size_value(conf.size_unit_arg, bytes,
203                                 conf.size_unit_arg == size_unit_arg_h,
204                                 formated_value);
205                         printf("%s%s%s",
206                                 (usi->flags & USF_PRINT_FILES)? "" : "\t",
207                                 formated_value,
208                                 usi->flags & USF_PRINT_DIRNAME?  "\t" : "\n"
209                         );
210                 }
211                 if (summary) {
212                         usi->ui->bytes += bytes;
213                         usi->ui->dirs++;
214                 }
215
216         }
217         if (usi->count && (usi->flags & USF_PRINT_DIRNAME)) {
218                 char *dirname;
219                 ret = osl(osl_get_object(usi->ui->table, row, UT_DIR_NUM, &obj));
220                 if (ret < 0)
221                         goto err;
222                 ret = get_dir_name_by_number((uint64_t *)obj.data, &dirname);
223                 if (ret < 0)
224                         goto err;
225                 printf("%s%s\n",
226                         (usi->flags & (USF_PRINT_BYTES | USF_PRINT_FILES))? "" : "\t",
227                         dirname);
228                 free(dirname);
229         }
230         if (usi->count > 0)
231                 usi->count--;
232         return 1;
233 err:
234         usi->ret = ret;
235         usi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
236         return -1;
237 }
238
239 static int global_stats_loop_function(struct osl_row *row, void *data)
240 {
241         struct global_stats_info *gsi = data;
242         struct osl_object obj;
243         char *dirname, formated_value[FORMATED_VALUE_SIZE];
244         int ret, summary = gsi->flags & GSF_COMPUTE_SUMMARY;
245
246         check_signals();
247         if (!gsi->count && !summary) {
248                 ret = -E_LOOP_COMPLETE;
249                 goto err;
250         }
251         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_FILES))) {
252                 uint64_t files;
253                 ret = osl(osl_get_object(dir_table, row, DT_FILES, &obj));
254                 if (ret < 0)
255                         goto err;
256                 files = *(uint64_t *)obj.data;
257                 if (gsi->count && (gsi->flags & GSF_PRINT_FILES)) {
258                         format_count_value(conf.count_unit_arg, files,
259                                 conf.count_unit_arg == count_unit_arg_h,
260                                 formated_value);
261                         printf("\t%s%s", formated_value,
262                                 (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_DIRNAME))?
263                                 "\t" : "\n");
264                 }
265                 if (summary)
266                         num_files += files;
267         }
268         if (summary || (gsi->count && (gsi->flags & GSF_PRINT_BYTES))) {
269                 uint64_t bytes;
270                 ret = osl(osl_get_object(dir_table, row, DT_BYTES, &obj));
271                 if (ret < 0)
272                         goto err;
273                 bytes = *(uint64_t *)obj.data;
274                 if (gsi->count && (gsi->flags & GSF_PRINT_BYTES)) {
275                         format_size_value(conf.size_unit_arg, bytes,
276                                 conf.size_unit_arg == size_unit_arg_h,
277                                 formated_value);
278                         printf("%s%s%s",
279                                 (gsi->flags & GSF_PRINT_FILES)? "" : "\t",
280                                 formated_value,
281                                 (gsi->flags & GSF_PRINT_DIRNAME)? "\t" : "\n"
282                         );
283                 }
284                 if (summary) {
285                         num_bytes += bytes;
286                         num_dirs++;
287                 }
288         }
289         if (gsi->count && (gsi->flags & GSF_PRINT_DIRNAME)) {
290                 ret = get_dir_name_of_row(row, &dirname);
291                 if (ret < 0)
292                         goto err;
293                 printf("%s%s\n",
294                         (gsi->flags & (GSF_PRINT_BYTES | GSF_PRINT_FILES))? "" : "\t",
295                         dirname);
296                 free(dirname);
297         }
298         if (gsi->count > 0)
299                 gsi->count--;
300         return 1;
301 err:
302         gsi->ret = ret;
303         gsi->osl_errno = (ret == -E_OSL)? osl_errno : 0;
304         return -1;
305 }
306
307 static int check_loop_return(int ret, int loop_ret, int loop_osl_errno)
308 {
309         if (ret >= 0)
310                 return ret;
311         assert(ret == -E_OSL);
312         if (osl_errno != E_OSL_LOOP)
313                 /* error not caused by loop function returning negative. */
314                 return ret;
315         assert(loop_ret < 0);
316         if (loop_ret == -E_LOOP_COMPLETE) /* no error */
317                 return 1;
318         if (loop_ret == -E_OSL) { /* osl error in loop function */
319                 assert(loop_osl_errno);
320                 osl_errno = loop_osl_errno;
321         }
322         return loop_ret;
323 }
324
325 static int adu_loop_reverse(struct osl_table *t, unsigned col_num, void *private_data,
326                 osl_rbtree_loop_func *func, int *loop_ret, int *loop_osl_errno)
327 {
328         int ret = osl(osl_rbtree_loop_reverse(t, col_num, private_data, func));
329         return check_loop_return(ret, *loop_ret, *loop_osl_errno);
330 }
331
332 static void print_global_summary(void)
333 {
334         char d[FORMATED_VALUE_SIZE], f[FORMATED_VALUE_SIZE],
335                 s[FORMATED_VALUE_SIZE];
336         enum enum_count_unit ud, uf;
337         enum enum_size_unit us;
338
339         if (conf.no_global_summary_given)
340                 return;
341         ud = format_count_value(conf.count_unit_arg, num_dirs, 0, d);
342         uf = format_count_value(conf.count_unit_arg, num_files, 0, f);
343         us = format_size_value(conf.size_unit_arg, num_bytes, 0, s);
344
345         if (!conf.no_headers_given)
346                 printf("Global summary "
347                         "(dirs(%c)/files(%c)/size(%c))\n",
348                         count_unit_abbrevs[ud],
349                         count_unit_abbrevs[uf],
350                         size_unit_abbrevs[us]
351                 );
352         printf("\t%s\t%s\t%s\n\n", d, f, s);
353 }
354
355 static int print_user_summary_line(struct user_info *ui, __a_unused void *data)
356 {
357         char formated_dir_count[FORMATED_VALUE_SIZE],
358                 formated_file_count[FORMATED_VALUE_SIZE],
359                 formated_bytes[FORMATED_VALUE_SIZE ];
360
361         format_count_value(conf.count_unit_arg, ui->dirs,
362                 conf.count_unit_arg == count_unit_arg_h,
363                 formated_dir_count);
364         format_count_value(conf.count_unit_arg, ui->files,
365                 conf.count_unit_arg == count_unit_arg_h,
366                 formated_file_count);
367         format_size_value(conf.size_unit_arg, ui->bytes,
368                 conf.size_unit_arg == size_unit_arg_h,
369                 formated_bytes);
370         printf("\t%s\t%u\t%s\t%s\t%s\n",
371                 ui->pw_name? ui->pw_name : "?",
372                 (unsigned)ui->uid,
373                 formated_dir_count,
374                 formated_file_count,
375                 formated_bytes
376         );
377         return 1;
378 }
379
380 static int name_comp(const void *a, const void *b)
381 {
382         char *x = ((struct user_info *)a)->pw_name;
383         char *y = ((struct user_info *)b)->pw_name;
384
385         if (!x)
386                 return 1;
387         if (!y)
388                 return -1;
389         return strcmp(x, y);
390 }
391
392 static int uid_comp(const void *a, const void *b)
393 {
394         return -NUM_COMPARE(((struct user_info *)a)->uid,
395                 ((struct user_info *)b)->uid);
396 }
397
398 static int dir_count_comp(const void *a, const void *b)
399 {
400         return NUM_COMPARE(((struct user_info *)a)->dirs,
401                 ((struct user_info *)b)->dirs);
402 }
403
404 static int file_count_comp(const void *a, const void *b)
405 {
406         return NUM_COMPARE(((struct user_info *)a)->files,
407                 ((struct user_info *)b)->files);
408 }
409
410 static int size_comp(const void *a, const void *b)
411 {
412         return NUM_COMPARE(((struct user_info *)a)->bytes,
413                 ((struct user_info *)b)->bytes);
414 }
415
416 /*
417  * The comparators for sorting the user summary.
418  *
419  * This is an array of pointers to functions taking two constant void *
420  * pointers and returning an int.
421  */
422 static int (*summary_comparators[])(const void *, const void *) = {
423         [user_summary_sort_arg_name] = name_comp,
424         [user_summary_sort_arg_uid] = uid_comp,
425         [user_summary_sort_arg_dir_count] = dir_count_comp,
426         [user_summary_sort_arg_file_count] = file_count_comp,
427         [user_summary_sort_arg_size] = size_comp,
428 };
429
430 static void print_user_summary(void)
431 {
432         if (conf.no_user_summary_given)
433                 return;
434         if (!conf.no_headers_given)
435                 printf("User summary "
436                         "(pw_name/uid/dirs%s/files%s/size%s):\n",
437                         count_unit_buf, count_unit_buf, size_unit_buf);
438         sort_hash_table(summary_comparators[conf.user_summary_sort_arg]);
439         for_each_admissible_user(print_user_summary_line, NULL);
440 }
441
442 static int print_user_list(struct user_info *ui, __a_unused void *data)
443 {
444         int ret;
445         struct user_stats_info usi;
446         enum enum_user_list ula = conf.user_list_arg;
447         int print_size_list = (ula == user_list_arg_size
448                 || ula == user_list_arg_both);
449
450         if (print_size_list) {
451                 usi.count = conf.limit_arg;
452                 usi.ui = ui;
453                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
454                 if (!conf.no_headers_given)
455                         printf("%s (uid %u), by size%s:\n",
456                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
457                                 size_unit_buf);
458                 ret = adu_loop_reverse(ui->table, UT_BYTES, &usi, user_stats_loop_function,
459                         &usi.ret, &usi.osl_errno);
460                 if (ret < 0)
461                         return ret;
462                 printf("\n");
463         }
464         if (ula == user_list_arg_file_count || ula == user_list_arg_both) {
465                 if (!conf.no_headers_given)
466                         printf("%s (uid %u), by file count%s:\n",
467                                 ui->pw_name? ui->pw_name : "?", (unsigned)ui->uid,
468                                 count_unit_buf);
469                 usi.count = conf.limit_arg,
470                 usi.ui = ui;
471                 usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
472                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
473                         &usi.ret, &usi.osl_errno);
474                 if (ret < 0)
475                         return ret;
476                 printf("\n");
477         }
478         if (ula == user_list_arg_none && !conf.no_user_summary_given) {
479                 usi.count = conf.limit_arg;
480                 usi.ui = ui;
481                 usi.flags = USF_COMPUTE_SUMMARY;
482                 ret = adu_loop_reverse(ui->table, UT_FILES, &usi, user_stats_loop_function,
483                         &usi.ret, &usi.osl_errno);
484                 if (ret < 0)
485                         return ret;
486         }
487         return 1;
488 }
489
490 static int print_user_lists(void)
491 {
492         return for_each_admissible_user(print_user_list, NULL);
493 }
494
495 static int print_global_lists(void)
496 {
497         struct global_stats_info gsi;
498         int ret;
499         enum enum_global_list gla = conf.global_list_arg;
500         int print_size_list = (gla == global_list_arg_size
501                 || gla == global_list_arg_both);
502
503         if (print_size_list) {
504                 gsi.count = conf.limit_arg;
505                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY;
506                 if (!conf.no_headers_given)
507                         printf("By size%s:\n", size_unit_buf);
508                 ret = adu_loop_reverse(dir_table, DT_BYTES, &gsi,
509                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
510                 if (ret < 0)
511                         return ret;
512                 printf("\n");
513         }
514         if (gla == global_list_arg_file_count || gla == global_list_arg_both) {
515                 gsi.count = conf.limit_arg;
516                 gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
517                 if (!print_size_list)
518                         gsi.flags |= GSF_COMPUTE_SUMMARY;
519                 if (!conf.no_headers_given)
520                         printf("By file count%s:\n", count_unit_buf);
521                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
522                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
523                 if (ret < 0)
524                         return ret;
525                 printf("\n");
526         }
527         if (gla == global_list_arg_none && !conf.no_global_summary_given) {
528                 /* must compute summary */
529                 gsi.count = conf.limit_arg;
530                 gsi.flags = GSF_COMPUTE_SUMMARY;
531                 ret = adu_loop_reverse(dir_table, DT_FILES, &gsi,
532                         global_stats_loop_function, &gsi.ret, &gsi.osl_errno);
533                 if (ret < 0)
534                         return ret;
535         }
536         return 1;
537 }
538
539 static int print_statistics(void)
540 {
541         int ret;
542
543         ret = print_global_lists();
544         if (ret < 0)
545                 return ret;
546         print_global_summary();
547         print_user_lists();
548         print_user_summary();
549         return 1;
550 }
551
552 static int read_uid_file(void)
553 {
554         size_t size;
555         uint32_t n;
556         char *filename = get_uid_list_name(), *map;
557         int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
558         unsigned bits;
559
560         if (ret < 0) {
561                 INFO_LOG("failed to map %s\n", filename);
562                 free(filename);
563                 return ret;
564         }
565         num_uids = size / 4;
566         INFO_LOG("found %u uids in %s\n", (unsigned)num_uids, filename);
567         free(filename);
568         /*
569          * Compute number of hash table bits. The hash table size must be a
570          * power of two and larger than the number of uids.
571          */
572         bits = 2;
573         while (1 << bits < num_uids)
574                 bits++;
575         create_hash_table(bits);
576         for (n = 0; n < num_uids; n++) {
577                 uint32_t uid = read_u32(map + n * sizeof(uid));
578                 ret = search_uid(uid, OPEN_USER_TABLE, NULL);
579                 if (ret < 0)
580                         goto out;
581         }
582 out:
583         adu_munmap(map, size);
584         return ret;
585 }
586
587 int com_select(void)
588 {
589         int ret;
590
591         if (conf.count_unit_arg != count_unit_arg_h)
592                 count_unit_buf[1] = count_unit_abbrevs[conf.count_unit_arg];
593         else
594                 count_unit_buf[0] = '\0';
595         if (conf.size_unit_arg != size_unit_arg_h)
596                 size_unit_buf[1] = size_unit_abbrevs[conf.size_unit_arg];
597         else
598                 size_unit_buf[0] = '\0';
599
600         ret = open_dir_table(0);
601         if (ret < 0)
602                 return ret;
603         check_signals();
604         ret = read_uid_file();
605         if (ret < 0)
606                 return ret;
607         check_signals();
608         ret = print_statistics();
609         close_all_tables();
610         return ret;
611 }