add file read/write stuff
[tfsprogs.git] / mktfs.c
blob8adebc4c41c1eb66ebe04160616f8e9e219ef5cb
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <malloc.h>
6 #include <unistd.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
10 #include "tfs.h"
11 #include "file.h"
13 /* From ext2fsprogs */
16 static void mark_used_blocks(struct tfs_sb_info *sbi)
18 int i = 0;
19 char *buf = malloc(sbi->s_block_size);
21 memset(buf, 0, sbi->s_block_size);
22 for (; i < sbi->s_data_area; i++)
23 set_bit(buf, i);
24 tfs_bwrite(sbi, sbi->s_block_bitmap, buf);
27 static int root_init(struct tfs_sb_info *sbi)
29 int res = 0;
30 int dirty = 0;
31 struct inode *root = tfs_root_init(sbi);
33 if (!root) {
34 printf("ERROR: failed to get root inode!\n");
35 res = -1;
36 goto out_nofree;
39 res = tfs_add_entry(sbi, root, ".", root->i_ino, &dirty);
40 if (res == -1) {
41 TFS_DEBUG("trying to add '.' under %s failed!\n", "/");
42 goto out;
45 res = tfs_add_entry(sbi, root, "..", root->i_ino, &dirty);
46 if (res == -1) {
47 TFS_DEBUG("trying to add .. under %s failed!\n", "/");
48 goto out;
51 if (dirty)
52 tfs_iwrite(sbi, root);
54 out:
55 free_inode(root);
56 out_nofree:
57 return res;
60 struct tfs_sb_info * tfs_mkfs(char *image, uint32_t offset)
62 struct tfs_super_block sb;
63 struct tfs_sb_info *sbi;
64 int block_size;
65 int block_bitmap_count, inode_bitmap_count, inode_table_count;
66 uint32_t fs_size;
68 fs_size = open_fs(image);
70 sbi = malloc(sizeof(*sbi));
71 if (!sbi) {
72 printf("malloc for sbi failed!\n");
73 return NULL;
76 memset(&sb, 0, sizeof sb);
77 memset(sbi, 0, sizeof(*sbi));
78 sb.s_magic = TFS_MAGIC;
79 sb.s_block_shift = sbi->s_block_shift = 9; /* 512 Bytes */
80 sbi->s_block_size = block_size = 1 << sb.s_block_shift;
81 sb.s_blocks_count = sbi->s_blocks_count = fs_size / block_size;
84 /* A half of blocks count */
85 sb.s_inodes_count = sbi->s_inodes_count = sb.s_blocks_count / 2;
87 sbi->s_block_bitmap_count = block_bitmap_count = roundup(sb.s_blocks_count, (block_size * 8));
88 sbi->s_inode_bitmap_count = inode_bitmap_count = roundup(sb.s_inodes_count, (block_size * 8));
89 sbi->s_inode_table_count = inode_table_count = sb.s_inodes_count * sizeof(struct tfs_inode) / block_size;
91 sb.s_inode_bitmap = sbi->s_inode_bitmap = 0;
92 sb.s_block_bitmap = sbi->s_block_bitmap = inode_bitmap_count;
93 sb.s_inode_table = sbi->s_inode_table = sb.s_block_bitmap + block_bitmap_count;
94 sb.s_data_area = sbi->s_data_area = sb.s_inode_table + inode_table_count;
96 /* Skip the boot sector and super block sector */
97 sb.s_offset = sbi->s_offset = offset + 2;
99 sb.s_free_inodes_count = sbi->s_free_inodes_count = sb.s_inodes_count;
100 sb.s_free_blocks_count = sbi->s_free_blocks_count = sb.s_blocks_count - sb.s_data_area;
102 sbi->s_inodes_per_block = sbi->s_block_size / sizeof(struct tfs_inode);
105 mark_used_blocks(sbi);
107 cache_init(sbi);
108 root_init(sbi);
110 /* Write the suber block back to image */
111 write_sector(sbi->s_offset - 1, &sb, 1);
113 return sbi;
117 void tfs_cp(struct tfs_sb_info *sbi, char *filename)
119 char *file = malloc(strlen(filename) + 2);
120 struct file *fobj;
121 char buf[512];
122 int count;
123 int fd;
124 int bytes_written;
126 strcpy(file, "/");
127 strcat(file, filename);
129 fobj = tfs_open(sbi, file);
130 if (!fobj) {
131 printf("tfs_open: open file %s error!\n", filename);
132 return;
135 fd = open("filename", O_RDONLY);
136 if (fd < 0) {
137 printf("open file %s error!\n", filename);
138 return;
140 while ((count = read(fd, buf, sizeof(buf))) > 0) {
141 bytes_written = tfs_write(fobj, buf, count);
142 if (bytes_written == -1)
143 printf("write error!\n");
144 else
145 printf("== %d bytes written!\n", bytes_written);
152 int main(int argc, char *argv[])
154 int size = sizeof(struct tfs_inode);
155 int offset = 0;
156 int i, count;
157 char **files;
158 struct tfs_sb_info *sbi;
160 #if 1
161 printf("inode size: %d(0x%x)\n", size, size);
163 size = sizeof(struct tfs_dir_entry);
164 printf("dentry size: %d(0x%x)\n", size, size);
166 size = sizeof(struct tfs_super_block);
167 printf("super block size: %d(0x%0x)\n", size, size);
168 #endif
170 if (argc < 2) {
171 printf("Usage: mktfs image offset files\n");
172 return 1;
173 } else if (argc == 3) {
174 offset = atoi(argv[2]);
176 count = argc - 3;
177 files = &argv[3];
179 sbi = tfs_mkfs(argv[1], offset);
180 if (!sbi) {
181 printf("mkfs failed!\n");
182 return -1;
185 for (i = 0; i < count; i++) {
186 printf("copying file %s... \n", files[i]);
187 tfs_cp(sbi, files[i]);
190 free(sbi);
192 return 0;