aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/write_inode.c
blob: d27d1b426e4b559c615f6da3170eea5c393bc906 (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
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * write_inode.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include "sqfs/meta_writer.h"
#include "sqfs/error.h"
#include "sqfs/inode.h"
#include "sqfs/dir.h"
#include "compat.h"

#include <string.h>

static int write_block_sizes(sqfs_meta_writer_t *ir,
			     const sqfs_inode_generic_t *n)
{
	sqfs_u32 *sizes;
	size_t i;

	if (n->payload_bytes_used < sizeof(sizes[0]))
		return 0;

	if ((n->payload_bytes_used % sizeof(sizes[0])) != 0)
		return SQFS_ERROR_CORRUPTED;

	sizes = alloca(n->payload_bytes_used);

	for (i = 0; i < (n->payload_bytes_used / sizeof(sizes[0])); ++i)
		sizes[i] = htole32(n->extra[i]);

	return sqfs_meta_writer_append(ir, sizes, n->payload_bytes_used);
}

static int write_dir_index(sqfs_meta_writer_t *ir, const sqfs_u8 *data,
			   size_t count)
{
	sqfs_dir_index_t ent;
	size_t len;
	int err;

	while (count > sizeof(ent)) {
		memcpy(&ent, data, sizeof(ent));
		data += sizeof(ent);
		count -= sizeof(ent);
		len = ent.size + 1;

		if (len > count)
			return SQFS_ERROR_CORRUPTED;

		ent.start_block = htole32(ent.start_block);
		ent.index = htole32(ent.index);
		ent.size = htole32(ent.size);

		err = sqfs_meta_writer_append(ir, &ent, sizeof(ent));
		if (err)
			return err;

		err = sqfs_meta_writer_append(ir, data, len);
		if (err)
			return err;

		data += len;
		count -= len;
	}

	return 0;
}

int sqfs_meta_writer_write_inode(sqfs_meta_writer_t *ir,
				 const sqfs_inode_generic_t *n)
{
	sqfs_inode_t base;
	int ret;

	base.type = htole16(n->base.type);
	base.mode = htole16(n->base.mode & ~SQFS_INODE_MODE_MASK);
	base.uid_idx = htole16(n->base.uid_idx);
	base.gid_idx = htole16(n->base.gid_idx);
	base.mod_time = htole32(n->base.mod_time);
	base.inode_number = htole32(n->base.inode_number);

	ret = sqfs_meta_writer_append(ir, &base, sizeof(base));
	if (ret)
		return ret;

	switch (n->base.type) {
	case SQFS_INODE_DIR: {
		sqfs_inode_dir_t dir = {
			.start_block = htole32(n->data.dir.start_block),
			.nlink = htole32(n->data.dir.nlink),
			.size = htole16(n->data.dir.size),
			.offset = htole16(n->data.dir.offset),
			.parent_inode = htole32(n->data.dir.parent_inode),
		};
		return sqfs_meta_writer_append(ir, &dir, sizeof(dir));
	}
	case SQFS_INODE_FILE: {
		sqfs_inode_file_t file = {
			.blocks_start = htole32(n->data.file.blocks_start),
			.fragment_index = htole32(n->data.file.fragment_index),
			.fragment_offset =
				htole32(n->data.file.fragment_offset),
			.file_size = htole32(n->data.file.file_size),
		};
		ret = sqfs_meta_writer_append(ir, &file, sizeof(file));
		if (ret)
			return ret;
		return write_block_sizes(ir, n);
	}
	case SQFS_INODE_SLINK: {
		sqfs_inode_slink_t slink = {
			.nlink = htole32(n->data.slink.nlink),
			.target_size = htole32(n->data.slink.target_size),
		};
		ret = sqfs_meta_writer_append(ir, &slink, sizeof(slink));
		if (ret)
			return ret;
		return sqfs_meta_writer_append(ir, n->extra,
					       n->data.slink.target_size);
	}
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV: {
		sqfs_inode_dev_t dev = {
			.nlink = htole32(n->data.dev.nlink),
			.devno = htole32(n->data.dev.devno),
		};
		return sqfs_meta_writer_append(ir, &dev, sizeof(dev));
	}
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET: {
		sqfs_inode_ipc_t ipc = {
			.nlink = htole32(n->data.ipc.nlink),
		};
		return sqfs_meta_writer_append(ir, &ipc, sizeof(ipc));
	}
	case SQFS_INODE_EXT_DIR: {
		sqfs_inode_dir_ext_t dir = {
			.nlink = htole32(n->data.dir_ext.nlink),
			.size = htole32(n->data.dir_ext.size),
			.start_block = htole32(n->data.dir_ext.start_block),
			.parent_inode = htole32(n->data.dir_ext.parent_inode),
			.inodex_count = htole16(n->data.dir_ext.inodex_count),
			.offset = htole16(n->data.dir_ext.offset),
			.xattr_idx = htole32(n->data.dir_ext.xattr_idx),
		};
		ret = sqfs_meta_writer_append(ir, &dir, sizeof(dir));
		if (ret)
			return ret;
		return write_dir_index(ir, (const sqfs_u8 *)n->extra,
				       n->payload_bytes_used);
	}
	case SQFS_INODE_EXT_FILE: {
		sqfs_inode_file_ext_t file = {
			.blocks_start = htole64(n->data.file_ext.blocks_start),
			.file_size = htole64(n->data.file_ext.file_size),
			.sparse = htole64(n->data.file_ext.sparse),
			.nlink = htole32(n->data.file_ext.nlink),
			.fragment_idx = htole32(n->data.file_ext.fragment_idx),
			.fragment_offset =
				htole32(n->data.file_ext.fragment_offset),
			.xattr_idx = htole32(n->data.file_ext.xattr_idx),
		};
		ret = sqfs_meta_writer_append(ir, &file, sizeof(file));
		if (ret)
			return ret;
		return write_block_sizes(ir, n);
	}
	case SQFS_INODE_EXT_SLINK: {
		sqfs_inode_slink_t slink = {
			.nlink = htole32(n->data.slink_ext.nlink),
			.target_size = htole32(n->data.slink_ext.target_size),
		};
		sqfs_u32 xattr = htole32(n->data.slink_ext.xattr_idx);

		ret = sqfs_meta_writer_append(ir, &slink, sizeof(slink));
		if (ret)
			return ret;
		ret = sqfs_meta_writer_append(ir, n->extra,
					      n->data.slink_ext.target_size);
		if (ret)
			return ret;
		return sqfs_meta_writer_append(ir, &xattr, sizeof(xattr));
	}
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV: {
		sqfs_inode_dev_ext_t dev = {
			.nlink = htole32(n->data.dev_ext.nlink),
			.devno = htole32(n->data.dev_ext.devno),
			.xattr_idx = htole32(n->data.dev_ext.xattr_idx),
		};
		return sqfs_meta_writer_append(ir, &dev, sizeof(dev));
	}
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET: {
		sqfs_inode_ipc_ext_t ipc = {
			.nlink = htole32(n->data.ipc_ext.nlink),
			.xattr_idx = htole32(n->data.ipc_ext.xattr_idx),
		};
		return sqfs_meta_writer_append(ir, &ipc, sizeof(ipc));
	}
	}

	return SQFS_ERROR_UNSUPPORTED;
}