summaryrefslogtreecommitdiff
path: root/lib/sqfs/inode.c
blob: 4a75e647fd8405048e8e99683f36c6fc8ace728e (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* SPDX-License-Identifier: LGPL-3.0-or-later */
/*
 * inode.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#define SQFS_BUILDING_DLL
#include "config.h"

#include "sqfs/inode.h"
#include "sqfs/error.h"

static int inverse_type[] = {
	[SQFS_INODE_DIR] = SQFS_INODE_EXT_DIR,
	[SQFS_INODE_FILE] = SQFS_INODE_EXT_FILE,
	[SQFS_INODE_SLINK] = SQFS_INODE_EXT_SLINK,
	[SQFS_INODE_BDEV] = SQFS_INODE_EXT_BDEV,
	[SQFS_INODE_CDEV] = SQFS_INODE_EXT_CDEV,
	[SQFS_INODE_FIFO] = SQFS_INODE_EXT_FIFO,
	[SQFS_INODE_SOCKET] = SQFS_INODE_EXT_SOCKET,
	[SQFS_INODE_EXT_DIR] = SQFS_INODE_DIR,
	[SQFS_INODE_EXT_FILE] = SQFS_INODE_FILE,
	[SQFS_INODE_EXT_SLINK] = SQFS_INODE_SLINK,
	[SQFS_INODE_EXT_BDEV] = SQFS_INODE_BDEV,
	[SQFS_INODE_EXT_CDEV] = SQFS_INODE_CDEV,
	[SQFS_INODE_EXT_FIFO] = SQFS_INODE_FIFO,
	[SQFS_INODE_EXT_SOCKET] = SQFS_INODE_SOCKET,
};

int sqfs_inode_get_xattr_index(const sqfs_inode_generic_t *inode,
			       sqfs_u32 *out)
{
	switch (inode->base.type) {
	case SQFS_INODE_DIR:
	case SQFS_INODE_FILE:
	case SQFS_INODE_SLINK:
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		*out = 0xFFFFFFFF;
		break;
	case SQFS_INODE_EXT_DIR:
		*out = inode->data.dir_ext.xattr_idx;
		break;
	case SQFS_INODE_EXT_FILE:
		*out = inode->data.file_ext.xattr_idx;
		break;
	case SQFS_INODE_EXT_SLINK:
		*out = inode->data.slink_ext.xattr_idx;
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		*out = inode->data.dev_ext.xattr_idx;
		break;
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		*out = inode->data.ipc_ext.xattr_idx;
		break;
	default:
		return SQFS_ERROR_CORRUPTED;
	}

	return 0;
}

int sqfs_inode_set_xattr_index(sqfs_inode_generic_t *inode, sqfs_u32 index)
{
	int err;

	if (index != 0xFFFFFFFF) {
		err = sqfs_inode_make_extended(inode);
		if (err)
			return err;
	}

	switch (inode->base.type) {
	case SQFS_INODE_DIR:
	case SQFS_INODE_FILE:
	case SQFS_INODE_SLINK:
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		break;
	case SQFS_INODE_EXT_DIR:
		inode->data.dir_ext.xattr_idx = index;
		break;
	case SQFS_INODE_EXT_FILE:
		inode->data.file_ext.xattr_idx = index;
		break;
	case SQFS_INODE_EXT_SLINK:
		inode->data.slink_ext.xattr_idx = index;
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		inode->data.dev_ext.xattr_idx = index;
		break;
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		inode->data.ipc_ext.xattr_idx = index;
		break;
	default:
		return SQFS_ERROR_CORRUPTED;
	}

	if (index == 0xFFFFFFFF) {
		err = sqfs_inode_make_basic(inode);
		if (err)
			return err;
	}

	return 0;
}

int sqfs_inode_make_extended(sqfs_inode_generic_t *inode)
{
	switch (inode->base.type) {
	case SQFS_INODE_DIR: {
		sqfs_inode_dir_ext_t temp = {
			.nlink = inode->data.dir.nlink,
			.size = inode->data.dir.size,
			.start_block = inode->data.dir.start_block,
			.parent_inode = inode->data.dir.parent_inode,
			.inodex_count = 0,
			.offset = inode->data.dir.offset,
			.xattr_idx = 0xFFFFFFFF,
		};
		inode->data.dir_ext = temp;
		break;
	}
	case SQFS_INODE_FILE: {
		sqfs_inode_file_ext_t temp = {
			.blocks_start = inode->data.file.blocks_start,
			.file_size = inode->data.file.file_size,
			.sparse = 0,
			.nlink = 1,
			.fragment_idx = inode->data.file.fragment_index,
			.fragment_offset = inode->data.file.fragment_offset,
			.xattr_idx = 0xFFFFFFFF,
		};
		inode->data.file_ext = temp;
		break;
	}
	case SQFS_INODE_SLINK:
		inode->data.slink_ext.xattr_idx = 0xFFFFFFFF;
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		inode->data.dev_ext.xattr_idx = 0xFFFFFFFF;
		break;
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		inode->data.dev_ext.xattr_idx = 0xFFFFFFFF;
		break;
	case SQFS_INODE_EXT_DIR:
	case SQFS_INODE_EXT_FILE:
	case SQFS_INODE_EXT_SLINK:
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		return 0;
	default:
		return SQFS_ERROR_CORRUPTED;
	}

	inode->base.type = inverse_type[inode->base.type];
	return 0;
}

int sqfs_inode_make_basic(sqfs_inode_generic_t *inode)
{
	sqfs_u32 xattr;
	int err;

	err = sqfs_inode_get_xattr_index(inode, &xattr);
	if (err != 0 || xattr != 0xFFFFFFFF)
		return err;

	switch (inode->base.type) {
	case SQFS_INODE_DIR:
	case SQFS_INODE_FILE:
	case SQFS_INODE_SLINK:
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		return 0;
	case SQFS_INODE_EXT_DIR: {
		sqfs_inode_dir_t temp = {
			.start_block = inode->data.dir_ext.start_block,
			.nlink = inode->data.dir_ext.nlink,
			.size = inode->data.dir_ext.size,
			.offset = inode->data.dir_ext.offset,
			.parent_inode = inode->data.dir_ext.parent_inode,
		};

		if (inode->data.dir_ext.size > 0x0FFFF)
			return 0;

		inode->data.dir = temp;
		break;
	}
	case SQFS_INODE_EXT_FILE: {
		sqfs_inode_file_t temp = {
			.blocks_start = inode->data.file_ext.blocks_start,
			.fragment_index = inode->data.file_ext.fragment_idx,
			.fragment_offset = inode->data.file_ext.fragment_offset,
			.file_size = inode->data.file_ext.file_size,
		};

		if (inode->data.file_ext.blocks_start > 0x0FFFFFFFFUL)
			return 0;
		if (inode->data.file_ext.file_size > 0x0FFFFFFFFUL)
			return 0;
		if (inode->data.file_ext.sparse > 0)
			return 0;
		if (inode->data.file_ext.nlink > 1)
			return 0;

		inode->data.file = temp;
		break;
	}
	case SQFS_INODE_EXT_SLINK:
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		break;
	default:
		return SQFS_ERROR_CORRUPTED;
	}

	inode->base.type = inverse_type[inode->base.type];
	return 0;
}

int sqfs_inode_set_file_size(sqfs_inode_generic_t *inode, sqfs_u64 size)
{
	if (inode->base.type == SQFS_INODE_EXT_FILE) {
		inode->data.file_ext.file_size = size;

		if (size < 0x0FFFFFFFFUL)
			sqfs_inode_make_basic(inode);
	} else if (inode->base.type == SQFS_INODE_FILE) {
		if (size > 0x0FFFFFFFFUL) {
			sqfs_inode_make_extended(inode);
			inode->data.file_ext.file_size = size;
		} else {
			inode->data.file.file_size = size;
		}
	} else {
		return SQFS_ERROR_NOT_FILE;
	}

	return 0;
}

int sqfs_inode_set_frag_location(sqfs_inode_generic_t *inode,
				 sqfs_u32 index, sqfs_u32 offset)
{
	if (inode->base.type == SQFS_INODE_EXT_FILE) {
		inode->data.file_ext.fragment_idx = index;
		inode->data.file_ext.fragment_offset = offset;
	} else if (inode->base.type == SQFS_INODE_FILE) {
		inode->data.file.fragment_index = index;
		inode->data.file.fragment_offset = offset;
	} else {
		return SQFS_ERROR_NOT_FILE;
	}

	return 0;
}

int sqfs_inode_set_file_block_start(sqfs_inode_generic_t *inode,
				    sqfs_u64 location)
{
	if (inode->base.type == SQFS_INODE_EXT_FILE) {
		inode->data.file_ext.blocks_start = location;

		if (location < 0x0FFFFFFFFUL)
			sqfs_inode_make_basic(inode);
	} else if (inode->base.type == SQFS_INODE_FILE) {
		if (location > 0x0FFFFFFFFUL) {
			sqfs_inode_make_extended(inode);
			inode->data.file_ext.blocks_start = location;
		} else {
			inode->data.file.blocks_start = location;
		}
	} else {
		return SQFS_ERROR_NOT_FILE;
	}

	return 0;
}

int sqfs_inode_get_file_size(const sqfs_inode_generic_t *inode, sqfs_u64 *size)
{
	if (inode->base.type == SQFS_INODE_EXT_FILE) {
		*size = inode->data.file_ext.file_size;
	} else if (inode->base.type == SQFS_INODE_FILE) {
		*size = inode->data.file.file_size;
	} else {
		return SQFS_ERROR_NOT_FILE;
	}

	return 0;
}

int sqfs_inode_get_frag_location(const sqfs_inode_generic_t *inode,
				 sqfs_u32 *index, sqfs_u32 *offset)
{
	if (inode->base.type == SQFS_INODE_EXT_FILE) {
		*index = inode->data.file_ext.fragment_idx;
		*offset = inode->data.file_ext.fragment_offset;
	} else if (inode->base.type == SQFS_INODE_FILE) {
		*index = inode->data.file.fragment_index;
		*offset = inode->data.file.fragment_offset;
	} else {
		return SQFS_ERROR_NOT_FILE;
	}

	return 0;
}

int sqfs_inode_get_file_block_start(const sqfs_inode_generic_t *inode,
				    sqfs_u64 *location)
{
	if (inode->base.type == SQFS_INODE_EXT_FILE) {
		*location = inode->data.file_ext.blocks_start;
	} else if (inode->base.type == SQFS_INODE_FILE) {
		*location = inode->data.file.blocks_start;
	} else {
		return SQFS_ERROR_NOT_FILE;
	}

	return 0;
}