-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.java
More file actions
205 lines (172 loc) · 5.53 KB
/
FileSystem.java
File metadata and controls
205 lines (172 loc) · 5.53 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
package OperatingSystem;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
public class FileSystem {
private String DIVIDER = "~";
int minimumFileSize = 4;
private FAT fat;
private MainMemory mainMemory;
public ArrayList<File> root;
private java.io.File file;
public FileSystem(FAT fat, MainMemory mainMemory) {
this.fat = fat;
this.mainMemory = mainMemory;
root = new ArrayList<File>();
file = new java.io.File("FILE_SYSTEM/persistentFileTable.txt");
try {
if (!file.exists()) {
file.createNewFile();
} else {
String content = FileUtils.readFileToString(file);
if (content.contains(DIVIDER))
for (int i = 0; i < content.split(DIVIDER).length; i++) {
String line = content.split(DIVIDER)[i];
root.add(new File(line.split(",")[0].trim(), Integer.parseInt(line.split(",")[1].trim()), Integer
.parseInt(line.split(",")[2].trim())));
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public File createFile(String fileName) {
return createFile(fileName, minimumFileSize);
}
public File createFile(String fileName, int size) {
/* If there isn't enough space, return false, else allocate */
if (!(fat.getTotalFreeSpace() >= size))
return null;
/* If name exists, don't create file */
for (File each : root)
if (each.getName().equals(fileName))
return null;
/*
* Traverse to all but last. For the last, store lastAddress and set it
* to -1.
*/
File newFile = new File(fileName, size, -1);
int startingAddress = -1;
int lastAddress = -1;
for (int i = 0; i <= size - 1; i++) {
int currentAddress = fat.getFreeBlock();
int nextAddress = fat.getNextFreeBlock();
fat.setElement(currentAddress, nextAddress);
if (i == 0) {
startingAddress = currentAddress;
}
if (i == (size - 2)) {
lastAddress = nextAddress;
}
}
fat.setElement(lastAddress, -1);
newFile.setStartingAddress(startingAddress);
write(newFile, "\0"); /* Write EOF word to newly created file */
root.add(newFile);
saveFileTableToPersistent();
return newFile;
}
public void deleteFile(File file) {
int currentAddress = file.getStartingAddress();
int nextAddress = file.getStartingAddress();
for (int i = 0; i < file.getSize(); i++) {
nextAddress = fat.getElement(nextAddress);
fat.setFree(currentAddress);
currentAddress = nextAddress;
}
root.remove(file);
saveFileTableToPersistent();
}
public void listFiles() {
for (File file : root) {
System.out.println(file.getName());
}
}
public boolean renameFile(File file, String fileName) {
if (getFileFromName(fileName) != null) {
System.out.print("\t Same file name exists");
return false;
}
file.setName(fileName);
saveFileTableToPersistent();
return true;
}
public File getFileFromName(String name) {
for (File file : root) {
if (file.getName().equals(name))
return file;
}
return null;
}
public boolean append(File file, String toWrite) {
return write(file, read(file).indexOf("\0"), toWrite);
}
public boolean write(File file, String toWrite) {
return write(file, 0, toWrite);
}
public boolean write(File file, int offset, String toWrite) {
toWrite += "\0";
/* If file requires more memory, allocate it */
if (((offset + toWrite.length()) / mainMemory.SIZE_OF_BLOCK) + 1 > file.getSize()) {
int blocksRequired = 1 + ((offset + toWrite.length()) / mainMemory.SIZE_OF_BLOCK) - file.getSize();
if (!allocateMemory(file, blocksRequired))
return false;
}
for (int i = 0; i < toWrite.length();) {
int block = (offset + i) / mainMemory.SIZE_OF_BLOCK;
int position = (offset + i) % mainMemory.SIZE_OF_BLOCK;
int lengthToWriteTo = i + mainMemory.SIZE_OF_BLOCK <= toWrite.length() ? i + mainMemory.SIZE_OF_BLOCK : toWrite
.length();
mainMemory.writeToMemory(file.getStartingAddress() + block, position, toWrite.substring(i, lengthToWriteTo));
i += (mainMemory.SIZE_OF_BLOCK - position);
}
saveFileTableToPersistent();
return true;
}
public String read(File file) {
String toRead = "";
int next = file.getStartingAddress();
while (next != -1) {
toRead += mainMemory.readFromMemory(next);
next = fat.getElement(next);
}
return toRead;
}
public boolean allocateMemory(File file, int blocksRequired) {
if (fat.getTotalFreeSpace() >= blocksRequired) {
System.out.println("\t Allocating " + blocksRequired + " blocks to file " + file.getName());
int lastIndex = -1;
int next = file.getStartingAddress();
while (next != -1) {
lastIndex = next;
next = fat.getElement(next);
}
for (int i = 0; i < blocksRequired; i++) {
int firstFree = fat.getFreeBlock();
fat.setElement(lastIndex, firstFree);
fat.setElement(firstFree, -1);
lastIndex = firstFree;
}
file.setSize(file.getSize() + blocksRequired);
return true;
} else {
System.out.println("\t Unable to Allocate More Memory");
return false;
}
}
private void saveFileTableToPersistent() {
StringBuilder builder = new StringBuilder();
for (File file : root) {
builder.append(file.toString());
builder.append(DIVIDER);
}
/* delete last DIVIDER */
if (builder.toString().contains(DIVIDER))
builder.deleteCharAt(builder.toString().length() - 1);
try {
FileUtils.write(file, builder.toString(), false);
} catch (IOException e) {
e.printStackTrace();
}
}
}