def create(self, path, mode): self.files[path] = dict(st_mode=(S_IFREG | mode), st_nlink=1, st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time()) name = path mode = self.files[path].get('st_mode') ctime = self.files[path].get('st_ctime') mtime = self.files[path].get('st_mtime') atime = self.files[path].get('st_atime') nlink = self.files[path].get('st_nlink') size = self.files[path].get('st_size') uid = os.getuid() gid = os.getgid() self.chown(path, uid, gid) # get free block for the new created file to save the metadata. num_array = Format.get_free_block(Format, 1) block_num = num_array[0] # set up the location in the disk inode_data = Format.set_inode(Format, name, mode, ctime, mtime, atime, nlink, uid, gid, size, block_num) disktools.write_block(block_num, inode_data) # update the free block bitmap Format.update_bit_map(Format, num_array) self.fd += 1 return self.fd
def mkdir(self, path, mode): self.files[path] = dict(st_mode=(S_IFDIR | mode), st_nlink=2, st_size=0, st_ctime=time(), st_mtime=time(), st_atime=time()) name = path mode = self.files[path].get('st_mode') ctime = self.files[path].get('st_ctime') mtime = self.files[path].get('st_mtime') atime = self.files[path].get('st_atime') nlink = self.files[path].get('st_nlink') size = self.files[path].get('st_size') uid = os.getuid() gid = os.getgid() self.chown(path, uid, gid) # get free block for the new created file to save the metadata. num_array = Format.get_free_block(Format, 1) block_num = num_array[0] # set up the location in the disk inode_data = Format.set_inode(Format, name, mode, ctime, mtime, atime, nlink, uid, gid, size, block_num) disktools.write_block(block_num, inode_data) # update the free block bitmap Format.update_bit_map(Format, num_array) # find the parent path and add the nlink by 1. parent_path = Format.find_parent_path(Format, path) self.files[parent_path]['st_nlink'] += 1 Format.update_nlink(Format, parent_path, 1)
def write(self, path, data, offset, fh): self.data[path] = ( # make sure the data gets inserted at the right offset self.data[path][:offset].ljust(offset, '\x00'.encode('ascii')) + data # and only overwrites the bytes that data is replacing + self.data[path][offset + len(data):]) self.files[path]['st_size'] = len(self.data[path]) # get the length of input data size = len(self.data[path]) # calculate the number of blocks that need to store the data no_of_blocks = (size // BLOCK_SIZE) + 1 # get the free blocks num_array = Format.get_free_block(Format, no_of_blocks) Format.clear_data_block(Format, path) # set the data block bitmap data_block_bitmap = Format.set_data_block_bitmap(Format, num_array) # get the input data input_data = self.data[path] # save the data to disk into the allocated blocks for i in range(no_of_blocks): if no_of_blocks == 1: disktools.write_block(num_array[i], input_data) else: disktools.write_block(num_array[i], input_data[i * 64:i * 64 + 64]) # Add the data block bitmap into the metadata information Format.update_file_location(Format, path, data_block_bitmap) # update the st_size in the metadata Format.update_size(Format, path, self.files[path]['st_size']) # update the st_mtime in the metadata Format.update_mtime(Format, path, int(time())) # update the free block bitmap Format.update_bit_map(Format, num_array) return len(data)