-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.java
More file actions
137 lines (109 loc) · 3.54 KB
/
FileSystem.java
File metadata and controls
137 lines (109 loc) · 3.54 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
package Files;
import java.util.ArrayList;
import java.util.Collections;
public class FileSystem {
private Directory root;
private Directory pwd;
public FileSystem() {
root = new Directory("~");
pwd = root;
}
public void mkdir(String name) {
if (pwd.getChildren().contains(name)) {
System.out.println("Already exists: " + name);
return;
}
Directory newDirectory = new Directory(name);
newDirectory.setParent(pwd);
pwd.addChild(newDirectory);
}
public void touch(String name) {
if (pwd.getChildren().contains(name)) {
System.out.println("Already exists: " + name);
return;
}
pwd.addChild(new File(name));
}
public void ls() {
for (String file : pwd.getChildren()) {
Node child = pwd.getChild(file);
if (child instanceof Directory) {
System.out.println(file + "/");
} else {
System.out.println(file);
}
}
}
public void cd() {
pwd = root;
}
public void cd(String name) {
if (name.equals("..")) {
if (pwd.getParent() != null) {
pwd = pwd.getParent();
}
return;
}
Node child = pwd.getChild(name);
if (child == null) {
System.out.println("No such file or directory: " + name);
return;
} else if (child instanceof File) {
System.out.println("Not a directory: " + name);
return;
}
pwd = (Directory) child;
}
public String getPwdPath() {
ArrayList<String> path = new ArrayList<>();
Directory tmp = pwd;
while (tmp != null) {
path.add(tmp.getName());
tmp = tmp.getParent();
}
Collections.reverse(path);
return String.join("/", path);
}
public void rm(String name) {
if (pwd.getChild(name) == null) {
System.out.println("No such file or directory: " + name);
return;
} else if (pwd.getChild(name) instanceof Directory) {
Directory removedDirectory = (Directory) pwd.getChild(name);
if (removedDirectory.getChildren().size() > 0) {
System.out.println("Error: " + name + " is not empty.");
return;
}
}
pwd.remove(name);
}
public void tree() {
System.out.println(pwd.getName());
dirTree(pwd, "");
}
public void dirTree(Directory dir, String indent) {
ArrayList<String> children = dir.getChildren();
boolean isLast;
String modIndent;
for (int i = 0; i < children.size(); i++) {
if (i == children.size() - 1) {
isLast = true;
} else {
isLast = false;
}
String name = children.get(i);
String connector = "";
if (isLast) {
connector = "└── ";
modIndent = indent + " ";
} else {
connector = "├── ";
modIndent = indent + "| ";
}
System.out.println(indent + connector + name);
if (dir.getChild(name) instanceof Directory) {
dirTree((Directory) dir.getChild(name), modIndent);
}
}
}
}