-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot_cli.py
More file actions
390 lines (313 loc) · 12.1 KB
/
root_cli.py
File metadata and controls
390 lines (313 loc) · 12.1 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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# SHOULD NOT BE ABLE TO CREATE TWO FOLDERS OF SAME NAME
import ram
import dbg
from call import *
import sys
from bcolors import *
from error import *
from _thread import *
import multiprocessing
def prompt() :
sys.stdout.write(f'{bcolors.OKCYAN}<root>{bcolors.ENDC} ')
sys.stdout.flush()
def root_cli():
currentUser = "root"
# add inode#0 whenever root_cli starts up
ram.user_space[currentUser] = [0,[],[]]
print(f"\n{bcolors.OKGREEN}MOUNTED SUCCESSFULLY{bcolors.ENDC}")
print(f"{bcolors.OKCYAN}You have been entered in CLI (enter e to exit CLI){bcolors.ENDC}\n")
while True:
prompt()
if ram.changes == ram.changes_threashold:
p1 = multiprocessing.Process(target=hardWrite, args=("autoSaved_"+ram.current_fileSystem, ))
p1.start()
ram.changes = 0
# print(ram.user_space)
# if len(ram.user_space[currentUser][2]) != 0:
# print(f"{bcolors.WARNING}Thread returned, use command {bcolors.OKCYAN}t show{bcolors.ENDC} to view threads returned data{bcolors.ENDC}")
command = input()
command = command.split()
arg = [000]
# e to exit
if command[0] == 'e':
sys.exit(0)
break
if command[0]=="t" and command[1]=="show":
for data in ram.user_space[currentUser][2]:
print(data+"\n")
ram.user_space[currentUser][2] = []
continue
# Prints
if command[0] == "ls":
l = cls(ram.user_space[currentUser][0]).split("-")
dir_item = l[0].split()
non_dir_item = l[1].split()
for i,item in enumerate(dir_item):
if (i+1)%8 == 0:
print(f'{bcolors.BOLD}{item}{bcolors.ENDC}')
else:
print(f'{bcolors.BOLD}{item}{bcolors.ENDC}',end=' ')
for i,item in enumerate(non_dir_item):
if (i+1)%8 == 0:
print(f'{bcolors.OKBLUE}{item}{bcolors.ENDC}')
else:
print(f'{bcolors.OKBLUE}{item}{bcolors.ENDC}',end=' ')
print()
continue
# t operation fname arguments
# Operations can be 0/1/2
# 0 = read, 1 = write, 2 = truncate
# arguments are the same as for read, write and truncate
if command[0] == "t":
try:
sub_command = command[1]
temp = sub_command.split('-')
sub_command = int(temp[0])
delay = 0
if len(temp) == 2:
delay = int(temp[1])
except:
arguments_not_int()
continue
if (sub_command == 0 or sub_command == 1) and len(command) < 5:
wrong_argument()
continue
if sub_command == 2 and len(command) != 4:
wrong_argument()
continue
if sub_command == 0:
try:
start = int(command[3])
end = int(command[4])
except:
arguments_not_int()
continue
start_new_thread(trwt, (command[2], sub_command, ram.user_space[currentUser][0], [start, end], currentUser, delay))
# trwt(command[2], sub_command, ram.user_space[currentUser][0], [start, end], currentUser)
continue
if sub_command == 1:
try:
loc = int(command[3])
except:
arguments_not_int()
continue
text = ' '.join(command[4:])
start_new_thread(trwt,(command[2], sub_command, ram.user_space[currentUser][0], [loc, text],currentUser, delay))
# trwt(command[2], sub_command, ram.user_space[currentUser][0], [loc, text],currentUser)
continue
if sub_command == 2:
try:
size = int(command[3])
except:
arguments_not_int()
continue
start_new_thread(trwt,(command[2], sub_command, ram.user_space[currentUser][0], [size], currentUser, delay))
# trwt(command[2], sub_command, ram.user_space[currentUser][0], [size], currentUser)
continue
wrong_argument()
continue
# If file being created is already in directory table then through error
if command[0] == "make":
if len(command) != 3:
wrong_argument();
continue;
# if it is anythin other then 0 then it is for file not dir
dir_flag = False if command[2]!="0" else True
cmakeFile(ram.user_space[currentUser][0], command[1], dir_flag)
ram.changes = ram.changes + 1
continue;
if command[0] == "delete":
if len(command) != 2:
wrong_argument();
continue
status = cdelete(command[1], ram.user_space[currentUser][0]);
if status == -4:
fname_not_in_dir()
continue
ram.changes = ram.changes + 1
continue
# Prints
if command[0] == "data":
if len(command) != 2:
wrong_argument()
continue
status, data = cshow_data(int(command[1]))
if status == -15:
index_out_of_bound()
continue
print(data)
continue;
if command[0] == "open":
if len(command) != 3:
wrong_argument()
continue;
mode = 1 if command[2]!="0" else 0
status = cOpen(command[1], mode, currentUser)
if status == -13:
file_already_open();
continue
if status == -11:
file_not_found()
continue
continue
if command[0] == "close":
if len(command) != 2:
wrong_argument()
continue
status = cClose(command[1])
if status == -12:
file_not_open()
continue
continue
# Prints
if command[0] == "read":
if len(command) != 4:
wrong_argument()
continue
try:
start = int(command[2])
end = int(command[3])
except:
arguments_not_int()
continue
status, text = cread(command[1], start, end)
if status == -12:
file_not_open();
continue
print(text)
continue
# write filename loc text
if command[0] == "write":
if len(command) < 4:
wrong_argument()
continue
fname = command[1]
try:
loc = int(command[2])
except:
arguments_not_int()
continue
text = ' '.join(command[3:])
status = cwrite(fname, loc, text)
if status == -7:
free_space_not_found()
continue
if status == -8:
out_of_scope_memory_accessed_in_file()
continue
if status == -12:
file_not_open();
continue
ram.changes = ram.changes + 1
continue
if command[0] == "truncate":
if len(command) != 3:
wrong_argument()
continue;
try:
size = int(command[2])
except:
arguments_not_int()
continue
status = ctruncate(command[1], size)
if status == -9:
trimming_empty_file()
continue;
if status == -12:
file_not_open();
continue
ram.changes = ram.changes + 1
continue
if command[0] == "move":
if len(command) != 3:
wrong_argument()
continue
status = cmove(command[1], command[2], ram.user_space[currentUser][0])
if status == -4:
fname_not_in_dir();
continue;
if status == -5:
file_not_a_dir();
continue
ram.changes = ram.changes + 1
continue
# Prints
if command[0] == "memmap":
if len(command) != 2:
wrong_argument()
continue
try:
index = int(command[1])
except:
arguments_not_int()
continue
mem =cmemmap(index)[1].split('\n')
for l in mem:
print(l);
continue
if command[0] == "chDir":
if len(command) != 2:
wrong_argument()
continue
status = cchDir(command[1], ram.user_space[currentUser][0], currentUser)
if status == -5:
file_not_a_dir()
continue
if status == -11:
file_not_found()
continue
continue
# Prints
if command[0] == "currentInode":
print(ccurrentInode(currentUser));
continue
# Prints
if command[0] == "partitionInfo":
print(cpartitionInfo());
continue
# Prints
if command[0] == "blockSize":
print(cblockSize())
continue
# Prints
if command[0] == "logicalSpace":
print(clogicalSpace())
continue
if command[0] == "freeSpace":
print(f"Unused memeory blocks:\n {cfreeSpace()}\n")
print(f"Note: Block# " + f"{bcolors.OKGREEN}{str(ram.out_scope_block)}{bcolors.ENDC}" + f" is {bcolors.OKCYAN}out of scope flag{bcolors.ENDC} for this hard disk,it can be different for different disks"
" it means this block number doesn't exist "
"it is kept to maintain heap of fixed size,"
" whenever a block is used a new out scope block# is added")
continue
if command[0] =="masterBlock":
print(f"Master block is at start of hard disk and {bcolors.OKGREEN}contain addresses of major block groups{bcolors.ENDC} which are important for starting filesystem.")
print("Major block groups are:\n"
f"\n\t-{bcolors.WARNING}Master Boot Record (mbr){bcolors.ENDC}\t\t->\tBoot record for File System, not system"
f"\n\t-{bcolors.WARNING}Free Blocks (fb){bcolors.ENDC}\t\t\t->\tContain unused block # to maintain a heap."
f"\n\t-{bcolors.WARNING}Inode Block (ib){bcolors.ENDC}\t\t\t->\tContains Inode Table")
print()
print("Block group name with start and end address is given below:")
print(cmasterBlock())
continue
if command[0] == "blocksConsumed":
if len(command) != 2:
wrong_argument()
continue
status, blocks = cblocksConsumed(command[1], currentUser)
if status == -11:
file_not_found()
continue
print(blocks)
continue
if command[0] == "saveChanges":
filename = input(f"Please name saved file, {bcolors.WARNING}you can also overwrite existing file{bcolors.ENDC}, don't forget extension .txt\nEnter {bcolors.OKCYAN}-1{bcolors.ENDC} to go back to CLI\n");
if filename == "-1":
print("");
continue
hardWrite(filename)
print(f"{bcolors.OKGREEN}{bcolors.OKCYAN}{filename}{bcolors.ENDC} has been updated{bcolors.ENDC}\n")
else:
wrong_command()
print("");
print()