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