aboutsummaryrefslogtreecommitdiff
path: root/extras/list_files.c
blob: 55b94e2f2e50729bf48b21e672c9240ab6c40bc8 (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
/* SPDX-License-Identifier: 0BSD */
/*
 * list_files.c
 *
 * Copyright (C) 2020 David Oberhollenzer <goliath@infraroot.at>
 */
#include "sqfs/compressor.h"
#include "sqfs/dir_reader.h"
#include "sqfs/dir_entry.h"
#include "sqfs/id_table.h"
#include "sqfs/inode.h"
#include "sqfs/super.h"
#include "sqfs/io.h"

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

static int count_entries(sqfs_dir_iterator_t *it, unsigned int *out)
{
	sqfs_dir_entry_t *ent;
	int ret;

	*out = 0;

	for (;;) {
		ret = it->next(it, &ent);
		if (ret < 0) {
			*out = 0;
			return -1;
		}
		if (ret > 0)
			break;

		*out += 1;
		free(ent);
	}

	return 0;
}

static void write_tree_dfs(sqfs_dir_iterator_t *it,
			   unsigned int mask, unsigned int level,
			   unsigned int count)
{
	sqfs_dir_entry_t *ent;
	unsigned int i;
	sqfs_u16 type;

	while (count--) {
		for (i = 0; i < level; ++i)
			fputs(mask & (1 << i) ? "│  " : "   ", stdout);

		if (it->next(it, &ent) != 0)
			break;

		fputs(count == 0 ? "└─ " : "├─ ", stdout);
		fputs(ent->name, stdout);

		type = ent->mode & SQFS_INODE_MODE_MASK;
		free(ent);

		if (type == SQFS_INODE_MODE_LNK) {
			char *target;

			if (it->read_link(it, &target) == 0)
				printf(" ⭢ %s", target);

			free(target);
		}

		fputc('\n', stdout);

		if (type == SQFS_INODE_MODE_DIR) {
			sqfs_dir_iterator_t *sub;
			unsigned int sub_count;

			it->open_subdir(it, &sub);
			count_entries(sub, &sub_count);
			sub = sqfs_drop(sub);

			it->open_subdir(it, &sub);
			write_tree_dfs(sub,
				       mask | (count > 0 ? (1 << level) : 0),
				       level + 1, sub_count);
			sqfs_drop(sub);
		}
	}
}

static sqfs_dir_iterator_t *create_root_iterator(sqfs_dir_reader_t *dr,
						 sqfs_id_table_t *idtbl,
						 const char *filename)
{
	sqfs_inode_generic_t *iroot;
	sqfs_dir_iterator_t *it;
	int ret;

	if (sqfs_dir_reader_get_root_inode(dr, &iroot)) {
		fprintf(stderr, "%s: error reading root inode.\n",
			filename);
		return NULL;
	}

	ret = sqfs_dir_iterator_create(dr, idtbl, NULL, NULL, iroot, &it);
	free(iroot);

	if (ret) {
		fprintf(stderr, "%s: error creating root iterator.\n",
			filename);
		return NULL;
	}

	return it;
}

int main(int argc, char **argv)
{
	int ret, status = EXIT_FAILURE;
	sqfs_dir_iterator_t *it = NULL;
	sqfs_compressor_t *cmp = NULL;
	sqfs_id_table_t *idtbl = NULL;
	sqfs_dir_reader_t *dr = NULL;
	sqfs_compressor_config_t cfg;
	sqfs_file_t *file = NULL;
	unsigned int root_count;
	sqfs_super_t super;

	/* open the SquashFS file we want to read */
	if (argc != 2) {
		fputs("Usage: list_files <squashfs-file>\n", stderr);
		return EXIT_FAILURE;
	}

	if (sqfs_file_open(&file, argv[1], SQFS_FILE_OPEN_READ_ONLY)) {
		fprintf(stderr, "%s: error opening file.\n", argv[1]);
		return EXIT_FAILURE;
	}

	/* read the super block, create a compressor and
	   process the compressor options */
	if (sqfs_super_read(&super, file)) {
		fprintf(stderr, "%s: error reading super block.\n", argv[1]);
		goto out;
	}

	sqfs_compressor_config_init(&cfg, super.compression_id,
				    super.block_size,
				    SQFS_COMP_FLAG_UNCOMPRESS);

	ret = sqfs_compressor_create(&cfg, &cmp);
	if (ret != 0) {
		fprintf(stderr, "%s: error creating compressor: %d.\n",
			argv[1], ret);
		goto out;
	}

	/* Create and read the UID/GID mapping table */
	idtbl = sqfs_id_table_create(0);
	if (idtbl == NULL) {
		fputs("Error creating ID table.\n", stderr);
		goto out;
	}

	if (sqfs_id_table_read(idtbl, file, &super, cmp)) {
		fprintf(stderr, "%s: error loading ID table.\n", argv[1]);
		goto out;
	}

	/* create a directory reader */
	dr = sqfs_dir_reader_create(&super, cmp, file, 0);
	if (dr == NULL) {
		fprintf(stderr, "%s: error creating directory reader.\n",
			argv[1]);
		goto out;
	}

	/* create a directory iterator for the root inode */
	it = create_root_iterator(dr, idtbl, argv[1]);
	if (it == NULL)
		goto out;

	if (count_entries(it, &root_count)) {
		fprintf(stderr, "%s: error counting root iterator entries.\n",
			argv[1]);
		goto out;
	}

	it = sqfs_drop(it);

	it = create_root_iterator(dr, idtbl, argv[1]);
	if (it == NULL)
		goto out;

	/* fancy print the hierarchy */
	printf("/\n");
	write_tree_dfs(it, 0, 0, root_count);

	/* cleanup */
	status = EXIT_SUCCESS;
out:
	sqfs_drop(it);
	sqfs_drop(dr);
	sqfs_drop(idtbl);
	sqfs_drop(cmp);
	sqfs_drop(file);
	return status;
}