-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
132 lines (118 loc) · 4.27 KB
/
main.cpp
File metadata and controls
132 lines (118 loc) · 4.27 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
#include <iostream>
#include "VirtualFileSystem.h"
using namespace std;
void printAbout() {
cout << "TRUSTech Virtual File System (TTvfs)" << endl;
cout << "Version - Alpha 0.1" << endl << endl;
}
// Print usage in case the entered command is wrong
void printUsage(const string &programName) {
cout << "Usage: " << programName << " <command> [options]" << endl;
cout << "----------------------------------------" << endl;
cout << "dmake <diskfile> [size_bytes] <- Create a new virtual disk file with optional size" << "\n" <<
"(default 10MB, min 4096 bytes, max 100MB)" << endl;
cout << "dremove <diskfile> <- Remove the virtual disk file" << endl;
cout << "dput <diskfile> <localfile> <- Copy a local file to the virtual disk" << endl;
cout << "dget <diskfile> <filename> [dest] <- Copy a file from the virtual disk" << endl;
cout << "ddel <diskfile> <filename> <- Deletes a file from the virtual disk" << endl;
cout << "dls <diskfile> <- List files in the virtual disk" << endl;
cout << "dmap <diskfile> <- Show block occupation on the virtual disk" << endl;
cout << "help <- Show this help message" << endl;
cout << "about <- For more information about the program" << endl;
}
int main(const int argc, char *argv[]) {
if (argc < 2) {
printUsage(argv[0]);
return 1;
}
// Complaint: 'string' not being allowed in a switch statement, ridiculous
// Thus using good-old if-else chain instead
if (const string cmd = argv[1]; cmd == "dmake") {
if (argc < 3) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
uint32_t size = DEFAULT_DISK_SIZE;
if (argc >= 4) {
size = static_cast<uint32_t>(stoul(argv[3]));
// We should also check for size in here
// too small would just cause a crash
// However, too big can cause serious issues
// Limiting to 100 MB for now
if (size < 4096 || size > 100 * 1024 * 1024) {
cerr << "Error: Disk size must be between 4096 bytes and 100 MB." << endl;
return 1;
}
}
if (VirtualFileSystem vfs(diskName); !vfs.createDisk(size)) return 1;
} else if (cmd == "dremove") {
if (argc < 3) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
VirtualFileSystem vfs(diskName);
vfs.removeDisk();
} else if (cmd == "dput") {
if (argc < 4) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
const string hostFile = argv[3];
VirtualFileSystem vfs(diskName);
if (!vfs.loadDisk()) return 1;
vfs.copyFromHost(hostFile);
} else if (cmd == "dget") {
if (argc < 4) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
const string fileName = argv[3];
const string dest = (argc >= 5 ? argv[4] : "");
VirtualFileSystem vfs(diskName);
if (!vfs.loadDisk()) return 1;
vfs.copyToHost(fileName, dest);
} else if (cmd == "ddel") {
if (argc < 4) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
const string fileName = argv[3];
VirtualFileSystem vfs(diskName);
if (!vfs.loadDisk()) return 1;
vfs.deleteFile(fileName);
} else if (cmd == "dls") {
if (argc < 3) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
VirtualFileSystem vfs(diskName);
if (!vfs.loadDisk()) return 1;
vfs.listFiles();
} else if (cmd == "dmap") {
if (argc < 3) {
printUsage(argv[0]);
return 1;
}
const string diskName = argv[2];
VirtualFileSystem vfs(diskName);
if (!vfs.loadDisk()) return 1;
vfs.showMap();
} else if (cmd == "help") {
printUsage(argv[0]);
return 0;
} else if (cmd == "about") {
printAbout();
return 0;
} else {
cout << "Unknown command: " << cmd << endl;
printUsage(argv[0]);
return 1;
}
return 0;
}