tree.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import json
  2. class directories:
  3. from file import files
  4. myfile = files("mainer")
  5. child = []
  6. namer = ""
  7. def __init__(self, name):
  8. self.namer = name
  9. def getName(self):
  10. return self.namer
  11. def addFile(self, name, txt):
  12. #calling offsetcalcFile to get the offset returned by the function
  13. a = self.offsetcalcFile()
  14. # converting the offset to integer
  15. b = int(a)
  16. # equating c to the page num
  17. c = self.pageallocF()
  18. d = int(c)
  19. e = b + 1
  20. f = str(e)
  21. g = self.offsetMM()
  22. h = int(g)
  23. i = h * 32
  24. j = i + 16
  25. self.myfile.writeToFile(f, 4, 0)
  26. self.dictToText(name, d, b)
  27. self.myfile.writeToFile(txt, d, 0)
  28. self.myfile.writeToFile("main ~ ", 2, i)
  29. self.myfile.writeToFile(name, 2, j)
  30. # a method to convert the file name or directory name entered by the user into a key-value pair and write into the file
  31. def dictToText(self, fname, pg, off):
  32. # takes filename and appends it with ":" and page number on which the file's data is stored
  33. a = fname + ":" + str(pg)
  34. # each entry is given a size of 16 bytes in the text file so that the files can be accessed easily
  35. b = off * 16
  36. # writing the data to text file
  37. self.myfile.writeToFile(a, 0, b)
  38. # returns the name of files
  39. def pagenumberFile(self):
  40. self.getFiles()
  41. # calculates offset, by which we mean that where the next the file data will be written on the text file
  42. def offsetcalcFile(self):
  43. #opens funner.dat for read and write mode, Note: if you are using this method for the first time then use "w+" to first create the file
  44. file = open("F:/labOS/funner.dat", "r+")
  45. # each page size equated to 16 bytes
  46. page_size = 16
  47. # total .dat file contains 64 pages each of 256 bytes, in order to move from one page to another we equate it to 256
  48. page_num = 256
  49. #moving the file pointer to read the contents
  50. file.seek(page_num * page_size)
  51. #reads and outputs the contents
  52. a = file.read(page_size)
  53. print(a)
  54. return a
  55. def offsetAdd(self):
  56. #takes in offset i.e the page number of file and add '1' to it each time for the next file to be entered
  57. a = self.offsetcalcFile()
  58. b = int(a)
  59. b += 1
  60. return b
  61. #deletes the file
  62. def delFile(self, filename):
  63. file = open("F:/labOS/funner.dat", "r+")
  64. #takes the length of file
  65. u = len(filename)
  66. #equating k to files already stored
  67. k = self.numberofFiles()
  68. pgnum = 0
  69. for i in range(k):
  70. #moving the pointer each time with 16 bytes
  71. file.seek(i * 16)
  72. #reads the file
  73. a = file.read(u)
  74. if a == filename:
  75. #if file founds it then overwrites the file data entered in the table with blank spaces, meaning that the file is removed
  76. print("file found")
  77. self.myfile.writeToFile(" ", 0, file.seek(i * 16))
  78. #also deletes the file from Memory Map
  79. self.delFfromMM(filename)
  80. # Creates a new directory
  81. def addChildDir(self, dirname):
  82. # returns the number of directories already made
  83. a = self.numberofDirs()
  84. b = int(a)
  85. # multiplied by 16 as of number of Bytes in each page
  86. c = b * 16
  87. # writes directory name to the funner.dat
  88. self.myfile.writeToFile(dirname, 1, c)
  89. # returns the number of directories in the file
  90. def numberofDirs(self):
  91. file = open("F:/labOS/funner.dat", "r+")
  92. page_size = 16
  93. counter = 0
  94. # total number of pages = 64
  95. page_num = 64
  96. # accesses the pages from idx 64 to 128 i.e page number 2 in the text file
  97. while page_num in range(128):
  98. # moves the file pointer
  99. file.seek(page_num * page_size)
  100. # reads the file
  101. a = file.read(page_size)
  102. # checks whether the entry in the page is empty, if empty then breaks the loop
  103. # Note there are 16 number of empty spaces, as each entry takes 16 specific bytes
  104. if a == " ":
  105. break
  106. # if there is a specific entry then increment both counter and page num
  107. else:
  108. counter += 1
  109. page_num += 1
  110. print(counter)
  111. return counter
  112. # deletes the directory given
  113. def delChildDir(self, dirname):
  114. file = open("F:/labOS/funner.dat", "r+")
  115. u = len(dirname)
  116. # returns the number of directories
  117. k = self.numberofDirs()
  118. offset = 0
  119. i = 64
  120. j = i + k
  121. while i in range(j):
  122. file.seek(i * 16)
  123. a = file.read(u)
  124. # if directory found then delete the file by overwriting with blank spaces
  125. if a == dirname:
  126. self.myfile.writeToFile(" ", 1, offset*16)
  127. i += 1
  128. offset += 1
  129. # also deletes the directory from the memory map
  130. self.deldirfromMM(dirname)
  131. # returns the file names
  132. def getFiles(self):
  133. file = open("F:/labOS/funner.dat", "r+")
  134. page_size = 16
  135. page_num = 0
  136. # returns the number of files
  137. k = self.numberofFiles()
  138. for page_num in range(k):
  139. # moves the file pointer towards the file name and prints its name
  140. file.seek(page_num * page_size)
  141. a = file.read(page_size)
  142. print(a)
  143. # returns the number of files created
  144. def numberofFiles(self):
  145. file = open("F:/labOS/funner.dat", "r+")
  146. page_size = 16
  147. counter = 0
  148. for page_num in range(64):
  149. file.seek(page_num * page_size)
  150. a = file.read(page_size)
  151. if a == " ":
  152. break
  153. else:
  154. counter += 1
  155. return counter
  156. # method to allocate file number to each file
  157. # each file number tells on which respective page the file data has been entered
  158. def pageallocF(self):
  159. a = self.numberofFiles()
  160. b = int(a)
  161. # incremented by 5 as bcz first 4 pages are used up by file names, directory names and directory structure
  162. c = b + 5
  163. print(c)
  164. return c
  165. # returns the directories names
  166. def getChildren(self):
  167. file = open("F:/labOS/funner.dat", "r+")
  168. page_size = 16
  169. counter = 0
  170. page_num = 64
  171. while page_num in range(128):
  172. file.seek(page_num * page_size)
  173. a = file.read(page_size)
  174. if a == " ":
  175. break
  176. else:
  177. print(a)
  178. page_num += 1
  179. # returns the memory map
  180. def memoryMap(self):
  181. file = open("F:/labOS/funner.dat", "r+")
  182. page_size = 32
  183. counter = 0
  184. page_num = 64
  185. while page_num in range(256):
  186. file.seek(page_num * page_size)
  187. a = file.read(page_size)
  188. if a == " ":
  189. break
  190. else:
  191. print(a)
  192. counter += 1
  193. page_num += 1
  194. return counter
  195. # method to calculate the offset for memory map
  196. def offsetMM(self):
  197. file = open("F:/labOS/funner.dat", "r+")
  198. page_size = 32
  199. counter = 0
  200. page_num = 64
  201. while page_num in range(256):
  202. file.seek(page_num * page_size)
  203. a = file.read(page_size)
  204. if a == " ":
  205. break
  206. else:
  207. counter += 1
  208. page_num += 1
  209. return counter
  210. # deletes the file name from memory map
  211. def delFfromMM(self, filename):
  212. file = open("F:/labOS/funner.dat", "r+")
  213. page_size = 32
  214. counter = 0
  215. page_num = 64
  216. u = len(filename)
  217. k = self.offsetMM()
  218. m = int(k)
  219. j = m * 16
  220. while page_num in range(256):
  221. file.seek(page_num * page_size)
  222. a = file.read(page_size)
  223. # slices b as first 16 bytes occupied by the memory map contains directory name
  224. # basically the following actually removes the name from the memory map i.e removes the pointer from directory to filename
  225. # it does not erases content of the file
  226. b = a[16:16+u]
  227. if b == filename:
  228. self.myfile.writeToFile(" ", 2, counter+16)
  229. page_num += 1
  230. counter += 32
  231. # moves the file from one directory to another directory
  232. def move(self, srcFilename, destDirectory):
  233. file = open("F:/labOS/funner.dat", "r+")
  234. page_size = 32
  235. counter = 0
  236. count = 0
  237. page_num = 64
  238. u = len(srcFilename)
  239. k = self.offsetMM()
  240. m = int(k)
  241. j = m * 16
  242. while page_num in range(256):
  243. file.seek(page_num * page_size)
  244. a = file.read(page_size)
  245. b = a[16:16 + u]
  246. c = len(destDirectory)
  247. d = a[0:c]
  248. x = 0
  249. y = 0
  250. f = len(destDirectory)
  251. e = 14 - f
  252. g = len(srcFilename)
  253. h = 16 - g
  254. i = 16 + g
  255. if b == srcFilename:
  256. self.myfile.writeToFile(" ", 2, counter)
  257. self.myfile.writeToFile(destDirectory, 2, counter)
  258. while e > x:
  259. f += 1
  260. self.myfile.writeToFile(" ", 2, f)
  261. x += 1
  262. self.myfile.writeToFile("~ ", 2, 14)
  263. self.myfile.writeToFile(srcFilename, 2, counter + 16)
  264. while h > y:
  265. i += 1
  266. self.myfile.writeToFile(" ", 2, i)
  267. y += 1
  268. page_num += 1
  269. counter += 32
  270. # deletes the directory name from memory map
  271. def deldirfromMM(self, dirname):
  272. file = open("F:/labOS/funner.dat", "r+")
  273. page_size = 32
  274. counter = 0
  275. page_num = 64
  276. u = len(dirname)
  277. k = self.offsetMM()
  278. m = int(k)
  279. j = m * 16 - 16
  280. while page_num in range(256):
  281. file.seek(page_num * page_size)
  282. a = file.read(page_size)
  283. c = len(dirname)
  284. d = a[0:c]
  285. if d == dirname:
  286. self.myfile.writeToFile(" ", 2, j)
  287. page_num += 1
  288. # checks the page number associated with the file mentioned
  289. def checkFileForPage(self, filename):
  290. file = open("F:/labOS/funner.dat", "r+")
  291. offset = 16
  292. u = len(filename)
  293. k = ""
  294. m = ""
  295. j = 0
  296. for page_num in range(64):
  297. file.seek(page_num * offset)
  298. a = file.read(offset)
  299. b = a[0:u]
  300. if b == filename:
  301. for i in a:
  302. if i == ":":
  303. k = a[j:]
  304. j += 1
  305. m = k[1:]
  306. return m
  307. # reads the contents of the file
  308. def read(self, filename):
  309. file = open("F:/labOS/funner.dat", "r+")
  310. a = self.checkFileForPage(filename)
  311. b = int(a)
  312. offset = 16
  313. page = b * 64
  314. limit = page + 64
  315. c = ""
  316. while page in range(limit):
  317. file.seek(page * offset)
  318. a = file.read(offset)
  319. c += a
  320. page += 1
  321. print(c)
  322. # truncates / trims the contents of the file upto maxsize limit
  323. def truncate(self, filename, maxsize):
  324. file = open("F:/labOS/funner.dat", "r+")
  325. a = self.checkFileForPage(filename)
  326. b = int(a)
  327. offset = 0
  328. page = b * 64
  329. limit = page + maxsize
  330. t = 64 - maxsize
  331. trun = page + 64
  332. while page in range(limit):
  333. file.seek(1)
  334. page += 1
  335. offset += 1
  336. while limit in range(trun):
  337. file.seek(limit * 16)
  338. self.myfile.writeToFile(" ", b, offset)
  339. limit += 1
  340. offset += 16
  341. # writes to file
  342. def writeFile(self, filename, position, text):
  343. file = open("F:/labOS/funner.dat", "r+")
  344. a = self.checkFileForPage(filename)
  345. b = int(a)
  346. offset = 0
  347. page = b * 64
  348. limit = page + position
  349. while page in range(limit):
  350. file.seek(1)
  351. page += 1
  352. offset += 1
  353. file.seek(limit)
  354. self.myfile.writeToFile(text, b, position)
  355. # check whether the specific directory is present or not
  356. def chDir(self, dirname):
  357. file = open("F:/labOS/funner.dat", "r+")
  358. page_size = 16
  359. count = len(dirname)
  360. page_num = 64
  361. c = ""
  362. while page_num in range(128):
  363. file.seek(page_num * page_size)
  364. a = file.read(page_size)
  365. b = a[0:count]
  366. if a == " ":
  367. break
  368. else:
  369. if b == dirname:
  370. c += "Directory Available. "
  371. page_num += 1
  372. if not c:
  373. c += "Directory not Available. "
  374. return c
  375. # moves the text within the file from a specific range to a specific place
  376. def movewithin(self, filename, start, size, target):
  377. file = open("F:/labOS/funner.dat", "r+")
  378. a = self.checkFileForPage(filename)
  379. b = int(a)
  380. offset = 0
  381. c = ""
  382. page = b * 64 + start
  383. limit = page + size
  384. end = page + 64
  385. while page in range(limit):
  386. file.seek(page)
  387. c += file.read(1)
  388. self.myfile.writeToFile(" ", b, start)
  389. file.seek(1)
  390. page += 1
  391. offset += 1
  392. print(c)
  393. root = directories("hsk")