blob: 750ff3c28772ef5f4aaff4707dc97dc5d8d48f58 [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
Theodore Ts'oefc6f622008-08-27 23:07:54 -04003 *
Theodore Ts'of3db3561997-04-26 13:34:30 +00004 * Copyright (C) 1994 Theodore Ts'o. This file may be redistributed
5 * under the terms of the GNU Public License.
6 */
7
JP Abgralle0ed7402014-03-19 19:08:39 -07008#ifndef _GNU_SOURCE
Theodore Ts'oefac5a72007-04-02 18:30:46 -04009#define _GNU_SOURCE /* for O_LARGEFILE */
JP Abgralle0ed7402014-03-19 19:08:39 -070010#endif
Theodore Ts'oefac5a72007-04-02 18:30:46 -040011
Theodore Ts'of3db3561997-04-26 13:34:30 +000012#include <stdio.h>
13#include <unistd.h>
14#include <stdlib.h>
15#include <ctype.h>
16#include <string.h>
17#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000018#ifdef HAVE_ERRNO_H
19#include <errno.h>
20#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000021#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000024#include <utime.h>
25#ifdef HAVE_GETOPT_H
26#include <getopt.h>
Theodore Ts'oefc6f622008-08-27 23:07:54 -040027#else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000028extern int optind;
29extern char *optarg;
30#endif
Theodore Ts'of3db3561997-04-26 13:34:30 +000031
32#include "debugfs.h"
33
Theodore Ts'o819157d2003-01-22 18:25:39 -050034#ifndef O_LARGEFILE
35#define O_LARGEFILE 0
36#endif
37
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000038/*
39 * The mode_xlate function translates a linux mode into a native-OS mode_t.
40 */
41static struct {
42 __u16 lmask;
43 mode_t mask;
44} mode_table[] = {
45 { LINUX_S_IRUSR, S_IRUSR },
46 { LINUX_S_IWUSR, S_IWUSR },
47 { LINUX_S_IXUSR, S_IXUSR },
48 { LINUX_S_IRGRP, S_IRGRP },
49 { LINUX_S_IWGRP, S_IWGRP },
50 { LINUX_S_IXGRP, S_IXGRP },
51 { LINUX_S_IROTH, S_IROTH },
52 { LINUX_S_IWOTH, S_IWOTH },
53 { LINUX_S_IXOTH, S_IXOTH },
54 { 0, 0 }
55};
Theodore Ts'oefc6f622008-08-27 23:07:54 -040056
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000057static mode_t mode_xlate(__u16 lmode)
58{
59 mode_t mode = 0;
60 int i;
61
62 for (i=0; mode_table[i].lmask; i++) {
63 if (lmode & mode_table[i].lmask)
64 mode |= mode_table[i].mask;
65 }
66 return mode;
67}
68
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000069static void fix_perms(const char *cmd, const struct ext2_inode *inode,
70 int fd, const char *name)
71{
72 struct utimbuf ut;
73 int i;
74
75 if (fd != -1)
76 i = fchmod(fd, mode_xlate(inode->i_mode));
77 else
78 i = chmod(name, mode_xlate(inode->i_mode));
79 if (i == -1)
80 com_err(cmd, errno, "while setting permissions of %s", name);
81
82#ifndef HAVE_FCHOWN
Theodore Ts'oc5de1d42000-12-31 01:39:54 +000083 i = chown(name, inode->i_uid, inode->i_gid);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000084#else
85 if (fd != -1)
86 i = fchown(fd, inode->i_uid, inode->i_gid);
87 else
88 i = chown(name, inode->i_uid, inode->i_gid);
89#endif
90 if (i == -1)
91 com_err(cmd, errno, "while changing ownership of %s", name);
92
93 if (fd != -1)
94 close(fd);
95
96 ut.actime = inode->i_atime;
97 ut.modtime = inode->i_mtime;
98 if (utime(name, &ut) == -1)
99 com_err(cmd, errno, "while setting times of %s", name);
100}
101
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000102static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
103 int preserve, char *outname)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000104{
105 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000106 struct ext2_inode inode;
JP Abgralle0ed7402014-03-19 19:08:39 -0700107 char *buf = 0;
Theodore Ts'o5a513841997-10-25 22:41:14 +0000108 ext2_file_t e2_file;
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000109 int nbytes;
JP Abgralle0ed7402014-03-19 19:08:39 -0700110 unsigned int got, blocksize = current_fs->blocksize;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400111
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500112 if (debugfs_read_inode(ino, &inode, cmdname))
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000113 return;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000114
Theodore Ts'o5a513841997-10-25 22:41:14 +0000115 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
116 if (retval) {
117 com_err(cmdname, retval, "while opening ext2 file");
Theodore Ts'of3db3561997-04-26 13:34:30 +0000118 return;
119 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700120 retval = ext2fs_get_mem(blocksize, &buf);
121 if (retval) {
122 com_err(cmdname, retval, "while allocating memory");
123 return;
124 }
Theodore Ts'o5a513841997-10-25 22:41:14 +0000125 while (1) {
JP Abgralle0ed7402014-03-19 19:08:39 -0700126 retval = ext2fs_file_read(e2_file, buf, blocksize, &got);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400127 if (retval)
Theodore Ts'o5a513841997-10-25 22:41:14 +0000128 com_err(cmdname, retval, "while reading ext2 file");
129 if (got == 0)
130 break;
131 nbytes = write(fd, buf, got);
Theodore Ts'o54434922003-12-07 01:28:50 -0500132 if ((unsigned) nbytes != got)
Theodore Ts'o5a513841997-10-25 22:41:14 +0000133 com_err(cmdname, errno, "while writing file");
134 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700135 if (buf)
136 ext2fs_free_mem(&buf);
Theodore Ts'o5a513841997-10-25 22:41:14 +0000137 retval = ext2fs_file_close(e2_file);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000138 if (retval) {
Theodore Ts'o5a513841997-10-25 22:41:14 +0000139 com_err(cmdname, retval, "while closing ext2 file");
140 return;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000141 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400142
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000143 if (preserve)
144 fix_perms("dump_file", &inode, fd, outname);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400145
Theodore Ts'of3db3561997-04-26 13:34:30 +0000146 return;
147}
148
149void do_dump(int argc, char **argv)
150{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000151 ext2_ino_t inode;
152 int fd;
153 int c;
154 int preserve = 0;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000155 char *in_fn, *out_fn;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400156
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400157 reset_getopt();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000158 while ((c = getopt (argc, argv, "p")) != EOF) {
159 switch (c) {
160 case 'p':
161 preserve++;
162 break;
163 default:
Theodore Ts'o52995802009-01-20 13:05:25 -0500164 print_usage:
165 com_err(argv[0], 0, "Usage: dump_inode [-p] "
166 "<file> <output_file>");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000167 return;
168 }
169 }
Theodore Ts'o52995802009-01-20 13:05:25 -0500170 if (optind != argc-2)
171 goto print_usage;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000172
173 if (check_fs_open(argv[0]))
174 return;
175
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000176 in_fn = argv[optind];
177 out_fn = argv[optind+1];
178
179 inode = string_to_inode(in_fn);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400180 if (!inode)
Theodore Ts'of3db3561997-04-26 13:34:30 +0000181 return;
182
Theodore Ts'o819157d2003-01-22 18:25:39 -0500183 fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000184 if (fd < 0) {
185 com_err(argv[0], errno, "while opening %s for dump_inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000186 out_fn);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000187 return;
188 }
189
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000190 dump_file(argv[0], inode, fd, preserve, out_fn);
JP Abgralle0ed7402014-03-19 19:08:39 -0700191 if (close(fd) != 0) {
192 com_err(argv[0], errno, "while closing %s for dump_inode",
193 out_fn);
194 return;
195 }
Theodore Ts'of3db3561997-04-26 13:34:30 +0000196
Theodore Ts'of3db3561997-04-26 13:34:30 +0000197 return;
198}
199
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000200static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000201 const char *fullname)
202{
203 ext2_file_t e2_file;
204 char *buf;
205 errcode_t retval;
206
207 buf = malloc(inode->i_size + 1);
208 if (!buf) {
209 com_err("rdump", errno, "while allocating for symlink");
210 goto errout;
211 }
212
213 /* Apparently, this is the right way to detect and handle fast
214 * symlinks; see do_stat() in debugfs.c. */
215 if (inode->i_blocks == 0)
216 strcpy(buf, (char *) inode->i_block);
217 else {
218 unsigned bytes = inode->i_size;
219 char *p = buf;
220 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
221 if (retval) {
222 com_err("rdump", retval, "while opening symlink");
223 goto errout;
224 }
225 for (;;) {
226 unsigned int got;
227 retval = ext2fs_file_read(e2_file, p, bytes, &got);
228 if (retval) {
229 com_err("rdump", retval, "while reading symlink");
230 goto errout;
231 }
232 bytes -= got;
233 p += got;
234 if (got == 0 || bytes == 0)
235 break;
236 }
237 buf[inode->i_size] = 0;
238 retval = ext2fs_file_close(e2_file);
239 if (retval)
240 com_err("rdump", retval, "while closing symlink");
241 }
242
243 if (symlink(buf, fullname) == -1) {
244 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
245 goto errout;
246 }
247
248errout:
249 free(buf);
250}
251
252static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
253
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000254static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000255 const char *name, const char *dumproot)
256{
257 char *fullname;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000258
259 /* There are more efficient ways to do this, but this method
260 * requires only minimal debugging. */
261 fullname = malloc(strlen(dumproot) + strlen(name) + 2);
262 if (!fullname) {
263 com_err("rdump", errno, "while allocating memory");
264 return;
265 }
266 sprintf(fullname, "%s/%s", dumproot, name);
267
268 if (LINUX_S_ISLNK(inode->i_mode))
269 rdump_symlink(ino, inode, fullname);
270 else if (LINUX_S_ISREG(inode->i_mode)) {
271 int fd;
Theodore Ts'oefac5a72007-04-02 18:30:46 -0400272 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000273 if (fd == -1) {
274 com_err("rdump", errno, "while dumping %s", fullname);
275 goto errout;
276 }
277 dump_file("rdump", ino, fd, 1, fullname);
JP Abgralle0ed7402014-03-19 19:08:39 -0700278 if (close(fd) != 0) {
279 com_err("rdump", errno, "while dumping %s", fullname);
280 goto errout;
281 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000282 }
283 else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
284 errcode_t retval;
285
286 /* Create the directory with 0700 permissions, because we
287 * expect to have to create entries it. Then fix its perms
288 * once we've done the traversal. */
289 if (mkdir(fullname, S_IRWXU) == -1) {
290 com_err("rdump", errno, "while making directory %s", fullname);
291 goto errout;
292 }
293
294 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
295 rdump_dirent, (void *) fullname);
296 if (retval)
297 com_err("rdump", retval, "while dumping %s", fullname);
298
299 fix_perms("rdump", inode, -1, fullname);
300 }
301 /* else do nothing (don't dump device files, sockets, fifos, etc.) */
302
303errout:
304 free(fullname);
305}
306
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400307static int rdump_dirent(struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -0500308 int offset EXT2FS_ATTR((unused)),
309 int blocksize EXT2FS_ATTR((unused)),
310 char *buf EXT2FS_ATTR((unused)), void *private)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000311{
Brian Behlendorfb7729002007-03-21 15:09:15 -0400312 char name[EXT2_NAME_LEN + 1];
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000313 int thislen;
314 const char *dumproot = private;
315 struct ext2_inode inode;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000316
JP Abgralle0ed7402014-03-19 19:08:39 -0700317 thislen = dirent->name_len & 0xFF;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000318 strncpy(name, dirent->name, thislen);
319 name[thislen] = 0;
320
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500321 if (debugfs_read_inode(dirent->inode, &inode, name))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000322 return 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000323
324 rdump_inode(dirent->inode, &inode, name, dumproot);
325
326 return 0;
327}
328
329void do_rdump(int argc, char **argv)
330{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000331 ext2_ino_t ino;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000332 struct ext2_inode inode;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000333 struct stat st;
334 int i;
335 char *p;
336
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500337 if (common_args_process(argc, argv, 3, 3, "rdump",
338 "<directory> <native directory>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000339 return;
340
341 ino = string_to_inode(argv[1]);
342 if (!ino)
343 return;
344
345 /* Ensure ARGV[2] is a directory. */
346 i = stat(argv[2], &st);
347 if (i == -1) {
348 com_err("rdump", errno, "while statting %s", argv[2]);
349 return;
350 }
351 if (!S_ISDIR(st.st_mode)) {
352 com_err("rdump", 0, "%s is not a directory", argv[2]);
353 return;
354 }
355
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500356 if (debugfs_read_inode(ino, &inode, argv[1]))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000357 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000358
359 p = strrchr(argv[1], '/');
360 if (p)
361 p++;
362 else
363 p = argv[1];
364
365 rdump_inode(ino, &inode, p, argv[2]);
366}
367
Theodore Ts'of3db3561997-04-26 13:34:30 +0000368void do_cat(int argc, char **argv)
369{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000370 ext2_ino_t inode;
Theodore Ts'of3db3561997-04-26 13:34:30 +0000371
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500372 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'of3db3561997-04-26 13:34:30 +0000373 return;
374
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000375 fflush(stdout);
376 fflush(stderr);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400377 dump_file(argv[0], inode, 1, 0, argv[2]);
Theodore Ts'of3db3561997-04-26 13:34:30 +0000378
379 return;
380}
381