-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cpp
More file actions
130 lines (118 loc) · 4.08 KB
/
Copy pathFileSystem.cpp
File metadata and controls
130 lines (118 loc) · 4.08 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
#include "FileSystem.hpp"
#include <iostream>
using namespace std;
void FileSystem::createFile(string filename){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
File* newFile = new File(filename);
files[filename] = newFile;
cout << "File created: " << filename << endl;
}
void FileSystem::readFile(string filename){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
it->second->read();
}
void FileSystem::insertContent(string filename, string content){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
it->second->insert(content);
cout << "Content inserted into " << filename << endl;
}
void FileSystem::updateFile(string filename, string content){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
it->second->update(content);
cout << "File updated: " << filename << endl;
}
void FileSystem::createSnapshot(string filename, string message){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
File* file = it->second;
if (!file->activeVersion){
cout << "No active version to snapshot" << endl;
return;
}
file->activeVersion->isSnapshot = true;
file->activeVersion->message = message;
file->activeVersion->snapshotTimestamp = time(0);
cout << "Snapshot created for " << filename << " with message: " << message << endl;
}
void FileSystem::rollbackToSnapshot(string filename, int versionID){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
if (it->second->rollback(versionID)){
cout << "Rolled back " << filename << " to version " << versionID << endl;
} else {
cout << "Snapshot version not found" << endl;
}
// if version id is not provided, rollback to parent of current active version
if (versionID == -1 && it->second->activeVersion){
Version* parent = it->second->activeVersion->parent;
if (parent && parent->isSnapshot){
it->second->activeVersion = parent;
cout << "Rolled back " << filename << " to previous snapshot version " << parent->versionID << endl;
} else {
cout << "No previous snapshot version to rollback to" << endl;
}
}
}
void FileSystem::viewHistory(string filename){
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
it->second->viewHistory();
}
void FileSystem::listRecentFiles(int num) {
vector<File*> fileList;
for (const auto& pair : files) {
fileList.push_back(pair.second);
}
sort(fileList.begin(), fileList.end(), [](File* a, File* b) {
return a->getLastModified() > b->getLastModified();
});
cout << "Most recently edited files:" << endl;
for (int i = 0; i < min(num, (int)fileList.size()); ++i) {
cout << fileList[i]->getName() << " (Last Modified: " << fileList[i]->getLastModified() << ")" << endl;
}
}
void FileSystem::listMostVersionedFiles(int num) {
vector<File*> fileList;
for (const auto& pair : files) {
fileList.push_back(pair.second);
}
sort(fileList.begin(), fileList.end(), [](File* a, File* b) {
return a->getVersionCount() > b->getVersionCount();
});
cout << "Files with most versions:" << endl;
for (int i = 0; i < min(num, (int)fileList.size()); ++i) {
cout << fileList[i]->getName() << " (Versions: " << fileList[i]->getVersionCount() << ")" << endl;
}
}
void FileSystem::getVersionCount(string filename) {
auto it = files.find(filename);
if (it == files.end()){
cout << "File not found" << endl;
return;
}
}