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