dump.c revision b044c2e02af46c54206f0f6e29896ab32681a7db
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#include <stdio.h> 9#include <unistd.h> 10#include <stdlib.h> 11#include <ctype.h> 12#include <string.h> 13#include <time.h> 14#ifdef HAVE_ERRNO_H 15#include <errno.h> 16#endif 17#include <sys/types.h> 18#include <sys/stat.h> 19#include <fcntl.h> 20#include <utime.h> 21#ifdef HAVE_GETOPT_H 22#include <getopt.h> 23#else 24extern int optind; 25extern char *optarg; 26#endif 27#ifdef HAVE_OPTRESET 28extern int optreset; /* defined by BSD, but not others */ 29#endif 30 31#include "debugfs.h" 32 33/* 34 * The mode_xlate function translates a linux mode into a native-OS mode_t. 35 */ 36static struct { 37 __u16 lmask; 38 mode_t mask; 39} mode_table[] = { 40 { LINUX_S_IRUSR, S_IRUSR }, 41 { LINUX_S_IWUSR, S_IWUSR }, 42 { LINUX_S_IXUSR, S_IXUSR }, 43 { LINUX_S_IRGRP, S_IRGRP }, 44 { LINUX_S_IWGRP, S_IWGRP }, 45 { LINUX_S_IXGRP, S_IXGRP }, 46 { LINUX_S_IROTH, S_IROTH }, 47 { LINUX_S_IWOTH, S_IWOTH }, 48 { LINUX_S_IXOTH, S_IXOTH }, 49 { 0, 0 } 50}; 51 52static mode_t mode_xlate(__u16 lmode) 53{ 54 mode_t mode = 0; 55 int i; 56 57 for (i=0; mode_table[i].lmask; i++) { 58 if (lmode & mode_table[i].lmask) 59 mode |= mode_table[i].mask; 60 } 61 return mode; 62} 63 64static void fix_perms(const char *cmd, const struct ext2_inode *inode, 65 int fd, const char *name) 66{ 67 struct utimbuf ut; 68 int i; 69 70 if (fd != -1) 71 i = fchmod(fd, mode_xlate(inode->i_mode)); 72 else 73 i = chmod(name, mode_xlate(inode->i_mode)); 74 if (i == -1) 75 com_err(cmd, errno, "while setting permissions of %s", name); 76 77#ifndef HAVE_FCHOWN 78 i = chown(name, inode->i_uid, inode->i_gid); 79#else 80 if (fd != -1) 81 i = fchown(fd, inode->i_uid, inode->i_gid); 82 else 83 i = chown(name, inode->i_uid, inode->i_gid); 84#endif 85 if (i == -1) 86 com_err(cmd, errno, "while changing ownership of %s", name); 87 88 if (fd != -1) 89 close(fd); 90 91 ut.actime = inode->i_atime; 92 ut.modtime = inode->i_mtime; 93 if (utime(name, &ut) == -1) 94 com_err(cmd, errno, "while setting times of %s", name); 95} 96 97static void dump_file(const char *cmdname, ext2_ino_t ino, int fd, 98 int preserve, char *outname) 99{ 100 errcode_t retval; 101 struct ext2_inode inode; 102 char buf[8192]; 103 ext2_file_t e2_file; 104 int nbytes; 105 unsigned int got; 106 107 retval = ext2fs_read_inode(current_fs, ino, &inode); 108 if (retval) { 109 com_err(cmdname, retval, 110 "while reading inode %u in dump_file", ino); 111 return; 112 } 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, 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 errcode_t retval; 304 305 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN 306 ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN); 307 strncpy(name, dirent->name, thislen); 308 name[thislen] = 0; 309 310 retval = ext2fs_read_inode(current_fs, dirent->inode, &inode); 311 if (retval) { 312 com_err("rdump", retval, "while dumping %s/%s", dumproot, name); 313 return 0; 314 } 315 316 rdump_inode(dirent->inode, &inode, name, dumproot); 317 318 return 0; 319} 320 321void do_rdump(int argc, char **argv) 322{ 323 ext2_ino_t ino; 324 struct ext2_inode inode; 325 errcode_t retval; 326 struct stat st; 327 int i; 328 char *p; 329 330 if (argc != 3) { 331 com_err(argv[0], 0, "Usage: rdump <directory> <native directory>"); 332 return; 333 } 334 335 if (check_fs_open(argv[0])) 336 return; 337 338 ino = string_to_inode(argv[1]); 339 if (!ino) 340 return; 341 342 /* Ensure ARGV[2] is a directory. */ 343 i = stat(argv[2], &st); 344 if (i == -1) { 345 com_err("rdump", errno, "while statting %s", argv[2]); 346 return; 347 } 348 if (!S_ISDIR(st.st_mode)) { 349 com_err("rdump", 0, "%s is not a directory", argv[2]); 350 return; 351 } 352 353 retval = ext2fs_read_inode(current_fs, ino, &inode); 354 if (retval) { 355 com_err("rdump", retval, "while dumping %s", argv[1]); 356 return; 357 } 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 368void do_cat(int argc, char **argv) 369{ 370 ext2_ino_t inode; 371 372 if (argc != 2) { 373 com_err(argv[0], 0, "Usage: cat <file>"); 374 return; 375 } 376 377 if (check_fs_open(argv[0])) 378 return; 379 380 inode = string_to_inode(argv[1]); 381 if (!inode) 382 return; 383 384 fflush(stdout); 385 fflush(stderr); 386 dump_file(argv[0], inode, 1, 0, argv[2]); 387 388 return; 389} 390 391