示例#1
0
    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)
示例#2
0
    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
示例#3
0
 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
示例#4
0
 def clear_metadata_block(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:
             num_array = []
             clean_block = bytearray([0] * BLOCK_SIZE)
             disktools.write_block(i, clean_block)
             num_array.append(i)
             self.update_bit_map(self, num_array)
示例#5
0
    def update_mode(self, path, mode):

        for i in range(1, NUM_BLOCKS, 1):
            block = disktools.read_block(i)
            if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path:
                block[MODE_START:MODE_FINISH] = disktools.int_to_bytes(mode, 2)
                disktools.write_block(i, block)

        return 0
示例#6
0
    def update_name(self, path, new_path):

        for i in range(1, NUM_BLOCKS, 1):
            block = disktools.read_block(i)
            if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path:
                block[NAME_START:NAME_FINISH] = new_path.encode()
                disktools.write_block(i, block)

        return 0
示例#7
0
    def update_file_location(self, path, bitmap):
        for i in range(1, NUM_BLOCKS, 1):
            block = disktools.read_block(i)
            if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path:
                block[LOCATION_START:LOCATION_FINISH] = disktools.int_to_bytes(
                    bitmap, 2)
                disktools.write_block(i, block)

        return 0
示例#8
0
    def update_owner(self, path, uid, gid):

        for i in range(1, NUM_BLOCKS, 1):
            block = disktools.read_block(i)
            if block[NAME_START:NAME_FINISH].decode().rstrip('\x00') == path:
                block[UID_START:UID_FINISH] = disktools.int_to_bytes(uid, 2)
                block[GID_START:GID_FINISH] = disktools.int_to_bytes(gid, 2)
                disktools.write_block(i, block)

        return 0
示例#9
0
    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
示例#10
0
 def read_full_file(self, at_location: int) -> bytearray:
     filetable_snapshot = self.get_filetable()
     current_block = at_location
     next_block = filetable_snapshot[at_location]
     return_array: bytearray = bytearray()
     while True:
         return_array += read_block(current_block)
         current_block = next_block
         if current_block == END_OF_FILE:
             break
         next_block = filetable_snapshot[current_block]
     return return_array
示例#11
0
    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
示例#12
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
示例#13
0
    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)
示例#14
0
    def set_inode(self, name, mode, ctime, mtime, atime, nlink, uid, gid, size,
                  block_num):
        block = disktools.read_block(block_num)
        block[MODE_START:MODE_FINISH] = disktools.int_to_bytes(mode, 2)
        block[UID_START:UID_FINISH] = disktools.int_to_bytes(uid, 2)
        block[GID_START:GID_FINISH] = disktools.int_to_bytes(gid, 2)
        block[NLINK_START:NLINK_FINISH] = disktools.int_to_bytes(nlink, 1)
        block[SIZE_START:SIZE_FINISH] = disktools.int_to_bytes(size, 2)
        block[CTIME_START:CTIME_FINISH] = disktools.int_to_bytes(int(ctime), 4)
        block[MTIME_START:MTIME_FINISH] = disktools.int_to_bytes(int(mtime), 4)
        block[ATIME_START:ATIME_FINISH] = disktools.int_to_bytes(int(atime), 4)
        block[LOCATION_START:LOCATION_FINISH] = disktools.int_to_bytes(0, 2)
        block[NAME_START:NAME_FINISH] = name.encode()

        return block
示例#15
0
    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)
示例#16
0
 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]
示例#17
0
 def get_block_metadata(self, block_index: int) -> Metadata:
     file = read_block(block_index)
     return Metadata.build_metadata(file[0:END_OF_METADATA])
示例#18
0
 def read_block(self, at_location: int) -> bytearray:
     return read_block(at_location)