1 /* 2 * get_num_dirs.c -- calculate number of directories 3 * 4 * Copyright 1997 by Theodore Ts'o 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Library 8 * General Public License, version 2. 9 * %End-Header% 10 */ 11 12 #include "config.h" 13 #include <stdio.h> 14 #if HAVE_UNISTD_H 15 #include <unistd.h> 16 #endif 17 #include <string.h> 18 #include <time.h> 19 20 #include "ext2_fs.h" 21 #include "ext2fsP.h" 22 23 /* 24 * Returns the number of directories in the filesystem as reported by 25 * the group descriptors. Of course, the group descriptors could be 26 * wrong! 27 */ ext2fs_get_num_dirs(ext2_filsys fs,ext2_ino_t * ret_num_dirs)28errcode_t ext2fs_get_num_dirs(ext2_filsys fs, ext2_ino_t *ret_num_dirs) 29 { 30 dgrp_t i; 31 ext2_ino_t num_dirs, max_dirs; 32 33 EXT2_CHECK_MAGIC(fs, EXT2_ET_MAGIC_EXT2FS_FILSYS); 34 35 num_dirs = 0; 36 max_dirs = fs->super->s_inodes_per_group; 37 for (i = 0; i < fs->group_desc_count; i++) { 38 if (ext2fs_bg_used_dirs_count(fs, i) > max_dirs) 39 num_dirs += max_dirs / 8; 40 else 41 num_dirs += ext2fs_bg_used_dirs_count(fs, i); 42 } 43 if (num_dirs > fs->super->s_inodes_count) 44 num_dirs = fs->super->s_inodes_count; 45 46 *ret_num_dirs = num_dirs; 47 48 return 0; 49 } 50 51