aboutsummaryrefslogtreecommitdiff
path: root/lib/common/serialize_fstree.c
blob: 0f34d7d6b6c959c3747cab0fc705ca8d25d919eb (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * serialize_fstree.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "common.h"

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

static sqfs_inode_generic_t *tree_node_to_inode(tree_node_t *node)
{
	sqfs_inode_generic_t *inode;
	size_t extra = 0;

	if (S_ISLNK(node->mode))
		extra = strlen(node->data.slink_target);

	inode = calloc(1, sizeof(*inode) + extra);
	if (inode == NULL) {
		perror("creating inode");
		return NULL;
	}

	switch (node->mode & S_IFMT) {
	case S_IFSOCK:
		inode->base.type = SQFS_INODE_SOCKET;
		inode->data.ipc.nlink = 1;
		break;
	case S_IFIFO:
		inode->base.type = SQFS_INODE_FIFO;
		inode->data.ipc.nlink = 1;
		break;
	case S_IFLNK:
		inode->base.type = SQFS_INODE_SLINK;
		inode->data.slink.nlink = 1;
		inode->data.slink.target_size = extra;
		inode->slink_target = (char *)inode->extra;
		memcpy(inode->extra, node->data.slink_target, extra);
		break;
	case S_IFBLK:
		inode->base.type = SQFS_INODE_BDEV;
		inode->data.dev.nlink = 1;
		inode->data.dev.devno = node->data.devno;
		break;
	case S_IFCHR:
		inode->base.type = SQFS_INODE_CDEV;
		inode->data.dev.nlink = 1;
		inode->data.dev.devno = node->data.devno;
		break;
	default:
		assert(0);
	}

	return inode;
}

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

	ret = sqfs_dir_writer_begin(dirw, 0);
	if (ret)
		goto fail;

	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)
			goto fail;
	}

	ret = sqfs_dir_writer_end(dirw);
	if (ret)
		goto fail;

	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) {
		ret = SQFS_ERROR_ALLOC;
		goto fail;
	}

	return inode;
fail:
	sqfs_perror(filename, "recoding directory entries", ret);
	return NULL;
}

static int serialize_tree_node(const char *filename, sqfs_writer_t *wr,
			       tree_node_t *n)
{
	sqfs_inode_generic_t *inode;
	sqfs_u32 offset;
	sqfs_u64 block;
	int ret;

	if (S_ISDIR(n->mode)) {
		inode = write_dir_entries(filename, wr->dirwr, n);
		ret = SQFS_ERROR_INTERNAL;
	} else if (S_ISREG(n->mode)) {
		inode = n->data.file.user_ptr;
		n->data.file.user_ptr = NULL;
		ret = SQFS_ERROR_INTERNAL;
	} else {
		inode = tree_node_to_inode(n);
		ret = SQFS_ERROR_ALLOC;
	}

	if (inode == NULL)
		return ret;

	inode->base.mode = n->mode;
	inode->base.mod_time = n->mod_time;
	inode->base.inode_number = n->inode_num;

	sqfs_inode_set_xattr_index(inode, n->xattr_idx);

	ret = sqfs_id_table_id_to_index(wr->idtbl, n->uid,
					&inode->base.uid_idx);
	if (ret)
		goto out;

	ret = sqfs_id_table_id_to_index(wr->idtbl, n->gid,
					&inode->base.gid_idx);
	if (ret)
		goto out;

	sqfs_meta_writer_get_position(wr->im, &block, &offset);
	n->inode_ref = (block << 16) | offset;

	ret = sqfs_meta_writer_write_inode(wr->im, inode);
out:
	free(inode);
	return ret;
}

static int serialize_recursive(const char *filename, sqfs_writer_t *wr,
			       tree_node_t *root)
{
	bool has_subdirs = false;
	tree_node_t *it;
	int ret;

	for (it = root->data.dir.children; it != NULL; it = it->next) {
		if (S_ISDIR(it->mode)) {
			has_subdirs = true;
			break;
		}
	}

	if (has_subdirs) {
		for (it = root->data.dir.children; it != NULL; it = it->next) {
			if (S_ISDIR(it->mode)) {
				ret = serialize_recursive(filename, wr, it);
				if (ret)
					return ret;
			}
		}
	}

	for (it = root->data.dir.children; it != NULL; it = it->next) {
		ret = serialize_tree_node(filename, wr, it);
		if (ret)
			return ret;
	}

	return 0;
}

int sqfs_serialize_fstree(const char *filename, sqfs_writer_t *wr)
{
	int ret;

	wr->super.inode_table_start = wr->outfile->get_size(wr->outfile);

	if (S_ISDIR(wr->fs.root->mode)) {
		ret = serialize_recursive(filename, wr, wr->fs.root);
		if (ret)
			goto out;
	}

	ret = serialize_tree_node(filename, wr, wr->fs.root);
	if (ret)
		goto out;

	ret = sqfs_meta_writer_flush(wr->im);
	if (ret)
		goto out;

	ret = sqfs_meta_writer_flush(wr->dm);
	if (ret)
		goto out;

	wr->super.root_inode_ref = wr->fs.root->inode_ref;
	wr->super.directory_table_start = wr->outfile->get_size(wr->outfile);

	ret = sqfs_meta_write_write_to_file(wr->dm);
	if (ret)
		goto out;

	ret = 0;
out:
	if (ret)
		sqfs_perror(filename, "storing filesystem tree", ret);
	return ret;
}