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