-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernelFS.cpp
More file actions
125 lines (106 loc) · 2.66 KB
/
KernelFS.cpp
File metadata and controls
125 lines (106 loc) · 2.66 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
#include "KernelFS.h"
#include "MyPartition.h"
#include "part.h"
#include <cstring>
KernelFS::KernelFS() {
partitions = new MyPartition*[26];
for (int i = 0; i < 26; i++) {
partitions[i] = nullptr;
}
fsMonitor = new WriterPreferance();
}
KernelFS::~KernelFS() {
delete[] partitions;
delete fsMonitor;
}
char KernelFS::mount(Partition *part) {
char ret = 0;
fsMonitor->startWrite();
for (int i = 0; i < 26; i++) {
if (partitions[i] == nullptr) {
partitions[i] = new MyPartition(part);
ret = 'A' + i;
break;
}
}
fsMonitor->endWrite();
return ret;
}
char KernelFS::unmount(char part) {
char ret = 0;
fsMonitor->startWrite();
MyPartition *partition = partitions[part - 'A'];
partitions[part - 'A'] = nullptr;
fsMonitor->endWrite();
if (partition != nullptr) {
delete partition;
ret = 1;
}
return ret;
}
char KernelFS::readRootDir(char part, EntryNum num, Directory& d) {
char ret = 0;
fsMonitor->startRead();
if (partitions[part - 'A'] != nullptr) {
ret = partitions[part - 'A']->readRootDir(num, d);
}
fsMonitor->endRead();
return ret;
}
KernelFS::ParsedName KernelFS::parseName(char *fullFname) {
ParsedName pn;
pn.part = (*fullFname);
int len = strlen(fullFname);
memcpy(pn.fext, fullFname + len - 3, 3);
memcpy(pn.fname, fullFname + 3, len - 7);
for (int i = len - 7; i < 8; i++) {
pn.fname[i] = ' ';
}
return pn;
}
char KernelFS::doesExist(char *fullFname) {
int len = strlen(fullFname);
if (len < 5 || len > 12) return 0;
ParsedName pn = parseName(fullFname);
char ret = 0;
fsMonitor->startRead();
if (partitions[pn.part - 'A'] != nullptr) {
ret = partitions[pn.part - 'A']->deleteFile(pn.fname, pn.fext);
}
fsMonitor->endRead();
return ret;
}
File* KernelFS::open(char *fullFname, char mode) {
File *file = nullptr;
ParsedName pn = parseName(fullFname);
fsMonitor->startRead();
if (partitions[pn.part - 'A'] != nullptr) {
KernelFile *kfile = partitions[pn.part - 'A']->openFile(pn.fname, pn.fext, mode);
if (kfile != nullptr) {
file = new File();
file->myImpl = kfile;
}
}
fsMonitor->endRead();
return file;
}
char KernelFS::deleteFile(char *fullFname) {
char ret = 1;
ParsedName pn = parseName(fullFname);
fsMonitor->startRead();
if (partitions[pn.part - 'A'] != nullptr) {
ret = partitions[pn.part - 'A']->deleteFile(pn.fname, pn.fext);
}
fsMonitor->endRead();
return ret;
}
char KernelFS::format(char part) {
char ret = 1;
fsMonitor->startRead();
if (partitions[part - 'A'] != nullptr) {
partitions[part - 'A']->format();
ret = 0;
}
fsMonitor->endRead();
return ret;
}