summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/serialize_fstree.c
blob: 278678975e47e0aa33666b7ed76fbc24a0966595 (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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* 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 "sqfs/inode.h"
#include "sqfs/dir.h"

#include "highlevel.h"
#include "util.h"

#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

static int get_type(tree_node_t *node)
{
	switch (node->mode & S_IFMT) {
	case S_IFSOCK: return SQFS_INODE_SOCKET;
	case S_IFIFO: return SQFS_INODE_FIFO;
	case S_IFLNK: return SQFS_INODE_SLINK;
	case S_IFBLK: return SQFS_INODE_BDEV;
	case S_IFCHR: return SQFS_INODE_CDEV;
	}
	assert(0);
}

static sqfs_inode_generic_t *tree_node_to_inode(sqfs_id_table_t *idtbl,
						tree_node_t *node)
{
	sqfs_inode_generic_t *inode;
	sqfs_u16 uid_idx, gid_idx;
	size_t extra = 0;

	if (S_ISREG(node->mode)) {
		inode = node->data.file.user_ptr;
		node->data.file.user_ptr = NULL;
	} else {
		if (S_ISLNK(node->mode))
			extra = strlen(node->data.slink_target);

		inode = alloc_flex(sizeof(*inode), 1, extra);
		if (inode == NULL) {
			perror("creating inode from file system tree node");
			return NULL;
		}

		if (S_ISLNK(node->mode)) {
			inode->slink_target = (char *)inode->extra;

			memcpy(inode->extra, node->data.slink_target, extra);
		}

		inode->base.type = get_type(node);
	}

	if (sqfs_id_table_id_to_index(idtbl, node->uid, &uid_idx))
		goto fail;

	if (sqfs_id_table_id_to_index(idtbl, node->gid, &gid_idx))
		goto fail;

	if (node->xattr_idx != 0xFFFFFFFF)
		sqfs_inode_make_extended(inode);

	inode->base.mode = node->mode;
	inode->base.uid_idx = uid_idx;
	inode->base.gid_idx = gid_idx;
	inode->base.mod_time = node->mod_time;
	inode->base.inode_number = node->inode_num;

	switch (inode->base.type) {
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		inode->data.ipc.nlink = 1;
		break;
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		inode->data.ipc_ext.nlink = 1;
		inode->data.ipc_ext.xattr_idx = node->xattr_idx;
		break;
	case SQFS_INODE_SLINK:
		inode->data.slink.nlink = 1;
		inode->data.slink.target_size = extra;
		break;
	case SQFS_INODE_EXT_SLINK:
		inode->data.slink_ext.nlink = 1;
		inode->data.slink_ext.target_size = extra;
		inode->data.slink_ext.xattr_idx = node->xattr_idx;
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		inode->data.dev.nlink = 1;
		inode->data.dev.devno = node->data.devno;
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		inode->data.dev_ext.nlink = 1;
		inode->data.dev_ext.devno = node->data.devno;
		inode->data.dev_ext.xattr_idx = node->xattr_idx;
		break;
	case SQFS_INODE_FILE:
		break;
	case SQFS_INODE_EXT_FILE:
		inode->data.file_ext.xattr_idx = node->xattr_idx;
		break;
	default:
		goto fail;
	}

	return inode;
fail:
	free(inode);
	return NULL;
}

static sqfs_inode_generic_t *write_dir_entries(sqfs_dir_writer_t *dirw,
					       tree_node_t *node,
					       sqfs_id_table_t *idtbl)
{
	sqfs_u32 xattr, parent_inode;
	sqfs_inode_generic_t *inode;
	tree_node_t *it;
	int ret;

	if (sqfs_dir_writer_begin(dirw, 0))
		return NULL;

	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 NULL;
	}

	if (sqfs_dir_writer_end(dirw))
		return NULL;

	xattr = node->xattr_idx;
	parent_inode = (node->parent == NULL) ? 0 : node->parent->inode_num;

	inode = sqfs_dir_writer_create_inode(dirw, 0, xattr, parent_inode);
	if (inode == NULL) {
		perror("creating inode");
		return NULL;
	}

	if (sqfs_id_table_id_to_index(idtbl, node->uid, &inode->base.uid_idx))
		goto fail_id;

	if (sqfs_id_table_id_to_index(idtbl, node->gid, &inode->base.gid_idx))
		goto fail_id;

	inode->base.mode = node->mode;
	inode->base.mod_time = node->mod_time;
	inode->base.inode_number = node->inode_num;
	return inode;
fail_id:
	fputs("failed to allocate IDs\n", stderr);
	free(inode);
	return NULL;
}

int sqfs_serialize_fstree(sqfs_file_t *file, sqfs_super_t *super, fstree_t *fs,
			  sqfs_compressor_t *cmp, sqfs_id_table_t *idtbl)
{
	sqfs_inode_generic_t *inode;
	sqfs_meta_writer_t *im, *dm;
	sqfs_dir_writer_t *dirwr;
	sqfs_u32 offset;
	sqfs_u64 block;
	int ret = -1;
	size_t i;

	im = sqfs_meta_writer_create(file, cmp, 0);
	if (im == NULL)
		return -1;

	dm = sqfs_meta_writer_create(file, cmp,
				     SQFS_META_WRITER_KEEP_IN_MEMORY);
	if (dm == NULL)
		goto out_im;

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

	super->inode_table_start = file->get_size(file);

	for (i = 0; i < fs->inode_tbl_size; ++i) {
		if (S_ISDIR(fs->inode_table[i]->mode)) {
			inode = write_dir_entries(dirwr, fs->inode_table[i],
						  idtbl);
		} else {
			inode = tree_node_to_inode(idtbl, fs->inode_table[i]);
		}

		if (inode == NULL)
			goto out;

		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;
		}

		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;
	super->directory_table_start = file->get_size(file);

	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;
}