]>
Commit | Line | Data |
---|---|---|
63cccae4 KZ |
1 | /* |
2 | * cramfsck - check a cramfs file system | |
3 | * | |
19922f22 KZ |
4 | * Copyright (C) 2000-2002 Transmeta Corporation |
5 | * 2005 Adrian Bunk | |
63cccae4 KZ |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
7cebf0bb SK |
17 | * You should have received a copy of the GNU General Public License along |
18 | * with this program; if not, write to the Free Software Foundation, Inc., | |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
63cccae4 KZ |
20 | * |
21 | * 1999/12/03: Linus Torvalds (cramfs tester and unarchive program) | |
22 | * 2000/06/03: Daniel Quinlan (CRC and length checking program) | |
23 | * 2000/06/04: Daniel Quinlan (merged programs, added options, support | |
24 | * for special files, preserve permissions and | |
25 | * ownership, cramfs superblock v2, bogus mode | |
26 | * test, pathname length test, etc.) | |
27 | * 2000/06/06: Daniel Quinlan (support for holes, pretty-printing, | |
28 | * symlink size test) | |
29 | * 2000/07/11: Daniel Quinlan (file length tests, start at offset 0 or 512, | |
30 | * fsck-compatible exit codes) | |
31 | * 2000/07/15: Daniel Quinlan (initial support for block devices) | |
19922f22 KZ |
32 | * 2002/01/10: Daniel Quinlan (additional checks, test more return codes, |
33 | * use read if mmap fails, standardize messages) | |
63cccae4 KZ |
34 | */ |
35 | ||
63cccae4 | 36 | #include <stdio.h> |
19922f22 | 37 | #include <stdarg.h> |
d51f37a3 | 38 | #include <stdint.h> |
63cccae4 KZ |
39 | #include <unistd.h> |
40 | #include <dirent.h> | |
41 | #include <stdlib.h> | |
42 | #include <errno.h> | |
43 | #include <string.h> | |
44 | #include <getopt.h> | |
45 | #include <utime.h> | |
46 | #include <fcntl.h> | |
2687686c KZ |
47 | |
48 | /* We don't use our include/crc32.h, but crc32 from zlib! | |
49 | * | |
50 | * The zlib implemenation performs pre/post-conditioning. The util-linux | |
51 | * imlemenation requires post-conditioning (xor) in the applications. | |
52 | */ | |
63cccae4 KZ |
53 | #include <zlib.h> |
54 | ||
55 | #include <sys/types.h> | |
56 | #include <sys/stat.h> | |
57 | #include <sys/mman.h> | |
63cccae4 | 58 | |
075d2c07 | 59 | #include "c.h" |
63cccae4 | 60 | #include "cramfs.h" |
63cccae4 | 61 | #include "nls.h" |
098fa6b1 | 62 | #include "blkdev.h" |
5d6fb944 | 63 | #include "exitcodes.h" |
5cd50ebc | 64 | #include "strutils.h" |
45ca68ec | 65 | #include "closestream.h" |
63cccae4 | 66 | |
70b604b8 | 67 | #define XALLOC_EXIT_CODE FSCK_EX_ERROR |
fc8dc410 SK |
68 | #include "xalloc.h" |
69 | ||
63cccae4 KZ |
70 | static int fd; /* ROM image file descriptor */ |
71 | static char *filename; /* ROM image filename */ | |
2ba641e5 | 72 | static struct cramfs_super super; /* just find the cramfs superblock once */ |
fbaec83b | 73 | static int cramfs_is_big_endian = 0; /* source is big endian */ |
63cccae4 | 74 | static int opt_verbose = 0; /* 1 = verbose (-v), 2+ = very verbose (-vv) */ |
34731f89 | 75 | static int opt_extract = 0; /* extract cramfs (-x) */ |
2ba641e5 | 76 | static char *extract_dir = ""; /* optional extraction directory (-x) */ |
63cccae4 | 77 | |
63cccae4 | 78 | #define PAD_SIZE 512 |
b22550fa | 79 | |
6ffd9b03 | 80 | static uid_t euid; /* effective UID */ |
19922f22 KZ |
81 | |
82 | /* (cramfs_super + start) <= start_dir < end_dir <= start_data <= end_data */ | |
6ffd9b03 SK |
83 | static unsigned long start_dir = ~0UL; /* start of first non-root inode */ |
84 | static unsigned long end_dir = 0; /* end of the directory structure */ | |
85 | static unsigned long start_data = ~0UL; /* start of the data (256 MB = max) */ | |
86 | static unsigned long end_data = 0; /* end of the data */ | |
19922f22 | 87 | |
63cccae4 KZ |
88 | |
89 | /* Guarantee access to at least 8kB at a time */ | |
90 | #define ROMBUFFER_BITS 13 | |
91 | #define ROMBUFFERSIZE (1 << ROMBUFFER_BITS) | |
6ffd9b03 | 92 | #define ROMBUFFERMASK (ROMBUFFERSIZE - 1) |
63cccae4 KZ |
93 | static char read_buffer[ROMBUFFERSIZE * 2]; |
94 | static unsigned long read_buffer_block = ~0UL; | |
95 | ||
19922f22 | 96 | static z_stream stream; |
63cccae4 | 97 | |
19922f22 KZ |
98 | /* Prototypes */ |
99 | static void expand_fs(char *, struct cramfs_inode *); | |
63cccae4 | 100 | |
19922f22 KZ |
101 | static char *outbuffer; |
102 | ||
5cd50ebc | 103 | static size_t blksize = 0; |
19922f22 | 104 | |
b7a75206 | 105 | |
63cccae4 | 106 | /* Input status of 0 to print help and exit without an error. */ |
e4a36b8b | 107 | static void __attribute__((__noreturn__)) usage(int status) |
63cccae4 | 108 | { |
7ee26cbf | 109 | FILE *out = status ? stderr : stdout; |
63cccae4 | 110 | |
7ee26cbf SK |
111 | fputs(USAGE_HEADER, out); |
112 | fprintf(out, | |
09af3db4 | 113 | _(" %s [options] <file>\n"), program_invocation_short_name); |
451dbcfa | 114 | |
7ee26cbf SK |
115 | fputs(USAGE_SEPARATOR, out); |
116 | fputs(_("Check and repair a compressed ROM filesystem.\n"), out); | |
117 | ||
118 | fputs(USAGE_OPTIONS, out); | |
119 | fputs(_(" -a for compatibility only, ignored\n"), out); | |
120 | fputs(_(" -v, --verbose be more verbose\n"), out); | |
121 | fputs(_(" -y for compatibility only, ignored\n"), out); | |
122 | fputs(_(" -b, --blocksize <size> use this blocksize, defaults to page size\n"), out); | |
123 | fputs(_(" --extract[=<dir>] test uncompression, optionally extract into <dir>\n"), out); | |
124 | fputs(USAGE_SEPARATOR, out); | |
125 | fputs(USAGE_HELP, out); | |
126 | fputs(USAGE_VERSION, out); | |
127 | fputs(USAGE_SEPARATOR, out); | |
128 | ||
63cccae4 KZ |
129 | exit(status); |
130 | } | |
131 | ||
d63eb75e | 132 | static int get_superblock_endianness(uint32_t magic) |
fbaec83b SRP |
133 | { |
134 | if (magic == CRAMFS_MAGIC) { | |
135 | cramfs_is_big_endian = HOST_IS_BIG_ENDIAN; | |
136 | return 0; | |
6ffd9b03 SK |
137 | } else if (magic == |
138 | u32_toggle_endianness(!HOST_IS_BIG_ENDIAN, CRAMFS_MAGIC)) { | |
fbaec83b SRP |
139 | cramfs_is_big_endian = !HOST_IS_BIG_ENDIAN; |
140 | return 0; | |
6ffd9b03 | 141 | } else |
fbaec83b | 142 | return -1; |
fbaec83b SRP |
143 | } |
144 | ||
6ffd9b03 SK |
145 | static void test_super(int *start, size_t * length) |
146 | { | |
19922f22 KZ |
147 | struct stat st; |
148 | ||
149 | /* find the physical size of the file or block device */ | |
6ffd9b03 | 150 | if (stat(filename, &st) < 0) |
fc14ceba | 151 | err(FSCK_EX_ERROR, _("stat of %s failed"), filename); |
6ffd9b03 | 152 | |
19922f22 | 153 | fd = open(filename, O_RDONLY); |
6ffd9b03 | 154 | if (fd < 0) |
289dcc90 | 155 | err(FSCK_EX_ERROR, _("cannot open %s"), filename); |
6ffd9b03 | 156 | |
19922f22 | 157 | if (S_ISBLK(st.st_mode)) { |
098fa6b1 | 158 | unsigned long long bytes; |
6ffd9b03 | 159 | if (blkdev_get_size(fd, &bytes)) |
70b604b8 | 160 | err(FSCK_EX_ERROR, |
6ffd9b03 SK |
161 | _("ioctl failed: unable to determine device size: %s"), |
162 | filename); | |
098fa6b1 | 163 | *length = bytes; |
6ffd9b03 | 164 | } else if (S_ISREG(st.st_mode)) |
19922f22 | 165 | *length = st.st_size; |
6ffd9b03 | 166 | else |
70b604b8 | 167 | errx(FSCK_EX_ERROR, _("not a block device or file: %s"), filename); |
19922f22 | 168 | |
6ffd9b03 | 169 | if (*length < sizeof(struct cramfs_super)) |
70b604b8 | 170 | errx(FSCK_EX_UNCORRECTED, _("file length too short")); |
19922f22 KZ |
171 | |
172 | /* find superblock */ | |
6ffd9b03 | 173 | if (read(fd, &super, sizeof(super)) != sizeof(super)) |
47481cbd | 174 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
6ffd9b03 | 175 | if (get_superblock_endianness(super.magic) != -1) |
19922f22 | 176 | *start = 0; |
19922f22 | 177 | else if (*length >= (PAD_SIZE + sizeof(super))) { |
b3f30f50 | 178 | if (lseek(fd, PAD_SIZE, SEEK_SET) == (off_t) -1) |
47481cbd | 179 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); |
6ffd9b03 | 180 | if (read(fd, &super, sizeof(super)) != sizeof(super)) |
47481cbd | 181 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
6ffd9b03 | 182 | if (get_superblock_endianness(super.magic) != -1) |
19922f22 | 183 | *start = PAD_SIZE; |
6ffd9b03 | 184 | else |
70b604b8 | 185 | errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); |
6ffd9b03 | 186 | } else |
70b604b8 | 187 | errx(FSCK_EX_UNCORRECTED, _("superblock magic not found")); |
fbaec83b | 188 | |
6ffd9b03 | 189 | if (opt_verbose) |
6de1396e SK |
190 | printf(_("cramfs endianness is %s\n"), |
191 | cramfs_is_big_endian ? _("big") : _("little")); | |
fbaec83b SRP |
192 | |
193 | super_toggle_endianness(cramfs_is_big_endian, &super); | |
6ffd9b03 | 194 | if (super.flags & ~CRAMFS_SUPPORTED_FLAGS) |
70b604b8 | 195 | errx(FSCK_EX_ERROR, _("unsupported filesystem features")); |
6ffd9b03 | 196 | |
f991dbd3 RM |
197 | /* What are valid superblock sizes? */ |
198 | if (super.size < sizeof(struct cramfs_super)) | |
70b604b8 | 199 | errx(FSCK_EX_UNCORRECTED, _("superblock size (%d) too small"), |
6ffd9b03 SK |
200 | super.size); |
201 | ||
19922f22 | 202 | if (super.flags & CRAMFS_FLAG_FSID_VERSION_2) { |
6ffd9b03 | 203 | if (super.fsid.files == 0) |
70b604b8 | 204 | errx(FSCK_EX_UNCORRECTED, _("zero file count")); |
6ffd9b03 | 205 | if (*length < super.size) |
70b604b8 | 206 | errx(FSCK_EX_UNCORRECTED, _("file length too short")); |
6ffd9b03 | 207 | else if (*length > super.size) |
deedae5f | 208 | warnx(_("file extends past end of filesystem")); |
6ffd9b03 | 209 | } else |
deedae5f | 210 | warnx(_("old cramfs format")); |
19922f22 KZ |
211 | } |
212 | ||
213 | static void test_crc(int start) | |
214 | { | |
215 | void *buf; | |
d51f37a3 | 216 | uint32_t crc; |
19922f22 KZ |
217 | |
218 | if (!(super.flags & CRAMFS_FLAG_FSID_VERSION_2)) { | |
deedae5f | 219 | warnx(_("unable to test CRC: old cramfs format")); |
19922f22 | 220 | return; |
19922f22 KZ |
221 | } |
222 | ||
87918040 | 223 | crc = crc32(0L, NULL, 0); |
19922f22 | 224 | |
6ffd9b03 SK |
225 | buf = |
226 | mmap(NULL, super.size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); | |
19922f22 | 227 | if (buf == MAP_FAILED) { |
6ffd9b03 SK |
228 | buf = |
229 | mmap(NULL, super.size, PROT_READ | PROT_WRITE, | |
230 | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); | |
19922f22 | 231 | if (buf != MAP_FAILED) { |
b3f30f50 | 232 | if (lseek(fd, 0, SEEK_SET) == (off_t) -1) |
47481cbd | 233 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); |
10c66b0b | 234 | if (read(fd, buf, super.size) != (ssize_t) super.size) |
47481cbd | 235 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
19922f22 KZ |
236 | } |
237 | } | |
238 | if (buf != MAP_FAILED) { | |
aa719998 | 239 | ((struct cramfs_super *)((unsigned char *) buf + start))->fsid.crc = |
87918040 | 240 | crc32(0L, NULL, 0); |
2687686c | 241 | crc = crc32(crc, (unsigned char *) buf + start, super.size - start); |
19922f22 | 242 | munmap(buf, super.size); |
6ffd9b03 | 243 | } else { |
19922f22 KZ |
244 | int retval; |
245 | size_t length = 0; | |
246 | ||
fc8dc410 | 247 | buf = xmalloc(4096); |
b3f30f50 | 248 | if (lseek(fd, start, SEEK_SET) == (off_t) -1) |
47481cbd | 249 | err(FSCK_EX_ERROR, _("seek on %s failed"), filename); |
19922f22 KZ |
250 | for (;;) { |
251 | retval = read(fd, buf, 4096); | |
6ffd9b03 | 252 | if (retval < 0) |
47481cbd | 253 | err(FSCK_EX_ERROR, _("cannot read %s"), filename); |
6ffd9b03 | 254 | else if (retval == 0) |
19922f22 | 255 | break; |
6ffd9b03 SK |
256 | if (length == 0) |
257 | ((struct cramfs_super *)buf)->fsid.crc = | |
87918040 | 258 | crc32(0L, NULL, 0); |
19922f22 | 259 | length += retval; |
6ffd9b03 | 260 | if (length > (super.size - start)) { |
2687686c | 261 | crc = crc32(crc, buf, |
6ffd9b03 SK |
262 | retval - (length - |
263 | (super.size - start))); | |
19922f22 KZ |
264 | break; |
265 | } | |
2687686c | 266 | crc = crc32(crc, buf, retval); |
19922f22 KZ |
267 | } |
268 | free(buf); | |
269 | } | |
270 | ||
6ffd9b03 | 271 | if (crc != super.fsid.crc) |
70b604b8 | 272 | errx(FSCK_EX_UNCORRECTED, _("crc error")); |
19922f22 KZ |
273 | } |
274 | ||
63cccae4 KZ |
275 | static void print_node(char type, struct cramfs_inode *i, char *name) |
276 | { | |
277 | char info[10]; | |
278 | ||
6ffd9b03 | 279 | if (S_ISCHR(i->mode) || (S_ISBLK(i->mode))) |
63cccae4 KZ |
280 | /* major/minor numbers can be as high as 2^12 or 4096 */ |
281 | snprintf(info, 10, "%4d,%4d", major(i->size), minor(i->size)); | |
6ffd9b03 | 282 | else |
63cccae4 KZ |
283 | /* size be as high as 2^24 or 16777216 */ |
284 | snprintf(info, 10, "%9d", i->size); | |
63cccae4 KZ |
285 | |
286 | printf("%c %04o %s %5d:%-3d %s\n", | |
a6f72ab4 KZ |
287 | type, i->mode & ~S_IFMT, info, i->uid, i->gid, |
288 | !*name && type == 'd' ? "/" : name); | |
63cccae4 KZ |
289 | } |
290 | ||
291 | /* | |
292 | * Create a fake "blocked" access | |
293 | */ | |
294 | static void *romfs_read(unsigned long offset) | |
295 | { | |
296 | unsigned int block = offset >> ROMBUFFER_BITS; | |
297 | if (block != read_buffer_block) { | |
52848780 KZ |
298 | ssize_t x; |
299 | ||
63cccae4 | 300 | read_buffer_block = block; |
b3f30f50 KZ |
301 | if (lseek(fd, block << ROMBUFFER_BITS, SEEK_SET) == (off_t) -1) |
302 | warn(_("seek failed")); | |
52848780 KZ |
303 | |
304 | x = read(fd, read_buffer, ROMBUFFERSIZE * 2); | |
305 | if (x < 0) | |
306 | warn(_("read romfs failed")); | |
63cccae4 KZ |
307 | } |
308 | return read_buffer + (offset & ROMBUFFERMASK); | |
309 | } | |
310 | ||
6ffd9b03 | 311 | static struct cramfs_inode *cramfs_iget(struct cramfs_inode *i) |
63cccae4 | 312 | { |
fc8dc410 | 313 | struct cramfs_inode *inode = xmalloc(sizeof(struct cramfs_inode)); |
19922f22 | 314 | |
fbaec83b | 315 | inode_to_host(cramfs_is_big_endian, i, inode); |
63cccae4 KZ |
316 | return inode; |
317 | } | |
318 | ||
319 | static struct cramfs_inode *iget(unsigned int ino) | |
320 | { | |
321 | return cramfs_iget(romfs_read(ino)); | |
322 | } | |
323 | ||
63cccae4 KZ |
324 | static void iput(struct cramfs_inode *inode) |
325 | { | |
326 | free(inode); | |
327 | } | |
63cccae4 KZ |
328 | |
329 | /* | |
19922f22 | 330 | * Return the offset of the root directory |
63cccae4 KZ |
331 | */ |
332 | static struct cramfs_inode *read_super(void) | |
333 | { | |
6ffd9b03 | 334 | struct cramfs_inode *root = cramfs_iget(&super.root); |
fbaec83b | 335 | unsigned long offset = root->offset << 2; |
19922f22 | 336 | |
fbaec83b | 337 | if (!S_ISDIR(root->mode)) |
70b604b8 | 338 | errx(FSCK_EX_UNCORRECTED, _("root inode is not directory")); |
19922f22 KZ |
339 | if (!(super.flags & CRAMFS_FLAG_SHIFTED_ROOT_OFFSET) && |
340 | ((offset != sizeof(struct cramfs_super)) && | |
6ffd9b03 | 341 | (offset != PAD_SIZE + sizeof(struct cramfs_super)))) { |
70b604b8 | 342 | errx(FSCK_EX_UNCORRECTED, _("bad root offset (%lu)"), offset); |
19922f22 | 343 | } |
fbaec83b | 344 | return root; |
63cccae4 KZ |
345 | } |
346 | ||
52848780 | 347 | static int uncompress_block(void *src, size_t len) |
63cccae4 KZ |
348 | { |
349 | int err; | |
350 | ||
351 | stream.next_in = src; | |
352 | stream.avail_in = len; | |
353 | ||
6ffd9b03 | 354 | stream.next_out = (unsigned char *)outbuffer; |
38141baf | 355 | stream.avail_out = blksize * 2; |
63cccae4 KZ |
356 | |
357 | inflateReset(&stream); | |
358 | ||
38141baf | 359 | if (len > blksize * 2) |
70b604b8 | 360 | errx(FSCK_EX_UNCORRECTED, _("data block too large")); |
6ffd9b03 | 361 | |
63cccae4 | 362 | err = inflate(&stream, Z_FINISH); |
6ffd9b03 | 363 | if (err != Z_STREAM_END) |
52848780 KZ |
364 | errx(FSCK_EX_UNCORRECTED, _("decompression error: %s"), |
365 | zError(err)); | |
63cccae4 KZ |
366 | return stream.total_out; |
367 | } | |
368 | ||
48d7b13a KZ |
369 | #if !HAVE_LCHOWN |
370 | #define lchown chown | |
63cccae4 | 371 | #endif |
6ffd9b03 | 372 | |
7ee26cbf | 373 | static void do_uncompress(char *path, int outfd, unsigned long offset, |
6ffd9b03 | 374 | unsigned long size) |
19922f22 | 375 | { |
38141baf | 376 | unsigned long curr = offset + 4 * ((size + blksize - 1) / blksize); |
19922f22 KZ |
377 | |
378 | do { | |
38141baf | 379 | unsigned long out = blksize; |
6ffd9b03 SK |
380 | unsigned long next = u32_toggle_endianness(cramfs_is_big_endian, |
381 | *(uint32_t *) | |
382 | romfs_read(offset)); | |
19922f22 | 383 | |
6ffd9b03 | 384 | if (next > end_data) |
19922f22 | 385 | end_data = next; |
19922f22 KZ |
386 | |
387 | offset += 4; | |
388 | if (curr == next) { | |
6ffd9b03 | 389 | if (opt_verbose > 1) |
e3ca1312 | 390 | printf(_(" hole at %lu (%zu)\n"), curr, |
38141baf RM |
391 | blksize); |
392 | if (size < blksize) | |
19922f22 KZ |
393 | out = size; |
394 | memset(outbuffer, 0x00, out); | |
6ffd9b03 SK |
395 | } else { |
396 | if (opt_verbose > 1) | |
e3ca1312 | 397 | printf(_(" uncompressing block at %lu to %lu (%lu)\n"), |
6ffd9b03 | 398 | curr, next, next - curr); |
19922f22 KZ |
399 | out = uncompress_block(romfs_read(curr), next - curr); |
400 | } | |
38141baf RM |
401 | if (size >= blksize) { |
402 | if (out != blksize) | |
70b604b8 | 403 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 | 404 | _("non-block (%ld) bytes"), out); |
19922f22 | 405 | } else { |
6ffd9b03 | 406 | if (out != size) |
70b604b8 | 407 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
408 | _("non-size (%ld vs %ld) bytes"), out, |
409 | size); | |
19922f22 KZ |
410 | } |
411 | size -= out; | |
74ce680a SK |
412 | if (*extract_dir != '\0' && write(outfd, outbuffer, out) < 0) |
413 | err(FSCK_EX_ERROR, _("write failed: %s"), path); | |
19922f22 KZ |
414 | curr = next; |
415 | } while (size); | |
416 | } | |
63cccae4 KZ |
417 | |
418 | static void change_file_status(char *path, struct cramfs_inode *i) | |
419 | { | |
420 | struct utimbuf epoch = { 0, 0 }; | |
421 | ||
422 | if (euid == 0) { | |
6ffd9b03 | 423 | if (lchown(path, i->uid, i->gid) < 0) |
70b604b8 | 424 | err(FSCK_EX_ERROR, _("lchown failed: %s"), path); |
63cccae4 KZ |
425 | if (S_ISLNK(i->mode)) |
426 | return; | |
6ffd9b03 | 427 | if (((S_ISUID | S_ISGID) & i->mode) && chmod(path, i->mode) < 0) |
70b604b8 | 428 | err(FSCK_EX_ERROR, _("chown failed: %s"), path); |
63cccae4 KZ |
429 | } |
430 | if (S_ISLNK(i->mode)) | |
431 | return; | |
6ffd9b03 | 432 | if (utime(path, &epoch) < 0) |
70b604b8 | 433 | err(FSCK_EX_ERROR, _("utime failed: %s"), path); |
19922f22 KZ |
434 | } |
435 | ||
436 | static void do_directory(char *path, struct cramfs_inode *i) | |
437 | { | |
438 | int pathlen = strlen(path); | |
439 | int count = i->size; | |
440 | unsigned long offset = i->offset << 2; | |
fc8dc410 | 441 | char *newpath = xmalloc(pathlen + 256); |
19922f22 | 442 | |
6ffd9b03 | 443 | if (offset == 0 && count != 0) |
70b604b8 | 444 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
445 | _("directory inode has zero offset and non-zero size: %s"), |
446 | path); | |
447 | ||
448 | if (offset != 0 && offset < start_dir) | |
19922f22 | 449 | start_dir = offset; |
6ffd9b03 | 450 | |
19922f22 KZ |
451 | /* TODO: Do we need to check end_dir for empty case? */ |
452 | memcpy(newpath, path, pathlen); | |
453 | newpath[pathlen] = '/'; | |
454 | pathlen++; | |
6ffd9b03 | 455 | if (opt_verbose) |
19922f22 | 456 | print_node('d', i, path); |
6ffd9b03 | 457 | |
2d317f11 | 458 | if (*extract_dir != '\0') { |
6ffd9b03 | 459 | if (mkdir(path, i->mode) < 0) |
70b604b8 | 460 | err(FSCK_EX_ERROR, _("mkdir failed: %s"), path); |
19922f22 KZ |
461 | change_file_status(path, i); |
462 | } | |
463 | while (count > 0) { | |
464 | struct cramfs_inode *child = iget(offset); | |
465 | int size; | |
466 | int newlen = child->namelen << 2; | |
467 | ||
468 | size = sizeof(struct cramfs_inode) + newlen; | |
469 | count -= size; | |
470 | ||
471 | offset += sizeof(struct cramfs_inode); | |
472 | ||
473 | memcpy(newpath + pathlen, romfs_read(offset), newlen); | |
474 | newpath[pathlen + newlen] = 0; | |
6ffd9b03 | 475 | if (newlen == 0) |
70b604b8 | 476 | errx(FSCK_EX_UNCORRECTED, _("filename length is zero")); |
6ffd9b03 | 477 | if ((pathlen + newlen) - strlen(newpath) > 3) |
70b604b8 | 478 | errx(FSCK_EX_UNCORRECTED, _("bad filename length")); |
19922f22 KZ |
479 | expand_fs(newpath, child); |
480 | ||
481 | offset += newlen; | |
482 | ||
6ffd9b03 | 483 | if (offset <= start_dir) |
70b604b8 | 484 | errx(FSCK_EX_UNCORRECTED, _("bad inode offset")); |
6ffd9b03 | 485 | if (offset > end_dir) |
19922f22 | 486 | end_dir = offset; |
6ffd9b03 | 487 | iput(child); /* free(child) */ |
19922f22 KZ |
488 | } |
489 | free(newpath); | |
490 | } | |
491 | ||
492 | static void do_file(char *path, struct cramfs_inode *i) | |
493 | { | |
494 | unsigned long offset = i->offset << 2; | |
7ee26cbf | 495 | int outfd = 0; |
19922f22 | 496 | |
6ffd9b03 | 497 | if (offset == 0 && i->size != 0) |
70b604b8 | 498 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
499 | _("file inode has zero offset and non-zero size")); |
500 | if (i->size == 0 && offset != 0) | |
70b604b8 | 501 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
502 | _("file inode has zero size and non-zero offset")); |
503 | if (offset != 0 && offset < start_data) | |
19922f22 | 504 | start_data = offset; |
6ffd9b03 | 505 | if (opt_verbose) |
19922f22 | 506 | print_node('f', i, path); |
2d317f11 | 507 | if (*extract_dir != '\0') { |
7ee26cbf SK |
508 | outfd = open(path, O_WRONLY | O_CREAT | O_TRUNC, i->mode); |
509 | if (outfd < 0) | |
289dcc90 | 510 | err(FSCK_EX_ERROR, _("cannot open %s"), path); |
19922f22 | 511 | } |
6ffd9b03 | 512 | if (i->size) |
7ee26cbf | 513 | do_uncompress(path, outfd, offset, i->size); |
2d317f11 | 514 | if ( *extract_dir != '\0') { |
7ee26cbf | 515 | if (close_fd(outfd) != 0) |
83f210e8 | 516 | err(FSCK_EX_ERROR, _("write failed: %s"), path); |
19922f22 | 517 | change_file_status(path, i); |
63cccae4 KZ |
518 | } |
519 | } | |
520 | ||
521 | static void do_symlink(char *path, struct cramfs_inode *i) | |
522 | { | |
523 | unsigned long offset = i->offset << 2; | |
524 | unsigned long curr = offset + 4; | |
6ffd9b03 SK |
525 | unsigned long next = |
526 | u32_toggle_endianness(cramfs_is_big_endian, | |
527 | *(uint32_t *) romfs_read(offset)); | |
63cccae4 KZ |
528 | unsigned long size; |
529 | ||
6ffd9b03 | 530 | if (offset == 0) |
70b604b8 | 531 | errx(FSCK_EX_UNCORRECTED, _("symbolic link has zero offset")); |
6ffd9b03 | 532 | if (i->size == 0) |
70b604b8 | 533 | errx(FSCK_EX_UNCORRECTED, _("symbolic link has zero size")); |
19922f22 | 534 | |
6ffd9b03 | 535 | if (offset < start_data) |
19922f22 | 536 | start_data = offset; |
6ffd9b03 | 537 | if (next > end_data) |
63cccae4 | 538 | end_data = next; |
63cccae4 KZ |
539 | |
540 | size = uncompress_block(romfs_read(curr), next - curr); | |
6ffd9b03 | 541 | if (size != i->size) |
70b604b8 | 542 | errx(FSCK_EX_UNCORRECTED, _("size error in symlink: %s"), path); |
63cccae4 KZ |
543 | outbuffer[size] = 0; |
544 | if (opt_verbose) { | |
545 | char *str; | |
546 | ||
6f312c89 | 547 | xasprintf(&str, "%s -> %s", path, outbuffer); |
63cccae4 | 548 | print_node('l', i, str); |
6ffd9b03 | 549 | if (opt_verbose > 1) |
e3ca1312 | 550 | printf(_(" uncompressing block at %lu to %lu (%lu)\n"), |
6ffd9b03 | 551 | curr, next, next - curr); |
19922f22 | 552 | free(str); |
63cccae4 | 553 | } |
2d317f11 | 554 | if (*extract_dir != '\0') { |
6ffd9b03 | 555 | if (symlink(outbuffer, path) < 0) |
70b604b8 | 556 | err(FSCK_EX_ERROR, _("symlink failed: %s"), path); |
63cccae4 KZ |
557 | change_file_status(path, i); |
558 | } | |
559 | } | |
560 | ||
561 | static void do_special_inode(char *path, struct cramfs_inode *i) | |
562 | { | |
563 | dev_t devtype = 0; | |
564 | char type; | |
565 | ||
6ffd9b03 SK |
566 | if (i->offset) |
567 | /* no need to shift offset */ | |
70b604b8 | 568 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 SK |
569 | _("special file has non-zero offset: %s"), path); |
570 | ||
63cccae4 KZ |
571 | if (S_ISCHR(i->mode)) { |
572 | devtype = i->size; | |
573 | type = 'c'; | |
6ffd9b03 | 574 | } else if (S_ISBLK(i->mode)) { |
63cccae4 KZ |
575 | devtype = i->size; |
576 | type = 'b'; | |
6ffd9b03 SK |
577 | } else if (S_ISFIFO(i->mode)) { |
578 | if (i->size != 0) | |
70b604b8 | 579 | errx(FSCK_EX_UNCORRECTED, _("fifo has non-zero size: %s"), |
6ffd9b03 | 580 | path); |
63cccae4 | 581 | type = 'p'; |
6ffd9b03 SK |
582 | } else if (S_ISSOCK(i->mode)) { |
583 | if (i->size != 0) | |
70b604b8 | 584 | errx(FSCK_EX_UNCORRECTED, |
6ffd9b03 | 585 | _("socket has non-zero size: %s"), path); |
63cccae4 | 586 | type = 's'; |
6ffd9b03 | 587 | } else { |
70b604b8 | 588 | errx(FSCK_EX_UNCORRECTED, _("bogus mode: %s (%o)"), path, i->mode); |
19922f22 | 589 | return; /* not reached */ |
63cccae4 KZ |
590 | } |
591 | ||
6ffd9b03 | 592 | if (opt_verbose) |
63cccae4 | 593 | print_node(type, i, path); |
63cccae4 | 594 | |
2d317f11 | 595 | if (*extract_dir != '\0') { |
6ffd9b03 | 596 | if (mknod(path, i->mode, devtype) < 0) |
70b604b8 | 597 | err(FSCK_EX_ERROR, _("mknod failed: %s"), path); |
63cccae4 KZ |
598 | change_file_status(path, i); |
599 | } | |
600 | } | |
601 | ||
19922f22 | 602 | static void expand_fs(char *path, struct cramfs_inode *inode) |
63cccae4 | 603 | { |
6ffd9b03 | 604 | if (S_ISDIR(inode->mode)) |
19922f22 | 605 | do_directory(path, inode); |
6ffd9b03 | 606 | else if (S_ISREG(inode->mode)) |
19922f22 | 607 | do_file(path, inode); |
6ffd9b03 | 608 | else if (S_ISLNK(inode->mode)) |
19922f22 | 609 | do_symlink(path, inode); |
6ffd9b03 | 610 | else |
19922f22 | 611 | do_special_inode(path, inode); |
63cccae4 KZ |
612 | } |
613 | ||
19922f22 | 614 | static void test_fs(int start) |
63cccae4 | 615 | { |
19922f22 | 616 | struct cramfs_inode *root; |
63cccae4 | 617 | |
19922f22 KZ |
618 | root = read_super(); |
619 | umask(0); | |
620 | euid = geteuid(); | |
621 | stream.next_in = NULL; | |
622 | stream.avail_in = 0; | |
623 | inflateInit(&stream); | |
624 | expand_fs(extract_dir, root); | |
625 | inflateEnd(&stream); | |
626 | if (start_data != ~0UL) { | |
6ffd9b03 | 627 | if (start_data < (sizeof(struct cramfs_super) + start)) |
70b604b8 | 628 | errx(FSCK_EX_UNCORRECTED, |
60cb2c37 | 629 | _("directory data start (%lu) < sizeof(struct cramfs_super) + start (%zu)"), |
6ffd9b03 SK |
630 | start_data, sizeof(struct cramfs_super) + start); |
631 | if (end_dir != start_data) | |
70b604b8 | 632 | errx(FSCK_EX_UNCORRECTED, |
60cb2c37 | 633 | _("directory data end (%lu) != file data start (%lu)"), |
6ffd9b03 SK |
634 | end_dir, start_data); |
635 | } | |
74ce680a SK |
636 | if (super.flags & CRAMFS_FLAG_FSID_VERSION_2 && end_data > super.size) |
637 | errx(FSCK_EX_UNCORRECTED, _("invalid file data offset")); | |
6ffd9b03 | 638 | |
19922f22 | 639 | iput(root); /* free(root) */ |
63cccae4 | 640 | } |
63cccae4 KZ |
641 | |
642 | int main(int argc, char **argv) | |
643 | { | |
63cccae4 KZ |
644 | int c; /* for getopt */ |
645 | int start = 0; | |
293889d0 | 646 | size_t length = 0; |
19922f22 | 647 | |
922ec175 | 648 | static const struct option longopts[] = { |
87918040 SK |
649 | {"verbose", no_argument, NULL, 'v'}, |
650 | {"version", no_argument, NULL, 'V'}, | |
651 | {"help", no_argument, NULL, 'h'}, | |
652 | {"blocksize", required_argument, NULL, 'b'}, | |
653 | {"extract", optional_argument, NULL, 'x'}, | |
654 | {NULL, 0, NULL, 0}, | |
922ec175 SK |
655 | }; |
656 | ||
e56644ca | 657 | setlocale(LC_MESSAGES, ""); |
5d4ece6e | 658 | setlocale(LC_CTYPE, ""); |
e56644ca PR |
659 | bindtextdomain(PACKAGE, LOCALEDIR); |
660 | textdomain(PACKAGE); | |
45ca68ec | 661 | atexit(close_stdout); |
e56644ca | 662 | |
63cccae4 | 663 | /* command line options */ |
2d317f11 | 664 | while ((c = getopt_long(argc, argv, "ayvVhb:", longopts, NULL)) != EOF) |
63cccae4 | 665 | switch (c) { |
6bf463c5 KZ |
666 | case 'a': /* ignore */ |
667 | case 'y': | |
668 | break; | |
63cccae4 | 669 | case 'h': |
70b604b8 | 670 | usage(FSCK_EX_OK); |
e4a36b8b | 671 | break; |
922ec175 SK |
672 | case 'V': |
673 | printf(UTIL_LINUX_VERSION); | |
674 | return EXIT_SUCCESS; | |
63cccae4 | 675 | case 'x': |
63cccae4 | 676 | opt_extract = 1; |
2d317f11 RM |
677 | if(optarg) |
678 | extract_dir = optarg; | |
63cccae4 | 679 | break; |
63cccae4 KZ |
680 | case 'v': |
681 | opt_verbose++; | |
682 | break; | |
5cd50ebc | 683 | case 'b': |
5cd50ebc | 684 | blksize = strtou32_or_err(optarg, _("invalid blocksize argument")); |
5cd50ebc | 685 | break; |
de173766 | 686 | default: |
677ec86c | 687 | errtryhelp(FSCK_EX_USAGE); |
63cccae4 | 688 | } |
63cccae4 KZ |
689 | |
690 | if ((argc - optind) != 1) | |
70b604b8 | 691 | usage(FSCK_EX_USAGE); |
63cccae4 KZ |
692 | filename = argv[optind]; |
693 | ||
19922f22 KZ |
694 | test_super(&start, &length); |
695 | test_crc(start); | |
34731f89 | 696 | |
2d317f11 RM |
697 | if(opt_extract) { |
698 | if (blksize == 0) | |
699 | blksize = getpagesize(); | |
700 | outbuffer = xmalloc(blksize * 2); | |
701 | test_fs(start); | |
702 | } | |
63cccae4 | 703 | |
6ffd9b03 | 704 | if (opt_verbose) |
6de1396e | 705 | printf(_("%s: OK\n"), filename); |
63cccae4 | 706 | |
70b604b8 | 707 | exit(FSCK_EX_OK); |
63cccae4 | 708 | } |