-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.c
More file actions
297 lines (256 loc) · 8.39 KB
/
Copy pathfs.c
File metadata and controls
297 lines (256 loc) · 8.39 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
// ============================================================================
// fs.c - user FileSytem API
// ============================================================================
#include "fs.h"
#include "bfs.h"
// ============================================================================
// Close the file currently open on file descriptor 'fd'.
// ============================================================================
i32 fsClose(i32 fd) {
i32 inum = bfsFdToInum(fd);
bfsDerefOFT(inum);
return 0;
}
// ============================================================================
// Create the file called 'fname'. Overwrite, if it already exsists.
// On success, return its file descriptor. On failure, EFNF
// ============================================================================
i32 fsCreate(str fname) {
i32 inum = bfsCreateFile(fname);
if (inum == EFNF) {
return EFNF;
}
return bfsInumToFd(inum);
}
// ============================================================================
// Format the BFS disk by initializing the SuperBlock, Inodes, Directory and
// Freelist. On succes, return 0. On failure, abort
// ============================================================================
i32 fsFormat() {
FILE* fp = fopen(BFSDISK, "w+b");
if (fp == NULL) {
FATAL(EDISKCREATE);
}
i32 ret = bfsInitSuper(fp); // initialize Super block
if (ret != 0) {
fclose(fp);
FATAL(ret);
}
ret = bfsInitInodes(fp); // initialize Inodes block
if (ret != 0) {
fclose(fp);
FATAL(ret);
}
ret = bfsInitDir(fp); // initialize Dir block
if (ret != 0) {
fclose(fp);
FATAL(ret);
}
ret = bfsInitFreeList(); // initialize Freelist
if (ret != 0) {
fclose(fp);
FATAL(ret);
}
fclose(fp);
return 0;
}
// ============================================================================
// Mount the BFS disk. It must already exist
// ============================================================================
i32 fsMount() {
FILE* fp = fopen(BFSDISK, "rb");
if (fp == NULL) {
FATAL(ENODISK); // BFSDISK not found
}
fclose(fp);
return 0;
}
// ============================================================================
// Open the existing file called 'fname'. On success, return its file
// descriptor. On failure, return EFNF
// ============================================================================
i32 fsOpen(str fname) {
i32 inum = bfsLookupFile(fname); // lookup 'fname' in Directory
if (inum == EFNF) {
return EFNF;
}
return bfsInumToFd(inum);
}
// ============================================================================
// Read 'numb' bytes of data from the cursor in the file currently fsOpen'd on
// File Descriptor 'fd' into 'buf'. On success, return actual number of bytes
// read (may be less than 'numb' if we hit EOF). On failure, abort
// ============================================================================
i32 fsRead(i32 fd, i32 numb, void* buf) {
if (numb < 0) {
//Invalid byte count
return ENEGNUMB;
}
if (buf == NULL) {
//Null buffer pointer
return ENULLPTR;
}
i32 inum = bfsFdToInum(fd);
if (inum < 0) {
//Invalid fd
return EBADINUM;
}
i32 ofte = bfsFindOFTE(inum);
if (ofte < 0) {
//Invalid OFT
return EOFTFULL;
}
i32 curs = g_oft[ofte].curs;
i32 size = bfsGetSize(inum);
if (size < 0) {
//Error getting size
return EBADINUM;
}
if (curs >= size) {
//EOF
return 0;
}
//Limit numb to available data
if (curs + numb > size) {
numb = size - curs;
}
i32 bytes_read = 0;
i8 temp[BYTESPERBLOCK];
while (bytes_read < numb) {
i32 fbn = (curs + bytes_read) / BYTESPERBLOCK;
i32 offset = (curs + bytes_read) % BYTESPERBLOCK;
i32 to_read = (BYTESPERBLOCK - offset < numb - bytes_read)
? (BYTESPERBLOCK - offset)
: (numb - bytes_read);
i32 ret = bfsRead(inum, fbn, temp);
if (ret != 0) {
//Read error
return EBADREAD;
}
memcpy((i8*)buf + bytes_read, temp + offset, to_read);
bytes_read += to_read;
}
//Update cursor
g_oft[ofte].curs += bytes_read;
return bytes_read;
}
// ============================================================================
// Move the cursor for the file currently open on File Descriptor 'fd' to the
// byte-offset 'offset'. 'whence' can be any of:
//
// SEEK_SET : set cursor to 'offset'
// SEEK_CUR : add 'offset' to the current cursor
// SEEK_END : add 'offset' to the size of the file
//
// On success, return 0. On failure, abort
// ============================================================================
i32 fsSeek(i32 fd, i32 offset, i32 whence) {
if (offset < 0) {
FATAL(EBADCURS);
}
i32 inum = bfsFdToInum(fd);
i32 ofte = bfsFindOFTE(inum);
switch (whence) {
case SEEK_SET:
g_oft[ofte].curs = offset;
break;
case SEEK_CUR:
g_oft[ofte].curs += offset;
break;
case SEEK_END: {
i32 end = fsSize(fd);
g_oft[ofte].curs = end + offset;
break;
}
default:
FATAL(EBADWHENCE);
}
return 0;
}
// ============================================================================
// Return the cursor position for the file open on File Descriptor 'fd'
// ============================================================================
i32 fsTell(i32 fd) {
return bfsTell(fd);
}
// ============================================================================
// Retrieve the current file size in bytes. This depends on the highest offset
// written to the file, or the highest offset set with the fsSeek function. On
// success, return the file size. On failure, abort
// ============================================================================
i32 fsSize(i32 fd) {
i32 inum = bfsFdToInum(fd);
return bfsGetSize(inum);
}
// ============================================================================
// Write 'numb' bytes of data from 'buf' into the file currently fsOpen'd on
// filedescriptor 'fd'. The write starts at the current file offset for the
// destination file. On success, return 0. On failure, abort
// ============================================================================
i32 fsWrite(i32 fd, i32 numb, void* buf) {
if (numb < 0) {
//Invalid write size
return ENEGNUMB;
}
if (buf == NULL) {
//Null buffer pointer
return ENULLPTR;
}
i32 inum = bfsFdToInum(fd);
if (inum < 0) {
//Invalid fd
return EBADINUM;
}
i32 ofte = bfsFindOFTE(inum);
if (ofte < 0) {
//Invalid OFT
return EOFTFULL;
}
i32 curs = g_oft[ofte].curs;
i32 size = bfsGetSize(inum);
if (size < 0) {
//Error getting size
return EBADINUM;
}
//Extend if necessary
if (curs + numb > size) {
i32 last_fbn = (curs + numb - 1) / BYTESPERBLOCK;
if (bfsExtend(inum, last_fbn) != 0) {
//No space
return EDISKFULL;
}
if (bfsSetSize(inum, curs + numb) != 0) {
//File size update error
return EBADWRITE;
}
}
i32 bytes_written = 0;
i8 temp[BYTESPERBLOCK];
while (bytes_written < numb) {
i32 fbn = (curs + bytes_written) / BYTESPERBLOCK;
i32 offset = (curs + bytes_written) % BYTESPERBLOCK;
i32 to_write = (BYTESPERBLOCK - offset < numb - bytes_written)
? (BYTESPERBLOCK - offset)
: (numb - bytes_written);
if (offset != 0 || to_write < BYTESPERBLOCK) {
if (bfsRead(inum, fbn, temp) != 0) {
//Read error
return EBADREAD;
}
}
memcpy(temp + offset, (i8*)buf + bytes_written, to_write);
i32 dbn = bfsFbnToDbn(inum, fbn);
if (dbn < 0) {
//Invalid DBN
return EBADDBN;
}
if (bioWrite(dbn, temp) != 0) {
//Write error
return EBADWRITE;
}
bytes_written += to_write;
}
//Update cursor
g_oft[ofte].curs += bytes_written;
return 0;
}