コード例 #1
0
 def test_add_write_read(self):
     reload(ds)
     ds.NUM_FILES_PER_DIR_BLOCK = 5
     reload(utils)
     reload(device_io)
     utils.makefs(PATH)
     self.cls = ds.DirectoryBlock(device=device_io.Disk(PATH))
     self.cls.add_entry('f1', 1)
     self.cls = ds.DirectoryBlock(device=self.cls._device, index=1)
     self.assertEqual(self.cls.entry_names, ['f1', '', '', '', ''])
     self.assertEqual(self.cls.entry_inode_indices, [1, 0, 0, 0, 0])
コード例 #2
0
ファイル: utils.py プロジェクト: angadgill/unix-fs-py
def makefs(root_path, verbose=False):
    """
        Layout:
        Superblock
        All Inodes
        Inode Freelist
        Block Freelist
        Root Directory
    """
    if verbose:
        print("Creating file system at {}".format(root_path))
    bootstrap_data = bytes(ds.SuperBlock())
    bootstrap_data += bytes(ds.BLOCK_SIZE * ds.NUM_INODES)
    bootstrap_data += bytes(ds.InodeFreeList())
    bootstrap_data += bytes(ds.DataBlockFreeList())
    # TODO: Update write a real root directory
    bootstrap_data += bytes(ds.DirectoryBlock())
    bootstrap_data += bytes(ds.BLOCK_SIZE * ds.NUM_DATA_BLOCKS)
    disk = device_io.Disk(root_path)
    disk.open()
    disk.seek(0)
    disk.write(bootstrap_data)
    # TODO: remove this hack when real root directory is written
    ds.DataBlockFreeList(device=disk).allocate()
    disk.close()
コード例 #3
0
 def setUp(self):
     ds.BLOCK_SIZE = device_io.BLOCK_SIZE = 10
     ds.NUM_DATA_BLOCKS = 10
     ds.MAX_FILENAME_LENGTH = 5
     ds.NUM_FILES_PER_DIR_BLOCK = 5
     self.cls = ds.DirectoryBlock()
     open(PATH, 'a').close()
コード例 #4
0
 def test_add_few__with_device(self):
     expected = (['test1', 'test2'], [1, 2])
     self.cls.add('test1', 1)
     self.cls.add('test2', 2)
     block = ds.DirectoryBlock(device=self.cls._device, index=1)
     self.cls = system.Directory(device=device_io.Disk(PATH), index=0)
     self.assertEqual(self.cls.read(), expected)
コード例 #5
0
    def test_read(self):
        self.cls = ds.DirectoryBlock(index=0)
        input_data = bytes(self.cls.address * ds.BLOCK_SIZE) + \
                     b'test\x00' \
                     b'f0\x00\x00\x00f1\x00\x00\x00f2\x00\x00\x00f3\x00\x00\x00f4\x00\x00\x00' + \
                     b'\x00\x00' \
                     b'\x00\x00\x00\x00\x00\x00\x00\x00' \
                     b'\x01\x00\x00\x00\x00\x00\x00\x00' \
                     b'\x02\x00\x00\x00\x00\x00\x00\x00' \
                     b'\x03\x00\x00\x00\x00\x00\x00\x00' \
                     b'\x04\x00\x00\x00\x00\x00\x00\x00'

        with open(PATH, 'wb') as f:
            f.write(input_data)
        self.cls = ds.DirectoryBlock(device=device_io.Disk(PATH), index=0)
        self.assertEqual(self.cls.name, 'test')
        self.assertEqual(self.cls.entry_names,
                         ['f{}'.format(i) for i in range(5)])
        self.assertEqual(self.cls.entry_inode_indices, list(range(5)))
コード例 #6
0
 def test_add_item_with_device(self):
     self.cls = ds.DirectoryBlock(index=0)
     self.cls._device = device_io.Disk(PATH)
     self.cls.name = 'test'
     self.cls.add_entry('f1', 1)
     # self.cls.__write__()
     expected = bytes(self.cls.address * ds.BLOCK_SIZE) + \
                b'test\x00' \
                b'f1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
                b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' \
                b'\x00\x00' \
                b'\x01\x00\x00\x00\x00\x00\x00\x00' \
                b'\x00\x00\x00\x00\x00\x00\x00\x00' \
                b'\x00\x00\x00\x00\x00\x00\x00\x00' \
                b'\x00\x00\x00\x00\x00\x00\x00\x00' \
                b'\x00\x00\x00\x00\x00\x00\x00\x00'
     with open(PATH, 'rb') as f:
         output = f.read()
     self.assertEqual(output, expected)
コード例 #7
0
 def test_write(self):
     self.cls = ds.DirectoryBlock(index=0)
     self.cls._device = device_io.Disk(PATH)
     self.cls.name = 'test'
     self.cls.entry_names = ['f{}'.format(i) for i in range(5)]
     self.cls.entry_inode_indices = list(range(5))
     self.cls.__write__()
     expected = bytes(self.cls.address * ds.BLOCK_SIZE) + \
                b'test\x00' \
                b'f0\x00\x00\x00f1\x00\x00\x00f2\x00\x00\x00f3\x00\x00\x00f4\x00\x00\x00' \
                b'\x00\x00' \
                b'\x00\x00\x00\x00\x00\x00\x00\x00' \
                b'\x01\x00\x00\x00\x00\x00\x00\x00' \
                b'\x02\x00\x00\x00\x00\x00\x00\x00' \
                b'\x03\x00\x00\x00\x00\x00\x00\x00' \
                b'\x04\x00\x00\x00\x00\x00\x00\x00'
     with open(PATH, 'rb') as f:
         output = f.read()
     self.assertEqual(output, expected)