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