-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeleteFile.cpp
More file actions
80 lines (74 loc) · 2.46 KB
/
deleteFile.cpp
File metadata and controls
80 lines (74 loc) · 2.46 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
#include "filesystem.h"
int FileSystem::deleteFile(const char *title) {
/*
objective: to delete a file (in recoverable form) from current directory
input: file title
return:
1: fail
0: success
effect: the sectors corresponding to a file are marked deleted (denoted by negative integers)
the type in directory entry for the file changes for 'F' to 'f', denoting deleted.
*/
char buf[kSectorSize];
// find position for file entry
bool positionFound = false;
bool alreadyDeleted = false;
int byteforEntry = 0;
int sectorForEntry = 0;
TypeCastEntry test;
for (int s = 0; s < sectorsForDir; ++s) {
readSector(currentDir + s, buf);
for (int b = 0; b < kSectorSize; b += 32) {
for (int k = 0; k < 32; ++k) {
test.str[k] = buf[b+k];
}
if (strcmp(test.entry.name, title) == 0) {
if (test.entry.type == 'F') {
positionFound = true;
sectorForEntry = s;
byteforEntry = b;
break;
} else if (test.entry.type == 'f') {
alreadyDeleted = true;
sectorForEntry = s;
byteforEntry = b;
break;
}
}
}
if (positionFound || alreadyDeleted) break;
}
if (!positionFound && !alreadyDeleted) {
std::cout << "No such file found." << std::endl;
return 1;
}
if (alreadyDeleted) {
// remove permanently
int status = test.entry.startsAt;
do {
int nxt = getStatus(-status);
updateStatus(-status, 0);
status = nxt;
} while (status != -1);
for (int i = byteforEntry, j = 0; j < 32; ++i, ++j) {
buf[i] = static_cast<char>(0);
}
writeSector(currentDir + sectorForEntry, buf);
std::cout << "permanently deleted file: " << title << std::endl;
return 0;
}
int status = test.entry.startsAt;
do {
int nxt = getStatus(status);
updateStatus(status, -nxt);
status = nxt;
} while (status != 1);
test.entry.type = 'f';
test.entry.startsAt = -test.entry.startsAt;
for (int i = byteforEntry, j = 0; j < 32; ++i, ++j) {
buf[i] = test.str[j];
}
writeSector(currentDir + sectorForEntry, buf);
std::cout << "deleted file: " << title << std::endl;
return 0;
}