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.
17 #include <sys/types.h>
35 * The mode_xlate function translates a linux mode into a native-OS mode_t.
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 },
53 static mode_t mode_xlate(__u16 lmode)
58 for (i=0; mode_table[i].lmask; i++) {
59 if (lmode & mode_table[i].lmask)
60 mode |= mode_table[i].mask;
65 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
66 int fd, const char *name)
72 i = fchmod(fd, mode_xlate(inode->i_mode));
74 i = chmod(name, mode_xlate(inode->i_mode));
76 com_err(cmd, errno, "while setting permissions of %s", name);
79 i = chown(name, inode->i_uid, inode->i_gid);
82 i = fchown(fd, inode->i_uid, inode->i_gid);
84 i = chown(name, inode->i_uid, inode->i_gid);
87 com_err(cmd, errno, "while changing ownership of %s", name);
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);
98 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
99 int preserve, char *outname)
102 struct ext2_inode inode;
108 if (debugfs_read_inode(ino, &inode, cmdname))
111 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
113 com_err(cmdname, retval, "while opening ext2 file");
117 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
119 com_err(cmdname, retval, "while reading ext2 file");
122 nbytes = write(fd, buf, got);
123 if ((unsigned) nbytes != got)
124 com_err(cmdname, errno, "while writing file");
126 retval = ext2fs_file_close(e2_file);
128 com_err(cmdname, retval, "while closing ext2 file");
133 fix_perms("dump_file", &inode, fd, outname);
140 void do_dump(int argc, char **argv)
146 const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
147 char *in_fn, *out_fn;
150 while ((c = getopt (argc, argv, "p")) != EOF) {
156 com_err(argv[0], 0, dump_usage);
160 if (optind != argc-2) {
161 com_err(argv[0], 0, dump_usage);
165 if (check_fs_open(argv[0]))
168 in_fn = argv[optind];
169 out_fn = argv[optind+1];
171 inode = string_to_inode(in_fn);
175 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
177 com_err(argv[0], errno, "while opening %s for dump_inode",
182 dump_file(argv[0], inode, fd, preserve, out_fn);
187 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
188 const char *fullname)
194 buf = malloc(inode->i_size + 1);
196 com_err("rdump", errno, "while allocating for symlink");
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);
205 unsigned bytes = inode->i_size;
207 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
209 com_err("rdump", retval, "while opening symlink");
214 retval = ext2fs_file_read(e2_file, p, bytes, &got);
216 com_err("rdump", retval, "while reading symlink");
221 if (got == 0 || bytes == 0)
224 buf[inode->i_size] = 0;
225 retval = ext2fs_file_close(e2_file);
227 com_err("rdump", retval, "while closing symlink");
230 if (symlink(buf, fullname) == -1) {
231 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
239 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
241 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
242 const char *name, const char *dumproot)
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);
250 com_err("rdump", errno, "while allocating memory");
253 sprintf(fullname, "%s/%s", dumproot, name);
255 if (LINUX_S_ISLNK(inode->i_mode))
256 rdump_symlink(ino, inode, fullname);
257 else if (LINUX_S_ISREG(inode->i_mode)) {
259 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
261 com_err("rdump", errno, "while dumping %s", fullname);
264 dump_file("rdump", ino, fd, 1, fullname);
266 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
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);
277 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
278 rdump_dirent, (void *) fullname);
280 com_err("rdump", retval, "while dumping %s", fullname);
282 fix_perms("rdump", inode, -1, fullname);
284 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
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)
295 char name[EXT2_NAME_LEN + 1];
297 const char *dumproot = private;
298 struct ext2_inode inode;
300 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
301 ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
302 strncpy(name, dirent->name, thislen);
305 if (debugfs_read_inode(dirent->inode, &inode, name))
308 rdump_inode(dirent->inode, &inode, name, dumproot);
313 void do_rdump(int argc, char **argv)
316 struct ext2_inode inode;
321 if (common_args_process(argc, argv, 3, 3, "rdump",
322 "<directory> <native directory>", 0))
325 ino = string_to_inode(argv[1]);
329 /* Ensure ARGV[2] is a directory. */
330 i = stat(argv[2], &st);
332 com_err("rdump", errno, "while statting %s", argv[2]);
335 if (!S_ISDIR(st.st_mode)) {
336 com_err("rdump", 0, "%s is not a directory", argv[2]);
340 if (debugfs_read_inode(ino, &inode, argv[1]))
343 p = strrchr(argv[1], '/');
349 rdump_inode(ino, &inode, p, argv[2]);
352 void do_cat(int argc, char **argv)
356 if (common_inode_args_process(argc, argv, &inode, 0))
361 dump_file(argv[0], inode, 1, 0, argv[2]);