-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectory.java
More file actions
63 lines (53 loc) · 1.69 KB
/
Directory.java
File metadata and controls
63 lines (53 loc) · 1.69 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
package file_structure;
// Directory.java - Manages directory entries
import java.util.*;
public class Directory {
private Inode inode;
private List<DirectoryEntry> entries;
public Directory(Inode inode) {
if (inode.getType() != Inode.FileType.DIRECTORY) {
throw new IllegalArgumentException("Inode must be a directory");
}
this.inode = inode;
this.entries = new ArrayList<>();
// Add . and .. entries
entries.add(new DirectoryEntry(inode.getInodeNumber(), ".", (byte) 2));
entries.add(new DirectoryEntry(inode.getInodeNumber(), "..", (byte) 2));
}
public boolean addEntry(DirectoryEntry entry) {
// Check if entry already exists
for (DirectoryEntry e : entries) {
if (e.getName().equals(entry.getName())) {
return false; // Entry already exists
}
}
entries.add(entry);
inode.updateModifiedTime();
return true;
}
public boolean removeEntry(String name) {
// Cannot remove . or ..
if (name.equals(".") || name.equals("..")) {
return false;
}
boolean removed = entries.removeIf(e -> e.getName().equals(name));
if (removed) {
inode.updateModifiedTime();
}
return removed;
}
public DirectoryEntry findEntry(String name) {
for (DirectoryEntry entry : entries) {
if (entry.getName().equals(name)) {
return entry;
}
}
return null;
}
public List<DirectoryEntry> listEntries() {
return new ArrayList<>(entries);
}
public Inode getInode() {
return inode;
}
}