xref: /minix3/minix/lib/libc/gen/fslib.c (revision 20a91f7755ccb876035f671d877a5065507dc887)
1 /* fslib.c - routines needed by fs and fs utilities */
2 
3 #include <minix/config.h>	/* for unused stuff in <minix/type.h> :-( */
4 #include <limits.h>
5 #include <dirent.h>
6 #include <assert.h>
7 #include <stdlib.h>		/* for abort() */
8 #include <sys/types.h>
9 #include <minix/const.h>
10 #include <minix/type.h>		/* for unshort :-( */
11 #include <minix/sysutil.h>
12 #include <minix/minlib.h>
13 #include "mfs/const.h"		/* depends of -I flag in Makefile */
14 #include "mfs/type.h"		/* ditto */
15 #include "mfs/inode.h"		/* ditto */
16 #include "mfs/super.h"
17 #include <minix/fslib.h>
18 #include <sys/stat.h>
19 
20 /* The next routine is copied from fsck.c and mkfs.c...  (Re)define some
21  * things for consistency.  Some things should be done better.
22  */
23 
24 /* Convert from bit count to a block count. The usual expression
25  *
26  *	(nr_bits + (1 << BITMAPSHIFT) - 1) >> BITMAPSHIFT
27  *
28  * doesn't work because of overflow.
29  *
30  * Other overflow bugs, such as the expression for N_ILIST overflowing when
31  * s_inodes is just over V*_INODES_PER_BLOCK less than the maximum+1, are not
32  * fixed yet, because that number of inodes is silly.
33  */
34 /* The above comment doesn't all apply now bit_t is long.  Overflow is now
35  * unlikely, but negative bit counts are now possible (though unlikely)
36  * and give silly results.
37  */
38 int bitmapsize(nr_bits, block_size)
39 bit_t nr_bits;
40 int block_size;
41 {
42   int nr_blocks;
43 
44   nr_blocks = (int) (nr_bits / FS_BITS_PER_BLOCK(block_size));
45   if (((bit_t) nr_blocks * FS_BITS_PER_BLOCK(block_size)) < nr_bits) ++nr_blocks;
46   return(nr_blocks);
47 }
48 
49 uint8_t fs_mode_to_type(mode_t mode)
50 {
51 	if(S_ISREG(mode)) return DT_REG;
52 	else if(S_ISDIR(mode)) return DT_DIR;
53 	else if(S_ISLNK(mode)) return DT_LNK;
54 	else if(S_ISCHR(mode)) return DT_CHR;
55 	else if(S_ISBLK(mode)) return DT_BLK;
56 	else if(S_ISFIFO(mode)) return DT_FIFO;
57 	else if(S_ISSOCK(mode)) return DT_SOCK;
58 
59 	assert(0 && "unknown type");
60 
61 	/* assert()s are removed on NDEBUG builds. */
62 	abort();
63 }
64 
65