summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/serialize_fstree.c
blob: 53e22366c6e30043a87e295814986e7f75a9a061 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * serialize_fstree.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "sqfs/meta_writer.h"
#include "highlevel.h"
#include "util.h"

#include <unistd.h>
#include <stdio.h>

int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs,
			  compressor_t *cmp, id_table_t *idtbl)
{
	sqfs_dir_writer_t *dirwr;
	meta_writer_t *im, *dm;
	uint32_t offset;
	uint64_t block;
	int ret = -1;
	size_t i;

	im = meta_writer_create(outfd, cmp, false);
	if (im == NULL)
		return -1;

	dm = meta_writer_create(outfd, cmp, true);
	if (dm == NULL)
		goto out_im;

	dirwr = sqfs_dir_writer_create(dm);
	if (dirwr == NULL)
		goto out_dm;

	for (i = 2; i < fs->inode_tbl_size; ++i) {
		if (meta_writer_write_inode(fs, idtbl, im, dirwr,
					    fs->inode_table[i])) {
			goto out;
		}
	}

	if (meta_writer_flush(im))
		goto out;

	if (meta_writer_flush(dm))
		goto out;

	super->root_inode_ref = fs->root->inode_ref;

	meta_writer_get_position(im, &block, &offset);
	super->inode_table_start = super->bytes_used;
	super->bytes_used += block;

	meta_writer_get_position(dm, &block, &offset);
	super->directory_table_start = super->bytes_used;
	super->bytes_used += block;

	if (meta_write_write_to_file(dm))
		goto out;

	ret = 0;
out:
	sqfs_dir_writer_destroy(dirwr);
out_dm:
	meta_writer_destroy(dm);
out_im:
	meta_writer_destroy(im);
	return ret;
}