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