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 */
22 #include <sys/types.h>
40 * The mode_xlate function translates a linux mode into a native-OS mode_t.
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 },
58 static mode_t mode_xlate(__u16 lmode)
63 for (i=0; mode_table[i].lmask; i++) {
64 if (lmode & mode_table[i].lmask)
65 mode |= mode_table[i].mask;
70 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
71 int fd, const char *name)
77 i = fchmod(fd, mode_xlate(inode->i_mode));
79 i = chmod(name, mode_xlate(inode->i_mode));
81 com_err(cmd, errno, "while setting permissions of %s", name);
84 i = chown(name, inode->i_uid, inode->i_gid);
87 i = fchown(fd, inode->i_uid, inode->i_gid);
89 i = chown(name, inode->i_uid, inode->i_gid);
92 com_err(cmd, errno, "while changing ownership of %s", name);
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);
103 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
104 int preserve, char *outname)
107 struct ext2_inode inode;
113 if (debugfs_read_inode(ino, &inode, cmdname))
116 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
118 com_err(cmdname, retval, "while opening ext2 file");
122 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
124 com_err(cmdname, retval, "while reading ext2 file");
127 nbytes = write(fd, buf, got);
128 if ((unsigned) nbytes != got)
129 com_err(cmdname, errno, "while writing file");
131 retval = ext2fs_file_close(e2_file);
133 com_err(cmdname, retval, "while closing ext2 file");
138 fix_perms("dump_file", &inode, fd, outname);
145 void do_dump(int argc, char **argv)
151 char *in_fn, *out_fn;
154 while ((c = getopt (argc, argv, "p")) != EOF) {
161 com_err(argv[0], 0, "Usage: dump_inode [-p] "
162 "<file> <output_file>");
166 if (optind != argc-2)
169 if (check_fs_open(argv[0]))
172 in_fn = argv[optind];
173 out_fn = argv[optind+1];
175 inode = string_to_inode(in_fn);
179 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
181 com_err(argv[0], errno, "while opening %s for dump_inode",
186 dump_file(argv[0], inode, fd, preserve, out_fn);
191 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
192 const char *fullname)
198 buf = malloc(inode->i_size + 1);
200 com_err("rdump", errno, "while allocating for symlink");
204 /* Apparently, this is the right way to detect and handle fast
205 * symlinks; see do_stat() in debugfs.c. */
206 if (inode->i_blocks == 0)
207 strcpy(buf, (char *) inode->i_block);
209 unsigned bytes = inode->i_size;
211 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
213 com_err("rdump", retval, "while opening symlink");
218 retval = ext2fs_file_read(e2_file, p, bytes, &got);
220 com_err("rdump", retval, "while reading symlink");
225 if (got == 0 || bytes == 0)
228 buf[inode->i_size] = 0;
229 retval = ext2fs_file_close(e2_file);
231 com_err("rdump", retval, "while closing symlink");
234 if (symlink(buf, fullname) == -1) {
235 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
243 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
245 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
246 const char *name, const char *dumproot)
250 /* There are more efficient ways to do this, but this method
251 * requires only minimal debugging. */
252 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
254 com_err("rdump", errno, "while allocating memory");
257 sprintf(fullname, "%s/%s", dumproot, name);
259 if (LINUX_S_ISLNK(inode->i_mode))
260 rdump_symlink(ino, inode, fullname);
261 else if (LINUX_S_ISREG(inode->i_mode)) {
263 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
265 com_err("rdump", errno, "while dumping %s", fullname);
268 dump_file("rdump", ino, fd, 1, fullname);
270 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
273 /* Create the directory with 0700 permissions, because we
274 * expect to have to create entries it. Then fix its perms
275 * once we've done the traversal. */
276 if (mkdir(fullname, S_IRWXU) == -1) {
277 com_err("rdump", errno, "while making directory %s", fullname);
281 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
282 rdump_dirent, (void *) fullname);
284 com_err("rdump", retval, "while dumping %s", fullname);
286 fix_perms("rdump", inode, -1, fullname);
288 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
294 static int rdump_dirent(struct ext2_dir_entry *dirent,
295 int offset EXT2FS_ATTR((unused)),
296 int blocksize EXT2FS_ATTR((unused)),
297 char *buf EXT2FS_ATTR((unused)), void *private)
299 char name[EXT2_NAME_LEN + 1];
301 const char *dumproot = private;
302 struct ext2_inode inode;
304 thislen = dirent->name_len & 0xFF;
305 strncpy(name, dirent->name, thislen);
308 if (debugfs_read_inode(dirent->inode, &inode, name))
311 rdump_inode(dirent->inode, &inode, name, dumproot);
316 void do_rdump(int argc, char **argv)
319 struct ext2_inode inode;
324 if (common_args_process(argc, argv, 3, 3, "rdump",
325 "<directory> <native directory>", 0))
328 ino = string_to_inode(argv[1]);
332 /* Ensure ARGV[2] is a directory. */
333 i = stat(argv[2], &st);
335 com_err("rdump", errno, "while statting %s", argv[2]);
338 if (!S_ISDIR(st.st_mode)) {
339 com_err("rdump", 0, "%s is not a directory", argv[2]);
343 if (debugfs_read_inode(ino, &inode, argv[1]))
346 p = strrchr(argv[1], '/');
352 rdump_inode(ino, &inode, p, argv[2]);
355 void do_cat(int argc, char **argv)
359 if (common_inode_args_process(argc, argv, &inode, 0))
364 dump_file(argv[0], inode, 1, 0, argv[2]);