-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfs.h
141 lines (105 loc) · 4.31 KB
/
fs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#ifndef FS_H
#define FS_H
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#include "disk.h"
#include "config.h"
// 1 block -> 1024B
// | super block | inode bitmap | block bitmap | inode table | data block |
// | 1 block | 1 block | 1 block | 56 block | 965 block |
typedef unsigned int ui32;
typedef unsigned short ui16;
typedef struct SuperBlock {
ui16 inode_bitmap_block; // 2B inode bitmap block number
ui16 block_bitmap_block; // 2B block bitmap block number
ui16 inode_table_block; // 2B inode table block number
ui16 data_block; // 2B data block number
ui16 inode_num; // 2B inode number
ui16 block_num; // 2B block number
ui16 inode_size; // 2B inode size
ui16 block_size; // 2B block size
} SuperBlock;
void initSuperBlock(SuperBlock* sb);
typedef struct INode {
ui16 inode_number; // 2B inode的编号
ui16 size; // 2B 文件大小
ui16 direct_block[10]; // 16B 直接块
ui16 first_inedxed_block; // 2B 一级索引块
ui16 second_indexed_block; // 2B 二级索引块
ui16 type; // 2B 文件属性 dir/file/link rwxrwxrwx
ui16 link_count; // 2B 链接数
time_t created_time; // 8B 创建时间
time_t modified_time; // 8B 修改时间
time_t access_time; // 8B 访问时间
} INode; // 56B
typedef unsigned int* InodeBitmap;
typedef unsigned int* BlockBitmap;
typedef struct Dentry {
ui16 inode; // 2B inode的编号
ui16 father_inode; // 2B 父目录inode编号
ui16 name_length; // 2B 文件名长度
char* name; // 8B 文件名
ui16 sub_dir_count; // 2B 子目录数
ui16* sub_dir_inode; // 8B 子目录inode编号
ui16* sub_dir_length; // 8B 子目录名长度
char** sub_dir; // 8B 子目录名
} Dentry; // 48B
typedef struct FileSystem {
Disk disk;
SuperBlock super_block;
InodeBitmap inode_bitmap;
BlockBitmap block_bitmap;
ui16 root_inode;
ui16 current_dir_inode;
char* current_dir_path;
} FileSystem;
typedef struct UserOpenItem {
INode inode; // 修改之后的文件inode, 用于保存变更, 也可用于判断是否打开
int offset; // 文件内读写偏移量
bool modify; // inode是否被修改
} UserOpenItem;
typedef struct UserOpenTable {
UserOpenItem* items; // 用户打开文件表
int size; // 用户打开文件数
int capacity; // 用户打开文件表容量
} UserOpenTable;
void tbl_push_back(UserOpenTable* tb, UserOpenItem item);
void tbl_pop_back(UserOpenTable* tb);
void tbl_init(UserOpenTable* tb);
void tbl_destroy(UserOpenTable* tb);
void tbl_resize(UserOpenTable* tb, int new_size);
void tbl_clear(UserOpenTable* tb);
void tbl_remove(UserOpenTable* tb, int index);
INode readInode(FileSystem* fs, ui16 inode_num) ;
// file system save to file
void saveFs(FileSystem* fs, FILE *stream);
// file system load from file
void loadFs(FileSystem* fs, FILE *stream);
// api
// my_format
void format(FileSystem* fs);
// my_mkdir
void mkdir(FileSystem* fs, char* path);
// my_rm + my_rmdir
// recursive = 0: rm file or empty dir; recursive = 1: rm dir and all its subdirs
void rm(FileSystem* fs, char* path, int recursive);
void saveDentry(FileSystem* fs, Dentry* dentry);
// my_ls
void ls(FileSystem* fs, char* path);
// my_cd
void cd(FileSystem* fs, char* path);
// my_create
void create(FileSystem* fs, char* path);
// my_open
void open(FileSystem* fs, UserOpenTable* tb, char* path);
// my_close
void close(FileSystem* fs, UserOpenTable* tb, char* path);
// my_read
int read(FileSystem* fs,UserOpenTable* tb, char* path, int length, void* content);
// my_write
void write(FileSystem* fs, UserOpenTable* tb, char* path, int length, char* content, int opt);
// exit fs
void exitfs(FileSystem* fs, UserOpenTable* tb, FILE* stream);
#endif