-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs.c
172 lines (124 loc) · 3.97 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
#include "fs.h"
void filesystem_init()
{
base_block = read_disk(BASE_BLOCK_ADDRESS);
}
void update_base_block(int new_head, int new_tail)
{
base_block->head = new_head;
base_block->tail = new_tail;
write_disk(BASE_BLOCK_ADDRESS, base_block);
}
metadata_t* load_metadata(int address)
{
metadata_t* metadata = read_disk(address);
return metadata;
}
int get_files_number()
{
if (base_block->head == 0)
return 0;
int files_number = 0;
metadata_t* curr_file = load_metadata(base_block->head);
while (1) {
files_number++;
if (curr_file->next_file_address == 0)
break;
curr_file = load_metadata(curr_file->next_file_address);
}
return files_number;
}
void create_file(char* filename, char* buffer)
{
int metadata_lba = (base_block->head == 0) ? BASE_BLOCK_ADDRESS + 1 : base_block->tail + 2;
int file_lba = metadata_lba + 1;
metadata_t* metadata = kalloc(sizeof(metadata_t));
metadata->next_file_address = 0;
int curr_idx;
for (curr_idx = 0; *filename != '\0' && curr_idx < FILENAME_LENGTH - 1; curr_idx++, filename++)
metadata->filename[curr_idx] = *filename;
metadata->filename[curr_idx] = '\0';
write_disk(metadata_lba, metadata);
write_disk(file_lba, buffer);
if (base_block->head == 0) {
update_base_block(metadata_lba, metadata_lba);
} else {
metadata_t* tail_metadata = load_metadata(base_block->tail);
tail_metadata->next_file_address = metadata_lba;
write_disk(base_block->tail, tail_metadata);
update_base_block(base_block->head, metadata_lba);
}
}
char** list_files()
{
if (base_block->head == 0)
return -1;
char** list;
list = kalloc(get_files_number() * sizeof(char*));
metadata_t* curr_file = load_metadata(base_block->head);
int i = 0;
while (1) {
list[i] = curr_file->filename;
if (curr_file->next_file_address == 0)
break;
curr_file = load_metadata(curr_file->next_file_address);
i++;
}
return list;
}
int get_address_by_filename(char* filename)
{
metadata_t* curr_file = load_metadata(base_block->head);
int curr_file_address = base_block->head;
int i = 0;
while (1) {
if (strcmp(curr_file->filename, filename) == 1)
return curr_file_address;
if (curr_file->next_file_address == 0)
break;
curr_file_address = curr_file->next_file_address;
curr_file = load_metadata(curr_file->next_file_address);
}
return 0;
}
char* read_file(char* filename)
{
int address = get_address_by_filename(filename);
if (address == 0)
return 0;
char* buffer = read_disk(address + 1);
return buffer;
}
int get_prev_file_address(int address)
{
metadata_t* prev_file = load_metadata(base_block->head);
int prev_file_address = base_block->head;
while (1) {
if (prev_file->next_file_address == address)
return prev_file_address;
prev_file_address = prev_file->next_file_address;
prev_file = load_metadata(prev_file->next_file_address);
}
return -1;
}
void delete_file(char* filename)
{
int curr_file_address = get_address_by_filename(filename);
if (curr_file_address == 0)
return;
metadata_t* curr_file_metadata = read_disk(curr_file_address);
if (get_files_number() == 1) {
update_base_block(0, 0);
return;
}
if (curr_file_address == base_block->head) {
update_base_block(curr_file_metadata->next_file_address, base_block->tail);
} else {
int prev_file_address = get_prev_file_address(curr_file_address);
metadata_t* prev_file = load_metadata(prev_file_address);
prev_file->next_file_address = curr_file_metadata->next_file_address;
write_disk(prev_file_address, prev_file);
if (curr_file_address == base_block->tail)
update_base_block(base_block->head, prev_file_address);
}
}