-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllocation.java
More file actions
438 lines (371 loc) · 14.8 KB
/
Allocation.java
File metadata and controls
438 lines (371 loc) · 14.8 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package virtual.file.system;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
class ContiguousAllocation extends Allocation {
public ContiguousAllocation(int n) {
super(n);
}
@Override
public int checkFreeSpace(int size) {
int s = 1;
int temp = 0;
for (int i = 0; i < diskBlocks.length; i++) {
if (diskBlocks[i] == false) {
s++;
} else if (diskBlocks[i] == true) {
// set it =1 because we want the space contigious
s = 1;
temp = i + 1; //the block after the allocated one
}
if (s == size) {
return temp;
}
}
return -1;
}
@Override
public boolean addFile(Directory directory, String path, int size, String n) {
int index = checkFreeSpace(size);
if (index == -1) {
return false;
} else {
freeSize -= size; // to update the free size of the disk.
File file = new File(path, size, n);
directory.addFile(file);
for (int i = 0; i < size; i++) {
diskBlocks[index] = true;
file.allocatedBlocks[i]=index;
index++;
}
return true;
}
}
@Override
public String storeData(String data, Directory d) {
if (!d.isDeleted()) {
if (!d.name.equals("root")) {
data += "CreateFolder " + d.directoryPath + d.name + System.getProperty("line.separator");
} else {
data += "CreateFolder " + d.name + System.getProperty("line.separator");
}
}
for (int i = 0; i < d.files.length; i++) {
if (!d.files[i].isDeleted()) {
data += "CreateFile " + d.files[i].filePath + d.files[i].name + " " + d.files[i].size + " " + d.files[i].allocatedBlocks[0] + " " + d.files[i].allocatedBlocks[d.files[i].allocatedBlocks.length - 1] + System.getProperty("line.separator");
}
}
if (d.subDirectories != null) {
for (int i = 0; i < d.subDirectories.length; i++) {
data += storeData(data, d.subDirectories[i]);
}
}
return data;
}
@Override
public void readData() {
BufferedReader reader = null;
try {
ArrayList <String> commands = new ArrayList <>();
FileInputStream ifile = new FileInputStream("ContigiousAllocation.txt");
Scanner sc = new Scanner(ifile);
while (sc.hasNextLine()) {
String command = sc.nextLine();
String[] split = command.split(" ");
//CreateFile root/file.txt 100
if (split[0].equals("CreateFile")) {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
}
// System.out.println(path[path.length-2]);
Directory directory = this.getPathDirectory(path);
if (directory == null) {
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;
continue;
}
}
if (noRepeat) {
this.freeSize -= Integer.parseInt(split[2]);
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
p += "/";
}
File file = new File(p, Integer.parseInt(split[2]), path[path.length - 1]);
file.allocatedBlocks = new int [Integer.parseInt(split[2])];
int j = 0;
for (int i = Integer.parseInt(split[3]); i <= Integer.parseInt(split[4]); i++) {
this.diskBlocks[i] = false;
file.allocatedBlocks[j] = i;
j++;
}
directory.addFile(file);
}
} //CreateFolder root/folder1
else if (split[0].equals("CreateFolder")) {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
p += "/";
}
Directory directory = this.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;
continue;
}
}
if (noRepeat) {
this.addDirectory(directory, path[path.length - 1],p);
}
}
// read next line
//command = sc.nextLine();
}
} catch (FileNotFoundException ex) {
System.out.println("read nothing from files");
}
}
@Override
public void deleteFile(File file) {
int[] blocks = file.getAllocatedBlocks();
for (int i = 0; i < blocks.length; i++) {
diskBlocks[blocks[i]] = false;
}
freeSize += file.size;
file.delete();
}
}
class IndexedAllocation extends Allocation {
public IndexedAllocation(int n) {
super(n);
}
@Override
public boolean addFile(Directory directory, String path, int size, String n) {
int index = checkFreeSpace(size+1);
if (index == -1) {
return false;
} else {
freeSize -= size;
File file = new File(path, size, n);
directory.addFile(file);
int j = 0;
int indexBlock = getIndexBlock();
file.setAllocatedBlock(j, indexBlock); // when the allocation is indexed I put only the index block
for (int i = index; i < diskBlocks.length; i++) {
if (diskBlocks[i] == false) {
diskBlocks[i] = true;
file.indexBlockContent += i + " "; // setting the blocks that the index block contains
// file.setAllocatedBlock(j, i);
j++;
if (j == size) {
break;
}
}
}
return true;
}
}
@Override
public int checkFreeSpace(int size) {
int s = 1;
int n = -1; // to get first empty
for (int i = 0; i < diskBlocks.length; i++) {
if (diskBlocks[i] == false) {
s++;
if (n == -1) { // get the first empty one
n = i;
}
}
if (s == size) {
return n;
}
}
return -1;
}
public int getIndexBlock() {
int n = 0;
for (int i = 0; i < diskBlocks.length; i++) {
if (diskBlocks[i] == false) {
return i;
}
}
return -1;
}
@Override
public String storeData(String data, Directory d) {
if (!d.isDeleted()) {
if(!d.name.equals("root"))
data += "CreateFolder " + d.directoryPath + d.name + System.getProperty("line.separator");
else
data += "CreateFolder " + d.name + System.getProperty("line.separator");
}
for (int i = 0; i < d.files.length; i++) {
if (!d.files[i].isDeleted()) {
data += "CreateFile " + d.files[i].filePath +d.files[i].name+" " + d.files[i].size+" " + d.files[i].allocatedBlocks[0] + " " + d.files[i].indexBlockContent + System.getProperty("line.separator");
}
}
if (d.subDirectories != null) {
for (int i = 0; i < d.subDirectories.length; i++) {
data+=storeData(data, d.subDirectories[i]);
}
}
return data;
}
@Override
public void readData() {
try {
ArrayList <String> commands = new ArrayList <>();
FileInputStream ifile = new FileInputStream("IndexedAllocation.txt");
Scanner sc = new Scanner(ifile);
while (sc.hasNextLine()) {
String command = sc.nextLine();
String[] split = command.split(" ");
//CreateFile root/file.txt 100
if (split[0].equals("CreateFile")) {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
}
// System.out.println(path[path.length-2]);
Directory directory = this.getPathDirectory(path);
this.freeSize -= Integer.parseInt(split[2]);
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
p+="/";
}
File file = new File(p, Integer.parseInt(split[2]), path[path.length - 1]);
this.diskBlocks[Integer.parseInt(split[3])] = false;
file.allocatedBlocks[0] = Integer.parseInt(split[3]);
for (int i = 4; i < split.length; i++) {
file.indexBlockContent += split[i];
}
directory.addFile(file);
} //CreateFolder root/folder1
else if (split[0].equals("CreateFolder")) {
String[] path = split[1].split("/");
String p = "";
for (int i = 0; i < path.length - 1; i++) {
p += path[i];
p+="/";
}
Directory directory = this.getPathDirectory(path);
this.addDirectory(directory, path[path.length - 1],p);
}
}
} catch (FileNotFoundException ex) {
System.out.println("read nothing from files");
}
}
@Override
public void deleteFile(File file) {
String[] blocks = file.indexBlockContent.split(" ");
for (int i = 0; i < blocks.length; i++) {
diskBlocks[Integer.parseInt(blocks[i])] = false;
}
diskBlocks[file.allocatedBlocks[0]] = false;
freeSize += file.size+1;
file.delete();
}
}
public abstract class Allocation {
boolean[] diskBlocks;
Directory root;
int freeSize;
int diskSize;
public Allocation(int n) {
diskBlocks = new boolean[n];
root = new Directory("root");
freeSize = n;
diskSize = n;
}
abstract public boolean addFile(Directory directory, String path, int size, String n);
abstract public void deleteFile(File file);
public Directory getPathDirectory(String[] path) {
Directory temp = this.root;
//System.out.println("m1");
int n = 1;
if(path.length>1){
while (!temp.name.equals(path[path.length - 2])) {
// System.out.println("m2");
for (int i = 0; i < temp.subDirectories.length; i++) {
if (temp.subDirectories[i].name.equals(path[n])) {
temp = temp.subDirectories[i];
n++;
break;
}
if (i == temp.subDirectories.length - 1)// means we will never find it because we did not find the directory
{
return null;
}
}
}}
// System.out.println("m3");
return temp;
}
public boolean addDirectory(Directory directory, String name, String path) {
Directory d = new Directory(path, name);
directory.addDirectory(d);
return true;
}
public void deleteDirectory(Directory d) {
d.delete();
}
public void printDirectoryStructure(String s, Directory d) {
if (!d.isDeleted()) {
System.out.println(s + d.name);
}
s += " ";
for (int i = 0; i < d.files.length; i++) {
if (!d.files[i].isDeleted()) {
System.out.println(s + d.files[i].name);
}
}
if (d.subDirectories != null) {
for (int i = 0; i < d.subDirectories.length; i++) {
printDirectoryStructure(s, d.subDirectories[i]);
}
}
}
public void DisplayDiskStatus() {
System.out.println("The free space = " + freeSize);
System.out.println("The allocated space = " + (diskSize - freeSize));
String freeBlocks = "";
String allocatedBlocks = "";
for (int i = 0; i < diskBlocks.length; i++) {
if (diskBlocks[i] == false) {
freeBlocks += " " + i;
} else {
allocatedBlocks += " " + i;
}
}
System.out.println("The free blocks are:");
System.out.println(freeBlocks);
System.out.println("The allocated blocks are:");
System.out.println(allocatedBlocks);
}
abstract public int checkFreeSpace(int size);
abstract public String storeData(String s, Directory d);
abstract public void readData();
}