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