Пример #1
0
def create_database(tree, path):
    '''Create a new, empty database at 'path' in 'tree'.

    A Database object for the new database is returned.
    '''
    if tree.does_path_exist(path):
        raise FileExistsError('Path already exists: ' + str(path))
    main = datafile.create_main_in_replacement_mode(tree, path)
    main.append_item(datafile.ItemSetting(b'checksum', b'sha256'))
    main.commit_and_close()
    datafile.create_content_in_replacement_mode(tree, path).commit_and_close()
    return Database(tree, path)
Пример #2
0
    def test_create_content_db_then_open_and_write_to_it(self):
        tree = FakeTree()
        tree._add_directory(('path', 'to', 'db'))

        content = datafile.create_content_in_replacement_mode(
            tree, ('path', 'to', 'db'))
        self.assertCountEqual(
            (('path', 'to', 'db', 'content.new'),),
            tree._files_modified)
        content.commit_and_close()
        self.assertCountEqual(
            (('path', 'to', 'db', 'content.new'),
             ('path', 'to', 'db', 'content')),
            tree._files_modified)
        tree._files_modified = []
        content = datafile.open_content(
            tree, ('path', 'to', 'db'), writable=True)
        cid1 = b'010----hhhh'
        content.append_item(datafile.ItemContent(cid1, cid1, 1417658340))
        cid2 = b'0200000000000000000000a'
        cksum2 = b'0200000000000000000000'
        item = datafile.ItemContent(cid2, cksum2, 1405569942)
        content.append_item(item)
        cid3 = b'040xxxxxx'
        item = datafile.ItemContent(cid3, cid3, 1402958556)
        content.append_item(item)
        content.close()
        self.assertCountEqual(
            (('path', 'to', 'db', 'content'),),
            tree._files_modified)
        tree._files_modified = []
        self.assertEqual(
            8192,
            len(tree._files[('path', 'to', 'db', 'content')].content))
        content = datafile.open_content(tree, ('path', 'to', 'db'))
        self.assertEqual('magic', next(content).kind)
        item = next(content)
        while item.kind == 'setting':
            item = next(content)
        self.assertEqual('content', item.kind)
        self.assertEqual(cid1, item.cid)
        self.assertEqual(cid1, item.checksum)
        self.assertEqual(1417658340, item.first)
        item = next(content)
        self.assertEqual('content', item.kind)
        self.assertEqual(cid2, item.cid)
        self.assertEqual(cksum2, item.checksum)
        self.assertEqual(1405569942, item.first)
        item = next(content)
        self.assertEqual('content', item.kind)
        self.assertEqual(cid3, item.cid)
        self.assertEqual(cid3, item.checksum)
        self.assertEqual(1402958556, item.first)
        self.assertRaises(StopIteration, next, content)
        content.close()
        self.assertCountEqual((), tree._files_modified)
Пример #3
0
    def test_create_multi_block_content_db(self):
        tree = FakeTree()
        tree._add_directory(('path', 'to', 'db'))

        content = datafile.create_content_in_replacement_mode(
            tree, ('path', 'to', 'db'))
        # This item is sized so that the first data block is exactly filled.
        content.append_item(
            datafile.ItemContent(b'000000', b'000000', 1403044159))
        cid1 = b'010----x'
        for i in range(500):
            item = datafile.ItemContent(cid1, cid1, 1417658340)
            content.append_item(item)
        self.assertCountEqual(
            (('path', 'to', 'db', 'content.new'),),
            tree._files_modified)
        content.commit_and_close()
        self.assertCountEqual(
            (('path', 'to', 'db', 'content.new'),
             ('path', 'to', 'db', 'content')),
            tree._files_modified)
        tree._files_modified = []
        self.assertEqual(
            4 * 4096,
            len(tree._files[('path', 'to', 'db', 'content')].content))
        content = datafile.open_content(tree, ('path', 'to', 'db'))
        self.assertEqual('magic', next(content).kind)
        item = next(content)
        while item.kind == 'setting':
            item = next(content)
        self.assertEqual('content', item.kind)
        self.assertEqual(b'000000', item.cid)
        self.assertEqual(b'000000', item.checksum)
        self.assertEqual(1403044159, item.first)
        for i in range(500):
            item = next(content)
            self.assertEqual('content', item.kind)
            self.assertEqual(cid1, item.cid)
            self.assertEqual(cid1, item.checksum)
            self.assertEqual(1417658340, item.first)
        self.assertRaises(StopIteration, next, content)
        content.close()
        self.assertCountEqual((), tree._files_modified)
        data = tree._files[('path', 'to', 'db', 'content')].content
        self.assertEqual(
            b'ebakup content data\nedb-blocksize:4096\n', data[:39])
        # Check that the first data block starts with the first item
        # (which is different from the others so it is identifiable).
        self.assertEqual(
            b'\xdd\x06\x06000000\x3f\xc1\xa0\x53\x3f\xc1\xa0\x53',
            data[4096:4113])
        # Check that the first data block is exactly filled.
        self.assertEqual(
            b'\xdd\x08\x08' + cid1 + b'\xe4\xbf\x7f\x54\xe4\xbf\x7f\x54',
            data[4096+4045:4096+4064])
        # Check that the second data block has its last item in the
        # expected place...
        self.assertEqual(
            b'\xdd\x08\x08' + cid1 + b'\xe4\xbf\x7f\x54\xe4\xbf\x7f\x54',
            data[8192+4028:8192+4047])
        # ... followed by correct padding
        self.assertEqual(b'\x00' * 17, data[8192+4047:8192+4064])
        # Check that the final item is in the expected place...
        self.assertEqual(
            b'\xdd\x08\x08' + cid1 + b'\xe4\xbf\x7f\x54\xe4\xbf\x7f\x54',
            data[12288 + 73 * 19 : 12288 + 74 * 19])
        # ... followed by correct padding
        self.assertEqual(b'\x00' * 2658, data[12288 + 74 * 19 : 12288 + 4064])