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