summaryrefslogtreecommitdiff
path: root/bin/rdsquashfs/stat.c
blob: ae6e10bee881999e88330d74bf1751382993ae05 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * stat.c
 *
 * Copyright (C) 2020 David Oberhollenzer <goliath@infraroot.at>
 */
#include "rdsquashfs.h"

static const char *inode_types[] = {
	[SQFS_INODE_DIR] = "directory",
	[SQFS_INODE_FILE] = "file",
	[SQFS_INODE_SLINK] = "symbolic link",
	[SQFS_INODE_BDEV] = "block device",
	[SQFS_INODE_CDEV] = "character device",
	[SQFS_INODE_FIFO] = "named pipe",
	[SQFS_INODE_SOCKET] = "socket",
	[SQFS_INODE_EXT_DIR] = "extended directory",
	[SQFS_INODE_EXT_FILE] = "extended file",
	[SQFS_INODE_EXT_SLINK] = "extended symbolic link",
	[SQFS_INODE_EXT_BDEV] = "extended block device",
	[SQFS_INODE_EXT_CDEV] = "extended character device",
	[SQFS_INODE_EXT_FIFO] = "extended named pipe",
	[SQFS_INODE_EXT_SOCKET] = "extended socket",
};

int stat_file(const sqfs_tree_node_t *node)
{
	sqfs_u32 xattr_idx = 0xFFFFFFFF, devno = 0, link_size = 0;
	const sqfs_inode_generic_t *inode = node->inode;
	const char *type = NULL, *link_target = NULL;
	sqfs_u32 frag_idx, frag_offset;
	bool have_devno = false;
	sqfs_u64 location, size;
	unsigned int nlinks = 0;
	sqfs_dir_index_t *idx;
	char buffer[64];
	time_t timeval;
	struct tm *tm;
	size_t i;
	int ret;

	/* decode */
	if ((size_t)inode->base.type <
	    sizeof(inode_types) / sizeof(inode_types[0])) {
		type = inode_types[inode->base.type];
	}

	sqfs_inode_get_xattr_index(inode, &xattr_idx);

	switch (inode->base.type) {
	case SQFS_INODE_DIR:
		nlinks = inode->data.dir.nlink;
		break;
	case SQFS_INODE_SLINK:
		nlinks = inode->data.slink.nlink;
		link_target = (const char *)inode->extra;
		link_size = inode->data.slink.target_size;
		break;
	case SQFS_INODE_BDEV:
	case SQFS_INODE_CDEV:
		nlinks = inode->data.dev.nlink;
		devno = inode->data.dev.devno;
		have_devno = true;
		break;
	case SQFS_INODE_FIFO:
	case SQFS_INODE_SOCKET:
		nlinks = inode->data.ipc.nlink;
		break;
	case SQFS_INODE_EXT_DIR:
		nlinks = inode->data.dir_ext.nlink;
		break;
	case SQFS_INODE_EXT_FILE:
		nlinks = inode->data.file_ext.nlink;
		break;
	case SQFS_INODE_EXT_SLINK:
		nlinks = inode->data.slink_ext.nlink;
		link_target = (const char *)inode->extra;
		link_size = inode->data.slink_ext.target_size;
		break;
	case SQFS_INODE_EXT_BDEV:
	case SQFS_INODE_EXT_CDEV:
		nlinks = inode->data.dev_ext.nlink;
		devno = inode->data.dev_ext.devno;
		have_devno = true;
		break;
	case SQFS_INODE_EXT_FIFO:
	case SQFS_INODE_EXT_SOCKET:
		nlinks = inode->data.ipc_ext.nlink;
		break;
	default:
		break;
	}

	timeval = inode->base.mod_time;
	tm = gmtime(&timeval);
	strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %z", tm);

	/* info dump */
	printf("Name: %s\n", (const char *)node->name);
	printf("Inode type: %s\n", type == NULL ? "UNKNOWN" : type);
	printf("Inode number: %u\n", inode->base.inode_number);
	printf("Access: 0%o\n",
	       (unsigned int)inode->base.mode & ~SQFS_INODE_MODE_MASK);
	printf("UID: %u (index = %u)\n", node->uid, inode->base.uid_idx);
	printf("GID: %u (index = %u)\n", node->gid, inode->base.gid_idx);
	printf("Last modified: %s (%u)\n", buffer, inode->base.mod_time);

	if (type != NULL && inode->base.type != SQFS_INODE_FILE)
		printf("Hard link count: %u\n", nlinks);

	if (type != NULL && inode->base.type >= SQFS_INODE_EXT_DIR)
		printf("Xattr index: 0x%X\n", xattr_idx);

	if (link_target != NULL)
		printf("Link target: %.*s\n", (int)link_size, link_target);

	if (have_devno) {
		printf("Device number: %u:%u (%u)\n",
		       major(devno), minor(devno), devno);
	}

	switch (inode->base.type) {
	case SQFS_INODE_FILE:
	case SQFS_INODE_EXT_FILE:
		sqfs_inode_get_file_block_start(inode, &location);
		sqfs_inode_get_file_size(inode, &size);
		sqfs_inode_get_frag_location(inode, &frag_idx, &frag_offset);

		printf("Fragment index: 0x%X\n", frag_idx);
		printf("Fragment offset: %u\n", frag_offset);
		printf("File size: %lu\n", (unsigned long)size);

		if (inode->base.type == SQFS_INODE_EXT_FILE)
			printf("Sparse: %lu\n", inode->data.file_ext.sparse);

		printf("Blocks start: %lu\n", (unsigned long)location);
		printf("Block count: %lu\n",
		       (unsigned long)sqfs_inode_get_file_block_count(inode));

		for (i = 0; i < sqfs_inode_get_file_block_count(inode); ++i) {
			printf("\tBlock #%lu size: %u (%s)\n", (unsigned long)i,
			       SQFS_ON_DISK_BLOCK_SIZE(inode->extra[i]),
			       SQFS_IS_BLOCK_COMPRESSED(inode->extra[i]) ?
			       "compressed" : "uncompressed");
		}
		break;
	case SQFS_INODE_DIR:
		printf("Start block: %u\n", inode->data.dir.start_block);
		printf("Offset: %u\n", inode->data.dir.offset);
		printf("Listing size: %u\n", inode->data.dir.size);
		printf("Parent inode: %u\n", inode->data.dir.parent_inode);
		break;
	case SQFS_INODE_EXT_DIR:
		printf("Start block: %u\n", inode->data.dir_ext.start_block);
		printf("Offset: %u\n", inode->data.dir_ext.offset);
		printf("Listing size: %u\n", inode->data.dir_ext.size);
		printf("Parent inode: %u\n", inode->data.dir_ext.parent_inode);
		printf("Directory index entries: %u\n",
		       inode->data.dir_ext.inodex_count);

		if (inode->data.dir_ext.size == 0)
			break;

		for (i = 0; ; ++i) {
			ret = sqfs_inode_unpack_dir_index_entry(inode, &idx, i);
			if (ret == SQFS_ERROR_OUT_OF_BOUNDS)
				break;
			if (ret < 0) {
				sqfs_perror(NULL, "reading directory index",
					    ret);
				return -1;
			}

			printf("\t'%.*s' -> block %u, header offset %u\n",
			       (int)(idx->size + 1), idx->name,
			       idx->start_block, idx->index);

			sqfs_free(idx);
		}
		break;
	default:
		break;
	}
	return 0;
}