-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualFileSystem.java
More file actions
222 lines (193 loc) · 9.06 KB
/
VirtualFileSystem.java
File metadata and controls
222 lines (193 loc) · 9.06 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package virtual.file.system;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Scanner;
public class VirtualFileSystem {
static Allocation allocation;
static int diskSize = 10000;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the allocation technique\n1-Contigious\n2-indexed ");
String allocationType = sc.nextLine();
if (allocationType.equals("1")) {
allocation = new ContiguousAllocation(diskSize);
// System.out.print(44);
} else if (allocationType.equals("2")) {
allocation = new IndexedAllocation(diskSize);
}
System.out.println("Do you want to upload the file system? y/n\nIf you entered n the file system will be re-initialized.");
String ans = sc.nextLine();
if (ans.equals("y")) {
allocation.readData();
}
while (true) {
System.out.print("Enter your command, to exit enter e: ");
String command = sc.nextLine();
String[] split = command.split(" ");
if (split[0].equals("e")) {
String data = "";
data = allocation.storeData(data, allocation.root);
ArrayList<String> commands = new ArrayList<>();
String[] parts = data.split(System.getProperty("line.separator"));
for (int i = 0; i < parts.length; i++) {
if (!commands.contains(parts[i]))
commands.add(parts[i]);
}
if (allocationType.equals("1")) {
//FileWriter fw=new FileWriter("ContigiousAllocation.txt");
//System.out.println("1");
//fw.write(data);
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("ContigiousAllocation.txt"), "utf-8"))) {
for (int i = 0; i < commands.size(); i++) {
writer.write(commands.get(i));
writer.write(System.getProperty("line.separator"));
}
}
} else if (allocationType.equals("2")) {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("IndexedAllocation.txt"), "utf-8"))) {
for (int i = 0; i < commands.size(); i++) {
writer.write(commands.get(i));
writer.write(System.getProperty("line.separator"));
}
}
}
break;
} //CreateFile root/file.txt 100
else if (split[0].equals("CreateFile")) {
if (split.length != 3) {
System.out.println("CreateFile takes 2 arguments: the path and the size.");
continue;
} else {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
if (!path[i].equals("")) {
p += path[i];
p += "/";
}
}
// System.out.println(path[path.length-2]);
Directory directory = allocation.getPathDirectory(path);
if (directory == null) {
System.out.println("The directory does not exists.");
continue;
}
boolean noRepeat = true;
for (int i = 0; i < directory.files.length; i++) {
if (directory.files[i].name.equals(path[path.length - 1])) {
System.out.println("There is another file with same name in the directory.");
noRepeat = false;
break;
}
}
if (noRepeat) {
allocation.addFile(directory, p, Integer.parseInt(split[2]), path[path.length - 1]);
}
}
} //CreateFolder root/folder1
else if (split[0].equals("CreateFolder")) {
if (split.length != 2) {
System.out.println("CreateFile takes one arguments: the path.");
continue;
} else {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
if (!path[i].equals("")) {
p += path[i];
p += "/";
}
}
Directory directory = allocation.getPathDirectory(path);
if (directory == null) {
System.out.println("The directory does not exists.");
continue;
}
boolean noRepeat = true;
for (int i = 0; i < directory.subDirectories.length; i++) {
if (directory.subDirectories[i].name.equals(path[path.length - 1])) {
System.out.println("There is another folder with same name in the directory.");
noRepeat = false;
break;
}
}
if (noRepeat) {
allocation.addDirectory(directory, path[path.length - 1], p);
}
}
} //DeleteFile root/folder1/file.txt
else if (split[0].equals("DeleteFile")) {
if (split.length != 2) {
System.out.println("DeleteFile takes one arguments: the path.");
continue;
} else {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
}
Directory directory = allocation.getPathDirectory(path);
if (directory == null) {
System.out.println("The directory does not exists.");
continue;
}
int k = -1;
for (int i = 0; i < directory.files.length; i++) {
if (directory.files[i].name.equals(path[path.length - 1])) {
k = i;
break;
}
}
if (k == -1) {
System.out.println("The file does not exists.");
continue;
}
allocation.deleteFile(directory.files[k]);
}
} //DeleteFolder root/folder1
else if (split[0].equals("DeleteFolder")) {
if (split.length != 2) {
System.out.println("DeleteFolder takes one arguments: the path.");
continue;
} else {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
}
Directory directory = allocation.getPathDirectory(path);
if (directory == null) {
System.out.println("The directory does not exists.");
continue;
}
int k = -1;
for (int i = 0; i < directory.subDirectories.length; i++) {
if (directory.subDirectories[i].name.equals(path[path.length - 1])) {
k = i;
}
}
if (k == -1) {
System.out.println("The directory does not exists.");
continue;
}
allocation.deleteDirectory(directory.subDirectories[k]);
}
} //DisplayDiskStatus
else if (split[0].equals("DisplayDiskStatus")) {
allocation.DisplayDiskStatus();
} //DisplayDiskStructure
else if (split[0].equals("DisplayDiskStructure")) {
allocation.printDirectoryStructure("", allocation.root);
}
else {
System.out.println("You have entered an invalid command!");
}
}
}
}