-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPartition.cpp
More file actions
223 lines (191 loc) · 6.53 KB
/
MyPartition.cpp
File metadata and controls
223 lines (191 loc) · 6.53 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
#include "MyPartition.h"
#include "part.h"
#include "MemoryModule.h"
#include "defs.h"
#include "MemoryManagment.h"
#include "DirectoryManagment.h"
#include "OpenFilesTable.h"
#include "Monitor.h"
MyPartition::MyPartition(Partition *partition) : myPartition(partition){
unsigned long numOfClusters = partition->getNumOfClusters();
partitionWrapper = new PartitionWrapper(partition, numOfClusters);
partitionBufferedWriter = new BufferedWriter(PARTITION_CACHE_SIZE_OF_ENTRY, partitionWrapper);
partitionCache = new ThreadSafeBlockCache(PARTITION_CACHE_NUM_OF_ENTRIES, PARTITION_CACHE_SIZE_OF_ENTRY, PARTITION_CACHE_LRU_REQUEST_BUFFER_SIZE, partitionBufferedWriter);
partitionMemoryManagmentUnit = new PartitionMemoryManagmentUnit(numOfClusters, partitionCache);
directoyManagmentUnit = new DirectoryManagmentUnit((numOfClusters + ClusterSize - 1) / ClusterSize, partitionCache, partitionMemoryManagmentUnit);
openFilesTable = new OpenFilesTable(OPEN_FILES_TABLE_SIZE);
formatFlag = new Flag();
partitionUseMonitor = new ReaderPreferance();
openDeleteMonitor = new UpgradeMonitor();
}
MyPartition::~MyPartition() {
partitionUseMonitor->startWrite();
delete directoyManagmentUnit;
delete partitionMemoryManagmentUnit;
delete partitionCache;
delete partitionBufferedWriter;
delete partitionWrapper;
delete openFilesTable;
delete partitionUseMonitor;
delete openDeleteMonitor;
delete formatFlag;
}
int MyPartition::readRootDir(EntryNum num, Directory& dir) {
return directoyManagmentUnit->readDirectory(num, dir);
}
char MyPartition::doesExist(const char *fname, const char *fext) {
partitionUseMonitor->startRead();
openDeleteMonitor->startRead();
char ret = 1;
if (openFilesTable->getFileInfo(fname, fext) == nullptr) {
DEntryInfo info = directoyManagmentUnit->getFileInfo(fname, fext);
if (info.entry.name[0] == 0) {
ret = 0;
}
}
openDeleteMonitor->endRead();
partitionUseMonitor->endRead();
return ret;
}
void MyPartition::format() {
formatFlag->setFlag(true);
partitionUseMonitor->startWrite();
openFilesTable->format();
directoyManagmentUnit->format();
partitionMemoryManagmentUnit->format();
partitionUseMonitor->endWrite();
formatFlag->setFlag(false);
}
KernelFile* MyPartition::openFile(const char *fname, const char *fext, char mode) {
if (formatFlag->flagValue()) {
return nullptr;
}
partitionUseMonitor->startRead();
openDeleteMonitor->startRead();
KernelFile *file = nullptr;
FileInfo *info = openFilesTable->getFileInfo(fname, fext);
if (info == nullptr) {
openDeleteMonitor->startWrite();
info = openFilesTable->getFileInfo(fname, fext);
if (info == nullptr) {
DEntryInfo dInfo = directoyManagmentUnit->getFileInfo(fname, fext);
if (dInfo.entry.name[0] != 0) {
info = new FileInfo(dInfo.entry.name, dInfo.entry.ext, dInfo.entry.size, dInfo.entry.indexCluster, dInfo.num);
openFilesTable->insertFileInfo(info);
} else if (mode == 'w' || mode == 'W') {
DEntryInfo finfo;
memcpy(finfo.entry.name, fname, 8);
memcpy(finfo.entry.ext, fext, 3);
finfo.entry.size = 0;
finfo.entry.indexCluster = 0;
finfo.num = directoyManagmentUnit->writeFileData(finfo.entry);
if (finfo.num != ~0UL) {
info = new FileInfo(fname, fext, finfo.entry.size, finfo.entry.indexCluster, finfo.num);
openFilesTable->insertFileInfo(info);
}
}
}
openDeleteMonitor->endWrite();
}
if (info != nullptr) {
info->getDMonitor()->startRead();
info->incNumOfUsers();
}
openDeleteMonitor->endRead();
if (info == nullptr) {
partitionUseMonitor->endRead();
return nullptr;
}
switch (mode) {
case 'r':
case 'R':{
info->getRWMonitor()->startRead();
file = new KernelFileDecorator(new ReadableKernelFile(info->getSize(), info->getIndexCluster(), partitionCache), info, this, mode);
break;
}
case 'w':
case 'W':{
info->getRWMonitor()->startWrite();
KernelFile *writeFile = new WritableKernelFile(info->getSize(), 0, info->getIndexCluster(), partitionCache, partitionMemoryManagmentUnit);
writeFile->truncate();
file = new KernelFileDecorator(writeFile, info, this, mode);
break;
}
case 'a':
case 'A': {
info->getRWMonitor()->startWrite();
file = new KernelFileDecorator(new WritableKernelFile(info->getSize(), info->getSize(), info->getIndexCluster(), partitionCache, partitionMemoryManagmentUnit), info, this, mode);
break;
}
}
return file;
}
void MyPartition::closeFile(FileInfo *info, char mode) {
openDeleteMonitor->startRead();
switch (mode) {
case 'r':
case 'R': {
info->getRWMonitor()->endRead();
break;
}
default: {
info->getRWMonitor()->endWrite();
break;
}
}
info->decNumOfUsers();
if (info->isNumOfUsersZero()) {
char fname[8], fext[3];
memcpy(fname, info->getFname(), 8);
memcpy(fext, info->getFext(), 3);
openDeleteMonitor->startWrite();
FileInfo *finfo = openFilesTable->getFileInfo(fname, fext);
if (finfo != nullptr && finfo->isNumOfUsersZero()) {
finfo = openFilesTable->deleteFileInfo(fname, fext);
if (finfo->getIsChanged()) {
DEntryInfo entry;
entry.num = finfo->getEntryNum();
memcpy(entry.entry.name, finfo->getFname(), 8);
memcpy(entry.entry.ext, finfo->getFext(), 3);
entry.entry.indexCluster = finfo->getIndexCluster();
entry.entry.size = finfo->getSize();
directoyManagmentUnit->updateFileInfo(entry);
}
delete finfo;
info = nullptr;
}
openDeleteMonitor->endWrite();
}
if (info != nullptr) {
info->getDMonitor()->endRead();
}
partitionUseMonitor->endRead();
openDeleteMonitor->endRead();
}
char MyPartition::deleteFile(const char *fname, const char *fext) {
char ret = 0;
FileInfo *finfo = nullptr;
partitionUseMonitor->startRead();
openDeleteMonitor->startRead();
openDeleteMonitor->startWrite();
DEntryInfo info = directoyManagmentUnit->getFileInfo(fname, fext);
if (info.entry.name[0] != 0) {
ret = 1;
directoyManagmentUnit->deleteFileInfo(info.num);
finfo = openFilesTable->getFileInfo(info.entry.name, info.entry.ext);
}
openDeleteMonitor->endWrite();
openDeleteMonitor->endRead();
if (finfo != nullptr) {
finfo->getDMonitor()->startWrite();
KernelFile *file = new WritableKernelFile(finfo->getSize(), 0, finfo->getIndexCluster(), partitionCache, partitionMemoryManagmentUnit);
file->truncate();
delete file;
delete finfo;
}
partitionUseMonitor->endRead();
return ret;
}
Partition* MyPartition::getMyPartition() {
return myPartition;
}