예제 #1
0
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()
예제 #2
0
 def test_decode_no_device(self):
     self.cls = ds.SuperBlock(None)
     input_data = b'\x1e\x00\x00\x00\x00\x00\x00\x00' + \
                  b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                  b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     expected = [30, 10]
     output = self.cls.__decode__(input_data)
     self.assertEqual(output, expected)
예제 #3
0
 def test_bytes(self):
     ds.BLOCK_SIZE = 30
     self.cls = ds.SuperBlock()
     expected = b'\x1e\x00\x00\x00\x00\x00\x00\x00' + \
                b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     output = bytes(self.cls)
     self.assertEqual(output, expected)
예제 #4
0
 def test_decode_2(self):
     ds.BLOCK_SIZE = 0  # precaution
     self.cls = ds.SuperBlock()
     input_data = b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                  b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                  b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     expected = [10, 10]
     output = self.cls.__decode__(input_data)
     self.assertEqual(output, expected)
예제 #5
0
 def test_read_2(self):
     input_data = b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                  b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                  b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     with open(PATH, 'wb') as f:
         f.write(input_data)
     device = device_io.Disk(PATH)
     self.cls = ds.SuperBlock(device)
     self.assertEqual(self.cls.block_size, 10)
     self.assertEqual(self.cls.num_inodes, 10)
예제 #6
0
 def test_allocate_with_device(self):
     input_data = bytes(ds.SuperBlock()) + \
                  bytes(ds.NUM_INODES * ds.BLOCK_SIZE) + \
                  bytes(ds.InodeFreeList())
     with open(PATH, 'wb') as f:
         f.write(input_data)
     self.cls = ds.Inode(device=device_io.Disk(PATH))
     # self.assertEqual(self.cls.index, None)  # New inode should have a None index
     # self.cls.allocate()
     self.assertEqual(self.cls.index, 0)
     with self.assertRaises(Exception):
         self.cls.allocate()
예제 #7
0
 def test_write_1(self):
     ds.BLOCK_SIZE = 30
     device_io.BLOCK_SIZE = 30
     ds.NUM_INODES = 10
     expected = b'\x1e\x00\x00\x00\x00\x00\x00\x00' + \
                b'\x0A\x00\x00\x00\x00\x00\x00\x00' + \
                b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
     self.cls = ds.SuperBlock(None)
     self.cls._device = device_io.Disk(PATH)
     self.cls.__write__()
     with open(PATH, 'rb') as f:
         output = f.read()
     self.assertEqual(output, expected)
예제 #8
0
 def setUp(self):
     self.cls = ds.SuperBlock()
     open(PATH, 'a').close()