kern

view fstools/mkfs/mkfs.c @ 98:921a264297a4

merged the filesystem stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 17:03:30 +0300
parents 7ff2b4971216
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <inttypes.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/ioctl.h>
9 #include <sys/stat.h>
10 #ifdef __linux__
11 #include <linux/fs.h>
12 #endif
13 #ifdef __darwin__
14 #include <dev/disk.h>
15 #endif
16 #include "fs.h"
18 int mkfs(int fd, int blksize, uint32_t nblocks);
19 uint32_t get_block_count(int fd, int blksize);
20 int user_readblock(int dev, uint32_t blk, void *buf);
21 int user_writeblock(int dev, uint32_t blk, void *buf);
22 int parse_args(int argc, char **argv);
24 int fd;
25 uint32_t num_blocks;
27 int main(int argc, char **argv)
28 {
29 if(parse_args(argc, argv) == -1) {
30 return 1;
31 }
33 if((num_blocks = get_block_count(fd, BLKSZ)) == 0) {
34 fprintf(stderr, "could not determine the number of blocks\n");
35 return 1;
36 }
37 printf("total blocks: %u\n", (unsigned int)num_blocks);
39 if(mkfs(fd, num_blocks) == -1) {
40 return 1;
41 }
43 return 0;
44 }
46 int mkfs(int fd, int blksize, uint32_t nblocks)
47 {
48 struct superblock *sb;
50 sb = malloc(BLKSZ);
51 assert(sb);
53 sb->magic = MAGIC;
54 sb->ver = 0;
55 sb->num_blocks = nblocks;
56 }
58 uint32_t get_block_count(int fd, int blksize)
59 {
60 unsigned long sz = 0;
61 uint64_t sz64 = 0;
62 struct stat st;
64 #ifdef BLKGETSIZE64
65 if(ioctl(fd, BLKGETSIZE64, &sz64) != -1) {
66 return sz64 / blksize;
67 }
68 #endif
70 #ifdef BLKGETSIZE
71 if(ioctl(fd, BLKGETSIZE, &sz) != -1) {
72 return sz / (blksize / 512);
73 }
74 #endif
76 #ifdef DKIOCGETBLOCKCOUNT
77 if(ioctl(fd, DKIOCGETBLOCKCOUNT, &sz64) != -1) {
78 return sz64 / (blksize / 512);
79 }
80 #endif
82 if(fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) {
83 return st.st_size / blksize;
84 }
86 return 0;
87 }
89 int blk_read(void*, uint32_t blk, int count, void *buf)
90 {
91 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
92 return -1;
93 }
94 if(read(fd, buf, BLKSZ * count) < BLKSZ * count) {
95 return -1;
96 }
97 return 0;
98 }
100 int blk_write(void*, uint32_t blk, int count, void *buf)
101 {
102 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
103 return -1;
104 }
105 if(write(fd, buf, BLKSZ * count) < BLKSZ * count) {
106 return -1;
107 }
108 return 0;
109 }
111 int parse_args(int argc, char **argv)
112 {
113 int i;
115 fd = -1;
117 for(i=1; i<argc; i++) {
118 if(argv[i][0] == '-' && argv[i][2] == 0) {
119 switch(argv[i][1]) {
120 case 'h':
121 printf("usage: %s <device file>\n", argv[0]);
122 exit(0);
124 default:
125 goto invalid;
126 }
127 } else {
128 if(fd != -1) {
129 goto invalid;
130 }
132 if((fd = open(argv[i], O_RDWR)) == -1) {
133 fprintf(stderr, "failed to open %s: %s\n", argv[i], strerror(errno));
134 return -1;
135 }
136 }
137 }
139 if(fd == -1) {
140 fprintf(stderr, "you must specify a device or image file\n");
141 return -1;
142 }
144 return 0;
146 invalid:
147 fprintf(stderr, "invalid argument: %s\n", argv[i]);
148 return -1;
149 }