summaryrefslogtreecommitdiff
path: root/lib/sqfshelper/tree_node_to_inode.c
blob: fa8c538810122545e21972efca3e90170f3512d7 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * write_inode.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "config.h"

#include "highlevel.h"
#include "sqfs/inode.h"
#include "sqfs/dir.h"
#include "util.h"

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

static int get_type(tree_node_t *node)
{
	switch (node->mode & S_IFMT) {
	case S_IFSOCK:
		if (node->xattr != NULL)
			return SQFS_INODE_EXT_SOCKET;
		return SQFS_INODE_SOCKET;
	case S_IFIFO:
		if (node->xattr != NULL)
			return SQFS_INODE_EXT_FIFO;
		return SQFS_INODE_FIFO;
	case S_IFLNK:
		if (node->xattr != NULL)
			return SQFS_INODE_EXT_SLINK;
		return SQFS_INODE_SLINK;
	case S_IFBLK:
		if (node->xattr != NULL)
			return SQFS_INODE_EXT_BDEV;
		return SQFS_INODE_BDEV;
	case S_IFCHR:
		if (node->xattr != NULL)
			return SQFS_INODE_EXT_CDEV;
		return SQFS_INODE_CDEV;
	case S_IFREG: {
		file_info_t *fi = node->data.file;

		if (node->xattr != NULL || fi->sparse > 0)
			return SQFS_INODE_EXT_FILE;

		if (fi->startblock > 0xFFFFFFFFUL || fi->size > 0xFFFFFFFFUL)
			return SQFS_INODE_EXT_FILE;

		return SQFS_INODE_FILE;
	}
	}
	assert(0);
}

static bool has_fragment(const fstree_t *fs, const file_info_t *file)
{
	if (file->size % fs->block_size == 0)
		return false;

	return file->fragment_offset < fs->block_size &&
		(file->fragment != 0xFFFFFFFF);
}

sqfs_inode_generic_t *tree_node_to_inode(fstree_t *fs, sqfs_id_table_t *idtbl,
					 tree_node_t *node)
{
	size_t i, extra = 0, block_count = 0;
	sqfs_inode_generic_t *inode;
	uint16_t uid_idx, gid_idx;
	uint32_t xattr = 0xFFFFFFFF;
	file_info_t *fi = NULL;

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

		block_count = fi->size / fs->block_size;

		if ((fi->size % fs->block_size) != 0 && !has_fragment(fs, fi))
			++block_count;

		extra = block_count * sizeof(uint32_t);
	}

	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);
	} else if (S_ISREG(node->mode)) {
		inode->block_sizes = (uint32_t *)inode->extra;

		for (i = 0; i < block_count; ++i) {
			inode->block_sizes[i] =
				node->data.file->block_size[i];
		}
	}

	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 != NULL)
		xattr = node->xattr->index;

	inode->base.type = get_type(node);
	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 = xattr;
		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 = xattr;
		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 = xattr;
		break;
	case SQFS_INODE_FILE:
		inode->data.file.blocks_start = fi->startblock;
		inode->data.file.fragment_index = 0xFFFFFFFF;
		inode->data.file.fragment_offset = 0xFFFFFFFF;
		inode->data.file.file_size = fi->size;

		if (has_fragment(fs, fi)) {
			inode->data.file.fragment_index = fi->fragment;
			inode->data.file.fragment_offset = fi->fragment_offset;
		}
		break;
	case SQFS_INODE_EXT_FILE:
		inode->data.file_ext.blocks_start = fi->startblock;
		inode->data.file_ext.file_size = fi->size;
		inode->data.file_ext.sparse = fi->sparse;
		inode->data.file_ext.nlink = 1;
		inode->data.file_ext.fragment_idx = 0xFFFFFFFF;
		inode->data.file_ext.fragment_offset = 0xFFFFFFFF;
		inode->data.file_ext.xattr_idx = xattr;

		if (has_fragment(fs, fi)) {
			inode->data.file_ext.fragment_idx = fi->fragment;
			inode->data.file_ext.fragment_offset =
				fi->fragment_offset;
		}
		break;
	default:
		goto fail;
	}

	inode->num_file_blocks = block_count;
	return inode;
fail:
	free(inode);
	return NULL;
}