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.
8 #define _GNU_SOURCE /* for O_LARGEFILE */
19 #include <sys/types.h>
37 * The mode_xlate function translates a linux mode into a native-OS mode_t.
43 { LINUX_S_IRUSR, S_IRUSR },
44 { LINUX_S_IWUSR, S_IWUSR },
45 { LINUX_S_IXUSR, S_IXUSR },
46 { LINUX_S_IRGRP, S_IRGRP },
47 { LINUX_S_IWGRP, S_IWGRP },
48 { LINUX_S_IXGRP, S_IXGRP },
49 { LINUX_S_IROTH, S_IROTH },
50 { LINUX_S_IWOTH, S_IWOTH },
51 { LINUX_S_IXOTH, S_IXOTH },
55 static mode_t mode_xlate(__u16 lmode)
60 for (i=0; mode_table[i].lmask; i++) {
61 if (lmode & mode_table[i].lmask)
62 mode |= mode_table[i].mask;
67 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
68 int fd, const char *name)
74 i = fchmod(fd, mode_xlate(inode->i_mode));
76 i = chmod(name, mode_xlate(inode->i_mode));
78 com_err(cmd, errno, "while setting permissions of %s", name);
81 i = chown(name, inode->i_uid, inode->i_gid);
84 i = fchown(fd, inode->i_uid, inode->i_gid);
86 i = chown(name, inode->i_uid, inode->i_gid);
89 com_err(cmd, errno, "while changing ownership of %s", name);
94 ut.actime = inode->i_atime;
95 ut.modtime = inode->i_mtime;
96 if (utime(name, &ut) == -1)
97 com_err(cmd, errno, "while setting times of %s", name);
100 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
101 int preserve, char *outname)
104 struct ext2_inode inode;
110 if (debugfs_read_inode(ino, &inode, cmdname))
113 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
115 com_err(cmdname, retval, "while opening ext2 file");
119 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
121 com_err(cmdname, retval, "while reading ext2 file");
124 nbytes = write(fd, buf, got);
125 if ((unsigned) nbytes != got)
126 com_err(cmdname, errno, "while writing file");
128 retval = ext2fs_file_close(e2_file);
130 com_err(cmdname, retval, "while closing ext2 file");
135 fix_perms("dump_file", &inode, fd, outname);
142 void do_dump(int argc, char **argv)
148 const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
149 char *in_fn, *out_fn;
152 while ((c = getopt (argc, argv, "p")) != EOF) {
158 com_err(argv[0], 0, dump_usage);
162 if (optind != argc-2) {
163 com_err(argv[0], 0, dump_usage);
167 if (check_fs_open(argv[0]))
170 in_fn = argv[optind];
171 out_fn = argv[optind+1];
173 inode = string_to_inode(in_fn);
177 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
179 com_err(argv[0], errno, "while opening %s for dump_inode",
184 dump_file(argv[0], inode, fd, preserve, out_fn);
189 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
190 const char *fullname)
196 buf = malloc(inode->i_size + 1);
198 com_err("rdump", errno, "while allocating for symlink");
202 /* Apparently, this is the right way to detect and handle fast
203 * symlinks; see do_stat() in debugfs.c. */
204 if (inode->i_blocks == 0)
205 strcpy(buf, (char *) inode->i_block);
207 unsigned bytes = inode->i_size;
209 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
211 com_err("rdump", retval, "while opening symlink");
216 retval = ext2fs_file_read(e2_file, p, bytes, &got);
218 com_err("rdump", retval, "while reading symlink");
223 if (got == 0 || bytes == 0)
226 buf[inode->i_size] = 0;
227 retval = ext2fs_file_close(e2_file);
229 com_err("rdump", retval, "while closing symlink");
232 if (symlink(buf, fullname) == -1) {
233 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
241 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
243 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
244 const char *name, const char *dumproot)
248 /* There are more efficient ways to do this, but this method
249 * requires only minimal debugging. */
250 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
252 com_err("rdump", errno, "while allocating memory");
255 sprintf(fullname, "%s/%s", dumproot, name);
257 if (LINUX_S_ISLNK(inode->i_mode))
258 rdump_symlink(ino, inode, fullname);
259 else if (LINUX_S_ISREG(inode->i_mode)) {
261 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
263 com_err("rdump", errno, "while dumping %s", fullname);
266 dump_file("rdump", ino, fd, 1, fullname);
268 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
271 /* Create the directory with 0700 permissions, because we
272 * expect to have to create entries it. Then fix its perms
273 * once we've done the traversal. */
274 if (mkdir(fullname, S_IRWXU) == -1) {
275 com_err("rdump", errno, "while making directory %s", fullname);
279 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
280 rdump_dirent, (void *) fullname);
282 com_err("rdump", retval, "while dumping %s", fullname);
284 fix_perms("rdump", inode, -1, fullname);
286 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
292 static int rdump_dirent(struct ext2_dir_entry *dirent,
293 int offset EXT2FS_ATTR((unused)),
294 int blocksize EXT2FS_ATTR((unused)),
295 char *buf EXT2FS_ATTR((unused)), void *private)
297 char name[EXT2_NAME_LEN + 1];
299 const char *dumproot = private;
300 struct ext2_inode inode;
302 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
303 ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
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]);