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;
111 unsigned int got, blocksize = current_fs->blocksize;
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");
121 retval = ext2fs_get_mem(blocksize, &buf);
123 com_err(cmdname, retval, "while allocating memory");
127 retval = ext2fs_file_read(e2_file, buf, blocksize, &got);
129 com_err(cmdname, retval, "while reading ext2 file");
132 nbytes = write(fd, buf, got);
133 if ((unsigned) nbytes != got)
134 com_err(cmdname, errno, "while writing file");
137 ext2fs_free_mem(&buf);
138 retval = ext2fs_file_close(e2_file);
140 com_err(cmdname, retval, "while closing ext2 file");
145 fix_perms("dump_file", &inode, fd, outname);
152 void do_dump(int argc, char **argv)
158 char *in_fn, *out_fn;
161 while ((c = getopt (argc, argv, "p")) != EOF) {
168 com_err(argv[0], 0, "Usage: dump_inode [-p] "
169 "<file> <output_file>");
173 if (optind != argc-2)
176 if (check_fs_open(argv[0]))
179 in_fn = argv[optind];
180 out_fn = argv[optind+1];
182 inode = string_to_inode(in_fn);
186 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
188 com_err(argv[0], errno, "while opening %s for dump_inode",
193 dump_file(argv[0], inode, fd, preserve, out_fn);
198 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
199 const char *fullname)
205 buf = malloc(inode->i_size + 1);
207 com_err("rdump", errno, "while allocating for symlink");
211 /* Apparently, this is the right way to detect and handle fast
212 * symlinks; see do_stat() in debugfs.c. */
213 if (inode->i_blocks == 0)
214 strcpy(buf, (char *) inode->i_block);
216 unsigned bytes = inode->i_size;
218 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
220 com_err("rdump", retval, "while opening symlink");
225 retval = ext2fs_file_read(e2_file, p, bytes, &got);
227 com_err("rdump", retval, "while reading symlink");
232 if (got == 0 || bytes == 0)
235 buf[inode->i_size] = 0;
236 retval = ext2fs_file_close(e2_file);
238 com_err("rdump", retval, "while closing symlink");
241 if (symlink(buf, fullname) == -1) {
242 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
250 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
252 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
253 const char *name, const char *dumproot)
257 /* There are more efficient ways to do this, but this method
258 * requires only minimal debugging. */
259 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
261 com_err("rdump", errno, "while allocating memory");
264 sprintf(fullname, "%s/%s", dumproot, name);
266 if (LINUX_S_ISLNK(inode->i_mode))
267 rdump_symlink(ino, inode, fullname);
268 else if (LINUX_S_ISREG(inode->i_mode)) {
270 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
272 com_err("rdump", errno, "while dumping %s", fullname);
275 dump_file("rdump", ino, fd, 1, fullname);
277 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
280 /* Create the directory with 0700 permissions, because we
281 * expect to have to create entries it. Then fix its perms
282 * once we've done the traversal. */
283 if (mkdir(fullname, S_IRWXU) == -1) {
284 com_err("rdump", errno, "while making directory %s", fullname);
288 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
289 rdump_dirent, (void *) fullname);
291 com_err("rdump", retval, "while dumping %s", fullname);
293 fix_perms("rdump", inode, -1, fullname);
295 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
301 static int rdump_dirent(struct ext2_dir_entry *dirent,
302 int offset EXT2FS_ATTR((unused)),
303 int blocksize EXT2FS_ATTR((unused)),
304 char *buf EXT2FS_ATTR((unused)), void *private)
306 char name[EXT2_NAME_LEN + 1];
308 const char *dumproot = private;
309 struct ext2_inode inode;
311 thislen = dirent->name_len & 0xFF;
312 strncpy(name, dirent->name, thislen);
315 if (debugfs_read_inode(dirent->inode, &inode, name))
318 rdump_inode(dirent->inode, &inode, name, dumproot);
323 void do_rdump(int argc, char **argv)
326 struct ext2_inode inode;
331 if (common_args_process(argc, argv, 3, 3, "rdump",
332 "<directory> <native directory>", 0))
335 ino = string_to_inode(argv[1]);
339 /* Ensure ARGV[2] is a directory. */
340 i = stat(argv[2], &st);
342 com_err("rdump", errno, "while statting %s", argv[2]);
345 if (!S_ISDIR(st.st_mode)) {
346 com_err("rdump", 0, "%s is not a directory", argv[2]);
350 if (debugfs_read_inode(ino, &inode, argv[1]))
353 p = strrchr(argv[1], '/');
359 rdump_inode(ino, &inode, p, argv[2]);
362 void do_cat(int argc, char **argv)
366 if (common_inode_args_process(argc, argv, &inode, 0))
371 dump_file(argv[0], inode, 1, 0, argv[2]);