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