OSDN Git Service

ext2fs: add multi-mount protection (INCOMPAT_MMP)
[android-x86/external-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[8192];
109         ext2_file_t     e2_file;
110         int             nbytes;
111         unsigned int    got;
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         while (1) {
122                 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
123                 if (retval)
124                         com_err(cmdname, retval, "while reading ext2 file");
125                 if (got == 0)
126                         break;
127                 nbytes = write(fd, buf, got);
128                 if ((unsigned) nbytes != got)
129                         com_err(cmdname, errno, "while writing file");
130         }
131         retval = ext2fs_file_close(e2_file);
132         if (retval) {
133                 com_err(cmdname, retval, "while closing ext2 file");
134                 return;
135         }
136
137         if (preserve)
138                 fix_perms("dump_file", &inode, fd, outname);
139         else if (fd != 1)
140                 close(fd);
141
142         return;
143 }
144
145 void do_dump(int argc, char **argv)
146 {
147         ext2_ino_t      inode;
148         int             fd;
149         int             c;
150         int             preserve = 0;
151         char            *in_fn, *out_fn;
152
153         reset_getopt();
154         while ((c = getopt (argc, argv, "p")) != EOF) {
155                 switch (c) {
156                 case 'p':
157                         preserve++;
158                         break;
159                 default:
160                 print_usage:
161                         com_err(argv[0], 0, "Usage: dump_inode [-p] "
162                                 "<file> <output_file>");
163                         return;
164                 }
165         }
166         if (optind != argc-2)
167                 goto print_usage;
168
169         if (check_fs_open(argv[0]))
170                 return;
171
172         in_fn = argv[optind];
173         out_fn = argv[optind+1];
174
175         inode = string_to_inode(in_fn);
176         if (!inode)
177                 return;
178
179         fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
180         if (fd < 0) {
181                 com_err(argv[0], errno, "while opening %s for dump_inode",
182                         out_fn);
183                 return;
184         }
185
186         dump_file(argv[0], inode, fd, preserve, out_fn);
187
188         return;
189 }
190
191 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
192                           const char *fullname)
193 {
194         ext2_file_t e2_file;
195         char *buf;
196         errcode_t retval;
197
198         buf = malloc(inode->i_size + 1);
199         if (!buf) {
200                 com_err("rdump", errno, "while allocating for symlink");
201                 goto errout;
202         }
203
204         /* Apparently, this is the right way to detect and handle fast
205          * symlinks; see do_stat() in debugfs.c. */
206         if (inode->i_blocks == 0)
207                 strcpy(buf, (char *) inode->i_block);
208         else {
209                 unsigned bytes = inode->i_size;
210                 char *p = buf;
211                 retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
212                 if (retval) {
213                         com_err("rdump", retval, "while opening symlink");
214                         goto errout;
215                 }
216                 for (;;) {
217                         unsigned int got;
218                         retval = ext2fs_file_read(e2_file, p, bytes, &got);
219                         if (retval) {
220                                 com_err("rdump", retval, "while reading symlink");
221                                 goto errout;
222                         }
223                         bytes -= got;
224                         p += got;
225                         if (got == 0 || bytes == 0)
226                                 break;
227                 }
228                 buf[inode->i_size] = 0;
229                 retval = ext2fs_file_close(e2_file);
230                 if (retval)
231                         com_err("rdump", retval, "while closing symlink");
232         }
233
234         if (symlink(buf, fullname) == -1) {
235                 com_err("rdump", errno, "while creating symlink %s -> %s", buf, fullname);
236                 goto errout;
237         }
238
239 errout:
240         free(buf);
241 }
242
243 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
244
245 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
246                         const char *name, const char *dumproot)
247 {
248         char *fullname;
249
250         /* There are more efficient ways to do this, but this method
251          * requires only minimal debugging. */
252         fullname = malloc(strlen(dumproot) + strlen(name) + 2);
253         if (!fullname) {
254                 com_err("rdump", errno, "while allocating memory");
255                 return;
256         }
257         sprintf(fullname, "%s/%s", dumproot, name);
258
259         if (LINUX_S_ISLNK(inode->i_mode))
260                 rdump_symlink(ino, inode, fullname);
261         else if (LINUX_S_ISREG(inode->i_mode)) {
262                 int fd;
263                 fd = open(fullname, O_WRONLY | O_CREAT | O_TRUNC | O_LARGEFILE, S_IRWXU);
264                 if (fd == -1) {
265                         com_err("rdump", errno, "while dumping %s", fullname);
266                         goto errout;
267                 }
268                 dump_file("rdump", ino, fd, 1, fullname);
269         }
270         else if (LINUX_S_ISDIR(inode->i_mode) && strcmp(name, ".") && strcmp(name, "..")) {
271                 errcode_t retval;
272
273                 /* Create the directory with 0700 permissions, because we
274                  * expect to have to create entries it.  Then fix its perms
275                  * once we've done the traversal. */
276                 if (mkdir(fullname, S_IRWXU) == -1) {
277                         com_err("rdump", errno, "while making directory %s", fullname);
278                         goto errout;
279                 }
280
281                 retval = ext2fs_dir_iterate(current_fs, ino, 0, 0,
282                                             rdump_dirent, (void *) fullname);
283                 if (retval)
284                         com_err("rdump", retval, "while dumping %s", fullname);
285
286                 fix_perms("rdump", inode, -1, fullname);
287         }
288         /* else do nothing (don't dump device files, sockets, fifos, etc.) */
289
290 errout:
291         free(fullname);
292 }
293
294 static int rdump_dirent(struct ext2_dir_entry *dirent,
295                         int offset EXT2FS_ATTR((unused)),
296                         int blocksize EXT2FS_ATTR((unused)),
297                         char *buf EXT2FS_ATTR((unused)), void *private)
298 {
299         char name[EXT2_NAME_LEN + 1];
300         int thislen;
301         const char *dumproot = private;
302         struct ext2_inode inode;
303
304         thislen = dirent->name_len & 0xFF;
305         strncpy(name, dirent->name, thislen);
306         name[thislen] = 0;
307
308         if (debugfs_read_inode(dirent->inode, &inode, name))
309                 return 0;
310
311         rdump_inode(dirent->inode, &inode, name, dumproot);
312
313         return 0;
314 }
315
316 void do_rdump(int argc, char **argv)
317 {
318         ext2_ino_t ino;
319         struct ext2_inode inode;
320         struct stat st;
321         int i;
322         char *p;
323
324         if (common_args_process(argc, argv, 3, 3, "rdump",
325                                 "<directory> <native directory>", 0))
326                 return;
327
328         ino = string_to_inode(argv[1]);
329         if (!ino)
330                 return;
331
332         /* Ensure ARGV[2] is a directory. */
333         i = stat(argv[2], &st);
334         if (i == -1) {
335                 com_err("rdump", errno, "while statting %s", argv[2]);
336                 return;
337         }
338         if (!S_ISDIR(st.st_mode)) {
339                 com_err("rdump", 0, "%s is not a directory", argv[2]);
340                 return;
341         }
342
343         if (debugfs_read_inode(ino, &inode, argv[1]))
344                 return;
345
346         p = strrchr(argv[1], '/');
347         if (p)
348                 p++;
349         else
350                 p = argv[1];
351
352         rdump_inode(ino, &inode, p, argv[2]);
353 }
354
355 void do_cat(int argc, char **argv)
356 {
357         ext2_ino_t      inode;
358
359         if (common_inode_args_process(argc, argv, &inode, 0))
360                 return;
361
362         fflush(stdout);
363         fflush(stderr);
364         dump_file(argv[0], inode, 1, 0, argv[2]);
365
366         return;
367 }
368