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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/* 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 <stdlib.h>
#include <stdio.h>
static int write_dir_entries(sqfs_dir_writer_t *dirw, tree_node_t *node)
{
tree_node_t *it;
uint64_t ref;
int ret;
if (sqfs_dir_writer_begin(dirw))
return -1;
for (it = node->data.dir->children; it != NULL; it = it->next) {
ret = sqfs_dir_writer_add_entry(dirw, it->name, it->inode_num,
it->inode_ref, it->mode);
if (ret)
return -1;
}
if (sqfs_dir_writer_end(dirw))
return -1;
ref = sqfs_dir_writer_get_dir_reference(dirw);
node->data.dir->size = sqfs_dir_writer_get_size(dirw);
node->data.dir->start_block = ref >> 16;
node->data.dir->block_offset = ref & 0xFFFF;
return 0;
}
int sqfs_serialize_fstree(int outfd, sqfs_super_t *super, fstree_t *fs,
compressor_t *cmp, id_table_t *idtbl)
{
sqfs_inode_generic_t *inode;
sqfs_meta_writer_t *im, *dm;
sqfs_dir_writer_t *dirwr;
uint32_t offset;
uint64_t block;
int ret = -1;
size_t i;
im = sqfs_meta_writer_create(outfd, cmp, false);
if (im == NULL)
return -1;
dm = sqfs_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 (S_ISDIR(fs->inode_table[i]->mode)) {
if (write_dir_entries(dirwr, fs->inode_table[i]))
goto out;
}
inode = tree_node_to_inode(fs, idtbl, fs->inode_table[i]);
if (inode == NULL)
goto out;
if (inode->base.type == SQFS_INODE_EXT_DIR) {
inode->data.dir_ext.inodex_count =
sqfs_dir_writer_get_index_size(dirwr);
}
sqfs_meta_writer_get_position(im, &block, &offset);
fs->inode_table[i]->inode_ref = (block << 16) | offset;
if (sqfs_meta_writer_write_inode(im, inode)) {
free(inode);
goto out;
}
if (inode->base.type == SQFS_INODE_EXT_DIR) {
if (sqfs_dir_writer_write_index(dirwr, im)) {
free(inode);
goto out;
}
}
free(inode);
}
if (sqfs_meta_writer_flush(im))
goto out;
if (sqfs_meta_writer_flush(dm))
goto out;
super->root_inode_ref = fs->root->inode_ref;
sqfs_meta_writer_get_position(im, &block, &offset);
super->inode_table_start = super->bytes_used;
super->bytes_used += block;
sqfs_meta_writer_get_position(dm, &block, &offset);
super->directory_table_start = super->bytes_used;
super->bytes_used += block;
if (sqfs_meta_write_write_to_file(dm))
goto out;
ret = 0;
out:
sqfs_dir_writer_destroy(dirwr);
out_dm:
sqfs_meta_writer_destroy(dm);
out_im:
sqfs_meta_writer_destroy(im);
return ret;
}
|