- if (usi->count > 0)
- usi->count--;
- return 1;
-}
-
-static void print_user_stats(void)
-{
- struct user_info *ui;
-
- FOR_EACH_USER(ui) {
- struct user_stats_info usi = {
- .count = conf.limit_arg,
- .ui = ui
- };
- if (!ui_used(ui) || !ui_admissible(ui))
- continue;
- usi.flags = USF_PRINT_DIRNAME | USF_PRINT_BYTES | USF_COMPUTE_SUMMARY;
- printf("************************************************ uid %u\n",
- (unsigned) ui->uid);
- printf("----------------- Largest dirs -------------------\n");
- osl_rbtree_loop_reverse(ui->table, UT_BYTES, &usi,
- user_stats_loop_function);
- printf("---------- dirs containing most files ------------\n");
- usi.count = conf.limit_arg,
- usi.flags = USF_PRINT_DIRNAME | USF_PRINT_FILES;
- osl_rbtree_loop_reverse(ui->table, UT_FILES, &usi,
- user_stats_loop_function);
- }
-}
-
-static int print_statistics(void)
-{
- int ret;
- struct global_stats_info gsi = {
- .count = conf.limit_arg,
- .flags = GSF_PRINT_DIRNAME | GSF_PRINT_BYTES | GSF_COMPUTE_SUMMARY
- };
-
- printf("----------------- Largest dirs -------------------\n");
- ret = osl_rbtree_loop_reverse(dir_table, DT_BYTES, &gsi,
- global_stats_loop_function);
- if (ret < 0 && ret != -E_LOOP_COMPLETE)
- return ret;
- gsi.count = conf.limit_arg;
-
- gsi.flags = GSF_PRINT_DIRNAME | GSF_PRINT_FILES;
- printf("---------- dirs containing most files ------------\n");
- ret = osl_rbtree_loop_reverse(dir_table, DT_FILES, &gsi,
- global_stats_loop_function);
- if (ret < 0 && ret != -E_LOOP_COMPLETE)
- return ret;
-
- printf("------------------ Global summary (dirs/files/bytes)\n"
- "%llu\t%llu\t%llu\n",
- (long long unsigned)num_dirs, (long long unsigned)num_files,
- (long long unsigned)num_bytes);
- print_user_stats();
- print_id_stats();
- return 1;
-}
-
-static char *get_uid_list_name(void)
-{
- return make_message("%s/uid_list", conf.database_dir_arg);
-}
-
-static int write_uid_list(void)
-{
- char *buf, *filename;
- uint32_t count = 0;
- struct user_info *ui;
- size_t size = num_uids * sizeof(uint32_t);
- int ret;
-
- if (!num_uids)
- return 0;
- buf = para_malloc(size);
- FOR_EACH_USER(ui) {
- if (!ui_used(ui) || !ui_admissible(ui))
- continue;
- DEBUG_LOG("saving uid %u\n", (unsigned) ui->uid);
- write_u32(buf + count++ * sizeof(uint32_t), ui->uid);
- }
- filename = get_uid_list_name();
- ret = para_write_file(filename, buf, size);
- free(filename);
- free(buf);
- return ret;
-}
-
-static int open_dir_table(void)
-{
- if (!dir_table_desc.dir) /* we did not create the table */
- dir_table_desc.dir = para_strdup(conf.database_dir_arg);
- return osl_open_table(&dir_table_desc, &dir_table);
-}
-
-static void close_dir_table(void)
-{
- int ret;
-
- if (!dir_table)
- return;
- ret = osl_close_table(dir_table, OSL_MARK_CLEAN);
- if (ret < 0)
- ERROR_LOG("failed to close dir table: %s\n", error_txt(-ret));
- free((char *)dir_table_desc.dir);
- dir_table = NULL;
-}
-
-static void close_user_table(struct user_info *ui)
-{
- int ret;
-
- if (!ui || !ui_used(ui) || !ui_admissible(ui))
- return;
- ret = osl_close_table(ui->table, OSL_MARK_CLEAN);
- if (ret < 0)
- ERROR_LOG("failed to close user table %u: %s\n",
- (unsigned) ui->uid, error_txt(-ret));
- free((char *)ui->desc->name);
- ui->desc->name = NULL;
- free((char *)ui->desc->dir);
- ui->desc->dir = NULL;
- free(ui->desc);
- ui->desc = NULL;
- ui->table = NULL;
- ui->flags = 0;
-}
-
-static void close_user_tables(void)
-{
- struct user_info *ui;
-
- FOR_EACH_USER(ui)
- close_user_table(ui);
-}
-
-static void close_all_tables(void)
-{
- close_dir_table();
- close_user_tables();
- free_hash_table();
-}
-
-static int com_create()
-{
- int ret = create_tables();
- if (ret < 0)
- return ret;
- ret = open_dir_table();
- if (ret < 0)
- return ret;
- ret = scan_dir(conf.base_dir_arg);
- if (ret < 0)
- goto out;
- ret = write_uid_list();
-out:
- close_all_tables();
- return ret;
-}
-
-static int read_uid_file(void)
-{
- size_t size;
- uint32_t n;
- char *filename = get_uid_list_name(), *map;
- int ret = mmap_full_file(filename, O_RDONLY, (void **)&map, &size, NULL);
-
- free(filename);
- if (ret < 0)
- return ret;
- num_uids = size / 4;
- /* hash table size should be a power of two and larger than the number of uids */
- uid_hash_table_size = 4;
- while (uid_hash_table_size < num_uids)
- uid_hash_table_size *= 2;
- create_hash_table();
- for (n = 0; n < num_uids; n++) {
- uint32_t uid = read_u32(map + n * sizeof(uid));
- ret = search_uid(uid, OPEN_USER_TABLE, NULL);
- if (ret < 0)
- goto out;
- }
-out:
- para_munmap(map, size);
- return ret;
-}
-
-static int com_select(void)
-{
- int ret;
-
- ret = open_dir_table();
- if (ret < 0)
- return ret;
- ret = read_uid_file();
- if (ret < 0)
- return ret;
- print_statistics();
- close_all_tables();
- return 1;
-}
-
-static int check_args(void)
-{
- int i, ret;
-