-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfile.c
536 lines (456 loc) · 16 KB
/
file.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
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "file.h"
#include "inode.h"
#include "parameters.h"
#include "builder.h"
#include "LibDisk.h"
#include "directory.h"
#include "LibFS.h"
#include <string.h>
#include <stdio.h>
#include "LibDisk.h"
// the fileTable in memory (static makes it private to the file)
static FileTableEntry* fileTable;
int addFile(int parentInode, char* fileName)
{
printf("filename : %s\n", fileName);
printf("parentInode : %d\n", parentInode);
// Get an inode block
int inodeIndex = FindNextAvailableInodeBlock();
if(inodeIndex < 0)
return -1;
// Get a data block
int dataIndex = FindNextAvailableDataBlock();
if(dataIndex < 0)
return -1;
// update bitmap blocks of inode and data blocks
ChangeInodeBitmapStatus(inodeIndex, OCCUPIED);
ChangeDataBitmapStatus(dataIndex, OCCUPIED);
char* inodeBlock = calloc(sizeof(char), SECTOR_SIZE/INODE_PER_BLOCK_NUM);
BuildInode(inodeBlock, FILE_ID);
InitializeFileInode(inodeBlock, dataIndex);
// check if failed to write inode in disk
if(WriteInodeInSector(inodeIndex, inodeBlock) == -1)
{
free(inodeBlock);
return -1;
}
free(inodeBlock);
char* dataBlock = calloc(sizeof(char), SECTOR_SIZE);
// check if failed to write data block in disk
if(Disk_Write(DATA_FIRST_BLOCK_INDEX + dataIndex, dataBlock) == -1)
{
printf("Failed to write datablock to disk");
free(dataBlock);
return -1;
}
DirectoryEntry * dirEntry = (DirectoryEntry *) malloc ( sizeof(DirectoryEntry));
dirEntry->inodePointer = inodeIndex;
strcpy(dirEntry->pathName, fileName);
if(addDirectoryEntry(parentInode, dirEntry) != 0)
{
free(dirEntry);
return -1;
}
// file create successfully...
free(dataBlock);
free(dirEntry);
return 0;
}
int CreateFileTable()
{
fileTable = (FileTableEntry *) calloc(OPEN_FILE_NUM_MAX, sizeof(FileTableEntry));
if(fileTable == NULL)
return -1;
return 0;
}
int getAvailabeFileDescriptor()
{
int i;
for(i = 0; i < OPEN_FILE_NUM_MAX; i++)
{
if(fileTable[i].isValid == NOT_VALID)
return i;
}
return -1;
}
int getInodePointerOfFileEntry(int fd)
{
if(fd < 0 || fd > OPEN_FILE_NUM_MAX)
return -1;
return fileTable[fd].inodePointer;
}
int updateFilePointerOfFileEntry(int fd, int filePointer)
{
if(fd < 0 || fd > OPEN_FILE_NUM_MAX)
return -1;
fileTable[fd].filePointer = filePointer;
return 0;
}
int SizeOfFile(int inodeNumber)
{
char* inodeBuffer=calloc(sizeof(char),INODE_SIZE);
char* inodeSegmentPointerToSector =calloc(sizeof(char),sizeof(int));
char* sectorBuffer=calloc(sizeof(char),SECTOR_SIZE);
char* sizeBuffer=calloc(sizeof(char),sizeof(int));
int inodePointerToSectorNumber;
int fileSize;
// Read the inode
if( ReadInode(inodeNumber, inodeBuffer) == -1)
{
printf("Disk failed to read inode block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
free(sizeBuffer);
return -1;
}
// Check that inode is File FILE_ID=0x80 , DIRECORY_ID=0x00
if (!(inodeBuffer[0] & FILE_ID))
{
printf("Inode is not directory, it is file.\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
free(sizeBuffer);
return -1;
}
//find appropriate sector which shows the size
memcpy((void*)inodeSegmentPointerToSector,(void*)inodeBuffer+META_DATA_PER_INODE_BYTE_NUM,sizeof(int));
inodePointerToSectorNumber=StringToInt(inodeSegmentPointerToSector);
//read the appropriate sector and write in sectorBuffer
if( Disk_Read(DATA_FIRST_BLOCK_INDEX + inodePointerToSectorNumber, sectorBuffer) == -1)
{
printf("Disk failed to read sector block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
free(sizeBuffer);
return -1;
}
//find size and convert it into int
memcpy((void*)sizeBuffer,(void*)sectorBuffer,sizeof(int));
fileSize=StringToInt(sizeBuffer);
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
free(sizeBuffer);
return fileSize;
}
int DataBlocksOccupiedByFile(int inodeNumber,int* sectorNumbers)
{
char* inodeBuffer=calloc(sizeof(char),INODE_SIZE);
char* inodeSegmentPointerToSector =calloc(sizeof(char),sizeof(int));
int inodePointerToSectorNumber;
int i;
int entryOccupiedNumber;
int fileSize=0;
// Find size of file
fileSize=SizeOfFile(inodeNumber);
if(fileSize == -1)
{
return -1;
}
// Number of occupied entry by file
entryOccupiedNumber=min(SECTOR_PER_FILE_MAX,(fileSize+sizeof(int)-1)/SECTOR_SIZE+1);
// Read the inode
if( ReadInode(inodeNumber, inodeBuffer) == -1)
{
printf("Disk failed to read inode block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
return -1;
}
//find appropriate sector and looking for search word
for (i=0;i<entryOccupiedNumber;i++)
{
//find sector number
memcpy((void*)inodeSegmentPointerToSector,(void*)inodeBuffer+META_DATA_PER_INODE_BYTE_NUM+i*sizeof(int),sizeof(int));
inodePointerToSectorNumber=StringToInt(inodeSegmentPointerToSector);
// add this sector numbers and increase counter
sectorNumbers[i]=inodePointerToSectorNumber;
}
free(inodeBuffer);
free(inodeSegmentPointerToSector);
return entryOccupiedNumber;
}
int openFileDescriptor(char *path)
{
int fd = getAvailabeFileDescriptor();
if(fd < 0)
{
osErrno = E_TOO_MANY_OPEN_FILES;
return -1;
}
// allocate memory for storing string...
char* array[128];
char myPath[256];
// make a copy of path to modify
strcpy(myPath, path);
// tokenize path and make array of path elements...
int i = BreakPathName(myPath, array);
int parent;
int current;
if(findLeafInodeNumber(myPath, array, i, &parent, ¤t, 0) != 0)
{
osErrno = E_NO_SUCH_FILE;
return -1;
}
fileTable[fd].fileDescriptor = fd;
fileTable[fd].inodePointer = current;
fileTable[fd].filePointer = 0;
fileTable[fd].sizeOfFile = SizeOfFile(current);
fileTable[fd].isValid = VALID;
strcpy(fileTable[fd].fileName, array[i-1]);
strcpy(fileTable[fd].filePath, path);
return fd;
}
int isFileOpen(int fd)
{
if(fd < 0 || fd > OPEN_FILE_NUM_MAX)
return -1;
if(fileTable[fd].isValid == VALID)
return 0;
return -1;
}
int removeFileTableEntry(int fd)
{
if(fd < 0 || fd > OPEN_FILE_NUM_MAX)
return -1;
fileTable[fd].isValid = NOT_VALID;
return 0;
}
int FileRead(int fd, char *buffer, int size){
char* inodeBuffer=calloc(sizeof(char),INODE_SIZE);
char* inodeSegmentPointerToSector =calloc(sizeof(char),sizeof(int));
char* sectorBuffer=calloc(sizeof(char),SECTOR_SIZE);
// determine how many bytes we must read
int readSize;
if(fileTable[fd].sizeOfFile-fileTable[fd].filePointer<0)
{
readSize=0;
}
else
{
readSize=min(fileTable[fd].sizeOfFile-fileTable[fd].filePointer,size);
}
int transferedSize=0;
int inodePointerToSectorNumber;
int i;
// Number of occupied entry by file
int entryOccupiedNumber=min(SECTOR_PER_FILE_MAX,(fileTable[fd].sizeOfFile+sizeof(int)-1)/SECTOR_SIZE+1);
// Read the inode
if( ReadInode(fileTable[fd].inodePointer, inodeBuffer) == -1)
{
printf("Disk failed to read inode block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
//find appropriate sectors and reading from them and save into buffer
for (i=0;i<entryOccupiedNumber;i++)
{
//find sector number
memcpy((void*)inodeSegmentPointerToSector,(void*)inodeBuffer+META_DATA_PER_INODE_BYTE_NUM+i*sizeof(int),sizeof(int));
inodePointerToSectorNumber=StringToInt(inodeSegmentPointerToSector);
//read the appropriate sector and write in sectorBuffer
if( Disk_Read(DATA_FIRST_BLOCK_INDEX + inodePointerToSectorNumber, sectorBuffer) == -1)
{
printf("Disk failed to read sector block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
//transfering data
if (i==0 && fileTable[fd].filePointer<(SECTOR_SIZE-sizeof(int)))
{
memcpy(buffer+transferedSize,sectorBuffer+sizeof(int)+fileTable[fd].filePointer,min(SECTOR_SIZE-sizeof(int),readSize));
transferedSize=transferedSize + min(SECTOR_SIZE-sizeof(int),readSize);
fileTable[fd].filePointer=fileTable[fd].filePointer+min(SECTOR_SIZE-sizeof(int),readSize);
}
else if (i==((fileTable[fd].filePointer+sizeof(int))/SECTOR_SIZE))
{
memcpy(buffer+transferedSize,sectorBuffer+((fileTable[fd].filePointer+sizeof(int))%SECTOR_SIZE),
min(SECTOR_SIZE,readSize-transferedSize));
transferedSize=transferedSize+min(SECTOR_SIZE,readSize-transferedSize);
fileTable[fd].filePointer=fileTable[fd].filePointer+min(SECTOR_SIZE,readSize-transferedSize);
}
if(transferedSize==readSize)
{
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return transferedSize;
}
if(transferedSize>readSize)
{
printf("Some error in transfering data\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
}
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return transferedSize;
}
//return -1 for errors
int FileWrite(int fd, char* buffer , int size)
{
char* inodeBuffer=calloc(sizeof(char),INODE_SIZE);
char* inodeSegmentPointerToSector =calloc(sizeof(char),sizeof(int));
char* sectorBuffer=calloc(sizeof(char),SECTOR_SIZE);
// determine how many bytes we must read
int transferedSize=0;
int newSizeOfFile;
int occupiedEntryBeforeWrite=(fileTable[fd].sizeOfFile+sizeof(int)-1)/SECTOR_SIZE+1;
int inodePointerToSectorNumber;
int i;
//determine new size of file
newSizeOfFile=max(fileTable[fd].sizeOfFile,fileTable[fd].filePointer+size);
if(newSizeOfFile>SECTOR_PER_FILE_MAX*SECTOR_SIZE-sizeof(int))
{
printf("There is not enough space to write\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
// Read the inode
if( ReadInode(fileTable[fd].inodePointer, inodeBuffer) == -1)
{
printf("Disk failed to read inode block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
//find appropriate sectors and reading from them and save into buffer
for (i=0;i<SECTOR_PER_FILE_MAX;i++)
{
//check for unvalid sector number
if (i>=occupiedEntryBeforeWrite)
{
inodePointerToSectorNumber=FindNextAvailableDataBlock();
if(inodePointerToSectorNumber==-1)
{
printf("There is not enough space\n");
osErrno=E_NO_SPACE;
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
if(ChangeDataBitmapStatus(inodePointerToSectorNumber, OCCUPIED)==-1)
{
printf("Some error has occured while changing data block bitmap\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
if(UpdateInodeDataSectorNumber(fileTable[fd].inodePointer,i,inodePointerToSectorNumber )==-1)
{
printf("Some error has occured while changing inode sector numbers\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
}
else
{
//find sector number
memcpy((void*)inodeSegmentPointerToSector,(void*)inodeBuffer+META_DATA_PER_INODE_BYTE_NUM+i*sizeof(int),sizeof(int));
inodePointerToSectorNumber=StringToInt(inodeSegmentPointerToSector);
}
//read the appropriate sector and write in sectorBuffer
if( Disk_Read(DATA_FIRST_BLOCK_INDEX + inodePointerToSectorNumber, sectorBuffer) == -1)
{
printf("Disk failed to read sector block\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
// write the new size in appropriate part
if ( i== 0)
{
sectorBuffer[0]=newSizeOfFile;
}
//transfering data
if (i==0 && fileTable[fd].filePointer<(SECTOR_SIZE-sizeof(int)))
{
memcpy(sectorBuffer+sizeof(int)+fileTable[fd].filePointer,buffer+transferedSize,min(SECTOR_SIZE-sizeof(int),size));
transferedSize=transferedSize + min(SECTOR_SIZE-sizeof(int),size);
fileTable[fd].filePointer=fileTable[fd].filePointer+min(SECTOR_SIZE-sizeof(int),size);
}
else if (i==((fileTable[fd].filePointer+sizeof(int))/SECTOR_SIZE))
{
memcpy(sectorBuffer+((fileTable[fd].filePointer+sizeof(int))%SECTOR_SIZE),buffer+transferedSize,min(SECTOR_SIZE,size-transferedSize));
transferedSize=transferedSize+min(SECTOR_SIZE,size-transferedSize);
fileTable[fd].filePointer=fileTable[fd].filePointer+min(SECTOR_SIZE,size-transferedSize);
}
if(transferedSize>size)
{
printf("Some error in transfering data. Note: some sectors have changed\n");
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
// write sectorBuffer in disk
if( Disk_Write(DATA_FIRST_BLOCK_INDEX + inodePointerToSectorNumber, sectorBuffer) == -1)
{
printf("Disk failed to write changed block\n");
osErrno=E_NO_SPACE;
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
return -1;
}
if(transferedSize==size)
{
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
fileTable[fd].sizeOfFile = newSizeOfFile;
return transferedSize;
}
}
free(inodeBuffer);
free(inodeSegmentPointerToSector);
free(sectorBuffer);
fileTable[fd].sizeOfFile = newSizeOfFile;
return transferedSize;
}
void printFileTableEntry(int fd)
{
if(fileTable[fd].isValid == NOT_VALID)
return;
printf("|%-4d |", fd);
printf("%-8d |", fileTable[fd].inodePointer);
printf("%-8d |", fileTable[fd].filePointer);
printf("%-8d|", fileTable[fd].sizeOfFile);
if(fileTable[fd].isValid == VALID)
printf("V |");
else
printf("I |");
printf("%-16s|", fileTable[fd].fileName);
printf("%-24s|\n", fileTable[fd].filePath);
}
void printFileTable()
{
int i;
puts("\n+-----------------+----------+--------+--+----------------+------------------------+");
puts("|fd |inode |fp |fz |V |filename |filepath |");
puts("+------+----------+----------+--------+--+----------------+------------------------+");
for(i = 0; i < OPEN_FILE_NUM_MAX; i++)
{
printFileTableEntry(i);
}
puts("+------+----------+----------+--------+--+----------------+------------------------+\n");
}