-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileDescriptor.cc
181 lines (152 loc) · 4.05 KB
/
FileDescriptor.cc
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
/** Rohit Sindhu [sindh010]
* Aravind Alagiri Ramkumar [alagi005]
* Aparna Mahadevan [mahad028]
*/
#include "FileDescriptor.h"
#include "Kernel.h"
#include <stdlib.h>
#include <string.h>
FileDescriptor::FileDescriptor(FileSystem * newFileSystem, IndexNode &newIndexNode, int newFlags)
{
deviceNumber = -1 ;
indexNodeNumber = -1 ;
offset = 0 ;
flags = newFlags ;
fileSystem = newFileSystem;
//copy index node info
newIndexNode.copy(indexNode);
bytes = new char[fileSystem->getBlockSize()];
memset(bytes, '\0', fileSystem->getBlockSize());
}
FileDescriptor::~FileDescriptor()
{
if(bytes != NULL)
{
delete[] bytes;
}
}
void FileDescriptor::setDeviceNumber(short newDeviceNumber)
{
deviceNumber = newDeviceNumber;
}
short FileDescriptor::getDeviceNumber()
{
return deviceNumber;
}
IndexNode * FileDescriptor::getIndexNode()
{
return &indexNode;
}
void FileDescriptor::setIndexNodeNumber(short newIndexNodeNumber)
{
indexNodeNumber = newIndexNodeNumber;
}
short FileDescriptor::getIndexNodeNumber()
{
return indexNodeNumber;
}
int FileDescriptor::getFlags()
{
return flags;
}
char * FileDescriptor::getBytes()
{
return bytes;
}
short FileDescriptor::getMode()
{
return indexNode.getMode();
}
int FileDescriptor::getSize()
{
return indexNode.getSize();
}
void FileDescriptor::setSize(int newSize)
{
indexNode.setSize(newSize);
// write the inode
fileSystem->writeIndexNode(&indexNode, indexNodeNumber);
}
short FileDescriptor::getBlockSize()
{
return fileSystem->getBlockSize();
}
int FileDescriptor::getOffset()
{
return offset;
}
void FileDescriptor::setOffset(int newOffset)
{
offset = newOffset;
}
int FileDescriptor::readBlock(short relativeBlockNumber)
{
// relative block number should be less than direct blocks number plus number of blocks values stored in indirect block
if(relativeBlockNumber > IndexNode::MAX_DIRECT_BLOCKS + (getBlockSize() / 3) )
{
Kernel::setErrno(Kernel::EFBIG);
return -1 ;
}
// ask the IndexNode for the actual block number
// given the relative block number
int blockOffset = indexNode.getBlockAddress(relativeBlockNumber, (void *) fileSystem);
int blockSize = fileSystem->getBlockSize();
if(blockOffset == FileSystem::NOT_A_BLOCK)
{
// clear the bytes if it's a block that was never written
for(int i=0;i<blockSize;i++)
{
bytes[i] = (char)0 ;
}
}
else
{
memset(bytes, '\0', blockSize);
// read the actual block into bytes
fileSystem->read(bytes, fileSystem->getDataBlockOffset() + blockOffset);
}
return 0 ;
}
int FileDescriptor::writeBlock(short relativeBlockNumber)
{
// relative block number should be less than direct blocks number plus number of blocks values stored in indirect block
if(relativeBlockNumber > IndexNode::MAX_DIRECT_BLOCKS + (getBlockSize() / 3))
{
Kernel::setErrno( Kernel::EFBIG ) ;
return -1 ;
}
// If there is no indirect block, allocate a indirect block
if ( relativeBlockNumber > IndexNode::MAX_DIRECT_BLOCKS - 1
&& indexNode.getIndirectBlock() == FileSystem::NOT_A_BLOCK){
int blockOffset = fileSystem->allocateBlock() ;
if( blockOffset < 0 )
{
return -1 ;
}
indexNode.setIndirectBlock(blockOffset);
// TODO: Fix this
for (int i = IndexNode::MAX_DIRECT_BLOCKS ; i < (IndexNode::MAX_DIRECT_BLOCKS + (getBlockSize() / 3) + 1) ; i++) {
indexNode.setBlockAddress(i, FileSystem::NOT_A_BLOCK, (void*) fileSystem);
}
fileSystem->writeIndexNode(&indexNode, indexNodeNumber);
}
// ask the IndexNode for the actual block number
// given the relative block number
int blockOffset = indexNode.getBlockAddress(relativeBlockNumber, (void*) fileSystem);
if(blockOffset == FileSystem::NOT_A_BLOCK)
{
// allocate a block; quit if we can't
blockOffset = fileSystem->allocateBlock() ;
if( blockOffset < 0 )
{
return -1 ;
}
// update the inode
indexNode.setBlockAddress(relativeBlockNumber, blockOffset, (void*) fileSystem);
// write the inode
fileSystem->writeIndexNode(&indexNode, indexNodeNumber);
}
// write the actual block from bytes
fileSystem->write(bytes, fileSystem->getDataBlockOffset() + blockOffset);
return 0 ;
}