-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanageFileSize.c
More file actions
109 lines (89 loc) · 3.2 KB
/
Copy pathmanageFileSize.c
File metadata and controls
109 lines (89 loc) · 3.2 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
/**************************************************************
* Class:: CSC-415-02 Spring 2024
* Name:: Tushin Kulshreshtha, Hayden Coke, Eric Khuong
* Student IDs:: 922180763, 921741974, 923406338
* GitHub-Name:: Dextron04, crowcode17, ekhuong
* Group-Name:: Something Simple
* Project:: Basic File System
*
* File:: manageFileSize.c
*
* Description:: The file contains functions to assist in writing files.
*
*
**************************************************************/
#include "b_io.h"
#include "fileSystem.h"
#define NO_LOCATION -1
// returns how much the file was grown by
int growSpace(b_fcb * fcb, int bytesNeeded){
// Will determine the minimum number of blocks that will be allocated
// to accomodate the newley added files
int filePos = getFilePosition(*fcb);
int minBlocks = bytesToBlocks(bytesNeeded + filePos);
int newBlocks = minBlocks;
// if the minBlocks is greater than the extraBlocks then the
// the value of the extraBlocks and the newBlocks that will be allocated
// gets updated.
if(minBlocks > fcb->extraBlocks){
fcb->extraBlocks += minBlocks;
newBlocks += fcb->extraBlocks;
}
// Allocate new blocks based on the above calculation in full block size
int newLocation = allocateBlocks(newBlocks * BlockSize);
// The function will link the end of the old chain and will attach it to the
// head of the newley allocated blocks.
if(fcb->fi->location != NO_LOCATION) {
int fcbSentinel = findSentinel(fcb->fi->location);
linkChain(fcbSentinel, newLocation);
} else {
fcb->fi->parent[fcb->fi->index].location = newLocation;
fcb->fi->location = newLocation;
}
fcb->fi->fileSize += (newBlocks * BlockSize);
// save actual DE to reflect changes to the file
fcb->fi->parent[fcb->fi->index].size = fcb->fi->fileSize;
saveDir(fcb->fi->parent);
fcb->preAllocatedBlocks += newBlocks;
return newBlocks;
}
// This function will check if a file requires a grow.
int checkGrow(b_fcb * fcb, int count){
int filePos = getFilePosition(*fcb);
if(filePos + count > fcb->fi->fileSize){
growSpace(fcb, count);
return 1; // true if grow is necessary
}
return 0; // false if grow is not necessary
}
// Will shrink the space as soon as the file is closed either after
// creation or updation.
int shrinkSpace(b_fcb * fcb){
int deallocateLocation;
// it will calculate the exact number of bytes that will be written to the disk
// and will shrink the space upto the block next to the data location that was
// recently added.
if(fcb->maxFilePos == 0) {
// deallocate ENTIRE file
fcb->fi->parent[fcb->fi->index].location = NO_LOCATION;
deallocateLocation = fcb->fi->location;
} else {
fcb->fi->parent[fcb->fi->index].size = fcb->maxFilePos;
// Convert the maxfilePos to blocks
int actualBlocksUsed = bytesToBlocks(fcb->maxFilePos);
int lastBlock = fcb->fi->location;
if(actualBlocksUsed > 1) {
lastBlock = getBlockLocation(fcb, actualBlocksUsed);
deallocateLocation = getNextBlock(lastBlock);
} else {
saveDir(fcb->fi->parent);
return 1;
}
}
// deallocate blocks from the calculated new location and save
// the updated directory.
deallocateBlocks(deallocateLocation);
saveDir(fcb->fi->parent);
// Successful shrink will return 0.
return 0;
}