-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfs.c
301 lines (273 loc) · 7.41 KB
/
fs.c
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//
// Created by Once on 2019/7/6.
//
#include "fs.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// 二叉树ADT,数据容器
Tree *tree_init(){
Tree *tree = malloc(sizeof(Tree));
if(!tree){
perror("init tree failed.");
return NULL;
}
tree->root = NULL;
tree->count = 0;
return tree;
}
int tree_is_full(Tree *tree){
Node *node = (Node *)malloc(sizeof(Node));
if(!node)
return 1;
free(node);
return 0;
}
int tree_is_empty(Tree *tree){
if(tree == NULL)
return 1;
return tree->count == 0;
}
/**
* 先序遍历:
* 先将根取出来处理:为空则添加数据
* 根不为空,将儿子取出,作为新的根
* 循环第二步
* */
static Node* add_node(Tree *tree, Node *root, FNode *fnode){
if(!root){
root = (Node*)malloc(sizeof(Node));
if(!root){
perror("init node error");
return NULL;
}
root->left = NULL;
root->right = NULL;
root->fnode = fnode;
tree->count++;
return root;
}
if(strcmp(fnode->file.file_name, root->fnode->file.file_name) < 0){
root->left = add_node(tree, root->left, fnode);
return root;
}
else if(strcmp(fnode->file.file_name, root->fnode->file.file_name) > 0){
root->right = add_node(tree, root->right, fnode);
return root;
}
else
return NULL;
}
int tree_add(Tree *tree, FNode *fnode){
if(tree == NULL || fnode == NULL)
return 0;
if(tree->count == 0){
tree->root = add_node(tree, tree->root, fnode);
return tree->root != NULL;
}
else
return add_node(tree, tree->root, fnode) != NULL;
}
// 时间复杂度平均为O(logN)
static FNode* search_node(Node *root, char *file_name){
if(!root)
return NULL;
if(strcmp(file_name, root->fnode->file.file_name) == 0)
return root->fnode;
else if(strcmp(file_name, root->fnode->file.file_name) < 0)
return search_node(root->left, file_name);
else
return search_node(root->right, file_name);
}
FNode *tree_search(Tree *tree, char *file_name){
if(tree == NULL || file_name == NULL)
return NULL;
return search_node(tree->root, file_name);
}
// 使用中序遍历
static void tree_each(Node *root, void (*run)(FNode*)){
if(!root)
return;
tree_each(root->left, run);
run(root->fnode);
tree_each(root->right, run);
}
void tree_for_each(Tree *tree, void (*run)(FNode*)){
if(tree == NULL || tree->count == 0 || run == NULL)
return;
tree_each(tree->root, run);
}
// 使用后序遍历
static int remove_node(Node *root){
if(!root)
return 0;
remove_node(root->left);
remove_node(root->right);
free(root);
return 1;
}
int tree_clear(Tree *tree){
if(tree == NULL)
return 0;
int result = remove_node(tree->root);
free(tree);
return result;
}
// 文件树ADT
FTree *ftree_init(){
FTree *ftree = (FTree*)malloc(sizeof(FTree));
if(!ftree){
perror("init ptree error");
return NULL;
}
ftree->root = NULL;
ftree->size = 0;
return ftree;
}
int ftree_is_full(FTree *ftree){
FNode *fnode = (FNode*)malloc(sizeof(FNode));
if(!fnode)
return 1;
free(fnode);
return 0;
}
int ftree_is_empty(FTree *ftree){
if(ftree == NULL)
return 0;
return ftree->size == 0;
}
/**
* 先序遍历:
* 先取出根结点,在根结点上做比较,为NULL则添加
* 从根结点搜索出目标儿子结点,将这个儿子结点作为新的根结点
* 循环第一步
* */
/**
* directory: /bin, fnode: mkdir => /bin/mkdir
* 1、根目录/是强制要求,不符合直接返回 => OK
* 2、directory经过的目录,ftree的结点中都有,在最后的目录下添加
* 3、directory经过的目录,中间结点位置P开始不匹配,在P中开始添加新目录,直到在最后的目录添加
* */
static FNode *add_fnode(FTree *ftree, FNode *root, char token[], File *file){
if(!root){
root = (FNode *)malloc(sizeof(FNode));
if(!root){
perror("init fnode error");
return NULL;
}
strcpy(root->file.file_name, token);
strcpy(root->file.file_content, token);
root->file.is_directory = file->is_directory;
root->files = NULL;
root->parent = NULL;
if(ftree->size == 0)
ftree->root = root;
ftree->size++;
return root;
}
FNode *node = tree_search(root->files, token);
if(!node){
FNode *fnode = add_fnode(ftree, node, token, file);
fnode->parent = root;
Tree *tree = root->files == NULL ? tree_init() : root->files;
tree_add(tree, fnode);
root->files = tree;
char *t = strtok(NULL, "/");
if(!t)
return fnode;
else
return add_fnode(ftree, fnode, t, file);
}
else
return add_fnode(ftree, node, strtok(NULL, "/"), file);
}
int ftree_add(FTree *ftree, char directory[], File *file){
if(ftree == NULL || directory == NULL || file == NULL || directory[0] != '/')
return 0;
char path[strlen(directory) + strlen(file->file_name)];
strcpy(path, directory);
strcat(path, "/");
strcat(path, file->file_name);
char *token = strtok(path, "/");
if(token == NULL || strlen(token) == 0)
token = "/";
return add_fnode(ftree, ftree->root, token, file) != NULL;
}
static int node_exist(FNode *root, char token[]){
FNode *node = tree_search(root->files, token);
char *t = strtok(NULL, "/");
if(node){
if(!t)
return 1;
else
return node_exist(node, t);
}
else
return 0;
}
int ftree_is_exist(FTree *ftree, char file_name_absolute[]){
if(file_name_absolute == NULL)
return 0;
if(strlen(file_name_absolute) == 1 && file_name_absolute[0] == '/')
return 1;
if(ftree == NULL || file_name_absolute[0] != '/')
return 0;
char path[strlen(file_name_absolute)];
strcpy(path, file_name_absolute);
char *token = strtok(path, "/");
return node_exist(ftree->root, token);
}
static void traverse(FNode *root);
static void run(FNode *fnode){
printf("/%s", fnode->file.file_name);
traverse(fnode);
}
static void traverse(FNode *root){
if(!root->files){
printf("\n");
return;
}
tree_for_each(root->files, &run);
}
void ftree_traverse(FTree *ftree){
traverse(ftree->root);
printf("\n");
}
/**
* 后缀遍历:
* 从根取出所有儿子结点,作为新的根结点
* 删除根结点
* */
static int tree_each_remove_task(Node *root, void (*task)(FNode *)){
if(!root)
return 0;
tree_each_remove_task(root->left, task);
tree_each_remove_task(root->right, task);
task(root->fnode);
return 1;
}
static int tree_each_remove(Tree *tree, void (*task)(FNode *)){
if(tree == NULL || tree->count == 0 || task == NULL)
return 0;
return tree_each_remove_task(tree->root, task);
}
static int remove_ftree(FNode *root);
static void task(FNode *root){
remove_ftree(root);
}
static int remove_ftree(FNode *root){
if(!root)
return 0;
Tree *tree = root->files;
tree_each_remove(tree, &task);
tree_clear(tree);
free(root);
return 1;
}
int ftree_clear(FTree *ftree){
if(ftree == NULL)
return 0;
int result = remove_ftree(ftree->root);
free(ftree);
return result;
}