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