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