-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.java
More file actions
331 lines (270 loc) · 10.3 KB
/
FileSystem.java
File metadata and controls
331 lines (270 loc) · 10.3 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
package file_structure;
// FileSystem.java - Main file system implementation
import java.util.*;
public class FileSystem {
private Superblock superblock;
private Bitmap inodeBitmap;
private Bitmap blockBitmap;
private Map<Integer, Inode> inodeTable;
private Map<Integer, Directory> directoryCache;
private Map<Integer, byte[]> blockStorage;
// Root directory is always inode 2
private static final int ROOT_INODE = 2;
public FileSystem(int blockSize, long totalBlocks, long totalInodes) {
this.superblock = new Superblock(blockSize, totalBlocks, totalInodes);
this.inodeBitmap = new Bitmap((int) totalInodes);
this.blockBitmap = new Bitmap((int) totalBlocks);
this.inodeTable = new HashMap<>();
this.directoryCache = new HashMap<>();
this.blockStorage = new HashMap<>();
// Reserve inode 0 and 1 (system reserved)
inodeBitmap.reserve(0);
inodeBitmap.reserve(1);
// Reserve block 0 (superblock)
blockBitmap.reserve(0);
// Create root directory
createRootDirectory();
}
private void createRootDirectory() {
Inode rootInode = new Inode(ROOT_INODE,
Inode.FileType.DIRECTORY, 0, 0);
inodeBitmap.reserve(ROOT_INODE);
inodeTable.put(ROOT_INODE, rootInode);
Directory rootDir = new Directory(rootInode);
directoryCache.put(ROOT_INODE, rootDir);
superblock.allocateInode();
}
// Create a new file
public boolean createFile(String path, int uid, int gid) {
String[] parts = parsePath(path);
if (parts.length == 0)
return false;
String fileName = parts[parts.length - 1];
String parentPath = getParentPath(parts);
// Find parent directory
Inode parentInode = resolvePath(parentPath);
if (parentInode == null ||
parentInode.getType() != Inode.FileType.DIRECTORY) {
return false;
}
Directory parentDir = getDirectory(parentInode.getInodeNumber());
// Check if file already exists
if (parentDir.findEntry(fileName) != null) {
return false;
}
// Allocate new inode
int inodeNum = inodeBitmap.allocate();
if (inodeNum < 0 || !superblock.allocateInode()) {
return false;
}
// Create new file inode
Inode fileInode = new Inode(inodeNum,
Inode.FileType.REGULAR_FILE, uid, gid);
inodeTable.put(inodeNum, fileInode);
// Add directory entry
DirectoryEntry entry = new DirectoryEntry(inodeNum, fileName, (byte) 1);
parentDir.addEntry(entry);
return true;
}
// Create a new directory
public boolean createDirectory(String path, int uid, int gid) {
String[] parts = parsePath(path);
if (parts.length == 0)
return false;
String dirName = parts[parts.length - 1];
String parentPath = getParentPath(parts);
// Find parent directory
Inode parentInode = resolvePath(parentPath);
if (parentInode == null ||
parentInode.getType() != Inode.FileType.DIRECTORY) {
return false;
}
Directory parentDir = getDirectory(parentInode.getInodeNumber());
// Check if directory already exists
if (parentDir.findEntry(dirName) != null) {
return false;
}
// Allocate new inode
int inodeNum = inodeBitmap.allocate();
if (inodeNum < 0 || !superblock.allocateInode()) {
return false;
}
// Create new directory inode
Inode dirInode = new Inode(inodeNum,
Inode.FileType.DIRECTORY, uid, gid);
inodeTable.put(inodeNum, dirInode);
// Create directory structure
Directory newDir = new Directory(dirInode);
directoryCache.put(inodeNum, newDir);
// Update .. to point to parent
newDir.removeEntry("..");
newDir.addEntry(new DirectoryEntry(
parentInode.getInodeNumber(), "..", (byte) 2));
// Add directory entry in parent
DirectoryEntry entry = new DirectoryEntry(inodeNum, dirName, (byte) 2);
parentDir.addEntry(entry);
return true;
}
// Write data to a file
public boolean writeFile(String path, byte[] data) {
Inode inode = resolvePath(path);
if (inode == null ||
inode.getType() != Inode.FileType.REGULAR_FILE) {
return false;
}
int blockSize = superblock.getBlockSize();
int blocksNeeded = (data.length + blockSize - 1) / blockSize;
// Allocate blocks
List<Integer> blocks = new ArrayList<>();
for (int i = 0; i < blocksNeeded; i++) {
int blockNum = blockBitmap.allocate();
if (blockNum < 0 || !superblock.allocateBlock()) {
// Rollback
for (int b : blocks) {
blockBitmap.free(b);
superblock.freeBlock();
}
return false;
}
blocks.add(blockNum);
inode.addBlock(blockNum);
}
// Write data to blocks
for (int i = 0; i < blocksNeeded; i++) {
int offset = i * blockSize;
int length = Math.min(blockSize, data.length - offset);
byte[] blockData = new byte[blockSize];
System.arraycopy(data, offset, blockData, 0, length);
blockStorage.put(blocks.get(i), blockData);
}
inode.setSize(data.length);
inode.updateModifiedTime();
return true;
}
// Read data from a file
public byte[] readFile(String path) {
Inode inode = resolvePath(path);
if (inode == null ||
inode.getType() != Inode.FileType.REGULAR_FILE) {
return null;
}
int blockSize = superblock.getBlockSize();
long fileSize = inode.getSize();
byte[] data = new byte[(int) fileSize];
int blocksToRead = (int) ((fileSize + blockSize - 1) / blockSize);
for (int i = 0; i < blocksToRead; i++) {
int blockNum = inode.getBlockNumber(i);
if (blockNum < 0)
continue;
byte[] blockData = blockStorage.get(blockNum);
if (blockData != null) {
int offset = i * blockSize;
int length = (int) Math.min(blockSize, fileSize - offset);
System.arraycopy(blockData, 0, data, offset, length);
}
}
inode.updateAccessTime();
return data;
}
// List directory contents
public List<String> listDirectory(String path) {
Inode inode = resolvePath(path);
if (inode == null ||
inode.getType() != Inode.FileType.DIRECTORY) {
return Collections.emptyList();
}
Directory dir = getDirectory(inode.getInodeNumber());
List<String> files = new ArrayList<>();
for (DirectoryEntry entry : dir.listEntries()) {
if (!entry.getName().equals(".") &&
!entry.getName().equals("..")) {
files.add(entry.getName());
}
}
return files;
}
// Delete a file
public boolean deleteFile(String path) {
String[] parts = parsePath(path);
if (parts.length == 0)
return false;
String fileName = parts[parts.length - 1];
String parentPath = getParentPath(parts);
Inode fileInode = resolvePath(path);
if (fileInode == null ||
fileInode.getType() != Inode.FileType.REGULAR_FILE) {
return false;
}
Inode parentInode = resolvePath(parentPath);
Directory parentDir = getDirectory(parentInode.getInodeNumber());
// Free all blocks
for (int i = 0; i < 12; i++) {
int blockNum = fileInode.getBlockNumber(i);
if (blockNum >= 0) {
blockBitmap.free(blockNum);
blockStorage.remove(blockNum);
superblock.freeBlock();
}
}
// Free inode
inodeBitmap.free(fileInode.getInodeNumber());
inodeTable.remove(fileInode.getInodeNumber());
superblock.freeInode();
// Remove directory entry
parentDir.removeEntry(fileName);
return true;
}
// Helper: Resolve path to inode
private Inode resolvePath(String path) {
if (path.equals("/")) {
return inodeTable.get(ROOT_INODE);
}
String[] parts = parsePath(path);
Inode current = inodeTable.get(ROOT_INODE);
for (String part : parts) {
if (current.getType() != Inode.FileType.DIRECTORY) {
return null;
}
Directory dir = getDirectory(current.getInodeNumber());
DirectoryEntry entry = dir.findEntry(part);
if (entry == null) {
return null;
}
current = inodeTable.get(entry.getInodeNumber());
if (current == null) {
return null;
}
}
return current;
}
private String[] parsePath(String path) {
if (path.equals("/"))
return new String[0];
path = path.replaceAll("^/+", "").replaceAll("/+$", "");
return path.isEmpty() ? new String[0] : path.split("/");
}
private String getParentPath(String[] parts) {
if (parts.length <= 1)
return "/";
return "/" + String.join("/",
Arrays.copyOfRange(parts, 0, parts.length - 1));
}
private Directory getDirectory(int inodeNum) {
return directoryCache.computeIfAbsent(inodeNum,
k -> new Directory(inodeTable.get(k)));
}
// Get file system statistics
public String getStats() {
return String.format(
"File System Stats:" +
" Block Size: %d bytes" +
" Free Blocks: %d" +
" Free Inodes: %d" +
" Total Files: %d" + " State: %s",
superblock.getBlockSize(),
superblock.getFreeBlocks(),
superblock.getFreeInodes(),
inodeTable.size(),
superblock.getState());
}
}