summaryrefslogtreecommitdiff
path: root/unpack/unsquashfs.c
blob: f5c3a43883c55a8e4bde8840e1ac3a4d27af4b00 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
#include "squashfs.h"
#include "compress.h"
#include "id_table.h"

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>

extern const char *__progname;

int main(int argc, char **argv)
{
	int fd, status = EXIT_FAILURE;
	sqfs_super_t super;
	compressor_t *cmp;
	id_table_t idtbl;

	if (argc != 2) {
		fprintf(stderr, "Usage: %s <filename>\n", __progname);
		return EXIT_FAILURE;
	}

	fd = open(argv[1], O_RDONLY);
	if (fd < 0) {
		perror(argv[1]);
		return EXIT_FAILURE;
	}

	if (sqfs_super_read(&super, fd))
		goto out;

	if ((super.version_major != SQFS_VERSION_MAJOR) ||
	    (super.version_minor != SQFS_VERSION_MINOR)) {
		fprintf(stderr,
			"The image uses squashfs version %d.%d\n"
			"We currently only supports version %d.%d (sorry).\n",
			super.version_major, super.version_minor,
			SQFS_VERSION_MAJOR, SQFS_VERSION_MINOR);
		goto out;
	}

	if (super.flags & SQFS_FLAG_COMPRESSOR_OPTIONS) {
		fputs("Image has been built with compressor options.\n"
		      "This is not yet supported.\n",
		      stderr);
		goto out;
	}

	if (!compressor_exists(super.compression_id)) {
		fputs("Image uses a compressor that has not been built in\n",
		      stderr);
		goto out;
	}

	cmp = compressor_create(super.compression_id, false, super.block_size);
	if (cmp == NULL)
		goto out;

	if (id_table_init(&idtbl))
		goto out_cmp;

	if (id_table_read(&idtbl, fd, &super, cmp))
		goto out_idtbl;

	status = EXIT_SUCCESS;
out_idtbl:
	id_table_cleanup(&idtbl);
out_cmp:
	cmp->destroy(cmp);
out:
	close(fd);
	return status;
}