This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileSystem.cpp
464 lines (385 loc) · 12.1 KB
/
FileSystem.cpp
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
/* Name : Dumitru
** Class : CS 330 - 001
** Date : Jan/Feb 2007
**
** Description: FileSystem.cpp
This file implements the functions of the file system class.
******************************************************************************/
#ifndef FILE_SYSTEM_CPP // avoid multiple declarations
#define FILE_SYSTEM_CPP
#include "FileSystem.h"
FileSystem::FileSystem()
/******************************************************************************
Default constructor. Sets the default values of the file system.
******************************************************************************/
{
name = "";
header.maxBytes = 0;
header.maxFiles = 0;
header.usedFiles = 0;
header.usedBytes = 0;
strcpy(header.ver,"");
for(int i = 0; i < MAX_NUM_FILES;i++)
{
strcpy(fiTable[i].name,"");
fiTable[i].length = 0;
fiTable[i].owner = 0;
fiTable[i].permissions = 0;
fiTable[i].startByte = -1;
}
}
void FileSystem::Install(string dName, int nBytes, fstream & output)
/******************************************************************************
Pre: dName and nBytes are valid
Installs a new disk with disk name = dName, and size = nBytes
******************************************************************************/
{
char nullChar = '\0';
fstream outFile;
outFile.open(dName.c_str(),ios_base::out | ios_base::binary);
// check if we can create a new file
if(!outFile)
{ // Error: Cannot create diskfile
output << ERROR[4] << endl; return;
}
// write out null values on disk
for(int i = 0; i < nBytes; i++)
outFile.write(&nullChar,1);
header.maxBytes = nBytes;
// close file stream
outFile.clear(); outFile.close();
}
void FileSystem::Format(string dName, string fsType,
int nBytes, int nFiles, fstream & output)
/******************************************************************************
Pre: dName, fsType, nBytes, nFiles are valid
Formats disk 'dName' with the 'fsTye' file system. The disk can hold up to
nFiles of files.
******************************************************************************/
{
int indexPointer = 0;
// default file to be written out
char defaultFile[] = "passwords.dat\0";
char fileContents[] = "root:\n";
fstream outFile;
outFile.open(dName.c_str(),ios_base::in | ios_base::out | ios_base::binary);
// check if we can read the disk
if(!outFile)
{ // Error: Cannot format disk
output << ERROR[7] << endl; return;
}
if(nBytes < (int)((sizeof(header)
+ sizeof(fiTable[0]) * nFiles) + sizeof(char) * 7))
{ // Error: Invalid parameter
output << ERROR[3] << endl; return;
}
// initialize the file system header
strcpy(header.ver,fsType.c_str());
header.maxBytes = nBytes;
header.maxFiles = nFiles;
header.usedFiles = 1;
header.usedBytes = sizeof(header)
+ sizeof(fileObj) * header.maxFiles
+ sizeof(char) * 7;
// set values for the first file to be on disk
strcpy(fiTable[0].name,defaultFile);
fiTable[0].length = sizeof(char) * 7;
fiTable[0].owner = 0;
fiTable[0].permissions = 0;
fiTable[0].startByte = sizeof(header) + sizeof(fileObj) * header.maxFiles;
// write the header file to disk
indexPointer = WriteHeader(outFile);
// write the file index on disk
indexPointer = WriteFiTable(outFile);
outFile.seekp(indexPointer);
// write the default file contents
outFile.write(reinterpret_cast<const char*>(&fileContents),sizeof(char)*7);
// close file stream
outFile.clear(); outFile.close();
}
void FileSystem::Dump(string dName, int startByte, int nBytes, fstream & output)
/******************************************************************************
Pre: dName, startByte and nBytes are valid
Displays the contents on the disk from startByte to (startByte + nBytes)
It displays each 4 bytes as an integer value as well as each each byte as a
character value.
******************************************************************************/
{
fstream inputFile;
char tempCh;
char theBuffer[4]; // buffer for input
int tempInt = 0;
// cant read past the end of the file
if((startByte + nBytes) > header.maxBytes)
{ // Error: Invalid parameter
output << ERROR[3] << endl; return;
}
inputFile.open(dName.c_str(),ios_base::in | ios_base::binary);
// check disk is available
if(!inputFile)
{ // Error: Cannot read filesystem
output << ERROR[11] << endl; return;
}
output << "++++++++++++++++++++++++++++++++++++++++++" << endl;
// display contents
for(
int i = startByte;
(i < (startByte + nBytes))&&(i < header.maxBytes);
i += 4
)
{
// display the integer value of the 4 bytes
output << "+" << setw(8) << right << i << "|";
inputFile.seekg(i);
inputFile.read(theBuffer,4);
tempInt = (*(reinterpret_cast<int*>(theBuffer)));
output << setw(12); // formatting
if(tempInt < 0)
output << "0";
else output << tempInt;
output << "|";
// displays contents as characters
for(int j = i; j < (i+4); j++)
{
inputFile.seekg(j);
inputFile.read(theBuffer,1);
tempCh = (*(reinterpret_cast<char*>(theBuffer)));
// formatting
if(j == i)
output << setw(5);
else output << setw(4);
// display the character value if it's a valid char
if(isPrintable(tempCh))
output << tempCh;
else output << " ";
}
output << " +" << endl;
}
output << "++++++++++++++++++++++++++++++++++++++++++" << endl;
// close file stream
inputFile.clear(); inputFile.close();
}
/*######################### accessor functions###############################*/
void FileSystem::SetName(string str) { name = str; }
int FileSystem::GetNumMaxBytes() { return header.maxBytes; }
int FileSystem::GetNumUsedFiles() { return header.usedFiles; }
int FileSystem::GetNumUsedBytes() { return header.usedBytes; }
int FileSystem::GetNumMaxFiles() { return header.maxFiles; }
string FileSystem::GetType() { return header.ver; }
string FileSystem::GetName() { return name; }
/*########################### PHASE II ######################################*/
void FileSystem::ImportFile(string fName, string dName, fstream& output)
{
int byteCount = 0, indexPointer = 0;
char fileBuffer[MAX_DISK_BYTE_SIZE];
char readBuffer[1];
fstream inputFile, outputFile;
if(header.usedBytes == header.maxFiles)
{
output << ERROR[9] << endl; return;
}
inputFile.open(fName.c_str(), ios_base::in | ios_base::binary);
outputFile.open(dName.c_str(), ios_base::in | ios_base::out | ios_base::binary);
if(!inputFile)
{
output << ERROR[22] << endl; return;
}
if(!outputFile)
{
output << ERROR[28] << endl; return;
}
inputFile.seekg(ios_base::beg);
while(inputFile.read(readBuffer,1))
{
fileBuffer[byteCount] = readBuffer[0];
if(byteCount >= (header.maxBytes - header.usedBytes))
{
output << ERROR[8] << endl; return;
}
byteCount++;
}
inputFile.clear(); inputFile.close();
strcpy(fiTable[header.usedFiles].name,fName.c_str());
fiTable[header.usedFiles].owner = 0;
fiTable[header.usedFiles].permissions = 0;
fiTable[header.usedFiles].startByte = header.usedBytes;
fiTable[header.usedFiles].length = byteCount;
header.usedBytes += byteCount;
header.usedFiles++;
indexPointer = WriteHeader(outputFile);
indexPointer = WriteFiTable(outputFile);
outputFile.seekp(fiTable[header.usedFiles-1].startByte);
outputFile.write(reinterpret_cast<const char*>(&fileBuffer),byteCount);
outputFile.clear(); outputFile.close();
}
void FileSystem::Dir(fstream& output)
{
output << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl
<< "+ Filename Size Owner Properties +" << endl;
for(int i = 0; i < header.usedFiles; i++)
{
int temp = 0;
for(temp = 0; fiTable[i].name[temp] != '\0';temp++);
output << "+ "
<< fiTable[i].name << setw(22 - temp) << right
<< fiTable[i].length << setw(8) << right
<< fiTable[i].owner << left << " "
<< fiTable[i].permissions
<< setw(25) << right << "+" << endl;
}
output << "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++" << endl;
}
void FileSystem::Owner(string fName, int newOwner, fstream& output)
{
int index = -1;
for(int i = 0; i < header.usedFiles;i++)
{
if(fiTable[i].name == fName)
{
index = i; break;
}
}
if(index == -1)
{
output << ERROR[25] << endl; return;
}
fiTable[index].owner = newOwner;
}
void FileSystem::Rename(string dName, string f1, string f2, fstream& output)
{
int index = -1;
fstream o;
for(int i = 0; i < header.usedFiles;i++)
{
if(fiTable[i].name == f1)
{
index = i; break;
}
}
if(index == -1)
{
output << ERROR[25] << endl; return;
}
o.open(dName.c_str(),ios_base::in | ios_base::out | ios_base::binary);
if(!o)
{
output << ERROR[28] << endl; return;
}
strcpy(fiTable[index].name,f2.c_str());
index = WriteFiTable(o);
}
void FileSystem::Export(string fName, string dName, fstream& output)
{
int index = -1;
char readBuffer[MAX_DISK_BYTE_SIZE];
fstream inputFile, outputFile;
inputFile.open(dName.c_str(),ios_base::in | ios_base::binary);
if(!inputFile)
{
output << ERROR[28] << endl; return;
}
for(int i = 0; i < header.usedFiles;i++)
{
if(fiTable[i].name == fName)
{
index = i; break;
}
}
if(index == -1)
{
output << ERROR[25] << endl; return;
}
outputFile.open(fName.c_str(),ios_base::out | ios_base::binary);
if(!outputFile)
{
output << ERROR[23] << endl; return;
}
inputFile.seekg(fiTable[index].startByte);
inputFile.read(readBuffer,fiTable[index].length);
outputFile.seekp(ios_base::beg);
outputFile.write(reinterpret_cast<const char*>(&readBuffer),fiTable[index].length);
inputFile.clear(); inputFile.close();
outputFile.clear(); outputFile.close();
}
void FileSystem::Cp(string dName, string f1, string f2, fstream& output)
{
int index = -1, indexPointer = 0;
char readBuffer[MAX_DISK_BYTE_SIZE];
fstream inputFile;
if(header.usedFiles == header.maxFiles)
{
output << ERROR[9] << endl; return;
}
for(int i = 0; i < header.usedFiles;i++)
{
if(fiTable[i].name == f2)
{
output << ERROR[26] << endl; return;
}
}
for(int i = 0; i < header.usedFiles;i++)
{
if(fiTable[i].name == f1)
{
index = i; break;
}
}
if(index == -1)
{
output << ERROR[25] << endl; return;
}
if(((fiTable[index].length)*2) > (header.maxBytes - header.usedBytes))
{
output << ERROR[8] << endl; return;
}
inputFile.open(dName.c_str(),ios_base::in | ios_base::out | ios_base::binary);
if(!inputFile)
{
output << ERROR[28] << endl; return;
}
cout << fiTable[index].startByte << endl
<< fiTable[index].length << endl;
inputFile.seekg(fiTable[index].startByte);
inputFile.read(readBuffer,fiTable[index].length);
strcpy(fiTable[header.usedFiles].name,f2.c_str());
fiTable[header.usedFiles].owner = fiTable[index].owner;
fiTable[header.usedFiles].permissions = fiTable[index].permissions;
fiTable[header.usedFiles].startByte = header.usedBytes;
fiTable[header.usedFiles].length = fiTable[index].length;
header.usedBytes += fiTable[index].length;
header.usedFiles++;
indexPointer = WriteHeader(inputFile);
indexPointer = WriteFiTable(inputFile);
inputFile.seekp(fiTable[header.usedFiles-1].startByte);
inputFile.write(reinterpret_cast<const char*>(&readBuffer),fiTable[index].length);
inputFile.clear(); inputFile.close();
}
int FileSystem::WriteHeader(fstream& o)
{
o.seekp(ios_base::beg);
o.write(reinterpret_cast<const char*>(&header.ver),sizeof(char)*8);
o.write(reinterpret_cast<const char*>(&header.maxFiles), sizeof(int));
o.write(reinterpret_cast<const char*>(&header.maxBytes), sizeof(int));
o.write(reinterpret_cast<const char*>(&header.usedFiles),sizeof(int));
o.write(reinterpret_cast<const char*>(&header.usedBytes),sizeof(int));
return (o.tellp());
}
int FileSystem::WriteFiTable(fstream& o)
{
o.seekp(24); // write it after the header
for(int i = 0; i < header.maxFiles; i++)
{
o.write(reinterpret_cast<const char*>(&fiTable[i].name)
,sizeof(char)*16);
o.write(reinterpret_cast<const char*>(&fiTable[i].startByte)
,sizeof(int));
o.write(reinterpret_cast<const char*>(&fiTable[i].length)
,sizeof(int));
o.write(reinterpret_cast<const char*>(&fiTable[i].owner)
,sizeof(int));
o.write(reinterpret_cast<const char*>(&fiTable[i].permissions)
,sizeof(int));
}
return (o.tellp());
}
#endif // FILE_SYSTEM_CPP