操作系统实验——简单文件系统

记录一下学校的操作系统实验内容:

(1)实验目的
本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。
(2)实验内容
为DOS系统设计一个简单的二级文件系统。要求做到以下几点:
①可以实现下列命令:
login 用户登录
dir 列文件目录
create 创建文件
delete 删除文件
open 打开文件
close 关闭文件
read 读文件
write 写文件

总结:

Map是个好东西

代码如下:

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
import java.util.HashMap;
import java.util.Scanner;

/**
* Created by Pure White on 2017/5/17.
*/

class UserSystem {
private static HashMap<String, User> users = new HashMap<>();

private UserSystem() {
}

private static boolean exist(String username) {
return users.containsKey(username);
}

public static User login(String username, String password) {
if (!exist(username)) {
return null;
}
User user = users.get(username);
return user.authenticate(password) ? user : null;
}

public static User createUser(String username, String password) {
if (exist(username)) {
return null;
}
User user = new User();
user.setUsername(username);
user.setPassword(password);
users.put(username, user);
return user;
}
}

class User {
private String username;
private String password;

void setPassword(String password) {
this.password = password;
}

boolean authenticate(String password) {
return this.password.equals(password);
}

public String getUsername() {
return this.username;
}

void setUsername(String username) {
this.username = username;
}
}


public class FileSystem {
private static HashMap<User, HashMap<String, File>> userFiles = new HashMap<>();
private static User nowUser;

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Please input command: ");
String cmd = scanner.nextLine().toLowerCase().trim();
String username;
String password;
String filename;
String content;
User user;
switch (cmd) {
case "login":
System.out.print("Username: ");
username = scanner.nextLine().toLowerCase().trim();
System.out.print("Password: ");
password = scanner.nextLine().toLowerCase().trim();
user = UserSystem.login(username, password);
if (user == null) {
System.out.println("Invalid User!");
} else {
nowUser = user;
System.out.println("Login success!");
}
break;
case "adduser":
System.out.print("Username: ");
username = scanner.nextLine().toLowerCase().trim();
System.out.print("Password: ");
password = scanner.nextLine().toLowerCase().trim();
user = UserSystem.createUser(username, password);
if (user == null) {
System.out.println("User already exists!");
} else {
nowUser = user;
userFiles.put(nowUser, new HashMap<>());
System.out.println("Create user success!");
}
break;
case "ls":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
for (File file : userFiles.get(nowUser).values()) {
System.out.printf("-- %s\n", file.getName());
}
break;
case "create":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
System.out.print("Filename: ");
filename = scanner.nextLine().toLowerCase().trim();
if (userFiles.get(nowUser).containsKey(filename)) {
System.out.println("File already exists!");
break;
} else {
File file = new File(filename, nowUser);
userFiles.get(nowUser).put(filename, file);
System.out.println("Create file success!");
}
break;
case "delete":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
System.out.print("Filename: ");
filename = scanner.nextLine().toLowerCase().trim();
if (!userFiles.get(nowUser).containsKey(filename)) {
System.out.println("File doesn't exist!");
break;
} else {
File file = userFiles.get(nowUser).get(filename);
if (!file.getWriteLockState()) {
System.out.println("File has already been opened!");
break;
}
userFiles.get(nowUser).remove(filename);
System.out.println("Delete file success!");
}
break;
case "open":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
System.out.print("Filename: ");
filename = scanner.nextLine().toLowerCase().trim();
if (!userFiles.get(nowUser).containsKey(filename)) {
System.out.println("File doesn't exist!");
break;
} else {
File file = userFiles.get(nowUser).get(filename);
if (!file.getWriteLockState()) {
System.out.println("File has already been opened!");
break;
}
file.writeLock();
System.out.println("Open file success!");
}
break;
case "close":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
System.out.print("Filename: ");
filename = scanner.nextLine().toLowerCase().trim();
if (!userFiles.get(nowUser).containsKey(filename)) {
System.out.println("File doesn't exist!");
break;
} else {
File file = userFiles.get(nowUser).get(filename);
if (file.getWriteLockState()) {
System.out.println("File hasn't been opened!");
break;
}
file.writeUnlock();
System.out.println("Close file success!");
}
break;
case "write":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
System.out.print("Filename: ");
filename = scanner.nextLine().toLowerCase().trim();
if (!userFiles.get(nowUser).containsKey(filename)) {
System.out.println("File doesn't exist!");
break;
} else {
File file = userFiles.get(nowUser).get(filename);
if (file.getWriteLockState()) {
System.out.println("File hasn't been opened!");
break;
}
System.out.print("Content: ");
content = scanner.nextLine();
file.setContent(content);
System.out.println("Write file success!");
}
break;
case "read":
if (nowUser == null) {
System.out.println("Please login first!");
break;
}
System.out.print("Filename: ");
filename = scanner.nextLine().toLowerCase().trim();
if (!userFiles.get(nowUser).containsKey(filename)) {
System.out.println("File doesn't exist!");
break;
} else {
File file = userFiles.get(nowUser).get(filename);
if (!file.getWriteLockState()) {
System.out.println("File has already been opened for write!");
break;
}
file.readLock();
content = file.getContent();
file.readUnlock();
System.out.println("Read file success!");
System.out.printf("Content: %s\n", content);
}
break;
case "exit":
System.exit(0);
default:
System.out.println("Command error!");
}
}
}
}

class File {
private String name;
private String content;
private User owner;
private boolean opened;
private boolean reading;

public File(String name, User user) {
setName(name);
setOwner(user);
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}

public User getOwner() {
return this.owner;
}

public void setOwner(User owner) {
this.owner = owner;
}

public void readLock() {
this.reading = true;
}

public void readUnlock() {
this.reading = false;
}

public void writeLock() {
this.opened = true;
}

public void writeUnlock() {
this.opened = false;
}

public boolean getWriteLockState() {
return !this.opened;
}
}
请博主喝杯咖啡~
0%