def change_rem_inode(self, inode_no): file = open(self.filepath, "r+b") self.LIL[0] = inode_no self.clear_inode(inode_no) write_str_bin(file=file, string=int_to_string(inode_no, 4)) file.close() return True
def write(self): file = open( "/home/zikin/Documents/Final_project_OS/hard_drive/" + str(self.get_block()) + ".block", "r+b") offset = self.get_offset() file.seek(offset) file.write(bytes([ord(self.owner)])) file.write(bytes([ord(self.group)])) file.write(bytes([ord(self.filetype)])) write_str_bin(file, self.access_permissions) write_str_bin(file, self.file_access_time) write_str_bin(file, self.links) write_str_bin(file, self.size) write_str_bin(file, self.table_of_contents) file.close() return True
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 pop(self): if self.LBL != None: file = open(self.filepath, "r+b") num = self.LBL.pop() ##checar file.seek(len(self.LBL) * 4, 0) write_str_bin(file=file, string=int_to_string(0, 4)) self.update_info() file.close() return num else: file = open(self.filepath, "r+b") num = self.LIL.pop() file.seek(len(self.LIL) * 4, 0) write_str_bin(file=file, string=int_to_string(0, 4)) self.update_info() file.close() return num
def append(self, num): if self.LBL != None: file = open(self.filepath, "r+b") file.seek(len(self.LBL) * 4, 0) write_str_bin(file=file, string=int_to_string(num, 4)) self.LBL.append(num) self.update_info() file.close() return True else: file = open(self.filepath, "r+b") file.seek(len(self.LIL) * 4, 0) self.clear_inode(num) write_str_bin(file=file, string=int_to_string(num, 4)) self.LIL.append(num) self.update_info() return True
def rm_from_dir(dir_name, cwd): byte_no = find_file(cwd, dir_name) files = find_files(cwd) if files.get(dir_name,False): inode_no = files[dir_name] else: return False block = find_block(byte_no // 1024, cwd.table_of_contents) file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(block) + ".block", "r+b") file.seek(byte_no%1024) bytes = (byte_no % 1024 + len(dir_name) + 1) if bytes < 1024: write_str_bin(file, chr(0)*(len(dir_name)+4)) file.close() else: write_str_bin(file, chr(0) * (1024- (byte_no%1024))) file.close() block = find_block(byte_no // 1024 +1, cwd.table_of_contents) file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(block) + ".block", "r+b") write_str_bin(file, chr(0) * (bytes -1023)) file.close() return int(inode_no)
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 add_file_to_dir(i_dir, i_file,name): i_dir = Inode(i_dir) i_dir.read() i_file = Inode(i_file) i_file.read() current_size = i_dir.size future_size = current_size +4 + 1 + len(name) block = future_size//1024 if (future_size //1024 == current_size //1024): offset = current_size % 1024 writing_block = find_block(block, i_dir.table_of_contents) file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(writing_block) + ".block", "r+b") file.seek(offset) write_str_bin(file, int_to_string(i_file.id, 4) + name + "\n") file.close() else: offset = current_size % 1024 writing_block = find_block(block, i_dir.table_of_contents) file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(writing_block) + ".block", "r+b") file.seek(offset) string1 = int_to_string(i_file.id, 4) + name + "\n"[0:1024 - offset] string2 = int_to_string(i_file.id, 4) + name + "\n"[1024 - offset:] write_str_bin(file, string1) file.close() LBL = B.super_block(2) LBL.load() if block < 8: i_dir.table_of_contents[block] = LBL.free_block() last_block = i_dir.table_of_contents[block] if block < 264: offset_1 = (block - 8) % 256 if offset_1== 0: i_dir.table_of_contents[8] = LBL.free_block() file_1 = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(i_dir.table_of_contents[8]) + ".block", "r+b") file_1.seek(offset_1 *4) new_block = LBL.free_block() write_str_bin(file_1,int_to_string(new_block,4)) last_block = new_block file_1.close() if block < 65800: offset_1 = (block - 264) % 65536 offset_R1 = (block - 264) // 256 offset_2 = (block - 264) % 256 if offset_1== 0: i_dir.table_of_contents[9] = LBL.free_block() file_1 = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(i_dir.table_of_contents[9]) + ".block", "r+b") file_1.seek(offset_R1 *4) if offset_2 == 0: new_block = LBL.free_block() write_str_bin(file_1, int_to_string(new_block, 4)) else: new_block = string_to_int(bytes_to_string(file_1.read(4))) file_1.close() file_2 = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(new_block) + ".block", "r+b") file_2.seek(offset_2 *4) new_block = LBL.free_block() write_str_bin(file_2, int_to_string(new_block, 4)) last_block = new_block file_2.close() else: offset_1 = (block - 65800) // 65536 offset_2 = (block - 65800) // 256 offset_3 = (block - 65800) % 256 if block == 65800: i_dir.table_of_contents[10] = LBL.free_block() file_1 = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(i_dir.table_of_contents[10]) + ".block", "r+b") file_1.seek(offset_1 *4) if offset_2 == 0: new_block = LBL.free_block() write_str_bin(file_1, int_to_string(new_block, 4)) else: new_block = string_to_int(bytes_to_string(file_1.read(4))) file_1.close() file_2 = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(new_block) + ".block", "r+b") file_2.seek(offset_2 *4) if offset_3 == 0: new_block = LBL.free_block() write_str_bin(file_2, int_to_string(new_block, 4)) else: new_block = string_to_int(bytes_to_string(file_2.read(4))) file_2.close() file_3 = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(new_block) + ".block", "r+b") file_3.seek(offset_3 *4) new_block = LBL.free_block() write_str_bin(file_3, int_to_string(new_block, 4)) last_block = new_block file_3.close() file = open("/home/zikin/Documents/Final_project_OS/hard_drive/" + str(last_block) + ".block", "r+b") write_str_bin(file, string2) file.close() i_dir.size = future_size i_dir.binarize_all() i_dir.write() return True
def write_info(self): #Writes the whole Block to disk file = open(self.filepath, "r+b") write_str_bin(file=file, string=self.info) return True