-
Notifications
You must be signed in to change notification settings - Fork 0
/
filesystem.js
275 lines (240 loc) · 6.92 KB
/
filesystem.js
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
illegal_file_characters = ["\n", ":", "<", ">", '"', "|", "?", "*", "\\"];
illegal_dir_characters = ["\n", ":", "<", ">", '"', "|", "?", "*", "\\", "."];
class SoyFileSystem {
constructor() {
this.root = {
type: "directory",
content: {},
};
}
resolveBackSteps(path) {
const segments = this.getPathSegments(path);
const resolvedSegments = [];
for (const segment of segments) {
if (segment === "..") {
if (resolvedSegments.length > 0) {
resolvedSegments.pop(); // Move up one directory
}
} else if (segment !== ".") {
// Ignore current directory references
resolvedSegments.push(segment);
}
}
return this.normalizePath(resolvedSegments.join("/"));
}
normalizePath(path) {
return this.getPathSegments(path.trim()).join("/") + "/";
}
createFile(path, content) {
const segments = this.getPathSegments(path);
const fileName = segments.pop();
const directory = this.traverse(segments);
if (
!directory ||
directory.content[fileName] ||
!path ||
!this.isValidFileName(fileName)
) {
console.error("File already exists or invalid path.");
return;
}
directory.content[fileName] = {
type: "file",
content: content,
};
}
pathExists(path) {
const segments = this.getPathSegments(path);
if (segments.length == 0) {
return true; // to ensure root is flagged as valid path
}
const fileName = segments.pop();
const directory = this.traverse(segments);
if (
!directory ||
!directory.content[fileName] ||
illegal_file_characters.some((v) => path.includes(v))
) {
return false;
}
return true;
}
createDirectory(path) {
const segments = this.getPathSegments(path);
const directoryName = segments.pop();
const directory = this.traverse(segments);
if (
!directory ||
directory.content[directoryName] ||
illegal_dir_characters.some((v) => path.includes(v))
) {
console.error("Directory already exists or invalid path.");
return;
}
directory.content[directoryName] = {
type: "directory",
content: {},
};
}
deletePath(path) {
const segments = this.getPathSegments(path);
if (segments.length == 0) {
this.root.content = {};
}
const fileName = segments.pop();
const parentPath = this.getParentDirectory(path);
const parentSegments = this.getPathSegments(parentPath);
const parentDirectory = this.traverse(parentSegments);
if (!parentDirectory || !parentDirectory.content[fileName]) {
console.error("File or directory not found or invalid path.");
return;
}
delete parentDirectory.content[fileName];
}
isValidFileName(filename) {
return (
!illegal_file_characters.some((v) => filename.includes(v)) &&
/\..+/.test(filename)
);
}
isValidFilePath(path) {
const segments = this.getPathSegments(path);
const fileName = segments.pop();
const directory = this.traverse(segments);
if (
!directory ||
(directory.content[fileName] &&
directory.content[fileName].type === "directory") ||
!this.isValidFileName(fileName)
) {
return false;
}
return true;
}
readFile(path) {
const segments = this.getPathSegments(path);
const fileName = segments.pop();
const directory = this.traverse(segments);
if (
!directory ||
!directory.content[fileName] ||
directory.content[fileName].type !== "file"
) {
console.error("File not found or invalid path.");
return null;
}
return directory.content[fileName].content;
}
isValidParentDirectory(path) {
const parentPath = this.getParentDirectory(path);
const segments = this.getPathSegments(parentPath);
const parentDirectory = this.traverse(segments);
return (
parentDirectory !== null &&
parentDirectory !== undefined &&
parentDirectory.type === "directory"
);
}
traverse(segments) {
let current = this.root;
for (const segment of segments) {
if (current.type !== "directory" || !current.content[segment]) {
return null;
}
current = current.content[segment];
}
return current;
}
writeFile(path, content) {
const segments = this.getPathSegments(path);
const fileName = segments.pop();
const directory = this.traverse(segments);
if (
!directory ||
!directory.content[fileName] ||
directory.content[fileName].type !== "file"
) {
console.error("File not found or invalid path.");
return;
}
directory.content[fileName].content = content;
}
readDirectory(path) {
const segments = this.getPathSegments(path);
const directory = this.traverse(segments);
if (
!directory ||
directory.type !== "directory" ||
illegal_dir_characters.some((v) => path.includes(v))
) {
console.error("Directory not found or invalid path.");
return null;
}
return Object.keys(directory.content);
}
isFile(path) {
const segments = this.getPathSegments(path);
const fileName = segments.pop();
const directory = this.traverse(segments);
return (
directory &&
directory.content[fileName] &&
directory.content[fileName].type === "file"
);
}
getParentDirectory(path) {
const segments = this.getPathSegments(path);
if (
segments.length === 0 ||
(segments.length === 1 && segments[0] === "")
) {
return path;
}
segments.pop();
var newPath = "/" + segments.join("/") + "/";
if (newPath == "//") {
newPath = "/";
}
return newPath;
}
moveFile(sourcePath, destinationPath) {
const sourceSegments = this.getPathSegments(sourcePath);
const sourceFileName = sourceSegments.pop();
const sourceDirectory = this.traverse(sourceSegments);
if (
!sourceDirectory ||
!sourceDirectory.content[sourceFileName] ||
sourceDirectory.content[sourceFileName].type !== "file"
) {
console.error("Source file not found or invalid path.");
return;
}
const destSegments = this.getPathSegments(destinationPath);
const destFileName = destSegments.pop();
const destDirectory = this.traverse(destSegments);
if (
!destDirectory ||
destDirectory.content[destFileName] ||
!this.isValidFileName(destFileName)
) {
console.error("Destination path is invalid or file already exists.");
return;
}
destDirectory.content[destFileName] =
sourceDirectory.content[sourceFileName];
delete sourceDirectory.content[sourceFileName];
}
getPathSegments(path) {
return path.split("/").filter((segment) => segment !== "");
}
exportToString() {
return JSON.stringify(this.root);
}
loadFromString(data) {
try {
this.root = JSON.parse(data);
} catch (error) {
console.error("Error parsing data:", error);
}
}
}