forked from Rodrig79/Project-3-A-Simple-Network-File-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSys.cpp
More file actions
306 lines (281 loc) · 7.75 KB
/
FileSys.cpp
File metadata and controls
306 lines (281 loc) · 7.75 KB
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
302
303
304
305
306
// CPSC 3500: File System
// Implements the file system commands that are available to the shell.
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <algorithm>
using namespace std;
#include "FileSys.h"
#include "BasicFileSys.h"
#include "Blocks.h"
// mounts the file system
void FileSys::mount(int sock) {
bfs.mount();
curr_dir = 1; //by default current directory is home directory, in disk block #1
bfs.read_block(curr_dir, (void*) &curr_dir_block);
fs_sock = sock; //use this socket to receive file system operations from the client and send back response messages
}
// unmounts the file system
void FileSys::unmount() {
bfs.unmount();
close(fs_sock);
}
// TODO: directory is full error
// make a directory
void FileSys::mkdir(const char *name)
{
short newBlock;
dirblock_t newDir;
if(get_block_index(name) >= 0){
send_message(ERR_502);
return;
} if(std::strlen(name) > MAX_FNAME_SIZE){
send_message(ERR_504);
return;
} if(curr_dir_block.num_entries == MAX_DIR_ENTRIES){
send_message(ERR_506);
return;
} if(!(newBlock = bfs.get_free_block())){
send_message(ERR_505);
return;
}
newDir.num_entries = 0;
bfs.write_block(newBlock, &newDir);
for(int i = 0; name[i] != '\0';i++)
curr_dir_block.dir_entries[curr_dir_block.num_entries].name[i] = name[i];
curr_dir_block.dir_entries[curr_dir_block.num_entries].block_num = newBlock;
curr_dir_block.num_entries++;
}
// TODO: entry not found response
// TODO cstring equality?
// switch to a directory
void FileSys::cd(const char *name)
{
dirblock_t new_dir_block;
short i;
if((i = get_block_index(name)) == -1){
send_message(ERR_503);
return;
}
bfs.read_block(curr_dir_block.dir_entries[i].block_num, (void*) &new_dir_block);
if(new_dir_block.magic != DIR_MAGIC_NUM){
send_message(ERR_500);
return;
}
curr_dir = curr_dir_block.dir_entries[i].block_num;
curr_dir_block = new_dir_block;
}
// switch to home directory
void FileSys::home() {
curr_dir = 1;
bfs.read_block(curr_dir, (void*) &curr_dir_block);
}
//TODO: make sure directory is empty
// remove a directory
void FileSys::rmdir(const char *name)
{
short i;
dirblock_t rem_block;
if((i = get_block_index(name)) == -1){
send_message(ERR_503);
return;
}
short rem = curr_dir_block.dir_entries[i].block_num;
bfs.read_block(rem, (void*) &rem_block);
if(rem_block.magic != DIR_MAGIC_NUM){
send_message(ERR_500);
return;
}
if(rem_block.num_entries > 0){
send_message(ERR_507);
return;
}
//this shit don't work for copy
std::swap(curr_dir_block.dir_entries[rem], curr_dir_block.dir_entries[curr_dir_block.num_entries - 1]);
curr_dir_block.num_entries--;
bfs.reclaim_block(rem);
}
// list the contents of current directory
//TODO: remove extra space at end
void FileSys::ls()
{
std::string file_list = "";
for(int i = 0; i < curr_dir_block.num_entries; i++){
file_list.append(curr_dir_block.dir_entries[i].name);
file_list += " ";
}
file_list += "\n";
send_message(file_list);
}
// TODO: error message if full
// create an empty data file
void FileSys::create(const char *name)
{
inode_t new_file;
short new_block;
if(get_block_index(name) >= 0){
send_message(ERR_502);
return;
}
if(std::strlen(name) > MAX_FNAME_SIZE){
send_message(ERR_504);
return;
}
if(curr_dir_block.num_entries == MAX_DIR_ENTRIES){
send_message(ERR_506);
return;
}
if(!(new_block = bfs.get_free_block())) {
send_message(ERR_505);
return;
}
new_file.size = 0;
bfs.write_block(new_block, &new_file);
std::copy(name, name + std::strlen(name), curr_dir_block.dir_entries[curr_dir_block.num_entries].name);
curr_dir_block.dir_entries[curr_dir_block.num_entries].block_num = new_block;
curr_dir_block.num_entries++;
}
// TODO: file doesn't exist, file too big
// append data to a data file
void FileSys::append(const char *name, const char *data)
{
short inode;
inode_t inode_block;
int data_index;
int data_offset;
datablock_t curr_block;
if((inode = get_block_index(name)) == -1) {
send_message(ERR_503);
return;
}
bfs.read_block(curr_dir_block.dir_entries[inode].block_num, (void*) &inode_block);
if(inode_block.magic != INODE_MAGIC_NUM){
send_message(ERR_501);
return;
}
data_index = inode_block.size / BLOCK_SIZE;
data_offset = inode_block.size % BLOCK_SIZE;
int i = 0;
bfs.read_block(inode_block.blocks[data_index], (void*) &curr_block);
while(data[i] != '\0'){
if(data_offset == BLOCK_SIZE) {
data_offset = 0;
bfs.write_block(inode_block.blocks[data_index++], (void*) &curr_block);
//ask which one of these errors to return first
if(data_index == MAX_DATA_BLOCKS) {
inode_block.size = data_index * BLOCK_SIZE;
bfs.write_block(inode, (void*) &inode_block);
send_message(ERR_508);
return;
}
if((inode_block.blocks[data_index] = bfs.get_free_block()) == 0) {
inode_block.size = data_index * BLOCK_SIZE;
bfs.write_block(inode, (void*) &inode_block);
send_message(ERR_505);
return;
}
curr_block = datablock_t();
}
curr_block.data[data_offset++] = data[i++];
}
inode_block.size = data_index * BLOCK_SIZE + data_offset;
bfs.write_block(inode, (void*) &inode_block);
}
// display the contents of a data file
void FileSys::cat(const char *name)
{
short inode_index;
inode_t inode_block;
datablock_t data_block;
int index = 0;
int offset = 0;
int block_num = 0;
if((inode_index = get_block_index(name)) == -1) {
send_message(ERR_503);
return;
}
bfs.read_block(curr_dir_block.dir_entries[inode_index].block_num, (void*) &inode_block);
if(inode_block.magic != INODE_MAGIC_NUM) {
send_message(ERR_501);
return;
}
char file_data[inode_block.size + 1];
for(int i = 0; i < inode_block.size; i++) {
if(offset == 0) {
bfs.read_block(inode_block.blocks[block_num++], (void*) &data_block);
}
file_data[index++] = data_block.data[offset++];
offset = offset % BLOCK_SIZE;
}
file_data[index] = '\0';
send_message(file_data);
}
// display the first N bytes of the file
void FileSys::head(const char *name, unsigned int n)
{
short inode_index;
inode_t inode_block;
datablock_t data_block;
int index = 0;
int offset = 0;
int block_num = 0;
if((inode_index = get_block_index(name)) == -1) {
send_message(ERR_503);
return;
}
bfs.read_block(curr_dir_block.dir_entries[inode_index].block_num, (void*) &inode_block);
if(inode_block.magic != INODE_MAGIC_NUM) {
send_message(ERR_501);
return;
}
n = (n > inode_block.size) ? inode_block.size : n;
char file_data[n + 1];
for(int i = 0; i < n; i++) {
if(offset == 0) {
bfs.read_block(inode_block.blocks[block_num++], (void*) &data_block);
}
file_data[index++] = data_block.data[offset++];
offset = offset % BLOCK_SIZE;
}
file_data[index] = '\0';
send_message(file_data);
}
// delete a data file
void FileSys::rm(const char *name)
{
short inode_index;
short inode_block_num;
inode_t inode_block;
int block_num;
if((inode_index = get_block_index(name)) == -1) {
send_message(ERR_503);
return;
}
inode_block_num = curr_dir_block.dir_entries[inode_index].block_num;
bfs.read_block(inode_block_num, (void*) &inode_block);
if(inode_block.magic != INODE_MAGIC_NUM) {
send_message(ERR_501);
return;
}
std::swap(curr_dir_block.dir_entries[inode_index], curr_dir_block.dir_entries[curr_dir_block.num_entries - 1]);
curr_dir_block.num_entries--;
block_num = (inode_block.size == 0) ? 0 : inode_block.size - 1 / BLOCK_SIZE + 1;
for(int i = 0; i < block_num; i++) {
bfs.reclaim_block(inode_block.blocks[i]);
}
bfs.reclaim_block(inode_block_num);
}
// display stats about file or directory
void FileSys::stat(const char *name)
{
}
// HELPER FUNCTIONS (optional)
short FileSys::get_block_index(const char* name) {
for(int i = 0; i < curr_dir_block.num_entries; i++) {
if(std::strcmp(curr_dir_block.dir_entries[i].name, name))
return i;
}
return -1;
}
void FileSys::send_message(std::string message) {
}