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