14 /* From ext2fsprogs */
17 static void mark_used_blocks(struct tfs_sb_info
*sbi
)
20 char *buf
= malloc(sbi
->s_block_size
);
22 memset(buf
, 0, sbi
->s_block_size
);
23 for (; i
< sbi
->s_data_area
; i
++)
25 tfs_bwrite(sbi
, sbi
->s_block_bitmap
, buf
);
28 static int root_init(struct tfs_sb_info
*sbi
)
32 struct inode
*root
= tfs_root_init(sbi
);
35 printf("ERROR: failed to get root inode!\n");
40 res
= tfs_add_entry(sbi
, root
, ".", root
->i_ino
, &dirty
);
42 TFS_DEBUG("trying to add '.' under %s failed!\n", "/");
46 res
= tfs_add_entry(sbi
, root
, "..", root
->i_ino
, &dirty
);
48 TFS_DEBUG("trying to add .. under %s failed!\n", "/");
53 tfs_iwrite(sbi
, root
);
61 struct tfs_sb_info
* tfs_mkfs(char *image
, uint32_t offset
)
63 struct tfs_super_block sb
;
64 struct tfs_sb_info
*sbi
;
66 int block_bitmap_count
, inode_bitmap_count
, inode_table_count
;
69 fs_size
= open_fs(image
);
71 sbi
= malloc(sizeof(*sbi
));
73 printf("malloc for sbi failed!\n");
77 memset(&sb
, 0, sizeof sb
);
78 memset(sbi
, 0, sizeof(*sbi
));
79 sb
.s_magic
= TFS_MAGIC
;
80 sb
.s_block_shift
= sbi
->s_block_shift
= 9; /* 512 Bytes */
81 sbi
->s_block_size
= block_size
= 1 << sb
.s_block_shift
;
82 sb
.s_blocks_count
= sbi
->s_blocks_count
= fs_size
/ block_size
;
85 /* A half of blocks count */
86 sb
.s_inodes_count
= sbi
->s_inodes_count
= sb
.s_blocks_count
/ 2;
88 sbi
->s_block_bitmap_count
= block_bitmap_count
= roundup(sb
.s_blocks_count
, (block_size
* 8));
89 sbi
->s_inode_bitmap_count
= inode_bitmap_count
= roundup(sb
.s_inodes_count
, (block_size
* 8));
90 sbi
->s_inode_table_count
= inode_table_count
= sb
.s_inodes_count
* sizeof(struct tfs_inode
) / block_size
;
92 sb
.s_inode_bitmap
= sbi
->s_inode_bitmap
= 0;
93 sb
.s_block_bitmap
= sbi
->s_block_bitmap
= inode_bitmap_count
;
94 sb
.s_inode_table
= sbi
->s_inode_table
= sb
.s_block_bitmap
+ block_bitmap_count
;
95 sb
.s_data_area
= sbi
->s_data_area
= sb
.s_inode_table
+ inode_table_count
;
97 /* Skip the boot sector and super block sector */
98 sb
.s_offset
= sbi
->s_offset
= offset
+ 2;
100 sb
.s_free_inodes_count
= sbi
->s_free_inodes_count
= sb
.s_inodes_count
;
101 sb
.s_free_blocks_count
= sbi
->s_free_blocks_count
= sb
.s_blocks_count
- sb
.s_data_area
;
103 sbi
->s_inodes_per_block
= sbi
->s_block_size
/ sizeof(struct tfs_inode
);
106 mark_used_blocks(sbi
);
111 /* Write the suber block back to image */
112 write_sector(sbi
->s_offset
- 1, &sb
, 1);
117 static char *get_full_path(char *filename
)
119 char *full
= malloc(strlen(filename
) + 2);
122 strcat(full
, filename
);
128 struct file
* tfs_file_open(struct tfs_sb_info
*sbi
, char *filename
)
131 char *file
= filename
;
133 fobj
= tfs_open(sbi
, file
);
135 printf("tfs_open: open file %s error!\n", filename
);
142 void tfs_cd(struct tfs_sb_info
*sbi
, char *dst_dir
)
147 this_dir
= tfs_opendir(sbi
, dst_dir
);
149 printf("cd: %s: no such directory\n", dst_dir
);
154 if (this_dir
->dd_dir
->inode
->i_mode
!= TFS_DIR
) {
155 printf("cd: %s: is not a directory\n", dst_dir
);
156 tfs_closedir(this_dir
);
165 void tfs_cat(struct tfs_sb_info
*sbi
, char *filename
)
167 struct file
*file
= tfs_file_open(sbi
, filename
);
173 while ((bytes_read
= tfs_read(file
, buf
, sizeof(buf
))) > 0)
174 write(1, buf
, bytes_read
);
177 void tfs_ls(struct tfs_sb_info
*sbi
, char *filename
)
179 DIR *dir
= tfs_opendir(sbi
, filename
);
183 printf("open dir %s failed!\n", get_full_path(filename
));
187 if (dir
->dd_dir
->inode
->i_mode
== TFS_FILE
) {
188 printf("%d\t %s\n", dir
->dd_dir
->inode
->i_ino
, filename
);
193 while (de
= tfs_readdir(dir
)) {
194 printf("%6d\t %s\n", de
->d_ino
, de
->d_name
);
201 void cmd_mkdir(struct tfs_sb_info
*sbi
, char *filename
)
203 tfs_mkdir(sbi
, filename
);
207 void tfs_cp(struct tfs_sb_info
*sbi
, char *filename
)
215 file
= tfs_file_open(sbi
, filename
);
219 fd
= open(filename
, O_RDONLY
);
221 printf("open file %s error!\n", filename
);
224 while ((count
= read(fd
, buf
, sizeof(buf
))) > 0) {
225 bytes_written
= tfs_write(file
, buf
, count
);
226 if (bytes_written
== -1)
227 printf("write error!\n");
229 printf(" == %d bytes written!\n", bytes_written
);
236 int main(int argc
, char *argv
[])
238 int size
= sizeof(struct tfs_inode
);
241 struct tfs_sb_info
*sbi
;
244 printf("inode size: %d(0x%x)\n", size
, size
);
246 size
= sizeof(struct tfs_dir_entry
);
247 printf("dentry size: %d(0x%x)\n", size
, size
);
249 size
= sizeof(struct tfs_super_block
);
250 printf("super block size: %d(0x%0x)\n", size
, size
);
254 printf("Usage: mktfs image offset\n");
258 sbi
= tfs_mkfs(argv
[1], offset
);
260 printf("mkfs failed!\n");
264 /* init the this_dir */
265 this_dir
= tfs_opendir(sbi
, "/");
267 printf("reading '/' as this_dir error!\n");
278 while((c
= getchar()) != '\n')
283 while (*p
!= ' ' && *p
)
294 if (strcmp(cmd
, "cp") == 0) {
295 printf("copying file '%s' ... \n", opt
);
297 } else if (strcmp(cmd
, "cat") == 0) {
298 printf("cating file '%s' ... \n", opt
);
300 } else if (strcmp(cmd
, "cd") == 0) {
301 printf("cd in '%s' ... \n", opt
);
303 } else if (strcmp(cmd
, "ls") == 0) {
306 printf("listing file '%s' ...\n", opt
);
308 } else if (strcmp(cmd
, "mkdir") == 0) {
309 printf("making dir '%s' ... \n", opt
);
311 } else if (strcmp(cmd
, "quit") == 0 ||
312 strcmp(cmd
, "exit") == 0) {
315 printf("unknow command!\n");