-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb_io.c
More file actions
396 lines (316 loc) · 9.99 KB
/
b_io.c
File metadata and controls
396 lines (316 loc) · 9.99 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**************************************************************
* Class:: CSC-415-02 Summer 2025
* Name::Phillip Davis Igor Tello Jared Aung Preet Vithani
* Student IDs::923980431, 922772159, 923575806, 923043807
* GitHub-Name::R3plug
* Group-Name::Team Kentucky Kernels
* Project:: Basic File System
*
* File:: b_io.c
*
* Description:: Basic File System - Key File I/O Operations
*
**************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h> // for malloc
#include <string.h> // for memcpy
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "b_io.h"
#include "dirLow.h"
#define MAXFCBS 20
#define B_CHUNK_SIZE 512
typedef struct b_fcb
{
char * buff; //holds the open file buffer
int index; //holds the current position in the buffer
int buflen; //holds how many valid bytes are in the buffer
off_t filePosition; // current offset in file (could use uint64_t ?)
uint64_t fileSize; // total size of file, updated from b_write
int accessMode; // for O_RDONLY, O_WRONLY, O_RDWR
DE *fi; // pointer to file's directory entry
} b_fcb;
b_fcb fcbArray[MAXFCBS];
int startup = 0; //Indicates that this has not been initialized
//Method to initialize our file system
void b_init ()
{
//init fcbArray to all free
for (int i = 0; i < MAXFCBS; i++)
{
fcbArray[i].buff = NULL; //indicates a free fcbArray
}
startup = 1;
}
//Method to get a free FCB element
b_io_fd b_getFCB ()
{
for (int i = 0; i < MAXFCBS; i++)
{
if (fcbArray[i].buff == NULL)
{
return i; //Not thread safe (But do not worry about it for this assignment)
}
}
return (-1); //all in use
}
// Interface to open a buffered file
// Modification of interface for this assignment, flags match the Linux flags for open
// O_RDONLY, O_WRONLY, or O_RDWR
b_io_fd b_open (char * filename, int flags){
if (startup == 0) b_init(); //Initialize our system
printf("b_open system initialized\n");
int access = flags & O_ACCMODE;
if(access != O_RDONLY && access != O_WRONLY && access != O_RDWR){
printf("Access denied\n");
return -1;
}
b_io_fd returnFd = b_getFCB(); // get our own file descriptor
// check for error - all used FCB's
if(returnFd < 0) {
printf("No available FCB\n");
return -1; //No available FCB
}
ppInfo info;
int pathResult = parsePath(filename, &info);
printf("moving on\n");
DE *fileEntry = NULL;
if(pathResult != 0 || info.index < 0){
printf("inside if\n");
if(!(flags & O_CREAT)){
printf("File does not exist and not creating\n");
return -1;
}
printf("creating new file\n");
DE *newFile = createFile(info.lastElementName, info.parent);
if(!newFile){
printf("New file creation failed\n");
return -1;
}
fileEntry = newFile;
}
else
{
printf("inside else\n");
//File exists
fileEntry = &(info.parent[info.index]);
if(fileEntry->isDir){
printf("Cannot open a dir\n");
return -1;
}
}
if((flags & O_TRUNC) && (access == O_WRONLY || access == O_RDWR)){
//freeExtents(fileEntry);
fileEntry->size = 0;
fileEntry->mem.extentCount = 0;
}
b_fcb *fcb = &fcbArray[returnFd];
memset(fcb, 0, sizeof(b_fcb));
fcb->buff = malloc(B_CHUNK_SIZE);
fcb->index = 0;
fcb->buflen = 0;
fcb->accessMode = access;
fcb->fi = fileEntry;
//Set file position to end of file
if(flags & O_APPEND){
fcb->filePosition = fileEntry->size;
} else{
fcb->filePosition = 0;
}
/*
DE *fileEntry = &info.parent->mem.extents[info.index];
if(fileEntry->isDir) return -1; // can't open a directory
fcbArray[returnFd].buff = malloc(B_CHUNK_SIZE);
fcbArray[returnFd].index = 0;
fcbArray[returnFd].buflen = 0;
fcbArray[returnFd].filePosition = 0;
fcbArray[returnFd].fi = fileEntry;
*/
return (returnFd);
}
// Interface to seek function
int b_seek (b_io_fd fd, off_t offset, int whence)
{
if (startup == 0) b_init(); //Initialize our system
// check that fd is between 0 and (MAXFCBS-1)
if ((fd < 0) || (fd >= MAXFCBS))
{
return (-1); //invalid file descriptor
}
b_fcb *fcb = &fcbArray[fd];
if(fcb->buff ==NULL || fcb->fi == NULL){
return -1;
}
off_t newPosition;
// Calculate file position based on whence
switch(whence) {
case SEEK_SET:
newPosition = offset;
break;
case SEEK_CUR:
newPosition = fcb->filePosition +offset;
break;
case SEEK_END:
newPosition = fcb->fileSize + offset;
break;
default:
return -1;
}
// check bounds to ensure newPosition is valid
if (newPosition < 0 || newPosition > fcb->fileSize){
return -1;
}
// set new position and invalidate buffer
fcb->filePosition = newPosition;
fcb->index = B_CHUNK_SIZE;
fcb->buflen = 0;
return newPosition;
}
// Interface to write function
int b_write (b_io_fd fd, char * buffer, int count)
{
if (startup == 0) b_init(); //Initialize our system
// check that fd is between 0 and (MAXFCBS-1)
if ((fd < 0) || (fd >= MAXFCBS))
{
return (-1); //invalid file descriptor
}
b_fcb *fcb = &fcbArray[fd];
if(fcb->buff ==NULL || fcb->fi == NULL){
return -1;
}
int written = 0;
while (written < count) {
int spaceRemain = B_CHUNK_SIZE - fcb->index;
int bytesToBuffer = (count - written < spaceRemain) ? (count -written) : spaceRemain;
// Copy data from input buffer to internal buffer
memcpy(fcb->buff + fcb->index, buffer + written, bytesToBuffer);
fcb->index += bytesToBuffer;
written += bytesToBuffer;
// when the buffer is filled, write to disk
if(fcb->index == B_CHUNK_SIZE) {
uint64_t blockOffset = fcb->filePosition / B_CHUNK_SIZE;
uint64_t writeResult = LBAwrite(fcb->buff,1, fcb->fi->mem.extents->block + blockOffset);
if(writeResult != 1) return -1;
fcb->filePosition += B_CHUNK_SIZE;
fcb->index = 0;
}
}
if(fcb->filePosition + fcb->index > fcb->fileSize){
fcb->fileSize = fcb->filePosition + fcb->index;
}
return written; //Change this
}
// Interface to read a buffer
// Filling the callers request is broken into three parts
// Part 1 is what can be filled from the current buffer, which may or may not be enough
// Part 2 is after using what was left in our buffer there is still 1 or more block
// size chunks needed to fill the callers request. This represents the number of
// bytes in multiples of the blocksize.
// Part 3 is a value less than blocksize which is what remains to copy to the callers buffer
// after fulfilling part 1 and part 2. This would always be filled from a refill
// of our buffer.
// +-------------+------------------------------------------------+--------+
// | | | |
// | filled from | filled direct in multiples of the block size | filled |
// | existing | | from |
// | buffer | |refilled|
// | | | buffer |
// | | | |
// | Part1 | Part 2 | Part3 |
// +-------------+------------------------------------------------+--------+
int b_read (b_io_fd fd, char * buffer, int count)
{
if (startup == 0) b_init(); //Initialize our system
// check that fd is between 0 and (MAXFCBS-1)
if ((fd < 0) || (fd >= MAXFCBS))
{
return (-1); //invalid file descriptor
}
if(fcbArray[fd].fi == NULL) return -1;
b_fcb *fcb = &fcbArray[fd];
// Check for end of file
if(fcb->filePosition >= fcb->fileSize){
return 0;
}
int bytesLeft = fcb->fileSize - fcb->filePosition;
int bytesToCopy = (count <bytesLeft) ? count : bytesLeft;
int totalCopied = 0;
// Copy remaining data in buffer
if(fcb->index < fcb->buflen){
int remaining = fcb->buflen - fcb->index;
int copySize = (bytesToCopy < remaining) ? bytesToCopy : remaining;
memcpy(buffer, fcb->buff + fcb->index, copySize);
fcb->index += copySize;
fcb->filePosition += copySize;
bytesToCopy -= copySize;
totalCopied += copySize;
buffer += copySize;
}
// Read full blocks directly into buffer
if(bytesToCopy >= B_CHUNK_SIZE){
int fullBlocks = bytesToCopy / B_CHUNK_SIZE;
uint64_t blockOffset = fcb->filePosition / B_CHUNK_SIZE;
uint64_t blocksRead = LBAread(buffer, fullBlocks, fcb->fi->mem.extents->block + blockOffset);
if(blocksRead != fullBlocks){
return totalCopied; // Partial read
}
int bytesRead = fullBlocks * B_CHUNK_SIZE;
fcb->filePosition += bytesRead;
bytesToCopy -= bytesRead;
totalCopied += bytesRead;
buffer += bytesRead;
fcb->index = 0;
fcb->buflen = 0;
}
// Load next block into buffer and copy remaining bytes
if(bytesToCopy > 0){
uint64_t blockOffset = fcb->filePosition / B_CHUNK_SIZE;
uint64_t result = LBAread(fcb->buff, 1, fcb->fi->mem.extents->block + blockOffset);
if(result != 1){
return totalCopied; // Error or Partial read
}
fcb->buflen = B_CHUNK_SIZE;
fcb->index = 0;
int offset = fcb->filePosition % B_CHUNK_SIZE;
int available = B_CHUNK_SIZE - offset;
int copySize = (bytesToCopy < available) ? bytesToCopy : available;
memcpy(buffer, fcb->buff + offset, copySize);
fcb->index = offset + copySize;
fcb->filePosition += copySize;
totalCopied += copySize;
}
return totalCopied;
}
// Interface to Close the file
int b_close (b_io_fd fd)
{
if(startup == 0) return 0;
if(fd < 0 || fd >= MAXFCBS) return -1;
b_fcb *fcb = &fcbArray[fd];
if(fcb->buff == NULL || fcb->fi == NULL) {
return -1;
}
// flush write buffer if there are unwritten data
if(fcb->index > 0){
uint64_t blockOffset = fcb->filePosition / B_CHUNK_SIZE;
uint64_t writeResult = LBAwrite(fcb->buff,1,fcb->fi->mem.extents->block + blockOffset);
if (writeResult != 1) return -1;
fcb->filePosition += fcb->index;
if (fcb->filePosition > fcb->fileSize){
fcb->fileSize = fcb->filePosition;
}
}
// free buffer
free(fcb->buff);
fcb->buff = NULL;
// Clear FCB fields
fcb->index = 0;
fcb->buflen = 0;
fcb->filePosition = 0;
fcb->fileSize = 0;
fcb->fi = NULL;
return 0;
}