def mkdir(name, cwd): LIL = B.super_block(1) LIL.load() LBL = B.super_block(2) LBL.load() dir = Inode(id = LIL.free_inode(), filetype='d') dir.read() dir.filetype = 'd' dir.size = 13 dir.table_of_contents[0] = LBL.free_block() file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(dir.table_of_contents[0]) + ".block", "r+b") write_str_bin(file,int_to_string(dir.id,4)+'.'+'\n') write_str_bin(file,int_to_string(cwd.id,4)+'..'+'\n') dir.binarize_all() dir.write() add_file_to_dir(cwd.id,dir.id,name)
def mk_file(user, file_content, ref, parent_inode): LIL = B.super_block(1) LBL = B.super_block(2) LIL.load() LBL.load() no_inodo = LIL.free_inode() inodo = Inode(id=no_inodo) inodo.read() inodo.filetype = 'x' inodo.owner = user inodo.size = len(file_content) no_blocks = math.ceil(len(file_content) / 1024) block = 0 while block < no_blocks: if block < 8: inodo.table_of_contents[block] = LBL.free_block() file = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(inodo.table_of_contents[block]) + ".block", "r+b") if (block + 1) * 1024 < len(file_content): write_str_bin(file, file_content[block * 1024:(block + 1) * 1024]) else: write_str_bin(file, file_content[block * 1024:]) file.close() block += 1 elif block < 264: inodo.table_of_contents[8] = LBL.free_block() offset = 0 file_1 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(inodo.table_of_contents[8]) + ".block", "r+b") while offset < 1024 and block < no_blocks: offset += 4 no_block = LBL.free_block() write_str_bin(file_1, int_to_string(no_block, 4)) file_2 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(no_block) + ".block", "r+b") if (block + 1) * 1024 < len(file_content): write_str_bin( file_2, file_content[block * 1024:(block + 1) * 1024]) else: write_str_bin(file_2, file_content[block * 1024:]) block += 1 file_2.close() file_1.close() elif block < 65800: inodo.table_of_contents[9] = LBL.free_block() offset_1 = 0 file_1 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(inodo.table_of_contents[9]) + ".block", "r+b") while offset_1 < 1024 and block < no_blocks: offset_1 += 4 no_block = LBL.free_block() write_str_bin(file_1, int_to_string(no_block, 4)) file_2 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(no_block) + ".block", "r+b") offset_2 = 0 while offset_2 < 1024 and block < no_blocks: offset_2 += 4 no_block = LBL.free_block() write_str_bin(file_2, int_to_string(no_block, 4)) file_3 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(no_block) + ".block", "r+b") if (block + 1) * 1024 < len(file_content): write_str_bin( file_3, file_content[block * 1024:(block + 1) * 1024]) else: write_str_bin(file_3, file_content[block * 1024:]) block += 1 file_3.close() file_2.close() file_1.close() else: inodo.table_of_contents[10] = LBL.free_block() offset_1 = 0 file_1 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(inodo.table_of_contents[10]) + ".block", "r+b") while offset_1 < 1024 and block < no_blocks: offset_1 += 4 no_block = LBL.free_block() write_str_bin(file_1, int_to_string(no_block, 4)) file_2 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(no_block) + ".block", "r+b") offset_2 = 0 while offset_2 < 1024 and block < no_blocks: offset_2 += 4 no_block = LBL.free_block() write_str_bin(file_2, int_to_string(no_block, 4)) file_3 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(no_block) + ".block", "r+b") offset_3 = 0 while offset_3 < 1024 and block < no_blocks: offset_3 += 4 no_block = LBL.free_block() write_str_bin(file_3, int_to_string(no_block, 4)) file_4 = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(no_block) + ".block", "r+b") if (block + 1) * 1024 < len(file_content): write_str_bin( file_4, file_content[block * 1024:(block + 1) * 1024]) else: write_str_bin(file_4, file_content[block * 1024:]) block += 1 file_4.close() file_3.close() file_2.close() file_1.close() inodo.binarize_all() inodo.write() add_file_to_dir(i_dir=parent_inode, i_file=inodo.id, name=ref) return True
def clear_inode(self, inode_no): inode = Inode(id=inode_no) inode.read() inode.filetype = "0" inode.delete() return True