-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHardDisk.cpp
More file actions
311 lines (259 loc) · 8.51 KB
/
HardDisk.cpp
File metadata and controls
311 lines (259 loc) · 8.51 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
#include "HardDisk.h"
#include "Node.h"
void HardDisk::readSectors()
{
this->sectors.clear();
std::string fileName = "freeSectors.dat";
std::ifstream binaryFile;
int size = 0;
int sector = -1;
binaryFile.open(fileName, std::ios::in | std::ios::binary);
binaryFile.seekg(0, std::ios::end);
size = (int)binaryFile.tellg();
binaryFile.seekg(0, std::ios::beg);
while(binaryFile.tellg() < size)
{
binaryFile.read((char*)§or, sizeof(sector));
this->sectors.push_back(sector);
}
binaryFile.close();
}
void HardDisk::initializeSectors()
{
readSectors();
}
HardDisk::HardDisk()
{
this->numberDisks = 4;
getcwd(this->cwd, sizeof(cwd));
if (FILE *file = fopen("disk0.dat", "r"))
{
std::cout << "Loading Hard Drives..." << std::endl;
}
else
{
std::cout << "Hard Drive not found. Creating one with default size (120Kb)..." << std::endl;
format(120000);
}
initializeSectors();
//Create slaves
for (int i = 0; i < this->numberDisks; i++)
{
MPI_Comm* communicator = new MPI_Comm[1];
MPI_Comm_spawn("slave", MPI_ARGV_NULL, 1, MPI_INFO_NULL, 0, MPI_COMM_SELF, communicator, MPI_ERRCODES_IGNORE);
int newID = this->comm.size();
this->comm.push_back(communicator);
MPI_Send(&newID, 1, MPI_INT, 0, 0, *communicator);
}
}
HardDisk::~HardDisk()
{
//Delete slaves
for (int i = 0; i < this->numberDisks; i++)
{
delete this->comm.at(i);
}
}
int HardDisk::getHddInCharge(int block)
{
int result = block % this->numberDisks;
return result;
}
void HardDisk::overrideSectors()
{
std::ofstream sectorsFile;
std::string string_cwd = std::string(this->cwd);
string_cwd += "/freeSectors.dat";
for (std::vector<int>::iterator it = this->sectors.begin(); it != this->sectors.end(); it++)
{
int number = *it;
sectorsFile.write((char*)&number, sizeof(int));
}
sectorsFile.close();
}
/*Subir*/
bool HardDisk::writeFile(Node* fileNode)
{
int restSize = -1;
bool flagRest = false;
bool diskFull = false;
//std::string string_cwd = std::string(this->cwd);
char cwd[PATH_MAX];
getcwd(cwd, sizeof(cwd));
std::string string_cwd = std::string(cwd);
//dividir el archivo en bloques (restante a cero)
off_t fileSize = fileNode->getByteSize();
const off_t blockSize = BLOCK_SIZE;
int numberOfBlocks = fileSize / blockSize;
if ((fileSize % blockSize) != 0)
{
numberOfBlocks++;
restSize = (numberOfBlocks*BLOCK_SIZE) - fileSize;
flagRest = true;
}
int pos = 0; //Last position of reader
//en un for, por cada bloque leer esa parte, mirar que disco está vacio, escribir
//se puede enviar el array de blocks y el array de datos
for (int i = 0; i < numberOfBlocks; i++)
{
std::ifstream fs;
char* binaryData = (char*)malloc(sizeof(char)*BLOCK_SIZE);
//Open File and Check size
fs.open(string_cwd + "/" + fileNode->getName(), std::ios::in | std::ios::binary);
if (!fs.is_open()) std::cout << "Cannot open file." << std::endl;
fs.seekg(pos, fs.beg);
fs.read((char*)binaryData, sizeof(char)*BLOCK_SIZE);
if (i == numberOfBlocks -1)
{
memset(binaryData+restSize, 0, (BLOCK_SIZE-restSize)*sizeof(char));
}
pos += BLOCK_SIZE;
if (this->sectors.empty())
{
std::cout << "Disk is empty, deleting " << fileNode->getName() << "..." << std::endl;
this->deleteNode(fileNode);
diskFull = true;
fs.close();
return false;
}
else
{
int HDD = getHddInCharge(this->sectors.at(0));
int block = this->sectors.at(0) / this->numberDisks;
(*fileNode).setBlock(this->sectors.at(0));
this->sectors.erase(this->sectors.begin());
int option = 10;
int size = sizeof(char)*BLOCK_SIZE;
MPI_Send(&option, 1, MPI_INT, 0, 0, *this->comm[HDD]);
MPI_Send(&block, 1, MPI_INT, 0, 0, *this->comm[HDD]);
MPI_Send(&size, 1, MPI_INT, 0, 0, *this->comm[HDD]);
MPI_Send(binaryData, size, MPI_CHAR, 0, 0, *this->comm[HDD]);
fs.close();
free(binaryData);
}
}
if(!diskFull)
{
//Actualizar el fichero de sectores
(*fileNode).setNumBlocksOccupied();
overrideSectors();
}
return true;
}
void HardDisk::readFile(Node* fileNode)
{
MPI_Status status;
std::ofstream file;
std::ifstream disk;
std::string string_cwd = std::string(this->cwd);
string_cwd += "/";
string_cwd += fileNode->getName();
file.open(string_cwd, std::ios::out | std::ios::binary | std::ios::trunc);
if (!file.is_open()) std::cout << "Cannot open file." << std::endl;
string_cwd = std::string(this->cwd);
std::vector<int>* blocks = fileNode->getBlockLocations();
for (int i = 0; i < fileNode->getNumBlocksOccupied()-1; i++)
{
int hdd = blocks->at(i) % this->numberDisks;
//saco bloque
int block = blocks->at(i) / this->numberDisks;
int size = BLOCK_SIZE;
char* binaryData = NULL;
//leo datos
int option = 11;
disk.open(string_cwd + "/disk" + std::to_string(hdd) + ".dat", std::ios::in | std::ios::binary);
MPI_Send(&option, 1, MPI_INT, 0, 0, *this->comm[hdd]);
MPI_Send(&block, 1, MPI_INT, 0, 0, *this->comm[hdd]);
MPI_Send(&size, 1, MPI_INT, 0, 0, *this->comm[hdd]);
binaryData = (char*)malloc(sizeof(char)*size);
MPI_Recv(binaryData, size, MPI_CHAR, 0, 0, *this->comm[hdd], &status);
file.seekp(BLOCK_SIZE*i);
file.write((char*)binaryData, sizeof(char)*BLOCK_SIZE);
//free
disk.close();
free(binaryData);
}
int hdd = (fileNode->getNumBlocksOccupied()-1) % this->numberDisks;
//saco bloque
int block = blocks->at(fileNode->getNumBlocksOccupied()-1) / this->numberDisks;
int size = fileNode->getByteSize() - ((fileNode->getNumBlocksOccupied()-1)*BLOCK_SIZE);;
char* binaryData = NULL;
//leo datos
disk.open(string_cwd + "/disk" + std::to_string(hdd) + ".dat", std::ios::in | std::ios::binary);
int option = 11;
MPI_Send(&option, 1, MPI_INT, 0, 0, *this->comm[hdd]);
MPI_Send(&block, 1, MPI_INT, 0, 0, *this->comm[hdd]);
MPI_Send(&size, 1, MPI_INT, 0, 0, *this->comm[hdd]);
binaryData = (char*)malloc(sizeof(char)*size);
MPI_Recv(binaryData, size, MPI_CHAR, 0, 0, *this->comm[hdd], &status);
file.seekp(BLOCK_SIZE*(fileNode->getNumBlocksOccupied() - 1));
file.write((char*)binaryData, sizeof(char)*size);
//free
disk.close();
file.close();
free(binaryData);
}
void HardDisk::formatDisk()
{
std::ofstream diskFile;
for (int i = 0; i < this->numberDisks; i++)
{
std::string string_cwd = std::string(this->cwd);
string_cwd += "/disk";
string_cwd += std::to_string(i);
string_cwd += ".dat";
diskFile.open(string_cwd, std::ios::in | std::ios::binary | std::ios::trunc);
diskFile.close();
}
}
void HardDisk::formatSectors()
{
std::ofstream sectorsFile;
std::string string_cwd = std::string(this->cwd);
string_cwd += "/freeSectors.dat";
sectorsFile.open(string_cwd, std::ios::in | std::ios::binary | std::ios::trunc);
for (int i = 0; i < this->diskSize; i++)
{
sectorsFile.write((char*)&i, sizeof(i));
}
sectorsFile.close();
}
bool HardDisk::checkIfExistsHDD()
{
for(int i = 0; i < this->numberDisks; i++)
{
std::string string_cwd = std::string(this->cwd);
string_cwd += "/disk";
string_cwd += std::to_string(i);
string_cwd += ".dat";
if (FILE *file = fopen("disk0.dat", "r"))
{
std::cout << "Loading Hard Drive " << i << std::endl;
}
else return false;
}
return true;
}
void HardDisk::format(int size)
{
std::cout << "Creating Hard Drives..." << std::endl;
std::cout << "Selected total size is: " << size << std::endl;
this->diskSize = size;
formatDisk();
formatSectors();
initializeSectors();
remove("tree.dat");
}
int HardDisk::getNumberOfDisks()
{
return this->numberDisks;
}
void HardDisk::deleteNode(Node* fileNode)
{
std::vector<int>* locations = fileNode->getBlockLocations();
for(int i = locations->size()-1; i >= 0; i--)
{
int loc = locations->at(i);
this->sectors.insert(this->sectors.begin(), loc);
}
}