-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommands.cpp
More file actions
200 lines (157 loc) · 4.57 KB
/
Commands.cpp
File metadata and controls
200 lines (157 loc) · 4.57 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
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
#include "Commands.h"
#include <iostream>
#include <fstream>
using namespace std;
namespace fs = filesystem;
Commands::Commands(const string& base) : baseDir(base) {
if (!baseDir.empty() && baseDir.back() != '\\' && baseDir.back() != '/') {
baseDir += "\\";
}
// eventually these should be customizable as well
inputDir = baseDir + "Input\\";
storageDir = baseDir + "Storage\\";
diskFolders = {
baseDir + "Disk_A\\",
baseDir + "Disk_B\\",
baseDir + "Disk_C\\",
baseDir + "Disk_D\\"
};
}
void Commands::init() {
vector<string> dirs = { baseDir, inputDir, storageDir };
dirs.insert(dirs.end(), diskFolders.begin(), diskFolders.end());
for (const auto& dir : dirs) {
if (!fs::exists(dir)) {
fs::create_directories(dir);
}
}
}
string Commands::getMetadataPath(const string& fileName) {
return storageDir + fileName + ".json";
}
void Commands::saveMetadata(const string& fileName, size_t size) {
string path = getMetadataPath(fileName);
ofstream json(path);
json << "{\n";
json << " \"originalName\": \"" << fileName << "\",\n";
json << " \"size\": " << size << "\n";
json << "}";
json.close();
cout << "Metadata saved to: " << path << endl;
}
size_t Commands::getFileSize(const string& fileName) {
string path = getMetadataPath(fileName);
ifstream json(path);
if (!json.is_open()) {
cerr << "Metadata not found for: " << fileName;
return 0;
}
string line;
size_t size = 0;
while (getline(json, line)) {
if (line.find("\"size\":") != string::npos) {
size_t colonPos = line.find(':');
if (colonPos != string::npos) {
string sizeStr = line.substr(colonPos + 1);
try {
// convert to unsigned long to handle larger file sizes
size = stoull(sizeStr);
}
catch (...) {
size = 0;
}
}
}
}
return size;
}
string Commands::getFullPath(const string& fileName) {
return inputDir + fileName;
}
void Commands::dmaOperationHandler(const string& inputFile,
const string& outputFile,
size_t fileSize,
RaidAction action) {
DMAController dma;
StripeMap stripeMap(diskFolders, CHUNK_SIZE);
RAID raid(stripeMap, dma);
if (action == RaidAction::UPLOAD) {
raid.disassemble(inputFile);
dma.waitForCompletion();
}
else if (action == RaidAction::DOWNLOAD) {
raid.reassemble(inputFile, outputFile, fileSize);
}
}
/* Writes a file to the drive and copies the file metadata as JSON
*
* This way, we don't need to preserve the original file and can simply
* reference the metadata to reconstruct the file (saves space long term)
*
* In a way this also "encrypts" the file by spreading it across each
* of our drives
*/
void Commands::handleWrite(const string& fileName) {
string fullPath = getFullPath(fileName);
if (!fs::exists(fullPath)) {
cerr << "ERROR | File not found: " << fullPath << endl;
exit(1);
}
size_t fileSize = fs::file_size(fullPath);
cout << "Uploading: " << fileName << "..." << endl;
dmaOperationHandler(fullPath, "", 0, RaidAction::UPLOAD);
saveMetadata(fileName, fileSize);
// we're only storing the original file temporarily
try {
fs::remove(fullPath);
cout << "Deleted local copy of: " << fullPath << endl;
}
catch (const fs::filesystem_error& err) {
cerr << "ERROR | Failed to delete input file: " << err.what() << endl;
}
cout << "Upload complete" << endl;
}
void Commands::handleRead(const string& fileName) {
size_t originalSize = 0;
try {
originalSize = getFileSize(fileName);
}
catch (const exception& err) {
cerr << "ERROR | " << err.what() << endl;
exit(1);
}
string outPath = inputDir + "temp_" + fileName;
cout << "Reassembling in progress" << endl;
dmaOperationHandler(fileName,
outPath,
originalSize,
RaidAction::DOWNLOAD);
if (fs::exists(outPath) && fs::file_size(outPath) == originalSize) {
cout << "Reassembly complete" << endl;
}
else {
cerr << "ERROR | Reassembly failed - size mismatch" << endl;
}
}
void Commands::handleDelete(const string& fileName) {
cout << "Deleting chunks for: " << fileName << endl;
for (const auto& diskPath : diskFolders) {
if (!fs::exists(diskPath)) continue;
for (const auto& entry : fs::directory_iterator(diskPath)) {
string diskFile = entry.path().filename().string();
if (diskFile.find(fileName + ".chunk") == 0) {
try {
fs::remove(entry.path());
cout << "Successfully deleted " << diskFile << " from: " << diskPath << endl;
}
catch (const fs::filesystem_error& err) {
cerr << "ERROR | Could not delete: " << diskFile << ": " << err.what() << endl;
}
}
}
}
string metadataPath = getMetadataPath(fileName);
if (fs::exists(metadataPath)) {
fs::remove(metadataPath);
}
}