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>
28 extern int optreset; /* defined by BSD, but not others */
34 * The mode_xlate function translates a linux mode into a native-OS mode_t.
40 { LINUX_S_IRUSR, S_IRUSR },
41 { LINUX_S_IWUSR, S_IWUSR },
42 { LINUX_S_IXUSR, S_IXUSR },
43 { LINUX_S_IRGRP, S_IRGRP },
44 { LINUX_S_IWGRP, S_IWGRP },
45 { LINUX_S_IXGRP, S_IXGRP },
46 { LINUX_S_IROTH, S_IROTH },
47 { LINUX_S_IWOTH, S_IWOTH },
48 { LINUX_S_IXOTH, S_IXOTH },
52 static mode_t mode_xlate(__u16 lmode)
57 for (i=0; mode_table[i].lmask; i++) {
58 if (lmode & mode_table[i].lmask)
59 mode |= mode_table[i].mask;
64 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
65 int fd, const char *name)
71 i = fchmod(fd, mode_xlate(inode->i_mode));
73 i = chmod(name, mode_xlate(inode->i_mode));
75 com_err(cmd, errno, "while setting permissions of %s", name);
78 i = chown(name, inode->i_uid, inode->i_gid);
81 i = fchown(fd, inode->i_uid, inode->i_gid);
83 i = chown(name, inode->i_uid, inode->i_gid);
86 com_err(cmd, errno, "while changing ownership of %s", name);
91 ut.actime = inode->i_atime;
92 ut.modtime = inode->i_mtime;
93 if (utime(name, &ut) == -1)
94 com_err(cmd, errno, "while setting times of %s", name);
97 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
98 int preserve, char *outname)
101 struct ext2_inode inode;
107 retval = ext2fs_read_inode(current_fs, ino, &inode);
109 com_err(cmdname, retval,
110 "while reading inode %u in dump_file", ino);
114 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
116 com_err(cmdname, retval, "while opening ext2 file");
120 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
122 com_err(cmdname, retval, "while reading ext2 file");
125 nbytes = write(fd, buf, got);
127 com_err(cmdname, errno, "while writing file");
129 retval = ext2fs_file_close(e2_file);
131 com_err(cmdname, retval, "while closing ext2 file");
136 fix_perms("dump_file", &inode, fd, outname);
143 void do_dump(int argc, char **argv)
149 const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
150 char *in_fn, *out_fn;
154 optreset = 1; /* Makes BSD getopt happy */
156 while ((c = getopt (argc, argv, "p")) != EOF) {
162 com_err(argv[0], 0, dump_usage);
166 if (optind != argc-2) {
167 com_err(argv[0], 0, dump_usage);
171 if (check_fs_open(argv[0]))
174 in_fn = argv[optind];
175 out_fn = argv[optind+1];
177 inode = string_to_inode(in_fn);
181 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC, 0666);
183 com_err(argv[0], errno, "while opening %s for dump_inode",
188 dump_file(argv[0], inode, fd, preserve, out_fn);
193 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
194 const char *fullname)
200 buf = malloc(inode->i_size + 1);
202 com_err("rdump", errno, "while allocating for symlink");
206 /* Apparently, this is the right way to detect and handle fast
207 * symlinks; see do_stat() in debugfs.c. */
208 if (inode->i_blocks == 0)
209 strcpy(buf, (char *) inode->i_block);
211 unsigned bytes = inode->i_size;
213 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
215 com_err("rdump", retval, "while opening symlink");
220 retval = ext2fs_file_read(e2_file, p, bytes, &got);
222 com_err("rdump", retval, "while reading symlink");
227 if (got == 0 || bytes == 0)
230 buf[inode->i_size] = 0;
231 retval = ext2fs_file_close(e2_file);
233 com_err("rdump", retval, "while closing symlink");
236 if (symlink(buf, fullname) == -1) {
237 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
245 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
247 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
248 const char *name, const char *dumproot)
252 /* There are more efficient ways to do this, but this method
253 * requires only minimal debugging. */
254 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
256 com_err("rdump", errno, "while allocating memory");
259 sprintf(fullname, "%s/%s", dumproot, name);
261 if (LINUX_S_ISLNK(inode->i_mode))
262 rdump_symlink(ino, inode, fullname);
263 else if (LINUX_S_ISREG(inode->i_mode)) {
265 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
267 com_err("rdump", errno, "while dumping %s", fullname);
270 dump_file("rdump", ino, fd, 1, fullname);
272 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
275 /* Create the directory with 0700 permissions, because we
276 * expect to have to create entries it. Then fix its perms
277 * once we've done the traversal. */
278 if (mkdir(fullname, S_IRWXU) == -1) {
279 com_err("rdump", errno, "while making directory %s", fullname);
283 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
284 rdump_dirent, (void *) fullname);
286 com_err("rdump", retval, "while dumping %s", fullname);
288 fix_perms("rdump", inode, -1, fullname);
290 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
296 static int rdump_dirent(struct ext2_dir_entry *dirent, int offset,
297 int blocksize, char *buf, void *private)
299 char name[EXT2_NAME_LEN];
301 const char *dumproot = private;
302 struct ext2_inode inode;
305 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
306 ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
307 strncpy(name, dirent->name, thislen);
310 retval = ext2fs_read_inode(current_fs, dirent->inode, &inode);
312 com_err("rdump", retval, "while dumping %s/%s", dumproot, name);
316 rdump_inode(dirent->inode, &inode, name, dumproot);
321 void do_rdump(int argc, char **argv)
324 struct ext2_inode inode;
331 com_err(argv[0], 0, "Usage: rdump <directory> <native directory>");
335 if (check_fs_open(argv[0]))
338 ino = string_to_inode(argv[1]);
342 /* Ensure ARGV[2] is a directory. */
343 i = stat(argv[2], &st);
345 com_err("rdump", errno, "while statting %s", argv[2]);
348 if (!S_ISDIR(st.st_mode)) {
349 com_err("rdump", 0, "%s is not a directory", argv[2]);
353 retval = ext2fs_read_inode(current_fs, ino, &inode);
355 com_err("rdump", retval, "while dumping %s", argv[1]);
359 p = strrchr(argv[1], '/');
365 rdump_inode(ino, &inode, p, argv[2]);
368 void do_cat(int argc, char **argv)
373 com_err(argv[0], 0, "Usage: cat <file>");
377 if (check_fs_open(argv[0]))
380 inode = string_to_inode(argv[1]);
386 dump_file(argv[0], inode, 1, 0, argv[2]);