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