-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesystem.c
119 lines (109 loc) · 2.89 KB
/
filesystem.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
#include "filesystem.h"
#include "disk.h"
#include "superblock.h"
#include "inode.h"
#include "dir.h"
#include "file.h"
#include <stdio.h>
int init_system() {
init_sp_block(&spb);
// print_sp_block(&spb);
if (!write_sp_block(&spb)) {
printf("Initial super bolck failed.\n");
return -1;
}
for (int i = 0; i < INODE_NUM; i++) {
if (!init_inode(&inode[i], 0, File, 0)) {
printf("Initial Inode failed.\n");
return -1;
}
if (!write_inode(&inode[i], i)) {
printf("Write Inode failed.\n");
return -1;
}
}
if (!init_root_dir()) {
printf("Initial root failed.\n");
return 0;
}
printf("Initial super bolck succeded. Enjoy your time.\n");
return 1;
}
int open_system() {
if (open_disk()<0) {
printf("Open disk error.\n");
return 0;
}
if (read_sp_block(&spb) && spb.magic_num == MAGICNUM) {
// print_sp_block(&spb);
for (int i = 0; i < INODE_NUM; i++) {
if (!read_inode(&inode[i], i)) {
printf("Load Inode failed.\n");
return -1;
}
}
printf("Find existed Ext2. Enjoy your time.\n");
} else {
printf("File System Unkonwn or didn't exist. Format disk and build a new file system.\n");
init_system();
}
return 1;
}
int close_system() {
if (!write_sp_block(&spb)) {
printf("Save super bolck failed.\n");
return -1;
}
printf("Save super bolck Succeeded.\n");
if (close_disk() < 0) {
printf("Close disk failed.\n");
return -1;
}
printf("Close Disk.\n");
return 1;
}
void mkdir(char* argv[], int argc) {
if (argc > 2) {
printf("\ntoo mant arguments for 'mkdir'\n");
} else if (argc == 2) {
if (!make_dir(argv[1])) {
printf("make dir failed.\n");
} else {
printf("make dir %s succeeded.\n", argv[1]);
}
} else {
printf("\ntoo few arguments for 'mkdir'\n");
}
}
void touch(char* argv[], int argc) {
if (argc > 2) printf("\ntoo many arguments for 'touch'\n");
else if (argc == 2) {
if (make_file(argv[1])) {
printf("touch %s succeeded.\n", argv[1]);
} else {
printf("touch %s failed\n", argv[1]);
}
} else {
printf("\ntoo few arguments for 'touch'\n");
}
}
void ls(char* argv[], int argc) {
if (argc > 2) {
printf("\nToo many arguments for 'ls'\n");
} else if (argc == 2) {
list_dir(argv[1]);
} else {
struct inode root;
read_root_dir(&root);
print_dir(&root);
}
}
void cp(char* argv[], int argc) {
if (argc > 3) {
printf("\nToo many arguments for 'cp'\n");
} else if (argc == 3) {
copy_file(argv[1], argv[2]);
} else {
printf("\ntoo few arguments for 'cp'\n");
}
}