-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilesystemwolf.py
More file actions
1121 lines (1019 loc) · 37.5 KB
/
filesystemwolf.py
File metadata and controls
1121 lines (1019 loc) · 37.5 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
* @@@@@@@@@@@@@@ TheYoungWolf Productions @@@@@@@@@@@@@@
*
* Copyright (C) 2020 File System Simulation
*
* Licensed under the Apache License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License from the author
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""
import os
import sys
import math
import llist
from llist import dllist, dllistnode # Using Double Linked List Library
from threading import Lock, Thread
from socket import *
# from _thread import *
import pickle
# ******************* Global Variables ********************
gl_inputfile = ""
gl_inputdir = ""
gl_disksize = 0
gl_blocksize = 0
gl_totalblock = 0
gl_filecount = 0
gl_dircount = 0
gl_fragmentation = 0
gl_freespace = 0
# ******************** All definitons **********************
# Node definition of Disk Linked List
class nodeDiskLL:
def __init__(self):
self.status = "FREE"
self.begin = 0
self.end = 0
# self.next = None
# Disk = nodeDiskLL()
Disk = dllist()
# Node definition of File node in tree
class nodeFile:
def __init__(self):
self.filename = ""
self.fullname = ""
self.path = ""
self.fileSize = 0
self.fileInfoList = None
# Node definition of Directory node in tree
class nodeDir:
def __init__(self):
self.parentDir = None
self.fullname = ""
self.name = ""
self.path = ""
self.dirDLList = None
self.fileDLList = None
# Creating a root node
root = None
# ************* Main program helper functions ***************
# dirORls(<dir node>) -> Prints out the items in a directory #DONE
# printout(<dir node>) -> Prints meta info about files in a directory #DONE
# chdir(<parentDir node>, <dir name>) -> Change Directory #DONE
# mkdir(<parentDir node>, <dir name>) -> Creates a new directory #DONE
# rmdir(<parentDir node>, <dir name>) -> Removes a directory #DONE
# mkfile(<parentDir node>, <file name>, <file size>) -> Creates a new file #DONE
# rmfile(<parentDir node>, <file name>) -> Removes a file #DONE
# remove(<parentDir node>, <file name>, <file size>) -> Remove bytes from file #DONE
# append(<parentDir node>, <file name>, <file size>) -> Append bytes to a file #DONE
# Prints out the items in a directory
def dirORls(node):
# print('in dirORls')
reply = ""
for i in range(node.dirDLList.size):
reply += node.dirDLList[i].name + "\n"
for j in range(node.fileDLList.size):
reply += node.fileDLList[j].filename + "\n"
return reply
# Print meta info about files in a directory
def printout(node):
reply = ''
l = node.fileDLList
for each in range(len(l)):
currentChild = l[each]
reply += ("-------------------------------------------------------------\n")
reply += ("1. File name: " + currentChild.filename + '\n')
reply += ("2. Full name: " + currentChild.fullname + '\n')
reply += ("3. File path: " + currentChild.path+"/\n")
reply += ("4. File size: " + str(currentChild.fileSize)+" bytes\n")
reply += printFileLL(currentChild.fileInfoList, currentChild.fileSize)
reply += ("-------------------------------------------------------------\n")
return reply
# Change directory
def chdir(dir, name):
l = dir.dirDLList
if(l.first != None):
for i in range(len(l)):
currentChild = l[i]
if(currentChild.name in name):
return currentChild
return None
return None
# Creates a new directory if it doesn't exist.
def mkdir(node, name):
global gl_dircount
new = nodeDir()
# Concat final full name of directory
fullname = node.fullname + name
# Creates a new directory if and only if it doesnt already exist
if (node.fullname != fullname):
new = initDir(node, fullname)
node.dirDLList.append(new)
gl_dircount += 1
file = open(gl_inputdir, "a")
file.write(fullname+"\n")
file.flush()
file.close()
# Removes a directory
def rmdir(node, name):
reply = ''
i = 0
# if there is no directory to remove
if node.dirDLList == None:
return reply
# Child directories
dirList = node.dirDLList
# Traverse over child directories to find directory to delete.
for i in range(node.dirDLList.size):
cd = dirList[i]
if cd.name == name:
# Inorder to remove a directory, it cannot have subdirectories
if (cd.dirDLList.first != None):
reply += ("Failed to remove directory because it has subdirectories!\n")
return reply
else:
dirList.remove(dirList.nodeat(i))
# Update the dir_list.txt file
fullname = cd.fullname
# print(fullname+str(len(fullname)))
file = open(gl_inputdir, "r")
lines = file.readlines()
file.close()
file2 = open(gl_inputdir, "w")
for line in lines:
if str(line.strip("\n")+"/") != fullname:
# print(line.strip("\n") + " " + fullname)
file2.write(line)
file2.flush()
file2.close()
return reply
# Creates a file
def mkfile(node, name, size):
reply = ''
global gl_filecount
if(size > gl_freespace):
reply += ("Not enough freespace, current freespace: " +
str(gl_freespace)+" bytes\n")
return reply
fullname = ""
f = nodeFile()
f.fileSize = size
# Concat into final fullname
fullname += node.fullname
fullname += name
# Assign to node variables.
f.fullname = fullname
f.filename = name
f.path = node.path
# Create a linked list for this file
f.fileInfoList = dllist()
createFileLL(f, f.fileSize)
# The parent node should have this file in its files DLL
node.fileDLList.append(f)
gl_filecount += 1
onFileAdd(size)
# Write new file to file_list.txt
file = open(gl_inputfile, "a")
file.write(str(size)+" "+fullname+"\n")
file.close()
return reply
# Removes a file
def rmfile(node, name):
reply = ''
global gl_freespace, gl_fragmentation
# First check whether this node(dir) has any files in it
# If not, return
if(node.fileDLList == None):
reply += ("No such file in this directory\n")
return reply
# We traverse the list and find the file node to be deleted from DLL.
for i in range(node.fileDLList.size):
currentChild = node.fileDLList[i]
# Once found, free up disk space, reduce fragmentation
# Free up that files own Linked List
# Lastly remove the file from the node(dir) DLL of all files
if(currentChild.filename == name):
fullname = currentChild.fullname
onFileRemove(currentChild.fileSize)
# gl_freespace += currentChild.fileSize
# gl_fragmentation -= countFragmentation(currentChild.fileSize)
freeFileLL(currentChild.fileInfoList)
node.fileDLList.remove(node.fileDLList.nodeat(i))
# Update the file_list.txt file.
file = open(gl_inputfile, "r")
lines = file.readlines()
file.close()
file2 = open(gl_inputfile, "w")
for line in lines:
each = line.split()
if each[1].strip("\n") != fullname:
file2.write(line)
file2.flush()
file2.close()
return reply
# Remove file data aka reduce file size
def remove(node, name, size):
reply = ''
global gl_blocksize
x = 0
# No files in the directory
if node.fileDLList.first == None:
return
if size <= 0:
return reply
# Access nodes Files DLL
filelist = node.fileDLList
# Iterate over every file
for i in range(node.fileDLList.size):
cf = filelist[i]
if cf.filename == name:
onFileRemove(cf.fileSize)
x = cf.fileSize - size
if(x < 0):
reply += ("Cannot remove more bytes than total file size\n")
return reply
cf.fileSize = x
f = open(gl_inputfile, "r")
lines = f.readlines()
f.close()
f2 = open(gl_inputfile, "w")
for line in lines:
each = line.split()
if(each[1].strip("\n") != cf.fullname):
f2.write(line)
else:
f2.write(str(x)+" "+each[1]+"\n")
f2.flush()
# filesize = cf.fileSize
f2.close()
onFileAdd(cf.fileSize)
if (x == 0):
freeFileLL(cf.fileInfoList)
cf.fileInfoList = None
cf.fileSize = 0
return reply
else:
freeFileLL(cf.fileInfoList)
cf.fileInfoList = None
createFileLL(cf, x)
# if x < gl_blocksize:
# return
# while x > gl_blocksize:
# x -= gl_blocksize
# if(temp == None):
# temp = cf.fileInfoList
# temp = temp.next
# freeFileLL(temp.next)
# temp.next = None
return reply
# Add file data aka increase file size
def append(root, name, size):
reply = ''
global gl_blocksize, gl_freespace
new = 0
# No files in the directory
if(size > gl_freespace):
reply += ("Not enough freespace\n")
return reply
if root.fileDLList.first == None:
return reply
if size <= 0:
return reply
# Access nodes files DLL
filelist = root.fileDLList
# Iterate over every file
for i in range(root.fileDLList.size):
cf = filelist[i]
if cf.filename == name:
onFileRemove(cf.fileSize)
cf.fileSize = cf.fileSize + size
new = cf.fileSize
f = open(gl_inputfile, "r")
lines = f.readlines()
f.close()
f2 = open(gl_inputfile, "w")
for line in lines:
each = line.split()
if(each[1].strip("\n") != cf.fullname):
f2.write(line)
else:
f2.write(str(new)+" "+each[1]+"\n")
# filesize = cf.fileSize
f2.flush()
f2.close()
onFileAdd(new)
if cf.fileInfoList == None:
createFileLL(cf, new)
return reply
else:
freeFileLL(cf.fileInfoList)
cf.fileInfoList = None
createFileLL(cf, new)
# while new > gl_blocksize:
# new -= gl_blocksize
# if cf.fileInfoList.next == None:
# cf.fileInfoList = cf.fileInfoList.next
# else:
# cf.fileInfoList.next = createFileLL(new)
return reply
# ********************** Input checks **********************
# inputCheck(<No. of args>, <args list>) -> Checks Input arguments #DONE
# inputError(arg) -> Displays Appropriate error #DONE (NOT USED THO)
# paramCheck(str, i) -> Helper function to inputCheck #DONE
# inputFiles() -> Prints the names of input files we'll run our script on #DONE
# readFile(argc) -> Opens the readme file #DONE (NOT USED THO)
# Check if there are 4 args. If yes, then checks their correctness
# If not, it displays the relavent error
def inputCheck(argc, argv):
r = 1
if(argc < 4):
if(argv[1] != "?" or argv[1] != "help"):
r = inputError(0)
else:
r = inputError(4-argc)
elif(argc > 4):
r = inputError(argc)
else:
for i in range(1, 4):
r = paramCheck(argv[i], i)
if(r != 1):
return r
return r
# Used to display various messages depending upon user entry
def inputError(arg):
if(arg == 0):
print("Please read the README.txt")
elif(arg < 4):
print("Some parameters missing\nSeek help using command ./FileSystem ? or ./FileSystem help")
else:
print("Too many arguments passed\nSeek help using command ./FileSystem ? or ./FileSystem help")
return 0
# Check Parameters
def paramCheck(str, i):
r = 1
size = 0
# Ignore the zeroth parameter
# CASE 1: Validating the first parameter
if(i == 1):
size = len(str)
if(size > 20):
print("First parameter out of boundary")
r = 0
else:
for k in range(size):
if(str[k] == "."):
print("First parameter wrong form")
r = 0
if(r == 1):
global gl_inputfile
gl_inputfile += "file_"+str+".txt"
global gl_inputdir
gl_inputdir += "dir_"+str+".txt"
# CASE 2: Validating the second parameter
elif(i == 2):
val = eval(str)
k = 1
for j in range(31):
if(val == k):
global gl_disksize
gl_disksize = val
return r
k *= 2
print("Second parameter is invalid")
r = 0
# CASE 3: Validating the third parameter
elif(i == 3):
val = eval(str)
k = 1
for j in range(11):
if(val == k):
global gl_blocksize
gl_blocksize = val
return r
k *= 2
print("Third parameter is invalid")
r = 0
return r
# Display Input files
def inputFiles():
print("Input file is "+gl_inputfile)
print("Input directory is "+gl_inputdir)
# Prints the helper document on terminal
def readFile(argc):
f = open(argc, "r")
if(f == None):
print("Cannot open help document")
exit(1)
print(f.read())
# ************************** Tree ***************************
# initDir(<parent Dir>, <dir name>) -> Initializes a directory node #DONE
# initFile(<file name>, <file size>) -> Initializes a file node #DONE
# findDir(<parent Dir>, <dir name>) -> Searches for a directory #DONE
# createTree() -> Runs the next two functions (Modularity :P) #DONE
# initDirDLL() -> Initializes Child dir list of every directory node #DONE
# initFileDLL() -> Initializes Child files list of every directory node #DONE
# printTree(<dir node>, <tab for spaces>) -> Prints tree to visualize #DONE
# Initializes a directory node
def initDir(parent, name):
dir = nodeDir()
dir.parentDir = parent
# Put absolute path given by name parameter into fullname variable
dir.fullname = name
# print(dir.fullname[2] == "\n")
# Put / symbol after the fullname except on root directory
if(parent != None):
dir.fullname += "/"
dir.dirDLList = dllist()
dir.fileDLList = dllist()
# rfind finds the last occuring / and reutrns its index
i = name.rfind("/")
if(i != -1):
# Put everything before the last / in path variable
for j in range(i):
dir.path += name[j]
# Put everything after the last / in name variable
for k in range(i+1, len(name)):
dir.name += name[k]
return dir
# Initializes a file node
def initFile(name, size=0):
global gl_freespace
if (size > gl_freespace):
print("Not enough memory space!\n")
return
newFile = nodeFile()
newFile.fullname = name
# newFile.fullname[len(newFile.filename)] = "\0"
i = name.rfind("/")
if(i != -1):
# Put everything before the last / in path variable
for j in range(i):
newFile.path += name[j]
# Put everything after the last / in filename variable
for k in range(i+1, len(name)):
newFile.filename += name[k]
newFile.fileSize = size
createFileLL(newFile, size)
return newFile
# Finds the directory of path given as 2nd argument
def findDir(dir, name):
if(dir.fullname in name):
if(dir.dirDLList.size == 0):
return dir
else:
i = 0
childList = dir.dirDLList
currentChild = childList[i]
while(currentChild.fullname not in name and i < childList.size):
currentChild = childList[i]
i += 1
if(currentChild.fullname in name):
return findDir(currentChild, name)
else:
return dir
return None
# Create a tree structure
def createTree():
initDirDLL()
initFileDLL()
# Yet to comment
def initDirDLL():
global root, gl_dircount
file = open(gl_inputdir, "r")
file.flush()
if(file == None):
print("Cannot open dir_file")
exit(1)
temp = file.read().splitlines()
for each in temp:
if(root == None):
root = initDir(root, each)
gl_dircount += 1
else:
current = findDir(root, each)
if(current.fullname != each):
new = initDir(current, each)
current.dirDLList.append(new)
gl_dircount += 1
file.flush()
file.close()
def initFileDLL():
global gl_filecount, root
# Input file has entries as follows
# (size) (name)
# 2048 ./1A/2B/meema.txt
f = open(gl_inputfile, "r")
f.flush()
if(f == None):
print("Cannot open dir_file")
exit(1)
# For each line in in inputfile
temp = f.read().splitlines()
for each in temp:
# Ex/ [int fSize, string fName]
arraySplit = each.split()
# initFile function instantiates a nodeFile instance
new = initFile(arraySplit[1], eval(arraySplit[0]))
# Re-calculating total space
onFileAdd(new.fileSize)
if(root == None):
print("Can't find the root folder")
else:
current = root
current = findDir(current, new.fullname)
current.fileDLList.append(new)
gl_filecount += 1
f.flush()
f.close()
# Print Tree recursively
def printTree(node, tab):
reply = ''
# Do nothing if current node is None
if(node == None):
return reply
# Create whitespace for display purpose
for i in range(tab):
reply += (".....")
# Print node name infront of the whitespace
reply += (node.name + '\n')
# Return if the current node has no more child directories
if(node.dirDLList == None):
return reply
dirChild = node.dirDLList
# Recursively go into directory depth
for j in range(node.dirDLList.size):
reply += printTree(dirChild[j], tab+1)
# Return if a directory has no files in it
if(node.fileDLList == None):
return reply
fileChild = node.fileDLList
# Print all files in the directory
for k in range(node.fileDLList.size):
for l in range(tab):
reply += (".....")
reply += (fileChild[k].filename + '\n')
return reply
# ******************** File Linked List *********************
# createFileLL(<file node>, <file size>) -> Returns a file storage info list
# printFileLL(<File info list>, <file size>) -> Prints the file storage info
# freeFileLL(<File info list>) -> Clears file info list and occupied space in disk
# countFragmentation(<File size>) -> Returns the leftover space in block
# onFileAdd(<file size>) -> Recalculates global freespace and global fragmentation
# onFileRemove(<file size>) -> Recalculates global freespace and global fragmentation
# Returns LL specifying all memory locations of file blocks.
def createFileLL(node, fSize):
global gl_blocksize
node.fileInfoList = dllist()
# t_file = nodeFileLL()
# current = nodeFileLL()
blocks = 1
i = 0
if fSize > 0:
# Obtain number of blocks needed to store file info.
blocks = math.floor(fSize/gl_blocksize)
# Add extra block if file needs more room
if fSize != (blocks * gl_blocksize):
blocks += 1
else:
return
for i in range(blocks):
# find offset of every block according to disk.
# new = nodeFileLL()
pma = requestDiskSpace() * gl_blocksize
node.fileInfoList.append(pma)
# for i in range(blocks):
# # find offset of every block according to disk.
# new.pma = requestDiskSpace() * gl_blocksize
# new.next = None
# if i == 0: # First node in LL.
# t_file = new
# current = new
# else: # All subsequent nodes
# current.next = new
# current = current.next
# return t_file
# Visits every node and prints its PMA value
# along with the size every node is taking in memory
def printFileLL(LL, fSize):
reply = ''
i = 1
reply += ("\nFile stored in " + str(LL.size)+" piece(s):\n\n")
for value in LL:
reply += ("\t"+str(i)+". Memory location: " + str(value) + '- ')
i += 1
if fSize > gl_blocksize:
reply += ("Used memory: " + str(gl_blocksize) +
"/"+str(gl_blocksize)+" bytes\n")
# Decrease size every iteration by block size.
fSize -= gl_blocksize
else:
# Print the value of used size within a block.
reply += ("Used memory: " + str(fSize) +
"/"+str(gl_blocksize)+" bytes\n")
return reply
# break
# while(LL != None):
# print("\t"+str(i)+". Memory location: " + str(LL.pma), end=" - ")
# i += 1
# if fSize > gl_blocksize:
# print("Used memory: " + str(gl_blocksize) +
# "/"+str(gl_blocksize)+" bytes")
# # Decrease size every iteration by block size.
# fSize -= gl_blocksize
# else:
# # Print the value of used size within a block.
# print("Used memory: " + str(fSize)+"/"+str(gl_blocksize)+" bytes")
# break
# LL = LL.next
# Frees up used blocks in disk
def freeFileLL(LL):
global gl_blocksize
if(LL == None or len(LL) == 0):
return
for value in LL:
freeOccupiedDiskSpace(value/gl_blocksize)
# while(LL != None):
# freeOccupiedDiskSpace(LL.pma/gl_blocksize)
# LL = LL.next
# Returns the leftover space in last block
def countFragmentation(fSize):
global gl_blocksize
i = fSize
if(i == 0):
return 0
while(i > gl_blocksize):
i -= gl_blocksize
return gl_blocksize - i
# Recalculates global freespace and global fragmentation
def onFileAdd(fSize):
global gl_freespace, gl_fragmentation
gl_freespace -= fSize
gl_fragmentation += countFragmentation(fSize)
# Recalculates global freespace and global fragmentation
def onFileRemove(fSize):
global gl_freespace, gl_fragmentation, gl_blocksize
gl_freespace += fSize
gl_fragmentation -= countFragmentation(fSize)
# ******************** Disk Linked List *********************
# initDisk() -> Initializes Disk using arguments given when running script
# printDisk() -> Prints the status of Disk
# requestDiskSpace() -> Finds free block and returns its block number
# updateDisk() -> Ensures we have at most 2 nodes in Disk Linked List
# freeOccupiedDiskSpace(<starting address>) -> Frees used space
def initDisk():
global gl_freespace, gl_fragmentation, gl_totalblock, gl_disksize, Disk
# Find the number of blocks in disk
gl_totalblock = gl_disksize/gl_blocksize
node = nodeDiskLL()
# Disk is empty aka free
node.status = "FREE"
node.begin = 0
# Disk blocks 0 to max-1 are all free
node.end = gl_totalblock - 1
# Only one node
Disk.append(node)
gl_fragmentation = 0
# All space is free
gl_freespace = gl_totalblock*gl_blocksize
def printDisk():
global Disk
reply = ''
# Traverse through Disk and print block number that are used and free
# Note: There will only be 2 nodes to traverse because remember, we ensure
# that using the updateDisk function
for i in range(len(Disk)):
if(Disk[i].status == "FREE"):
reply += "Free blocks from " + \
str(Disk[i].begin) + " to " + str(Disk[i].end) + '\n'
else:
reply += "Used blocks from " + \
str(Disk[i].end) + " to " + str(Disk[i].begin) + '\n'
reply += "Fragmentation: " + str(gl_fragmentation) + " bytes" + '\n'
reply += "Free Disk Space: " + str(gl_freespace) + " bytes" + '\n'
return reply
# Returns the block number of the first vacant block found.
def requestDiskSpace():
global Disk
temp = nodeDiskLL()
current = Disk
i = 0
# Iterate till an empty space is found
for i in range(current.size):
# FREE space found
# If more than one contigious block is free
# Isolate the first free block and return its block number
if(current[i].status == "FREE"):
if(current[i].begin != current[i].end):
temp.status = "USED"
temp.begin = current[i].begin
temp.end = current[i].begin
current[i].begin += 1
current.appendleft(temp)
else: # Only single block is free, use it.
current[i].status = "USED"
# 2 nodes at all times
updateDisk()
return temp.begin
# Updates disk such that we have only 2 nodes(with status USED and FREE)
def updateDisk():
global Disk
# Run Disksize - 1 to prevent index out of bounds
for i in range(Disk.size - 1):
nextChildBlock = Disk[i+1]
childBlock = Disk[i]
# If both statuses same, merge the blocks together
if(childBlock.status == nextChildBlock.status):
childBlock.end = nextChildBlock.end
Disk.remove(Disk.nodeat(i+1))
updateDisk()
# Important to break, or else it will
# go out of bound on its way back from recursion
break
# Frees up the used blocks depending on starting address
def freeOccupiedDiskSpace(start):
global Disk
current = Disk
# Create a free block node
new = nodeDiskLL()
new.status = "FREE"
new.begin = start
new.end = start
for i in range(Disk.size):
# If current node is before or after start address
if(current[i].begin <= start and current[i].end >= start):
# Begin address matches start address
if(current[i].begin == start):
current.insert(new, current[i])
current[i].begin += 1
updateDisk()
break
# End address matches start address
elif(current[i].end == start):
current[i].end -= 1
current.insert(new, current[i+1])
updateDisk()
break
else: # start address is somewhere in between begin and end
temp = nodeDiskLL()
temp.status = "USED"
temp.begin = current[i].begin
temp.end = start - 1
current.insert(temp, current[i])
current.insert(new, current[i])
current.begin = start + 1
updateDisk()
break
def main(argc, argv):
# If terminal command not found, return Error.
if (os.getcwd() == None):
print("Virtual Memory Exhausted!")
exit(1)
n = 0
# Check input arguments to ensure it conforms.
x = inputCheck(argc, argv)
if (x == 0):
exit(1)
# Create server socket at 127.0.0.1 and port 95.
ServerSideSocket = socket(AF_INET, SOCK_STREAM)
ServerSideSocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
host = '127.0.0.1'
# host = '192.356.31.3'
port = 21018
# This is the ID to uniquely identify a client.
ThreadCount = 0
# Instantiate Lock
mutex = Lock()
# An array to store input thread
threadsArr = []
# An array to simply keep track of active threads
runningThreads = []
# A DYNAMIC* array holding all data of connected users.
# Dynamic: Everytime a user connects or disconnects, array auto resizes.
users = []
# Instantiate the file system on server.
inputFiles()
initDisk()
createTree()
#
print("\n-------- FileSystem status --------")
print(printDisk())
print("-----------------------------------\n")
# Bind server and start listening to incoming connections.
try:
ServerSideSocket.bind((host, port))
except socket.error as e:
print('Here ' + str(e))
print('Socket is listening..\n')
ServerSideSocket.listen(5)
# This function is not used because we don't want to wait
# for every other thread to complete before exiting from one thread.
# It is still mentioned so it can directly be used in future (if needed)
def joinThreads():
# Join threads
for i in range(len(threadsArr)):
print(i)
threadsArr[i].join()
# This function is responsible for accessing the filesystem
# Mutex is acquired before every "core" function
# in order to maintain mutual exclusion for thread synchronization.
# The current directory and filesystem response is returned
def run(str, current):
vect = str.split()
# if (vect[0] == "exit"):
# exit()
if ((vect[0] == "dir") or (vect[0] == "ls")):
mutex.acquire()
reply = dirORls(current)
mutex.release()
return current, reply
elif (vect[0] == "printdisk"):
mutex.acquire()
reply = printDisk()
mutex.release()
return current, reply
elif (vect[0] == "cd"):
reply = ''
if (vect[1] == ".."):
mutex.acquire()
current = current.parentDir
mutex.release()
return current, reply
else:
mutex.acquire()
temp = chdir(current, vect[1])
if (temp != None):
current = temp
else:
reply += (vect[1]+": No such file or directory\n")
mutex.release()
return current, reply
elif (vect[0] == "printfiles"):
reply = ''
mutex.acquire()
reply = printout(current)
mutex.release()
return current, reply
elif (vect[0] == "printtree"):
reply = ''
mutex.acquire()
reply += printTree(current, 0)
mutex.release()
return current, reply
elif (vect[0] == "mkdir"):
reply = ''
mutex.acquire()
if (len(vect) != 2):
reply += ("Wrong input, use 'mkdir <directory name>'\n")
mkdir(current, vect[1])
mutex.release()
return current, reply
elif (vect[0] == "rmdir"):
reply = ''
mutex.acquire()
if (len(vect) != 2):
reply += ("Wrong input, use 'rmdir <directory name>'\n")
reply += rmdir(current, vect[1])
mutex.release()
return current, reply
elif (vect[0] == "create"):
reply = ''
mutex.acquire()
if (len(vect) != 3):
reply += ("Wrong input, use 'create <file name> <file size>'\n")
reply += mkfile(current, vect[1], eval(vect[2]))
mutex.release()
return current, reply
elif (vect[0] == "removefile"):
reply = ''
mutex.acquire()
if (len(vect) != 2):
reply += ("Wrong input, use 'removefile <file name>'\n")
reply += rmfile(current, vect[1])
mutex.release()
return current, reply
elif (vect[0] == "removebytes"):
reply = ''
mutex.acquire()
if (len(vect) != 3):