kern

view fstools/mkfs/mkfs.c @ 90:7ff2b4971216

started work on the filesystem
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Dec 2011 13:44:15 +0200
parents
children 083849df660b
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 if(!(sb = malloc(BLKSZ))) {
51 perror("failed to allocate memory");
52 return -1;
53 }
54 }
56 uint32_t get_block_count(int fd, int blksize)
57 {
58 unsigned long sz = 0;
59 uint64_t sz64 = 0;
60 struct stat st;
62 #ifdef BLKGETSIZE64
63 if(ioctl(fd, BLKGETSIZE64, &sz64) != -1) {
64 return sz64 / blksize;
65 }
66 #endif
68 #ifdef BLKGETSIZE
69 if(ioctl(fd, BLKGETSIZE, &sz) != -1) {
70 return sz / (blksize / 512);
71 }
72 #endif
74 #ifdef DKIOCGETBLOCKCOUNT
75 if(ioctl(fd, DKIOCGETBLOCKCOUNT, &sz64) != -1) {
76 return sz64 / (blksize / 512);
77 }
78 #endif
80 if(fstat(fd, &st) != -1 && S_ISREG(st.st_mode)) {
81 return st.st_size / blksize;
82 }
84 return 0;
85 }
87 int user_readblock(int dev, uint32_t blk, void *buf)
88 {
89 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
90 return -1;
91 }
92 if(read(fd, buf, BLKSZ) < BLKSZ) {
93 return -1;
94 }
95 return 0;
96 }
98 int user_writeblock(int dev, uint32_t blk, void *buf)
99 {
100 if(lseek(fd, blk * BLKSZ, SEEK_SET) == -1) {
101 return -1;
102 }
103 if(write(fd, buf, BLKSZ) < BLKSZ) {
104 return -1;
105 }
106 return 0;
107 }
109 int parse_args(int argc, char **argv)
110 {
111 int i;
113 fd = -1;
115 for(i=1; i<argc; i++) {
116 if(argv[i][0] == '-' && argv[i][2] == 0) {
117 switch(argv[i][1]) {
118 case 'h':
119 printf("usage: %s <device file>\n", argv[0]);
120 exit(0);
122 default:
123 goto invalid;
124 }
125 } else {
126 if(fd != -1) {
127 goto invalid;
128 }
130 if((fd = open(argv[i], O_RDWR)) == -1) {
131 fprintf(stderr, "failed to open %s: %s\n", argv[i], strerror(errno));
132 return -1;
133 }
134 }
135 }
137 if(fd == -1) {
138 fprintf(stderr, "you must specify a device or image file\n");
139 return -1;
140 }
142 return 0;
144 invalid:
145 fprintf(stderr, "invalid argument: %s\n", argv[i]);
146 return -1;
147 }