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