-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDOS.cpp
400 lines (379 loc) · 11.4 KB
/
DOS.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
#include "DOS.h"
DOS::DOS(string fname) :disk(fname)
{
diskDirList = disk.readIndex();//读取磁盘目录
//如果磁盘目录为空,创建根目录
if (diskDirList.empty()) {
cout << "No index file found, creating a new one..." << endl;
IndexNode root("", 'D', -1, -1);
diskDirList.push_back(root);
disk.writeIndex(diskDirList);
}
//打印目录
curDir = 0;//当前目录指针,指向根目录
//_curDir = diskDirList.begin();
//cout << "Current directory: _" << curDir->name << "/" << endl;
}
DOS::~DOS() {
disk.writeIndex(diskDirList);
disk.readIndex();
disk.writeIndex(diskDirList);
}
//成员函数---------------------------------------------------------
string DOS::fullPath() {
if (diskDirList.size() <= curDir) {
cerr << "curr out of range!" << endl;
}
string tmp = "/" + string(diskDirList.at(curDir).name);
long pathPtr = diskDirList.at(curDir).father;
while (pathPtr > 0) {
tmp = "/" + string(diskDirList.at(pathPtr).name) + tmp;
pathPtr = diskDirList.at(pathPtr).father;
}
return tmp;
}
int DOS::ls() {
cout << "name\ttype" << endl;
for (long i = 0; i < diskDirList.at(curDir).children.size(); ++i) {
cout << diskDirList.at(diskDirList.at(curDir).children.at(i)).name << '\t' <<
diskDirList.at(diskDirList.at(curDir).children.at(i)).type << endl;
}
cout << endl;
return 0;
}
int DOS::help(string command) {
//默认为""(空字符串)
if (command == "") {
cout << "help: show all commands" << endl;
cout << "ls: list all files and directories" << endl;
cout << "cd: change directory" << endl;
cout << "mkdir: make a new directory" << endl;
cout << "rm: remove a file or directory" << endl;
cout << "cat: create a new file" << endl;
cout << "exit: exit the system" << endl;
}
//help ls
else if (command == "ls") {
cout << "ls: list all files and directories" << endl;
}
//help cd
else if (command == "cd") {
cout << "cd: change directory" << endl;
}
//help mkdir
else if (command == "mkdir") {
cout << "mkdir: make a new directory" << endl;
}
//help rm
else if (command == "rm") {
cout << "rm: remove a file or directory" << endl;
}
//help cat
else if (command == "cat") {
cout << "cat: create, modify(append) or read a file" << endl;
}
//help exit
else if (command == "exit") {
cout << "exit: exit the system" << endl;
}
else {
cout << "No such command!" << endl;
return -1;
}
return 0;
};
int DOS::cd(string childDirName) {
if (strcmp(childDirName.c_str(), "..") == 0) {
this->curDir = diskDirList.at(curDir).father;
if (curDir < 0) {
curDir = 0;
}
return 0;
}
if (strcmp(childDirName.c_str(), ".") == 0) {
return 0;
}
long childAbsNum = -1;
bool exist = false;
for (long i = 0; i < diskDirList.at(curDir).children.size(); i++) {
if (strcmp(diskDirList.at(diskDirList.at(curDir).children.at(i)).name, childDirName.c_str()) == 0) {
childAbsNum = diskDirList.at(curDir).children.at(i);
exist = true;
break;
}
}
//
if (!exist) {
cout << "None directory named " << childDirName << endl;
return -1;
}
if (diskDirList.at(childAbsNum).type != 'D') {
cout << childDirName << " is not a directory" << endl;
return -1;
}
curDir = childAbsNum;
return 0;
}
int DOS::mkdir(string name) {
//如果含有空格,斜杠等符号,报错
if (
name.find(' ') != string::npos ||
name.find('\\') != string::npos ||
name.find('/') != string::npos ||
name.find('-') != string::npos ||
name.find('|') != string::npos ||
name.find('#') != string::npos ||
name.find('@') != string::npos ||
name.find('!') != string::npos
) {
cout << "Invalid name! Do not use special char in name." << endl;
return -1;
}
//查找当前目录下是否存同名文件或目录
for (long i = 0; i < diskDirList.at(curDir).children.size(); i++) {
if (strcmp(diskDirList.at(diskDirList.at(curDir).children.at(i)).name, name.c_str()) == 0) {
cout << "Same name file or directory exists! (name: " << name << " )\n" << endl;
return -1;
}
}
//创建新目录
IndexNode newDir(name.c_str(), 'D', -1, curDir);
diskDirList.push_back(newDir);
if (!diskDirList.empty()) {
diskDirList.at(curDir).children.push_back(long(diskDirList.size() - 1));
}
else {
cerr << "Error: diskDirList is empty!" << endl;
return -1;
}
cout << "New directory created! " << name << endl << endl;
//将更新后的目录写入磁盘
disk.writeIndex(diskDirList);
disk.readIndex();
return 0;
}
int DOS::rm(string trashName) {
//查找当前目录下是否存在该文件或目录
long trash = -1;
bool exist = false;
for (long i = 0; i < diskDirList.at(curDir).children.size(); i++) {
if (strcmp(diskDirList.at(diskDirList.at(curDir).children.at(i)).name, trashName.c_str()) == 0) {
trash = diskDirList.at(curDir).children.at(i);
exist = true;
break;
}
}
//
if (!exist) {
cout << "None file or directory named " << trashName << endl;
return -1;
}
//记录所有需要递归删除的元素(广度优先)
vector<long> toDel;
toDel.push_back(trash);
long rP = 0;
while (rP < toDel.size()) {
for (long i = 0; i < diskDirList.at(toDel.at(rP)).children.size(); i++) {
toDel.push_back(diskDirList.at(toDel.at(rP)).children.at(i));
}
rP++;
}
//执行删除
while (!toDel.empty()) {
long dP = toDel.back();
toDel.pop_back();
//如果diskDirList.at(dP)是文件,释放文件快空间
if (diskDirList.at(dP).type == 'F') {
disk.clearBlocks(diskDirList.at(dP).fBlock);
}
/*
//diskDirList.at(dP)之后的所有元素{father大于dP则自减,children中的所有元素自减}
for (unsigned long i = dP + 1; i < diskDirList.size(); i++) {
for (unsigned long j = 0; j < diskDirList.at(i).children.size(); j++) {
diskDirList.at(i).children.at(j)--;
}
if (diskDirList.at(i).father > dP) { diskDirList.at(i).father--; }
}
*/
//删除diskDirList.at(diskDirList.at(dP).father).children中的dP记录
//cout << "dad before:" << diskDirList.at(diskDirList.at(dP).father).children.size() << endl;
diskDirList.at(diskDirList.at(dP).father).children.erase(
std::remove(
diskDirList.at(diskDirList.at(dP).father).children.begin(),
diskDirList.at(diskDirList.at(dP).father).children.end(),
dP
),
diskDirList.at(diskDirList.at(dP).father).children.end()
);
//cout << "dad after:" << diskDirList.at(diskDirList.at(dP).father).children.size() << endl;
//删除diskDirList.at(dP)记录
/*
cout << "\n\ner:" << dP<<" name:" << diskDirList.at(dP).name<<" siz:"<< diskDirList.size() << endl;
diskDirList.erase(diskDirList.begin() + dP);
cout << " siz2:" << diskDirList.size() << endl;
*/
}
//将更新后的目录写入磁盘
disk.writeIndex(diskDirList);
disk.readIndex();
return 0;
}
int DOS::cat_r(string fName) {
long fNum = -1;
bool exist = false;
for (long i = 0; i < diskDirList.at(curDir).children.size(); i++) {
if (strcmp(diskDirList.at(diskDirList.at(curDir).children.at(i)).name, fName.c_str()) == 0) {
fNum = diskDirList.at(curDir).children.at(i);
exist = true;
break;
}
}
//
if (!exist) {
cout << "File Non Exist! " << fName << endl;
return -1;
}
else if (diskDirList.at(fNum).type != 'F') {
cout << fName << " is Not a File!" << endl;
return -1;
}
vector<char> temp = disk.readFile(diskDirList.at(fNum).fBlock);
//输出temp内容
cout << "File name: " << fName << "\tFile Content: " << endl;
cout << "----------File-Start-----------" << endl;
for (vector<char>::iterator it = temp.begin(); it != temp.end(); it++) { cout << *it; }
cout << endl << "------------File-END------------" << endl << endl;
return 0;
}
int DOS::cat_w(string fName, vector<char>& cont) {
//判断文件是否存在
long fNum = -1;
bool exist = false;
for (long i = 0; i < diskDirList.at(curDir).children.size(); i++) {
if (strcmp(diskDirList.at(diskDirList.at(curDir).children.at(i)).name, fName.c_str()) == 0) {
fNum = diskDirList.at(curDir).children.at(i);
exist = true;
break;
}
}
//
if (!exist) {
cout << "File Non Exist,create a new one." << endl;
//创建新文件
//如果含有空格,斜杠等符号,报错
if (
fName.find(' ') != string::npos ||
fName.find('\\') != string::npos ||
fName.find('/') != string::npos ||
fName.find('-') != string::npos ||
fName.find('|') != string::npos ||
fName.find('#') != string::npos ||
fName.find('@') != string::npos ||
fName.find('!') != string::npos
) {
cout << "Invalid name! Do not use special char in name." << endl;
return -1;
}
//创建新目录记录
IndexNode newDir(fName.c_str(), 'F', disk.minAvailable, curDir);
BlockHead th('F', -1, -1);
disk.writeBlock(Block(th), disk.minAvailable);
diskDirList.push_back(newDir);
if (!diskDirList.empty()) {
diskDirList.at(curDir).children.push_back(long(diskDirList.size() - 1));
while (disk.blockStatus[disk.minAvailable] != 'N' && disk.minAvailable < blockNum) {
disk.minAvailable++;
}
}
else {
cerr << "Error: diskDirList is empty!" << endl;
return -1;
}
cout << "New file created! " << fName << endl << endl;
//将更新后的目录写入磁盘
disk.writeIndex(diskDirList);
disk.readIndex();
fNum = diskDirList.size() - 1;
}
//如果fName存在,判断是否为文件
else if (diskDirList.at(fNum).type != 'F') {
cout << fName << " is Not a File!" << endl;
return -1;
}
//写入文件内容
cout << "Write to file: " << fName << endl;
disk.writeFile(cont, diskDirList.at(fNum).fBlock);
return 0;
}
int DOS::cat_a(string fName, vector<char>& cont) {
long fNum = -1;
bool exist = false;
for (long i = 0; i < diskDirList.at(curDir).children.size(); i++) {
if (strcmp(diskDirList.at(diskDirList.at(curDir).children.at(i)).name, fName.c_str()) == 0) {
fNum = diskDirList.at(curDir).children.at(i);
exist = true;
break;
}
}
//
if (!exist) {
cout << "File Non Exist,create a new one." << endl;
//创建新文件
//如果含有空格,斜杠等符号,报错
if (
fName.find(' ') != string::npos ||
fName.find('\\') != string::npos ||
fName.find('/') != string::npos ||
fName.find('-') != string::npos ||
fName.find('|') != string::npos ||
fName.find('#') != string::npos ||
fName.find('@') != string::npos ||
fName.find('!') != string::npos
) {
cout << "Invalid name! Do not use special char in name." << endl;
return -1;
}
//创建新目录记录
IndexNode newDir(fName.c_str(), 'F', disk.minAvailable, curDir);
BlockHead th('F', -1, -1);
disk.writeBlock(Block(th), disk.minAvailable);
diskDirList.push_back(newDir);
if (!diskDirList.empty()) {
diskDirList.at(curDir).children.push_back(long(diskDirList.size() - 1));
while (disk.blockStatus[disk.minAvailable] != 'N' && disk.minAvailable < blockNum) {
disk.minAvailable++;
}
}
else {
cerr << "Error: diskDirList is empty!" << endl;
return -1;
}
cout << "New file created! " << fName << endl << endl;
//将更新后的目录写入磁盘
disk.writeIndex(diskDirList);
disk.readIndex();
fNum = diskDirList.size() - 1;
}
else if (diskDirList.at(fNum).type != 'F') {
cout << fName << " is Not a File!" << endl;
return -1;
}
vector<char> temp = disk.readFile(diskDirList.at(fNum).fBlock);
//输出temp内容
cout << "File name: " << fName << "\tFile Content: " << endl;
cout << "----------File-Start-----------" << endl;
for (vector<char>::iterator it = temp.begin(); it != temp.end(); it++) { cout << *it; }
cout << endl << "------------File-END------------" << endl;
cout << "New content appended:" << endl;
//删除temp结尾‘\0’
while (temp.back() == '\0') { temp.pop_back(); }
for (vector<char>::iterator it = cont.begin(); it != cont.end(); it++) {
cout << *it;
temp.push_back(*it);
}
cout << endl << "-----------Append-END-----------" << endl << endl;
//写入文件内容
//temp.insert(temp.end(), cont.begin(), cont.end());
disk.writeFile(temp, diskDirList.at(fNum).fBlock);
return 0;
}