Home | History | Annotate | Download | only in debugfs
      1 /*
      2  * dump.c --- dump the contents of an inode out to a file
      3  *
      4  * Copyright (C) 1994 Theodore Ts'o.  This file may be redistributed
      5  * under the terms of the GNU Public License.
      6  */
      7 
      8 #ifndef _GNU_SOURCE
      9 #define _GNU_SOURCE /* for O_LARGEFILE */
     10 #endif
     11 
     12 #include <stdio.h>
     13 #include <unistd.h>
     14 #include <stdlib.h>
     15 #include <ctype.h>
     16 #include <string.h>
     17 #include <time.h>
     18 #ifdef HAVE_ERRNO_H
     19 #include <errno.h>
     20 #endif
     21 #include <sys/types.h>
     22 #include <sys/stat.h>
     23 #include <fcntl.h>
     24 #include <utime.h>
     25 #ifdef HAVE_GETOPT_H
     26 #include <getopt.h>
     27 #else
     28 extern int optind;
     29 extern char *optarg;
     30 #endif
     31 
     32 #include "debugfs.h"
     33 
     34 #ifndef O_LARGEFILE
     35 #define O_LARGEFILE 0
     36 #endif
     37 
     38 /*
     39  * The mode_xlate function translates a linux mode into a native-OS mode_t.
     40  */
     41 static struct {
     42 	__u16 lmask;
     43 	mode_t mask;
     44 } mode_table[] = {
     45 	{ LINUX_S_IRUSR, S_IRUSR },
     46 	{ LINUX_S_IWUSR, S_IWUSR },
     47 	{ LINUX_S_IXUSR, S_IXUSR },
     48 	{ LINUX_S_IRGRP, S_IRGRP },
     49 	{ LINUX_S_IWGRP, S_IWGRP },
     50 	{ LINUX_S_IXGRP, S_IXGRP },
     51 	{ LINUX_S_IROTH, S_IROTH },
     52 	{ LINUX_S_IWOTH, S_IWOTH },
     53 	{ LINUX_S_IXOTH, S_IXOTH },
     54 	{ 0, 0 }
     55 };
     56 
     57 static mode_t mode_xlate(__u16 lmode)
     58 {
     59 	mode_t	mode = 0;
     60 	int	i;
     61 
     62 	for (i=0; mode_table[i].lmask; i++) {
     63 		if (lmode & mode_table[i].lmask)
     64 			mode |= mode_table[i].mask;
     65 	}
     66 	return mode;
     67 }
     68 
     69 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
     70 		      int fd, const char *name)
     71 {
     72 	struct utimbuf ut;
     73 	int i;
     74 
     75 	if (fd != -1)
     76 		i = fchmod(fd, mode_xlate(inode->i_mode));
     77 	else
     78 		i = chmod(name, mode_xlate(inode->i_mode));
     79 	if (i == -1)
     80 		com_err(cmd, errno, "while setting permissions of %s", name);
     81 
     82 #ifndef HAVE_FCHOWN
     83 	i = chown(name, inode->i_uid, inode->i_gid);
     84 #else
     85 	if (fd != -1)
     86 		i = fchown(fd, inode->i_uid, inode->i_gid);
     87 	else
     88 		i = chown(name, inode->i_uid, inode->i_gid);
     89 #endif
     90 	if (i == -1)
     91 		com_err(cmd, errno, "while changing ownership of %s", name);
     92 
     93 	if (fd != -1)
     94 		close(fd);
     95 
     96 	ut.actime = inode->i_atime;
     97 	ut.modtime = inode->i_mtime;
     98 	if (utime(name, &ut) == -1)
     99 		com_err(cmd, errno, "while setting times of %s", name);
    100 }
    101 
    102 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
    103 		      int preserve, char *outname)
    104 {
    105 	errcode_t retval;
    106 	struct ext2_inode	inode;
    107 	char		*buf = 0;
    108 	ext2_file_t	e2_file;
    109 	int		nbytes;
    110 	unsigned int	got, blocksize = current_fs->blocksize;
    111 
    112 	if (debugfs_read_inode(ino, &inode, cmdname))
    113 		return;
    114 
    115 	retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
    116 	if (retval) {
    117 		com_err(cmdname, retval, "while opening ext2 file");
    118 		return;
    119 	}
    120 	retval = ext2fs_get_mem(blocksize, &buf);
    121 	if (retval) {
    122 		com_err(cmdname, retval, "while allocating memory");
    123 		return;
    124 	}
    125 	while (1) {
    126 		retval = ext2fs_file_read(e2_file, buf, blocksize, &got);
    127 		if (retval)
    128 			com_err(cmdname, retval, "while reading ext2 file");
    129 		if (got == 0)
    130 			break;
    131 		nbytes = write(fd, buf, got);
    132 		if ((unsigned) nbytes != got)
    133 			com_err(cmdname, errno, "while writing file");
    134 	}
    135 	if (buf)
    136 		ext2fs_free_mem(&buf);
    137 	retval = ext2fs_file_close(e2_file);
    138 	if (retval) {
    139 		com_err(cmdname, retval, "while closing ext2 file");
    140 		return;
    141 	}
    142 
    143 	if (preserve)
    144 		fix_perms("dump_file", &inode, fd, outname);
    145 
    146 	return;
    147 }
    148 
    149 void do_dump(int argc, char **argv)
    150 {
    151 	ext2_ino_t	inode;
    152 	int		fd;
    153 	int		c;
    154 	int		preserve = 0;
    155 	char		*in_fn, *out_fn;
    156 
    157 	reset_getopt();
    158 	while ((c = getopt (argc, argv, "p")) != EOF) {
    159 		switch (c) {
    160 		case 'p':
    161 			preserve++;
    162 			break;
    163 		default:
    164 		print_usage:
    165 			com_err(argv[0], 0, "Usage: dump_inode [-p] "
    166 				"<file> <output_file>");
    167 			return;
    168 		}
    169 	}
    170 	if (optind != argc-2)
    171 		goto print_usage;
    172 
    173 	if (check_fs_open(argv[0]))
    174 		return;
    175 
    176 	in_fn = argv[optind];
    177 	out_fn = argv[optind+1];
    178 
    179 	inode = string_to_inode(in_fn);
    180 	if (!inode)
    181 		return;
    182 
    183 	fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
    184 	if (fd < 0) {
    185 		com_err(argv[0], errno, "while opening %s for dump_inode",
    186 			out_fn);
    187 		return;
    188 	}
    189 
    190 	dump_file(argv[0], inode, fd, preserve, out_fn);
    191 	if (close(fd) != 0) {
    192 		com_err(argv[0], errno, "while closing %s for dump_inode",
    193 			out_fn);
    194 		return;
    195 	}
    196 
    197 	return;
    198 }
    199 
    200 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
    201 			  const char *fullname)
    202 {
    203 	ext2_file_t e2_file;
    204 	char *buf;
    205 	errcode_t retval;
    206 
    207 	buf = malloc(inode->i_size + 1);
    208 	if (!buf) {
    209 		com_err("rdump", errno, "while allocating for symlink");
    210 		goto errout;
    211 	}
    212 
    213 	/* Apparently, this is the right way to detect and handle fast
    214 	 * symlinks; see do_stat() in debugfs.c. */
    215 	if (inode->i_blocks == 0)
    216 		strcpy(buf, (char *) inode->i_block);
    217 	else {
    218 		unsigned bytes = inode->i_size;
    219 		char *p = buf;
    220 		retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
    221 		if (retval) {
    222 			com_err("rdump", retval, "while opening symlink");
    223 			goto errout;
    224 		}
    225 		for (;;) {
    226 			unsigned int got;
    227 			retval = ext2fs_file_read(e2_file, p, bytes, &got);
    228 			if (retval) {
    229 				com_err("rdump", retval, "while reading symlink");
    230 				goto errout;
    231 			}
    232 			bytes -= got;
    233 			p += got;
    234 			if (got == 0 || bytes == 0)
    235 				break;
    236 		}
    237 		buf[inode->i_size] = 0;
    238 		retval = ext2fs_file_close(e2_file);
    239 		if (retval)
    240 			com_err("rdump", retval, "while closing symlink");
    241 	}
    242 
    243 	if (symlink(buf, fullname) == -1) {
    244 		com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
    245 		goto errout;
    246 	}
    247 
    248 errout:
    249 	free(buf);
    250 }
    251 
    252 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
    253 
    254 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
    255 			const char *name, const char *dumproot)
    256 {
    257 	char *fullname;
    258 
    259 	/* There are more efficient ways to do this, but this method
    260 	 * requires only minimal debugging. */
    261 	fullname = malloc(strlen(dumproot) + strlen(name) + 2);
    262 	if (!fullname) {
    263 		com_err("rdump", errno, "while allocating memory");
    264 		return;
    265 	}
    266 	sprintf(fullname, "%s/%s", dumproot, name);
    267 
    268 	if (LINUX_S_ISLNK(inode->i_mode))
    269 		rdump_symlink(ino, inode, fullname);
    270 	else if (LINUX_S_ISREG(inode->i_mode)) {
    271 		int fd;
    272 		fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
    273 		if (fd == -1) {
    274 			com_err("rdump", errno, "while dumping %s", fullname);
    275 			goto errout;
    276 		}
    277 		dump_file("rdump", ino, fd, 1, fullname);
    278 		if (close(fd) != 0) {
    279 			com_err("rdump", errno, "while dumping %s", fullname);
    280 			goto errout;
    281 		}
    282 	}
    283 	else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
    284 		errcode_t retval;
    285 
    286 		/* Create the directory with 0700 permissions, because we
    287 		 * expect to have to create entries it.  Then fix its perms
    288 		 * once we've done the traversal. */
    289 		if (mkdir(fullname, S_IRWXU) == -1) {
    290 			com_err("rdump", errno, "while making directory %s", fullname);
    291 			goto errout;
    292 		}
    293 
    294 		retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
    295 					    rdump_dirent, (void *) fullname);
    296 		if (retval)
    297 			com_err("rdump", retval, "while dumping %s", fullname);
    298 
    299 		fix_perms("rdump", inode, -1, fullname);
    300 	}
    301 	/* else do nothing (don't dump device files, sockets, fifos, etc.) */
    302 
    303 errout:
    304 	free(fullname);
    305 }
    306 
    307 static int rdump_dirent(struct ext2_dir_entry *dirent,
    308 			int offset EXT2FS_ATTR((unused)),
    309 			int blocksize EXT2FS_ATTR((unused)),
    310 			char *buf EXT2FS_ATTR((unused)), void *private)
    311 {
    312 	char name[EXT2_NAME_LEN + 1];
    313 	int thislen;
    314 	const char *dumproot = private;
    315 	struct ext2_inode inode;
    316 
    317 	thislen = dirent->name_len & 0xFF;
    318 	strncpy(name, dirent->name, thislen);
    319 	name[thislen] = 0;
    320 
    321 	if (debugfs_read_inode(dirent->inode, &inode, name))
    322 		return 0;
    323 
    324 	rdump_inode(dirent->inode, &inode, name, dumproot);
    325 
    326 	return 0;
    327 }
    328 
    329 void do_rdump(int argc, char **argv)
    330 {
    331 	ext2_ino_t ino;
    332 	struct ext2_inode inode;
    333 	struct stat st;
    334 	int i;
    335 	char *p;
    336 
    337 	if (common_args_process(argc, argv, 3, 3, "rdump",
    338 				"<directory> <native directory>", 0))
    339 		return;
    340 
    341 	ino = string_to_inode(argv[1]);
    342 	if (!ino)
    343 		return;
    344 
    345 	/* Ensure ARGV[2] is a directory. */
    346 	i = stat(argv[2], &st);
    347 	if (i == -1) {
    348 		com_err("rdump", errno, "while statting %s", argv[2]);
    349 		return;
    350 	}
    351 	if (!S_ISDIR(st.st_mode)) {
    352 		com_err("rdump", 0, "%s is not a directory", argv[2]);
    353 		return;
    354 	}
    355 
    356 	if (debugfs_read_inode(ino, &inode, argv[1]))
    357 		return;
    358 
    359 	p = strrchr(argv[1], '/');
    360 	if (p)
    361 		p++;
    362 	else
    363 		p = argv[1];
    364 
    365 	rdump_inode(ino, &inode, p, argv[2]);
    366 }
    367 
    368 void do_cat(int argc, char **argv)
    369 {
    370 	ext2_ino_t	inode;
    371 
    372 	if (common_inode_args_process(argc, argv, &inode, 0))
    373 		return;
    374 
    375 	fflush(stdout);
    376 	fflush(stderr);
    377 	dump_file(argv[0], inode, 1, 0, argv[2]);
    378 
    379 	return;
    380 }
    381 
    382