blob: fbaf703e0f639d766c1cb00071a458057a5c05a8 [file] [log] [blame]
Theodore Ts'of3db3561997-04-26 13:34:30 +00001/*
2 * dump.c --- dump the contents of an inode out to a file
3 *
4 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
8#include <stdio.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <ctype.h>
12#include <string.h>
13#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000014#ifdef HAVE_ERRNO_H
15#include <errno.h>
16#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000017#include <sys/types.h>
18#include <sys/stat.h>
19#include <fcntl.h>
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000020#include <utime.h>
21#ifdef HAVE_GETOPT_H
22#include <getopt.h>
23#else
24extern int optind;
25extern char *optarg;
26#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000027
28#include "debugfs.h"
29
Theodore Ts'o819157d2003-01-22 18:25:39 -050030#ifndef O_LARGEFILE
31#define O_LARGEFILE 0
32#endif
33
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000034/*
35 * The mode_xlate function translates a linux mode into a native-OS mode_t.
36 */
37static struct {
38 __u16 lmask;
39 mode_t mask;
40} mode_table[] = {
41 { LINUX_S_IRUSR, S_IRUSR },
42 { LINUX_S_IWUSR, S_IWUSR },
43 { LINUX_S_IXUSR, S_IXUSR },
44 { LINUX_S_IRGRP, S_IRGRP },
45 { LINUX_S_IWGRP, S_IWGRP },
46 { LINUX_S_IXGRP, S_IXGRP },
47 { LINUX_S_IROTH, S_IROTH },
48 { LINUX_S_IWOTH, S_IWOTH },
49 { LINUX_S_IXOTH, S_IXOTH },
50 { 0, 0 }
51};
52
53static mode_t mode_xlate(__u16 lmode)
54{
55 mode_t mode = 0;
56 int i;
57
58 for (i=0; mode_table[i].lmask; i++) {
59 if (lmode & mode_table[i].lmask)
60 mode |= mode_table[i].mask;
61 }
62 return mode;
63}
64
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000065static void fix_perms(const char *cmd, const struct ext2_inode *inode,
66 int fd, const char *name)
67{
68 struct utimbuf ut;
69 int i;
70
71 if (fd != -1)
72 i = fchmod(fd, mode_xlate(inode->i_mode));
73 else
74 i = chmod(name, mode_xlate(inode->i_mode));
75 if (i == -1)
76 com_err(cmd, errno, "while setting permissions of %s", name);
77
78#ifndef HAVE_FCHOWN
Theodore Ts'oc5de1d42000-12-31 01:39:54 +000079 i = chown(name, inode->i_uid, inode->i_gid);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000080#else
81 if (fd != -1)
82 i = fchown(fd, inode->i_uid, inode->i_gid);
83 else
84 i = chown(name, inode->i_uid, inode->i_gid);
85#endif
86 if (i == -1)
87 com_err(cmd, errno, "while changing ownership of %s", name);
88
89 if (fd != -1)
90 close(fd);
91
92 ut.actime = inode->i_atime;
93 ut.modtime = inode->i_mtime;
94 if (utime(name, &ut) == -1)
95 com_err(cmd, errno, "while setting times of %s", name);
96}
97
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000098static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
99 int preserve, char *outname)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000100{
101 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000102 struct ext2_inode inode;
Theodore Ts'o5a513841997-10-25 22:41:14 +0000103 char buf[8192];
104 ext2_file_t e2_file;
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000105 int nbytes;
106 unsigned int got;
Theodore Ts'o5a513841997-10-25 22:41:14 +0000107
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500108 if (debugfs_read_inode(ino, &inode, cmdname))
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000109 return;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000110
Theodore Ts'o5a513841997-10-25 22:41:14 +0000111 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
112 if (retval) {
113 com_err(cmdname, retval, "while opening ext2 file");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000114 return;
115 }
Theodore Ts'o5a513841997-10-25 22:41:14 +0000116 while (1) {
117 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
118 if (retval)
119 com_err(cmdname, retval, "while reading ext2 file");
120 if (got == 0)
121 break;
122 nbytes = write(fd, buf, got);
Theodore Ts'o54434922003-12-07 01:28:50 -0500123 if ((unsigned) nbytes != got)
Theodore Ts'o5a513841997-10-25 22:41:14 +0000124 com_err(cmdname, errno, "while writing file");
125 }
126 retval = ext2fs_file_close(e2_file);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000127 if (retval) {
Theodore Ts'o5a513841997-10-25 22:41:14 +0000128 com_err(cmdname, retval, "while closing ext2 file");
129 return;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000130 }
Theodore Ts'o5a513841997-10-25 22:41:14 +0000131
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000132 if (preserve)
133 fix_perms("dump_file", &inode, fd, outname);
134 else if (fd != 1)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000135 close(fd);
136
Theodore Ts'of3db3561997-04-26 13:34:30 +0000137 return;
138}
139
140void do_dump(int argc, char **argv)
141{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000142 ext2_ino_t inode;
143 int fd;
144 int c;
145 int preserve = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000146 const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000147 char *in_fn, *out_fn;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000148
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400149 reset_getopt();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000150 while ((c = getopt (argc, argv, "p")) != EOF) {
151 switch (c) {
152 case 'p':
153 preserve++;
154 break;
155 default:
156 com_err(argv[0], 0, dump_usage);
157 return;
158 }
159 }
160 if (optind != argc-2) {
161 com_err(argv[0], 0, dump_usage);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000162 return;
163 }
164
165 if (check_fs_open(argv[0]))
166 return;
167
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000168 in_fn = argv[optind];
169 out_fn = argv[optind+1];
170
171 inode = string_to_inode(in_fn);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000172 if (!inode)
173 return;
174
Theodore Ts'o819157d2003-01-22 18:25:39 -0500175 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000176 if (fd < 0) {
177 com_err(argv[0], errno, "while opening %s for dump_inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000178 out_fn);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000179 return;
180 }
181
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000182 dump_file(argv[0], inode, fd, preserve, out_fn);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000183
Theodore Ts'of3db3561997-04-26 13:34:30 +0000184 return;
185}
186
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000187static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000188 const char *fullname)
189{
190 ext2_file_t e2_file;
191 char *buf;
192 errcode_t retval;
193
194 buf = malloc(inode->i_size + 1);
195 if (!buf) {
196 com_err("rdump", errno, "while allocating for symlink");
197 goto errout;
198 }
199
200 /* Apparently, this is the right way to detect and handle fast
201 * symlinks; see do_stat() in debugfs.c. */
202 if (inode->i_blocks == 0)
203 strcpy(buf, (char *) inode->i_block);
204 else {
205 unsigned bytes = inode->i_size;
206 char *p = buf;
207 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
208 if (retval) {
209 com_err("rdump", retval, "while opening symlink");
210 goto errout;
211 }
212 for (;;) {
213 unsigned int got;
214 retval = ext2fs_file_read(e2_file, p, bytes, &got);
215 if (retval) {
216 com_err("rdump", retval, "while reading symlink");
217 goto errout;
218 }
219 bytes -= got;
220 p += got;
221 if (got == 0 || bytes == 0)
222 break;
223 }
224 buf[inode->i_size] = 0;
225 retval = ext2fs_file_close(e2_file);
226 if (retval)
227 com_err("rdump", retval, "while closing symlink");
228 }
229
230 if (symlink(buf, fullname) == -1) {
231 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
232 goto errout;
233 }
234
235errout:
236 free(buf);
237}
238
239static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
240
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000241static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000242 const char *name, const char *dumproot)
243{
244 char *fullname;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000245
246 /* There are more efficient ways to do this, but this method
247 * requires only minimal debugging. */
248 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
249 if (!fullname) {
250 com_err("rdump", errno, "while allocating memory");
251 return;
252 }
253 sprintf(fullname, "%s/%s", dumproot, name);
254
255 if (LINUX_S_ISLNK(inode->i_mode))
256 rdump_symlink(ino, inode, fullname);
257 else if (LINUX_S_ISREG(inode->i_mode)) {
258 int fd;
259 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
260 if (fd == -1) {
261 com_err("rdump", errno, "while dumping %s", fullname);
262 goto errout;
263 }
264 dump_file("rdump", ino, fd, 1, fullname);
265 }
266 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
267 errcode_t retval;
268
269 /* Create the directory with 0700 permissions, because we
270 * expect to have to create entries it. Then fix its perms
271 * once we've done the traversal. */
272 if (mkdir(fullname, S_IRWXU) == -1) {
273 com_err("rdump", errno, "while making directory %s", fullname);
274 goto errout;
275 }
276
277 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
278 rdump_dirent, (void *) fullname);
279 if (retval)
280 com_err("rdump", retval, "while dumping %s", fullname);
281
282 fix_perms("rdump", inode, -1, fullname);
283 }
284 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
285
286errout:
287 free(fullname);
288}
289
Theodore Ts'o54434922003-12-07 01:28:50 -0500290static int rdump_dirent(struct ext2_dir_entry *dirent,
291 int offset EXT2FS_ATTR((unused)),
292 int blocksize EXT2FS_ATTR((unused)),
293 char *buf EXT2FS_ATTR((unused)), void *private)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000294{
295 char name[EXT2_NAME_LEN];
296 int thislen;
297 const char *dumproot = private;
298 struct ext2_inode inode;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000299
300 thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
301 ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
302 strncpy(name, dirent->name, thislen);
303 name[thislen] = 0;
304
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500305 if (debugfs_read_inode(dirent->inode, &inode, name))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000306 return 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000307
308 rdump_inode(dirent->inode, &inode, name, dumproot);
309
310 return 0;
311}
312
313void do_rdump(int argc, char **argv)
314{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000315 ext2_ino_t ino;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000316 struct ext2_inode inode;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000317 struct stat st;
318 int i;
319 char *p;
320
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500321 if (common_args_process(argc, argv, 3, 3, "rdump",
322 "<directory> <native directory>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000323 return;
324
325 ino = string_to_inode(argv[1]);
326 if (!ino)
327 return;
328
329 /* Ensure ARGV[2] is a directory. */
330 i = stat(argv[2], &st);
331 if (i == -1) {
332 com_err("rdump", errno, "while statting %s", argv[2]);
333 return;
334 }
335 if (!S_ISDIR(st.st_mode)) {
336 com_err("rdump", 0, "%s is not a directory", argv[2]);
337 return;
338 }
339
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500340 if (debugfs_read_inode(ino, &inode, argv[1]))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000341 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000342
343 p = strrchr(argv[1], '/');
344 if (p)
345 p++;
346 else
347 p = argv[1];
348
349 rdump_inode(ino, &inode, p, argv[2]);
350}
351
Theodore Ts'of3db3561997-04-26 13:34:30 +0000352void do_cat(int argc, char **argv)
353{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000354 ext2_ino_t inode;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000355
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500356 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000357 return;
358
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000359 fflush(stdout);
360 fflush(stderr);
361 dump_file(argv[0], inode, 1, 0, argv[2]);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000362
363 return;
364}
365