def __init__(self, file_name, uid=0): self._file = open(file_name, 'rb+') self._superblock = SuperBlock.read(self._file) self._fat = FAT.read(self._superblock.cluster_num, self._superblock.fat_offset, self._file, self._superblock) self._inode_map = InodeMap.read(self._superblock.cluster_num, self._superblock.inode_map_offset, self._file) self._root = Root(self._superblock, self._fat, self._file) self._uid = uid
def format(file_name, password='******', size=50 * 1024 * 1024): file = open(file_name, 'wb') file.seek(size - 1) file.write(b'\0') superblock = SuperBlock.default(size) file.seek(superblock.inode_array_offset) inode_table = [Inode(id) for id in range(superblock.cluster_num)] for inode in inode_table: file.write(inode.pack()) cluster_size = superblock.cluster_size offset = cluster_size - file.tell() % cluster_size superblock.first_cluster_offset = file.tell() + offset superblock.free_cluster_num = ( superblock.cluster_num - superblock.first_cluster_offset // cluster_size) fat = FAT.empty(superblock.free_cluster_num, superblock) inode_map = InodeMap.empty(superblock.free_cluster_num) Root.write(superblock, fat, inode_map, file) superblock.write(file) fat.write(superblock.fat_offset, file) inode_map.write(superblock.inode_map_offset, file) file.close() fs = FileSystem(file_name, 0) fs.create('users') hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode() fs.write('users', '0 admin %s' % hash)