Whamcloud - gitweb
9409ab656db96e0d32e760e9ac0b4576f2102643
[tools/e2fsprogs.git] / debugfs / dump.c
1 /*
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 #ifndef _GNU_SOURCE
9 #define _GNU_SOURCE /* for O_LARGEFILE */
10 #endif
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <time.h>
19 #ifdef HAVE_ERRNO_H
20 #include <errno.h>
21 #endif
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <utime.h>
26 #ifdef HAVE_GETOPT_H
27 #include <getopt.h>
28 #else
29 extern int optind;
30 extern char *optarg;
31 #endif
32
33 #include "debugfs.h"
34
35 #ifndef O_LARGEFILE
36 #define O_LARGEFILE 0
37 #endif
38
39 /*
40  * The mode_xlate function translates a linux mode into a native-OS mode_t.
41  */
42 static struct {
43         __u16 lmask;
44         mode_t mask;
45 } mode_table[] = {
46         { LINUX_S_IRUSR, S_IRUSR },
47         { LINUX_S_IWUSR, S_IWUSR },
48         { LINUX_S_IXUSR, S_IXUSR },
49         { LINUX_S_IRGRP, S_IRGRP },
50         { LINUX_S_IWGRP, S_IWGRP },
51         { LINUX_S_IXGRP, S_IXGRP },
52         { LINUX_S_IROTH, S_IROTH },
53         { LINUX_S_IWOTH, S_IWOTH },
54         { LINUX_S_IXOTH, S_IXOTH },
55         { 0, 0 }
56 };
57
58 static mode_t mode_xlate(__u16 lmode)
59 {
60         mode_t  mode = 0;
61         int     i;
62
63         for (i=0; mode_table[i].lmask; i++) {
64                 if (lmode & mode_table[i].lmask)
65                         mode |= mode_table[i].mask;
66         }
67         return mode;
68 }
69
70 static void fix_perms(const char *cmd, const struct ext2_inode *inode,
71                       int fd, const char *name)
72 {
73         struct utimbuf ut;
74         int i;
75
76         if (fd != -1)
77                 i = fchmod(fd, mode_xlate(inode->i_mode));
78         else
79                 i = chmod(name, mode_xlate(inode->i_mode));
80         if (i == -1)
81                 com_err(cmd, errno, "while setting permissions of %s", name);
82
83 #ifndef HAVE_FCHOWN
84         i = chown(name, inode->i_uid, inode->i_gid);
85 #else
86         if (fd != -1)
87                 i = fchown(fd, inode->i_uid, inode->i_gid);
88         else
89                 i = chown(name, inode->i_uid, inode->i_gid);
90 #endif
91         if (i == -1)
92                 com_err(cmd, errno, "while changing ownership of %s", name);
93
94         if (fd != -1)
95                 close(fd);
96
97         ut.actime = inode->i_atime;
98         ut.modtime = inode->i_mtime;
99         if (utime(name, &ut) == -1)
100                 com_err(cmd, errno, "while setting times of %s", name);
101 }
102
103 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
104                       int preserve, char *outname)
105 {
106         errcode_t retval;
107         struct ext2_inode       inode;
108         char            *buf = 0;
109         ext2_file_t     e2_file;
110         int             nbytes;
111         unsigned int    got, blocksize = current_fs->blocksize;
112
113         if (debugfs_read_inode(ino, &inode, cmdname))
114                 return;
115
116         retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
117         if (retval) {
118                 com_err(cmdname, retval, "while opening ext2 file");
119                 return;
120         }
121         retval = ext2fs_get_mem(blocksize, &buf);
122         if (retval) {
123                 com_err(cmdname, retval, "while allocating memory");
124                 return;
125         }
126         while (1) {
127                 retval = ext2fs_file_read(e2_file, buf, blocksize, &got);
128                 if (retval)
129                         com_err(cmdname, retval, "while reading ext2 file");
130                 if (got == 0)
131                         break;
132                 nbytes = write(fd, buf, got);
133                 if ((unsigned) nbytes != got)
134                         com_err(cmdname, errno, "while writing file");
135         }
136         if (buf)
137                 ext2fs_free_mem(&buf);
138         retval = ext2fs_file_close(e2_file);
139         if (retval) {
140                 com_err(cmdname, retval, "while closing ext2 file");
141                 return;
142         }
143
144         if (preserve)
145                 fix_perms("dump_file", &inode, fd, outname);
146         else if (fd != 1)
147                 close(fd);
148
149         return;
150 }
151
152 void do_dump(int argc, char **argv)
153 {
154         ext2_ino_t      inode;
155         int             fd;
156         int             c;
157         int             preserve = 0;
158         char            *in_fn, *out_fn;
159
160         reset_getopt();
161         while ((c = getopt (argc, argv, "p")) != EOF) {
162                 switch (c) {
163                 case 'p':
164                         preserve++;
165                         break;
166                 default:
167                 print_usage:
168                         com_err(argv[0], 0, "Usage: dump_inode [-p] "
169                                 "<file> <output_file>");
170                         return;
171                 }
172         }
173         if (optind != argc-2)
174                 goto print_usage;
175
176         if (check_fs_open(argv[0]))
177                 return;
178
179         in_fn = argv[optind];
180         out_fn = argv[optind+1];
181
182         inode = string_to_inode(in_fn);
183         if (!inode)
184                 return;
185
186         fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
187         if (fd < 0) {
188                 com_err(argv[0], errno, "while opening %s for dump_inode",
189                         out_fn);
190                 return;
191         }
192
193         dump_file(argv[0], inode, fd, preserve, out_fn);
194
195         return;
196 }
197
198 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
199                           const char *fullname)
200 {
201         ext2_file_t e2_file;
202         char *buf;
203         errcode_t retval;
204
205         buf = malloc(inode->i_size + 1);
206         if (!buf) {
207                 com_err("rdump", errno, "while allocating for symlink");
208                 goto errout;
209         }
210
211         /* Apparently, this is the right way to detect and handle fast
212          * symlinks; see do_stat() in debugfs.c. */
213         if (inode->i_blocks == 0)
214                 strcpy(buf, (char *) inode->i_block);
215         else {
216                 unsigned bytes = inode->i_size;
217                 char *p = buf;
218                 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
219                 if (retval) {
220                         com_err("rdump", retval, "while opening symlink");
221                         goto errout;
222                 }
223                 for (;;) {
224                         unsigned int got;
225                         retval = ext2fs_file_read(e2_file, p, bytes, &got);
226                         if (retval) {
227                                 com_err("rdump", retval, "while reading symlink");
228                                 goto errout;
229                         }
230                         bytes -= got;
231                         p += got;
232                         if (got == 0 || bytes == 0)
233                                 break;
234                 }
235                 buf[inode->i_size] = 0;
236                 retval = ext2fs_file_close(e2_file);
237                 if (retval)
238                         com_err("rdump", retval, "while closing symlink");
239         }
240
241         if (symlink(buf, fullname) == -1) {
242                 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
243                 goto errout;
244         }
245
246 errout:
247         free(buf);
248 }
249
250 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
251
252 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
253                         const char *name, const char *dumproot)
254 {
255         char *fullname;
256
257         /* There are more efficient ways to do this, but this method
258          * requires only minimal debugging. */
259         fullname = malloc(strlen(dumproot) + strlen(name) + 2);
260         if (!fullname) {
261                 com_err("rdump", errno, "while allocating memory");
262                 return;
263         }
264         sprintf(fullname, "%s/%s", dumproot, name);
265
266         if (LINUX_S_ISLNK(inode->i_mode))
267                 rdump_symlink(ino, inode, fullname);
268         else if (LINUX_S_ISREG(inode->i_mode)) {
269                 int fd;
270                 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
271                 if (fd == -1) {
272                         com_err("rdump", errno, "while dumping %s", fullname);
273                         goto errout;
274                 }
275                 dump_file("rdump", ino, fd, 1, fullname);
276         }
277         else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
278                 errcode_t retval;
279
280                 /* Create the directory with 0700 permissions, because we
281                  * expect to have to create entries it.  Then fix its perms
282                  * once we've done the traversal. */
283                 if (mkdir(fullname, S_IRWXU) == -1) {
284                         com_err("rdump", errno, "while making directory %s", fullname);
285                         goto errout;
286                 }
287
288                 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
289                                             rdump_dirent, (void *) fullname);
290                 if (retval)
291                         com_err("rdump", retval, "while dumping %s", fullname);
292
293                 fix_perms("rdump", inode, -1, fullname);
294         }
295         /* else do nothing (don't dump device files, sockets, fifos, etc.) */
296
297 errout:
298         free(fullname);
299 }
300
301 static int rdump_dirent(struct ext2_dir_entry *dirent,
302                         int offset EXT2FS_ATTR((unused)),
303                         int blocksize EXT2FS_ATTR((unused)),
304                         char *buf EXT2FS_ATTR((unused)), void *private)
305 {
306         char name[EXT2_NAME_LEN + 1];
307         int thislen;
308         const char *dumproot = private;
309         struct ext2_inode inode;
310
311         thislen = dirent->name_len & 0xFF;
312         strncpy(name, dirent->name, thislen);
313         name[thislen] = 0;
314
315         if (debugfs_read_inode(dirent->inode, &inode, name))
316                 return 0;
317
318         rdump_inode(dirent->inode, &inode, name, dumproot);
319
320         return 0;
321 }
322
323 void do_rdump(int argc, char **argv)
324 {
325         ext2_ino_t ino;
326         struct ext2_inode inode;
327         struct stat st;
328         int i;
329         char *p;
330
331         if (common_args_process(argc, argv, 3, 3, "rdump",
332                                 "<directory> <native directory>", 0))
333                 return;
334
335         ino = string_to_inode(argv[1]);
336         if (!ino)
337                 return;
338
339         /* Ensure ARGV[2] is a directory. */
340         i = stat(argv[2], &st);
341         if (i == -1) {
342                 com_err("rdump", errno, "while statting %s", argv[2]);
343                 return;
344         }
345         if (!S_ISDIR(st.st_mode)) {
346                 com_err("rdump", 0, "%s is not a directory", argv[2]);
347                 return;
348         }
349
350         if (debugfs_read_inode(ino, &inode, argv[1]))
351                 return;
352
353         p = strrchr(argv[1], '/');
354         if (p)
355                 p++;
356         else
357                 p = argv[1];
358
359         rdump_inode(ino, &inode, p, argv[2]);
360 }
361
362 void do_cat(int argc, char **argv)
363 {
364         ext2_ino_t      inode;
365
366         if (common_inode_args_process(argc, argv, &inode, 0))
367                 return;
368
369         fflush(stdout);
370         fflush(stderr);
371         dump_file(argv[0], inode, 1, 0, argv[2]);
372
373         return;
374 }
375