Get rid of the osl code.
[adu.git] / adu.c
1 #include "adu.h"
2 #include <dirent.h> /* readdir() */
3
4 #include "gcc-compat.h"
5 #include "fd.h"
6 #include "string.h"
7 #include "error.h"
8
9 DEFINE_ERRLIST;
10
11
12
13 /** evaluates to 1 if x < y, to -1 if x > y and to 0 if x == y */
14 #define NUM_COMPARE(x, y) ((int)((x) < (y)) - (int)((x) > (y)))
15
16
17 /**
18  * The log function.
19  *
20  * \param ll Loglevel.
21  * \param fml Usual format string.
22  *
23  * All XXX_LOG() macros use this function.
24  */
25 __printf_2_3 void __log(int ll, const char* fmt,...)
26 {
27         va_list argp;
28         FILE *outfd;
29         struct tm *tm;
30         time_t t1;
31         char str[255] = "";
32
33         if (ll < 4)
34                 return;
35         outfd = stderr;
36         time(&t1);
37         tm = localtime(&t1);
38         strftime(str, sizeof(str), "%b %d %H:%M:%S", tm);
39         fprintf(outfd, "%s ", str);
40         va_start(argp, fmt);
41         vfprintf(outfd, fmt, argp);
42         va_end(argp);
43 }
44
45 /**
46  * Compare the size of two directories
47  *
48  * \param obj1 Pointer to the first object.
49  * \param obj2 Pointer to the second object.
50  *
51  * This function first compares the size values as usual integers. If they compare as
52  * equal, the address of \a obj1 and \a obj2 are compared. So this compare function
53  * returns zero if and only if \a obj1 and \a obj2 point to the same memory area.
54  */
55 static int size_compare(const struct osl_object *obj1, const struct osl_object *obj2)
56 {
57         uint64_t d1 = *(uint64_t *)obj1->data;
58         uint64_t d2 = *(uint64_t *)obj2->data;
59         int ret = NUM_COMPARE(d2, d1);
60
61         if (ret)
62                 return ret;
63         //INFO_LOG("addresses: %p, %p\n", obj1->data, obj2->data);
64         return NUM_COMPARE(obj2->data, obj1->data);
65 }
66
67 /**
68  * Compare two osl objects of string type.
69  *
70  * \param obj1 Pointer to the first object.
71  * \param obj2 Pointer to the second object.
72  *
73  * In any case, only \p MIN(obj1->size, obj2->size) characters of each string
74  * are taken into account.
75  *
76  * \return It returns an integer less than, equal to, or greater than zero if
77  * \a obj1 is found, respectively, to be less than, to match, or be greater than
78  * obj2.
79  *
80  * \sa strcmp(3), strncmp(3), osl_compare_func.
81  */
82 int string_compare(const struct osl_object *obj1, const struct osl_object *obj2)
83 {
84         const char *str1 = (const char *)obj1->data;
85         const char *str2 = (const char *)obj2->data;
86         return strncmp(str1, str2, MIN(obj1->size, obj2->size));
87 }
88
89 /** The columns of the directory table. */
90 enum dir_table_columns {
91         /** The name of the directory. */
92         DT_NAME,
93         /** The dir count number. */
94         DT_NUM,
95         /** The number of bytes of all regular files. */
96         DT_BYTES,
97         /** The number of all regular files. */
98         DT_FILES,
99         /** Number of columns in this table. */
100         NUM_DT_COLUMNS
101 };
102
103 static struct osl_column_description dir_table_cols[] = {
104         [DT_NAME] = {
105                 .storage_type = OSL_MAPPED_STORAGE,
106                 .storage_flags = OSL_RBTREE | OSL_UNIQUE,
107                 .name = "dir",
108                 .compare_function = string_compare,
109         },
110         [DT_NUM] = {
111                 .storage_type = OSL_MAPPED_STORAGE,
112                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
113                 .name = "num",
114                 .compare_function = uint32_compare,
115                 .data_size = sizeof(uint32_t)
116         },
117         [DT_BYTES] = {
118                 .storage_type = OSL_MAPPED_STORAGE,
119                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
120                 .compare_function = size_compare,
121                 .name = "num_bytes",
122                 .data_size = sizeof(uint64_t)
123         },
124         [DT_FILES] = {
125                 .storage_type = OSL_MAPPED_STORAGE,
126                 .storage_flags =  OSL_RBTREE | OSL_FIXED_SIZE,
127                 .compare_function = size_compare,
128                 .name = "num_files",
129                 .data_size = sizeof(uint64_t)
130         }
131 };
132
133 static struct osl_table_description dir_table_desc = {
134         .name = "dir_table",
135         .num_columns = NUM_DT_COLUMNS,
136         .flags = 0,
137         .column_descriptions = dir_table_cols,
138         .dir = "/tmp/adu"
139 };
140
141 /** The columns of the id table. */
142 enum id_table_columns {
143         /** The user id. */
144         IDT_UID,
145         /** The number of bytes of all regular files owned by this id. */
146         IDT_BYTES,
147         /** The number of regular files owned by this id. */
148         IDT_FILES,
149         /** The user table for this uid. */
150         IDT_TABLE,
151         /** Number of columns in this table. */
152         NUM_IDT_COLUMNS
153 };
154
155 static struct osl_column_description id_table_cols[] = {
156         [IDT_UID] = {
157                 .storage_type = OSL_MAPPED_STORAGE,
158                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
159                 .name = "uid",
160                 .compare_function = uint32_compare,
161                 .data_size = sizeof(uint32_t)
162         },
163         [IDT_BYTES] = {
164                 .storage_type = OSL_MAPPED_STORAGE,
165                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
166                 .compare_function = size_compare,
167                 .name = "num_bytes",
168                 .data_size = sizeof(uint64_t)
169         },
170         [IDT_FILES] = {
171                 .storage_type = OSL_MAPPED_STORAGE,
172                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
173                 .compare_function = size_compare,
174                 .name = "num_filess",
175                 .data_size = sizeof(uint64_t)
176         },
177         [IDT_TABLE] = {
178                 .storage_type = OSL_NO_STORAGE,
179                 .storage_flags = OSL_FIXED_SIZE | OSL_UNIQUE,
180                 .name = "user_table",
181                 .data_size = sizeof(void *)
182         }
183 };
184
185 static struct osl_table_description id_table_desc = {
186         .name = "id_table",
187         .num_columns = NUM_IDT_COLUMNS,
188         .flags = 0,
189         .column_descriptions = id_table_cols,
190         .dir = "/tmp/adu"
191 };
192
193 /** The columns of the id table. */
194 enum user_table_columns {
195         /** The numer of the directory. */
196         UT_DIR_NUM,
197         /** The number of bytes of all regular files in this dir owned by this id. */
198         UT_BYTES,
199         /** The number of files in this dir owned by this id. */
200         UT_FILES,
201         /** Number of columns in this table. */
202         NUM_UT_COLUMNS
203 };
204
205 static struct osl_column_description user_table_cols[] = {
206         [UT_DIR_NUM] = {
207                 .storage_type = OSL_MAPPED_STORAGE,
208                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE | OSL_UNIQUE,
209                 .name = "dir_num",
210                 .compare_function = uint32_compare,
211                 .data_size = sizeof(uint32_t)
212         },
213         [IDT_BYTES] = {
214                 .storage_type = OSL_MAPPED_STORAGE,
215                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
216                 .compare_function = size_compare,
217                 .name = "num_bytes",
218                 .data_size = sizeof(uint64_t)
219         },
220         [IDT_FILES] = {
221                 .storage_type = OSL_MAPPED_STORAGE,
222                 .storage_flags = OSL_RBTREE | OSL_FIXED_SIZE,
223                 .compare_function = size_compare,
224                 .name = "num_files",
225                 .data_size = sizeof(uint64_t)
226         },
227 };
228
229 static struct osl_table_description user_table_desc = {
230         .num_columns = NUM_UT_COLUMNS,
231         .flags = 0,
232         .column_descriptions = user_table_cols,
233         .dir = "/tmp/adu"
234 };
235 static struct osl_table *dir_table;
236 static struct osl_table *id_table;
237
238 static int create_tables(void)
239 {
240         int ret = osl_create_table(&dir_table_desc);
241         if (ret < 0)
242                 return ret;
243         ret = osl_create_table(&id_table_desc);
244         if (ret < 0)
245                 return ret;
246         return 1;
247 }
248
249 int add_directory(char *dirname, uint32_t dir_num, uint64_t *dir_size,
250                 uint64_t *dir_files)
251 {
252         struct osl_object dir_objects[NUM_DT_COLUMNS];
253
254         INFO_LOG("adding #%u: %s\n", dir_num, dirname);
255         dir_objects[DT_NAME].data = dirname;
256         dir_objects[DT_NAME].size = strlen(dirname) + 1;
257         dir_objects[DT_NUM].data = &dir_num;
258         dir_objects[DT_NUM].size = sizeof(dir_num);
259         dir_objects[DT_BYTES].data = dir_size;
260         dir_objects[DT_BYTES].size = sizeof(*dir_size);
261         dir_objects[DT_FILES].data = dir_files;
262         dir_objects[DT_FILES].size = sizeof(*dir_files);
263
264         return osl_add_row(dir_table, dir_objects);
265 }
266
267 int create_and_open_user_table(uint32_t uid, struct osl_table **t)
268 {
269         int ret;
270         struct osl_table_description *desc = para_malloc(sizeof(*desc));
271
272         desc->num_columns = NUM_UT_COLUMNS;
273         desc->flags = 0;
274         desc->column_descriptions = user_table_cols;
275         desc->dir = para_strdup("/tmp/adu");
276         desc->name = make_message("%u", uid);
277         INFO_LOG("................................. %u\n", uid);
278 //      user_table_desc.name = make_message("%u", uid);
279         ret = osl_create_table(desc);
280         if (ret < 0)
281                 return ret;
282         return osl_open_table(desc, t);
283 }
284
285 static int insert_id_row(uint32_t uid, struct osl_table *t, struct osl_row **row)
286 {
287         struct osl_object objects[NUM_IDT_COLUMNS];
288         uint64_t num = 0;
289
290         struct osl_table **table_ptr = para_malloc(sizeof(*table_ptr));
291         *table_ptr = t;
292
293         INFO_LOG("§§§§§§§§§§§§§§§§§§§§§ uid: %d, t: %p\n", uid, t);
294         objects[IDT_UID].data = &uid;
295         objects[IDT_UID].size = sizeof(uid);
296         objects[IDT_BYTES].data = &num;
297         objects[IDT_BYTES].size = sizeof(num);
298         objects[IDT_FILES].data = &num;
299         objects[IDT_FILES].size = sizeof(num);
300         objects[IDT_TABLE].data = table_ptr;
301         objects[IDT_TABLE].size = sizeof(*table_ptr);
302         return osl_add_and_get_row(id_table, objects, row);
303 }
304
305 static int get_user_table(struct osl_row *row, struct osl_table **t)
306 {
307         struct osl_object obj;
308
309         int ret = osl_get_object(id_table, row, IDT_TABLE, &obj);
310         if (ret < 0)
311                 return ret;
312         *t = *(struct osl_table **)obj.data;
313         INFO_LOG("^^^^^^^^^^^^^^^^^^ t: %p\n", *t);
314         return 1;
315 }
316
317 static int add_id_bytes(struct osl_row *row, uint64_t *add)
318 {
319         uint64_t num;
320         struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
321
322         /* update number of bytes */
323         int ret = osl_get_object(id_table, row, IDT_BYTES, &obj1);
324         if (ret < 0)
325                 return ret;
326         num = *(uint64_t *)obj1.data + *add;
327         ret = osl_update_object(id_table, row, IDT_BYTES, &obj2);
328         if (ret < 0)
329                 return ret;
330         /* increment number of files */
331         ret = osl_get_object(id_table, row, IDT_FILES, &obj1);
332         if (ret < 0)
333                 return ret;
334         num = *(uint64_t *)obj1.data + 1;
335         return osl_update_object(id_table, row, IDT_FILES, &obj2);
336 }
337
338 static int update_user_row(struct osl_table *t, uint32_t dir_num,
339                 uint64_t *add)
340 {
341         struct osl_row *row;
342         struct osl_object obj = {.data = &dir_num, .size = sizeof(dir_num)};
343
344         int ret = osl_get_row(t, UT_DIR_NUM, &obj, &row);
345
346         if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
347                 return ret;
348         if (ret < 0) { /* this is the first file we add */
349                 struct osl_object objects[NUM_UT_COLUMNS];
350                 uint64_t num_files = 1;
351
352                 objects[UT_DIR_NUM].data = &dir_num;
353                 objects[UT_DIR_NUM].size = sizeof(dir_num);
354                 objects[UT_BYTES].data = add;
355                 objects[UT_BYTES].size = sizeof(*add);
356                 objects[UT_FILES].data = &num_files;
357                 objects[UT_FILES].size = sizeof(num_files);
358                 INFO_LOG("######################### ret: %d\n", ret);
359                 ret = osl_add_row(t, objects);
360                 INFO_LOG("######################### ret: %d\n", ret);
361                 return ret;
362         } else { /* add size and increment file count */
363                 uint64_t num;
364                 struct osl_object obj1, obj2 = {.data = &num, .size = sizeof(num)};
365
366                 ret = osl_get_object(t, row, UT_BYTES, &obj1);
367                 if (ret < 0)
368                         return ret;
369                 num = *(uint64_t *)obj1.data + *add;
370                 ret = osl_update_object(t, row, UT_BYTES, &obj2);
371                 if (ret < 0)
372                         return ret;
373                 ret = osl_get_object(t, row, UT_FILES, &obj1);
374                 if (ret < 0)
375                         return ret;
376                 num = *(uint64_t *)obj1.data + 1;
377                 return osl_update_object(t, row, UT_FILES, &obj2);
378         }
379 }
380
381 static uint32_t dir_num;
382
383 int scan_dir(char *dirname)
384 {
385         DIR *dir;
386         struct dirent *entry;
387         int ret, cwd_fd, ret2;
388         uint64_t dir_size = 0, dir_files = 0;
389         struct osl_object obj;
390
391         INFO_LOG("----------------- %s\n", dirname);
392         ret = para_opendir(dirname, &dir, &cwd_fd);
393         if (ret < 0) {
394                 if (ret != -ERRNO_TO_ERROR(EACCES))
395                         return ret;
396                 WARNING_LOG("permission denied for %s\n", dirname);
397                 return 1;
398         }
399         while ((entry = readdir(dir))) {
400                 mode_t m;
401                 char *tmp;
402                 struct stat s;
403                 uint32_t uid;
404                 uint64_t size;
405                 struct osl_row *id_row;
406                 struct osl_table *user_table;
407
408                 if (!strcmp(entry->d_name, "."))
409                         continue;
410                 if (!strcmp(entry->d_name, ".."))
411                         continue;
412                 if (lstat(entry->d_name, &s) == -1)
413                         continue;
414                 m = s.st_mode;
415                 if (!S_ISREG(m) && !S_ISDIR(m))
416                         continue;
417                 if (S_ISDIR(m)) {
418                         tmp = make_message("%s/%s", dirname, entry->d_name);
419                         ret = scan_dir(tmp);
420                         free(tmp);
421                         if (ret < 0)
422                                 goto out;
423                         continue;
424                 }
425                 /* regular file */
426                 size = s.st_size;
427                 dir_size += size;
428                 dir_files++;
429                 uid = s.st_uid;
430                 INFO_LOG("++++++++++++++++++++++++++ %s, uid: %u\n", entry->d_name, uid);
431                 obj.data = &uid;
432                 obj.size = sizeof(uid);
433                 ret = osl_get_row(id_table, IDT_UID, &obj, &id_row);
434                 if (ret < 0 && ret != -E_RB_KEY_NOT_FOUND)
435                         goto out;
436                 if (ret < 0) {
437                         ret = create_and_open_user_table(uid, &user_table);
438                         if (ret < 0)
439                                 goto out;
440                         ret = insert_id_row(uid, user_table, &id_row);
441                         if (ret < 0)
442                                 goto out;
443                 } else {
444                         ret = get_user_table(id_row, &user_table);
445                         if (ret < 0)
446                                 goto out;
447                 }
448                 ret = add_id_bytes(id_row, &size);
449                 if (ret < 0)
450                         goto out;
451                 INFO_LOG("user_table: %p\n", user_table);
452                 ret = update_user_row(user_table, dir_num, &size);
453                 INFO_LOG("update_user  ret: %d\n", ret);
454                 if (ret < 0)
455                         goto out;
456         }
457         ret = add_directory(dirname, dir_num++, &dir_size, &dir_files);
458 out:
459         closedir(dir);
460         ret2 = para_fchdir(cwd_fd);
461         if (ret2 < 0 && ret >= 0)
462                 ret = ret2;
463         close(cwd_fd);
464         return ret;
465 }
466
467 static int get_dir_name(struct osl_row *row, char **name)
468 {
469         struct osl_object obj;
470         int ret = osl_get_object(dir_table, row, DT_NAME, &obj);
471
472         if (ret < 0)
473                 return ret;
474         *name = obj.data;
475         return 1;
476 }
477
478 static int print_dirname_and_size(struct osl_row *row, void *data)
479 {
480         unsigned *count = data;
481         struct osl_object obj;
482         char *name;
483         int ret;
484
485         if ((*count)++ > 100)
486                 return -E_LOOP_COMPLETE;
487         ret = get_dir_name(row, &name);
488         if (ret < 0)
489                 return ret;
490         ret = osl_get_object(dir_table, row, DT_BYTES, &obj);
491         if (ret < 0)
492                 return ret;
493         printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
494         return 1;
495 }
496
497 static int print_dirname_and_file_count(struct osl_row *row, void *data)
498 {
499         unsigned *count = data;
500         struct osl_object obj;
501         char *name;
502         int ret;
503
504         if ((*count)++ > 100)
505                 return -E_LOOP_COMPLETE;
506         ret = get_dir_name(row, &name);
507         if (ret < 0)
508                 return ret;
509         ret = osl_get_object(dir_table, row, DT_FILES, &obj);
510         if (ret < 0)
511                 return ret;
512         printf("%s\t%llu\n", name, *(long long unsigned *)obj.data);
513         return 1;
514 }
515
516 static int print_id_stats(struct osl_row *row, __a_unused void *data)
517 {
518         struct osl_object obj;
519         uint32_t uid;
520         uint64_t bytes, files;
521         int ret = osl_get_object(id_table, row, IDT_UID, &obj);
522
523         if (ret < 0)
524                 return ret;
525         uid = *(uint32_t *)obj.data;
526         ret = osl_get_object(id_table, row, IDT_BYTES, &obj);
527         if (ret < 0)
528                 return ret;
529         bytes = *(uint64_t *)obj.data;
530         ret = osl_get_object(id_table, row, IDT_FILES, &obj);
531         if (ret < 0)
532                 return ret;
533         files = *(uint64_t *)obj.data;
534
535         printf("%u\t%llu\t%llu\n", (unsigned)uid, (long long unsigned)files,
536                 (long long unsigned)bytes);
537         return 1;
538 }
539
540 struct id_dir_stat_info {
541         unsigned count;
542         struct osl_table *user_table;
543 };
544
545 static int print_big_dir(struct osl_row *row, void *data)
546 {
547         struct id_dir_stat_info *info = data;
548         info->count++;
549         int ret;
550         struct osl_row *dir_row;
551         char *dirname;
552         uint64_t bytes;
553         struct osl_object obj;
554
555         if (info->count > 10)
556                 return -E_LOOP_COMPLETE;
557         ret = osl_get_object(info->user_table, row, UT_BYTES, &obj);
558         if (ret < 0)
559                 return ret;
560         bytes = *(uint64_t *)obj.data;
561         ret = osl_get_object(info->user_table, row, UT_DIR_NUM, &obj);
562         if (ret < 0)
563                 return ret;
564         ret = osl_get_row(dir_table, DT_NUM, &obj, &dir_row);
565         if (ret < 0)
566                 return ret;
567         ret = osl_get_object(dir_table, dir_row, DT_NAME, &obj);
568         if (ret < 0)
569                 return ret;
570         dirname = obj.data;
571         printf("%s: %llu\n", dirname, (long long unsigned)bytes);
572         return 1;
573 }
574
575 static int print_id_dir_stats(struct osl_row *row, __a_unused void *data)
576 {
577         struct osl_object obj;
578         uint32_t uid;
579         int ret = osl_get_object(id_table, row, IDT_UID, &obj);
580         struct id_dir_stat_info info = {.count = 0};
581
582         if (ret < 0)
583                 return ret;
584         uid = *(uint32_t *)obj.data;
585
586         ret = osl_get_object(id_table, row, IDT_TABLE, &obj);
587         if (ret < 0)
588                 return ret;
589         info.user_table = *(struct osl_table **)obj.data;
590
591         printf("************************* Big dirs owned by uid %u\n", (unsigned) uid);
592         osl_rbtree_loop_reverse(info.user_table, IDT_BYTES, &info, print_big_dir);
593         return 1;
594 }
595
596 static int print_statistics(void)
597 {
598         unsigned count = 0;
599         int ret;
600
601         printf("************************* Biggest dirs\n");
602         ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &count, print_dirname_and_size);
603         if (ret < 0 && ret != -E_LOOP_COMPLETE)
604                 return ret;
605         count = 0;
606         printf("************************* dirs containing many files\n");
607         ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &count, print_dirname_and_file_count);
608         if (ret < 0 && ret != -E_LOOP_COMPLETE)
609                 return ret;
610
611         printf("************************* dirs stats by owner\n");
612         ret = osl_rbtree_loop(id_table, IDT_BYTES, NULL, print_id_stats);
613         if (ret < 0)
614                 return ret;
615
616         return osl_rbtree_loop(id_table, IDT_BYTES, NULL, print_id_dir_stats);
617 }
618
619
620 int main(int argc, char **argv)
621 {
622         int ret = create_tables();
623         if (ret < 0)
624                 goto out;
625         ret = osl_open_table(&dir_table_desc, &dir_table);
626         if (ret < 0)
627                 goto out;
628         ret = osl_open_table(&id_table_desc, &id_table);
629         if (ret < 0)
630                 goto out;
631         ret = -E_SYNTAX;
632         if (argc != 2)
633                 goto out;
634         ret = scan_dir(argv[1]);
635         if (ret < 0)
636                 goto out;
637         print_statistics();
638 out:
639         if (ret < 0) {
640                 ERROR_LOG("%s\n", error_txt(-ret));
641                 return -EXIT_FAILURE;
642         }
643         return EXIT_SUCCESS;
644 }
645