-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
177 lines (155 loc) · 3.67 KB
/
main.c
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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2020 Bootlin
*
* Author: Joao Marcos Costa <[email protected]>
*
* main.c: parse the command line and redirect its arguments to
* its respective implemented function
*/
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#include "sqfs_filesystem.h"
#include "sqfs_utils.h"
#define SQFS_USAGE \
"usage: sqfs [-h]\n" \
" sqfs [-s] [-i] [-d] <fs-image>\n" \
" sqfs [-e] <fs-image> /path/to/dir/\n" \
" sqfs [-e] <fs-image> /path/to/file\n" \
"\n" \
"Tool to analyze the content of a SquashFS image\n" \
"\n" \
"Options:\n" \
" -h: Prints the usage and exits\n" \
" -s: Dumps the contents of a SquashFS image's superblock\n" \
" -i: Dumps the contents of a SquashFS image's inode table\n" \
" -d: Dumps the contents of a SquashFS image's directory table\n"\
" -e: Dumps the contents of a SquashFS image's"\
" file or directory.\n\t For directories, end path with '/'.\n"\
"\n" \
"Parameters:\n" \
" <fs-image>: Path to the filesystem image\n" \
"\n"
int main(int argc, char *argv[])
{
bool dump_sb = false, dump_inodes = false, dump_dir_table = false,
dump_entry = false;
char *fs_image = NULL;
void *file_mapping;
struct stat sb;
int opt, ret;
int fd;
/* Command line parsing */
while ((opt = getopt(argc, argv, "hside")) != -1) {
switch (opt) {
case 'h':
printf(SQFS_USAGE);
return EXIT_SUCCESS;
case 's':
dump_sb = true;
break;
case 'i':
dump_inodes = true;
break;
case 'd':
dump_dir_table = true;
break;
case 'e':
dump_entry = true;
break;
default:
break;
}
}
/*
* Incorrect argument number. For -e option (dump_entry): argc must be
* 3 or 4.
*/
if ((optind != (argc - 1)) && !dump_entry) {
printf(SQFS_USAGE);
return EXIT_FAILURE;
} else if (dump_entry) {
if (!(argc == 3 || argc == 4)) {
printf(SQFS_USAGE);
return EXIT_FAILURE;
}
}
fs_image = argv[optind];
fd = open(fs_image, O_RDONLY);
if (fd < 0) {
printf("No such file or directory\n");
return EXIT_FAILURE;
}
/* Memory mapping of SquashFS image */
fstat(fd, &sb);
file_mapping = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if (file_mapping == MAP_FAILED) {
fprintf(stderr, "Error: file could not be read\n");
return -errno;
}
/* Command execution */
if (dump_sb) {
ret = sqfs_dump_sblk(file_mapping);
if (ret) {
errno = ret;
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_FAILURE;
}
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_SUCCESS;
}
if (dump_inodes) {
ret = sqfs_dump_inode_table(file_mapping);
if (ret) {
errno = ret;
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_FAILURE;
}
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_SUCCESS;
}
if (dump_dir_table) {
ret = sqfs_dump_directory_table(file_mapping);
if (ret) {
errno = ret;
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_FAILURE;
}
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_SUCCESS;
}
if (dump_entry) {
/* If no path is given, presume it is intended to be root */
if (argc == 3)
ret = sqfs_dump_entry(file_mapping, "/");
else
ret = sqfs_dump_entry(file_mapping, argv[3]);
if (ret) {
errno = ret;
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_FAILURE;
}
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_SUCCESS;
}
/* Wrong command */
printf(SQFS_USAGE);
munmap(file_mapping, sb.st_size);
close(fd);
return EXIT_FAILURE;
}