2 * dump.c --- dump the contents of an inode out to a file
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
9 #define _GNU_SOURCE /* for O_LARGEFILE */
21 #include <sys/types.h>
39 * The mode_xlate function translates a linux mode into a native-OS mode_t.
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 },
57 static mode_t mode_xlate(__u16 lmode)
62 for (i=0; mode_table[i].lmask; i++) {
63 if (lmode & mode_table[i].lmask)
64 mode |= mode_table[i].mask;
69 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
70 int fd, const char *name)
76 i = fchmod(fd, mode_xlate(inode->i_mode));
78 i = chmod(name, mode_xlate(inode->i_mode));
80 com_err(cmd, errno, "while setting permissions of %s", name);
83 i = chown(name, inode->i_uid, inode->i_gid);
86 i = fchown(fd, inode->i_uid, inode->i_gid);
88 i = chown(name, inode->i_uid, inode->i_gid);
91 com_err(cmd, errno, "while changing ownership of %s", name);
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);
102 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
103 int preserve, char *outname)
106 struct ext2_inode inode;
112 if (debugfs_read_inode(ino, &inode, cmdname))
115 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
117 com_err(cmdname, retval, "while opening ext2 file");
121 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
123 com_err(cmdname, retval, "while reading ext2 file");
126 nbytes = write(fd, buf, got);
127 if ((unsigned) nbytes != got)
128 com_err(cmdname, errno, "while writing file");
130 retval = ext2fs_file_close(e2_file);
132 com_err(cmdname, retval, "while closing ext2 file");
137 fix_perms("dump_file", &inode, fd, outname);
144 void do_dump(int argc, char **argv)
150 char *in_fn, *out_fn;
153 while ((c = getopt (argc, argv, "p")) != EOF) {
160 com_err(argv[0], 0, "Usage: dump_inode [-p] "
161 "<file> <output_file>");
165 if (optind != argc-2)
168 if (check_fs_open(argv[0]))
171 in_fn = argv[optind];
172 out_fn = argv[optind+1];
174 inode = string_to_inode(in_fn);
178 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
180 com_err(argv[0], errno, "while opening %s for dump_inode",
185 dump_file(argv[0], inode, fd, preserve, out_fn);
190 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
191 const char *fullname)
197 buf = malloc(inode->i_size + 1);
199 com_err("rdump", errno, "while allocating for symlink");
203 /* Apparently, this is the right way to detect and handle fast
204 * symlinks; see do_stat() in debugfs.c. */
205 if (inode->i_blocks == 0)
206 strcpy(buf, (char *) inode->i_block);
208 unsigned bytes = inode->i_size;
210 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
212 com_err("rdump", retval, "while opening symlink");
217 retval = ext2fs_file_read(e2_file, p, bytes, &got);
219 com_err("rdump", retval, "while reading symlink");
224 if (got == 0 || bytes == 0)
227 buf[inode->i_size] = 0;
228 retval = ext2fs_file_close(e2_file);
230 com_err("rdump", retval, "while closing symlink");
233 if (symlink(buf, fullname) == -1) {
234 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
242 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
244 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
245 const char *name, const char *dumproot)
249 /* There are more efficient ways to do this, but this method
250 * requires only minimal debugging. */
251 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
253 com_err("rdump", errno, "while allocating memory");
256 sprintf(fullname, "%s/%s", dumproot, name);
258 if (LINUX_S_ISLNK(inode->i_mode))
259 rdump_symlink(ino, inode, fullname);
260 else if (LINUX_S_ISREG(inode->i_mode)) {
262 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
264 com_err("rdump", errno, "while dumping %s", fullname);
267 dump_file("rdump", ino, fd, 1, fullname);
269 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
272 /* Create the directory with 0700 permissions, because we
273 * expect to have to create entries it. Then fix its perms
274 * once we've done the traversal. */
275 if (mkdir(fullname, S_IRWXU) == -1) {
276 com_err("rdump", errno, "while making directory %s", fullname);
280 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
281 rdump_dirent, (void *) fullname);
283 com_err("rdump", retval, "while dumping %s", fullname);
285 fix_perms("rdump", inode, -1, fullname);
287 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
293 static int rdump_dirent(struct ext2_dir_entry *dirent,
294 int offset EXT2FS_ATTR((unused)),
295 int blocksize EXT2FS_ATTR((unused)),
296 char *buf EXT2FS_ATTR((unused)), void *private)
298 char name[EXT2_NAME_LEN + 1];
300 const char *dumproot = private;
301 struct ext2_inode inode;
303 thislen = dirent->name_len & 0xFF;
304 strncpy(name, dirent->name, thislen);
307 if (debugfs_read_inode(dirent->inode, &inode, name))
310 rdump_inode(dirent->inode, &inode, name, dumproot);
315 void do_rdump(int argc, char **argv)
318 struct ext2_inode inode;
323 if (common_args_process(argc, argv, 3, 3, "rdump",
324 "<directory> <native directory>", 0))
327 ino = string_to_inode(argv[1]);
331 /* Ensure ARGV[2] is a directory. */
332 i = stat(argv[2], &st);
334 com_err("rdump", errno, "while statting %s", argv[2]);
337 if (!S_ISDIR(st.st_mode)) {
338 com_err("rdump", 0, "%s is not a directory", argv[2]);
342 if (debugfs_read_inode(ino, &inode, argv[1]))
345 p = strrchr(argv[1], '/');
351 rdump_inode(ino, &inode, p, argv[2]);
354 void do_cat(int argc, char **argv)
358 if (common_inode_args_process(argc, argv, &inode, 0))
363 dump_file(argv[0], inode, 1, 0, argv[2]);