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 dump_file(char *cmdname, ino_t ino, int fd, int preserve,
68 struct ext2_inode inode;
74 retval = ext2fs_read_inode(current_fs, ino, &inode);
76 com_err(cmdname, retval,
77 "while reading inode %u in dump_file", ino);
81 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
83 com_err(cmdname, retval, "while opening ext2 file");
87 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
89 com_err(cmdname, retval, "while reading ext2 file");
92 nbytes = write(fd, buf, got);
94 com_err(cmdname, errno, "while writing file");
96 retval = ext2fs_file_close(e2_file);
98 com_err(cmdname, retval, "while closing ext2 file");
104 if (fchown(fd, inode.i_uid, inode.i_gid) < 0)
105 com_err("dump_file", errno,
106 "while changing ownership of %s", outname);
108 if (chown(outname, inode.i_uid, inode.i_gid) < 0)
109 com_err("dump_file", errno,
110 "while changing ownership of %s", outname);
113 if (fchmod(fd, mode_xlate(inode.i_mode)) < 0)
114 com_err("dump_file", errno,
115 "while setting permissions of %s", outname);
116 ut.actime = inode.i_atime;
117 ut.modtime = inode.i_mtime;
119 if (utime(outname, &ut) < 0)
120 com_err("dump_file", errno,
121 "while setting times on %s", outname);
128 void do_dump(int argc, char **argv)
134 const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
135 char *in_fn, *out_fn;
139 optreset = 1; /* Makes BSD getopt happy */
141 while ((c = getopt (argc, argv, "p")) != EOF) {
147 com_err(argv[0], 0, dump_usage);
151 if (optind != argc-2) {
152 com_err(argv[0], 0, dump_usage);
156 if (check_fs_open(argv[0]))
159 in_fn = argv[optind];
160 out_fn = argv[optind+1];
162 inode = string_to_inode(in_fn);
166 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC, 0666);
168 com_err(argv[0], errno, "while opening %s for dump_inode",
173 dump_file(argv[0], inode, fd, preserve, out_fn);
178 void do_cat(int argc, char **argv)
183 com_err(argv[0], 0, "Usage: cat <file>");
187 if (check_fs_open(argv[0]))
190 inode = string_to_inode(argv[1]);
196 dump_file(argv[0], inode, 1, 0, argv[2]);