aboutsummaryrefslogtreecommitdiff
path: root/lib/sqfs/tree_node_from_inode.c
blob: 84ebbb24f8fefdff7768d78a26cb6fc61e45d16b (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "config.h"

#include "highlevel.h"

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

static size_t compute_size(sqfs_inode_generic_t *inode, const char *name,
			   size_t block_size)
{
	size_t size = sizeof(tree_node_t) + strlen(name) + 1;
	size_t block_count = 0;

	switch (inode->base.type) {
	case SQFS_INODE_DIR:
	case SQFS_INODE_EXT_DIR:
		size += sizeof(dir_info_t);
		break;
	case SQFS_INODE_FILE:
		size += sizeof(file_info_t);
		block_count = inode->data.file.file_size / block_size;
		if ((inode->data.file.file_size % block_size) != 0)
			++block_count;
		break;
	case SQFS_INODE_EXT_FILE:
		size += sizeof(file_info_t);
		block_count = inode->data.file_ext.file_size / block_size;
		if ((inode->data.file_ext.file_size % block_size) != 0)
			++block_count;
		break;
	case SQFS_INODE_SLINK:
	case SQFS_INODE_EXT_SLINK:
		size += strlen(inode->slink_target) + 1;
		break;
	default:
		break;
	}

	return size + block_count * sizeof(((file_info_t *)0)->blocks[0]);
}

static void copy_block_sizes(sqfs_inode_generic_t *inode, tree_node_t *out,
			     size_t block_size)
{
	size_t i, block_count = out->data.file->size / block_size;

	if ((out->data.file->size % block_size) != 0) {
		if (out->data.file->fragment == 0xFFFFFFFF ||
		    out->data.file->fragment_offset == 0xFFFFFFFF) {
			++block_count;
		} else {
			out->data.file->flags |= FILE_FLAG_HAS_FRAGMENT;
		}
	}

	out->name += block_count * sizeof(out->data.file->blocks[0]);

	for (i = 0; i < block_count; ++i)
		out->data.file->blocks[i].size = inode->block_sizes[i];
}

tree_node_t *tree_node_from_inode(sqfs_inode_generic_t *inode,
				  const id_table_t *idtbl,
				  const char *name,
				  size_t block_size)
{
	tree_node_t *out;

	if (inode->base.uid_idx >= idtbl->num_ids) {
		fputs("converting inode to fs tree node: UID out of range\n",
		      stderr);
		return NULL;
	}

	if (inode->base.gid_idx >= idtbl->num_ids) {
		fputs("converting inode to fs tree node: GID out of range\n",
		      stderr);
		return NULL;
	}

	out = calloc(1, compute_size(inode, name, block_size));
	if (out == NULL) {
		perror("converting inode to fs tree node");
		return NULL;
	}

	out->uid = idtbl->ids[inode->base.uid_idx];
	out->gid = idtbl->ids[inode->base.gid_idx];
	out->mode = inode->base.mode;
	out->inode_num = inode->base.inode_number;
	out->mod_time = inode->base.mod_time;
	out->name = (char *)out->payload;

	switch (inode->base.type) {
	case SQFS_INODE_DIR:
		out->data.dir = (dir_info_t *)out->payload;
		out->name += sizeof(dir_info_t);

		out->data.dir->size = inode->data.dir.size;
		out->data.dir->start_block = inode->data.dir.start_block;
		out->data.dir->block_offset = inode->data.dir.offset;
		break;
	case SQFS_INODE_EXT_DIR:
		out->data.dir = (dir_info_t *)out->payload;
		out->name += sizeof(dir_info_t);

		out->data.dir->size = inode->data.dir_ext.size;
		out->data.dir->start_block = inode->data.dir_ext.start_block;
		out->data.dir->block_offset = inode->data.dir_ext.offset;
		break;
	case SQFS_INODE_FILE:
		out->data.file = (file_info_t *)out->payload;
		out->name += sizeof(file_info_t);

		out->data.file->size = inode->data.file.file_size;
		out->data.file->startblock = inode->data.file.blocks_start;
		out->data.file->fragment = inode->data.file.fragment_index;
		out->data.file->fragment_offset =
			inode->data.file.fragment_offset;

		copy_block_sizes(inode, out, block_size);
		break;
	case SQFS_INODE_EXT_FILE:
		out->data.file = (file_info_t *)out->payload;
		out->name += sizeof(file_info_t);

		out->data.file->size = inode->data.file_ext.file_size;
		out->data.file->sparse = inode->data.file_ext.sparse;
		out->data.file->startblock = inode->data.file_ext.blocks_start;
		out->data.file->fragment = inode->data.file_ext.fragment_idx;
		out->data.file->fragment_offset =
			inode->data.file_ext.fragment_offset;

		copy_block_sizes(inode, out, block_size);
		break;
	case SQFS_INODE_SLINK:
	case SQFS_INODE_EXT_SLINK:
		out->data.slink_target = (char *)out->payload;
		strcpy(out->data.slink_target, inode->slink_target);

		out->name = (char *)out->payload +
			strlen(inode->slink_target) + 1;
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		out->name = (char *)out->payload;
		out->data.devno = inode->data.dev.devno;
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		out->name = (char *)out->payload;
		out->data.devno = inode->data.dev_ext.devno;
		break;
	default:
		break;
	}

	strcpy(out->name, name);
	return out;
}