def get_files(self): files = {} for i in range(1, NUM_BLOCKS, 1): block = disktools.read_block(i) if (block[NAME_START:NAME_FINISH].decode().rstrip('\x00') != '') & (disktools.bytes_to_int(block[0:2]) == 0): files[block[NAME_START:NAME_FINISH].decode().rstrip( '\x00')] = dict(st_mode=disktools.bytes_to_int( block[MODE_START:MODE_FINISH]), st_nlink=disktools.bytes_to_int( block[NLINK_START:NLINK_FINISH]), st_size=disktools.bytes_to_int( block[SIZE_START:SIZE_FINISH]), st_ctime=disktools.bytes_to_int( block[CTIME_START:CTIME_FINISH]), st_mtime=disktools.bytes_to_int( block[MTIME_START:MTIME_FINISH]), st_atime=disktools.bytes_to_int( block[ATIME_START:ATIME_FINISH]), st_gid=disktools.bytes_to_int( block[GID_START:GID_FINISH]), st_uid=disktools.bytes_to_int( block[UID_START:UID_FINISH])) return files
def set_with_bytes(self, metadata_bytes: bytearray): self.name = bytes_to_str(metadata_bytes[0:16]) self.size = bytes_to_int(metadata_bytes[16:18]) self.nlinks = bytes_to_int(metadata_bytes[18:19]) self.mode = bytes_to_int(metadata_bytes[19:21]) self.uid = bytes_to_int(metadata_bytes[21:23]) self.gid = bytes_to_int(metadata_bytes[23:25]) self.ctime = bytes_to_int(metadata_bytes[25:29]) self.mtime = bytes_to_int(metadata_bytes[29:33]) self.atime = bytes_to_int(metadata_bytes[33:37]) self.location = bytes_to_int(metadata_bytes[37:38]) self.type = bytes_to_int(metadata_bytes[38:39]) return self
def build_metadata(metadata_bytes: bytearray) -> Metadata: return Metadata( NAME=bytes_to_str(metadata_bytes[0:16]), SIZE=bytes_to_int(metadata_bytes[16:18]), NLINKS=bytes_to_int(metadata_bytes[18:19]), MODE=bytes_to_int(metadata_bytes[19:21]), UID=bytes_to_int(metadata_bytes[21:23]), GID=bytes_to_int(metadata_bytes[23:25]), CTIME=bytes_to_int(metadata_bytes[25:29]), MTIME=bytes_to_int(metadata_bytes[29:33]), ATIME=bytes_to_int(metadata_bytes[33:37]), LOCATION=bytes_to_int(metadata_bytes[37:38]), TYPE=bytes_to_int(metadata_bytes[38:39]), )
def clear_data_block(self, path): for i in range(1, NUM_BLOCKS, 1): data_block = [] num_array = [] clean_block = bytearray([0] * BLOCK_SIZE) block = disktools.read_block(i) if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path: block_number = disktools.bytes_to_int( block[LOCATION_START:LOCATION_FINISH]) block_number_bin = bin(block_number) for b in block_number_bin[2:]: num_array.append(int(b)) num_array = list(reversed(num_array)) for idx, val in enumerate(num_array): if val == 1: data_block.append(idx) for data in data_block: disktools.write_block(data, clean_block) self.update_bit_map(self, data_block)
def check_file_data(self, path): for i in range(1, NUM_BLOCKS, 1): block = disktools.read_block(i) if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path: if disktools.bytes_to_int( block[LOCATION_START:LOCATION_FINISH]) != 0: return True else: return False
def update_bit_map(self, used_block): block = disktools.read_block(0) # get the int number of bitmap bitmap = disktools.bytes_to_int(block[BITMAP_START:BITMAP_FINISH]) for i in used_block: bitmap = bits.toggleBit(bitmap, i) block[BITMAP_START:BITMAP_FINISH] = disktools.int_to_bytes(bitmap, 2) disktools.write_block(0, block) return bitmap
def update_nlink(self, path, update): for i in range(0, NUM_BLOCKS, 1): block = disktools.read_block(i) if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path: new_nlink = disktools.bytes_to_int( block[NLINK_START:NLINK_FINISH]) + update block[NLINK_START:NLINK_FINISH] = disktools.int_to_bytes( new_nlink, 1) disktools.write_block(i, block) return 0
def get_data(self): total_data = defaultdict(bytes) for i in range(1, NUM_BLOCKS, 1): file_data = [] data_block = [] num_array = [] block = disktools.read_block(i) path = block[NAME_START:NAME_FINISH].decode().rstrip('\x00') # if the location bytes are not empty & the first two bytes are empty means the block saves metadata of a file. if (disktools.bytes_to_int(block[LOCATION_START:LOCATION_FINISH]) != 0) & (disktools.bytes_to_int(block[0:2]) == 0): # get the bitmap of the location of the file data. block_number = disktools.bytes_to_int( block[LOCATION_START:LOCATION_FINISH]) block_number_bin = bin(block_number) for b in block_number_bin[2:]: num_array.append(int(b)) num_array = list(reversed(num_array)) for idx, val in enumerate(num_array): if val == 1: data_block.append(idx) #read the data in each block for data in data_block: file_content = disktools.read_block(data).decode().rstrip( '\x00') file_data.append(file_content) # combine all the data if needed. file_data = ''.join(map(str, file_data)) file_data = file_data.encode() # form the same structure as the data variable in memory total_data[path] = (file_data) return total_data
def __init__(self): block = disktools.read_block(0) self.files = Format.get_files(Format) self.data = Format.get_data(Format) self.fd = 0 self.files['/'] = dict( st_mode=disktools.bytes_to_int(block[MODE_START:MODE_FINISH]), st_ctime=disktools.bytes_to_int(block[CTIME_START:CTIME_FINISH]), st_mtime=disktools.bytes_to_int(block[MTIME_START:MTIME_FINISH]), st_atime=disktools.bytes_to_int(block[ATIME_START:ATIME_FINISH]), st_nlink=disktools.bytes_to_int(block[NLINK_START:NLINK_FINISH])) uid = disktools.bytes_to_int(block[UID_START:UID_FINISH]) gid = disktools.bytes_to_int(block[GID_START:GID_FINISH]) self.chown('/', uid, gid)
def get_free_block(self, num_of_blocks): block = disktools.read_block(0) bit_array = [] free_block = [] # get int value of bitmap bitmap_int = disktools.bytes_to_int(block[BITMAP_START:BITMAP_FINISH]) # transfer the bitmap to binary bitmap_bin = bin(bitmap_int) # add each bit into a int array for b in bitmap_bin[2:]: bit_array.append(int(b)) # reverse the order of the array bit_array = reversed(bit_array) # return the index of each bit which is 1 for idx, val in enumerate(bit_array): if val == 1: free_block.append(idx) return free_block[0:num_of_blocks]
def initial_free_block_bitmap(self): # initialise the bitmap int value to 0 bitmap = 0 # counter for count the blocks count = 0 for i in range(NUM_BLOCKS): # read each block in the disk block = disktools.read_block(i) # set the value to 0 for the non-free block in bit map if disktools.bytes_to_int(block) != 0: bitmap = bits.clearBit(bitmap, count) count += 1 else: bitmap = bits.setBit(bitmap, count) count += 1 # set the first block to 1 which means it is not free(will store bitmap into block 0). bitmap = bits.clearBit(bitmap, 0) # write the bitmap value at the first two bytes at block 0 bitmap_disk = disktools.read_block(0) bitmap_disk[BITMAP_START:BITMAP_FINISH] = disktools.int_to_bytes( bitmap, 2) disktools.write_block(0, bitmap_disk)