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 = chmod(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(char *cmdname, ino_t ino, int fd, int preserve,
101 struct ext2_inode inode;
108 retval = ext2fs_read_inode(current_fs, ino, &inode);
110 com_err(cmdname, retval,
111 "while reading inode %u in dump_file", ino);
115 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
117 com_err(cmdname, retval, "while opening ext2 file");
121 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
123 com_err(cmdname, retval, "while reading ext2 file");
126 nbytes = write(fd, buf, got);
128 com_err(cmdname, errno, "while writing file");
130 retval = ext2fs_file_close(e2_file);
132 com_err(cmdname, retval, "while closing ext2 file");
137 fix_perms("dump_file", &inode, fd, outname);
144 void do_dump(int argc, char **argv)
150 const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
151 char *in_fn, *out_fn;
155 optreset = 1; /* Makes BSD getopt happy */
157 while ((c = getopt (argc, argv, "p")) != EOF) {
163 com_err(argv[0], 0, dump_usage);
167 if (optind != argc-2) {
168 com_err(argv[0], 0, dump_usage);
172 if (check_fs_open(argv[0]))
175 in_fn = argv[optind];
176 out_fn = argv[optind+1];
178 inode = string_to_inode(in_fn);
182 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC, 0666);
184 com_err(argv[0], errno, "while opening %s for dump_inode",
189 dump_file(argv[0], inode, fd, preserve, out_fn);
194 static void rdump_symlink(ino_t ino, struct ext2_inode *inode,
195 const char *fullname)
201 buf = malloc(inode->i_size + 1);
203 com_err("rdump", errno, "while allocating for symlink");
207 /* Apparently, this is the right way to detect and handle fast
208 * symlinks; see do_stat() in debugfs.c. */
209 if (inode->i_blocks == 0)
210 strcpy(buf, (char *) inode->i_block);
212 unsigned bytes = inode->i_size;
214 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
216 com_err("rdump", retval, "while opening symlink");
221 retval = ext2fs_file_read(e2_file, p, bytes, &got);
223 com_err("rdump", retval, "while reading symlink");
228 if (got == 0 || bytes == 0)
231 buf[inode->i_size] = 0;
232 retval = ext2fs_file_close(e2_file);
234 com_err("rdump", retval, "while closing symlink");
237 if (symlink(buf, fullname) == -1) {
238 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
246 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
248 static void rdump_inode(ino_t ino, struct ext2_inode *inode,
249 const char *name, const char *dumproot)
254 /* There are more efficient ways to do this, but this method
255 * requires only minimal debugging. */
256 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
258 com_err("rdump", errno, "while allocating memory");
261 sprintf(fullname, "%s/%s", dumproot, name);
263 if (LINUX_S_ISLNK(inode->i_mode))
264 rdump_symlink(ino, inode, fullname);
265 else if (LINUX_S_ISREG(inode->i_mode)) {
267 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
269 com_err("rdump", errno, "while dumping %s", fullname);
272 dump_file("rdump", ino, fd, 1, fullname);
274 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
277 /* Create the directory with 0700 permissions, because we
278 * expect to have to create entries it. Then fix its perms
279 * once we've done the traversal. */
280 if (mkdir(fullname, S_IRWXU) == -1) {
281 com_err("rdump", errno, "while making directory %s", fullname);
285 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
286 rdump_dirent, (void *) fullname);
288 com_err("rdump", retval, "while dumping %s", fullname);
290 fix_perms("rdump", inode, -1, fullname);
292 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
298 static int rdump_dirent(struct ext2_dir_entry *dirent, int offset,
299 int blocksize, char *buf, void *private)
301 char name[EXT2_NAME_LEN];
303 const char *dumproot = private;
304 struct ext2_inode inode;
307 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
308 ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
309 strncpy(name, dirent->name, thislen);
312 retval = ext2fs_read_inode(current_fs, dirent->inode, &inode);
314 com_err("rdump", retval, "while dumping %s/%s", dumproot, name);
318 rdump_inode(dirent->inode, &inode, name, dumproot);
323 void do_rdump(int argc, char **argv)
326 struct ext2_inode inode;
333 com_err(argv[0], 0, "Usage: rdump <directory> <native directory>");
337 if (check_fs_open(argv[0]))
340 ino = string_to_inode(argv[1]);
344 /* Ensure ARGV[2] is a directory. */
345 i = stat(argv[2], &st);
347 com_err("rdump", errno, "while statting %s", argv[2]);
350 if (!S_ISDIR(st.st_mode)) {
351 com_err("rdump", 0, "%s is not a directory", argv[2]);
355 retval = ext2fs_read_inode(current_fs, ino, &inode);
357 com_err("rdump", retval, "while dumping %s", argv[1]);
361 p = strrchr(argv[1], '/');
367 rdump_inode(ino, &inode, p, argv[2]);
370 void do_cat(int argc, char **argv)
375 com_err(argv[0], 0, "Usage: cat <file>");
379 if (check_fs_open(argv[0]))
382 inode = string_to_inode(argv[1]);
388 dump_file(argv[0], inode, 1, 0, argv[2]);