Whamcloud - gitweb
util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog:
[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 #include <stdio.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <time.h>
14 #ifdef HAVE_ERRNO_H
15 #include <errno.h>
16 #endif
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <utime.h>
21 #ifdef HAVE_GETOPT_H
22 #include <getopt.h>
23 #else 
24 extern int optind;
25 extern char *optarg;
26 #endif
27
28 #include "debugfs.h"
29
30 #ifndef O_LARGEFILE
31 #define O_LARGEFILE 0
32 #endif
33
34 /*
35  * The mode_xlate function translates a linux mode into a native-OS mode_t.
36  */
37 static 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  
53 static 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
65 static 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
79         i = chown(name, inode->i_uid, inode->i_gid);
80 #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
98 static void dump_file(const char *cmdname, ext2_ino_t ino, int fd,
99                       int preserve, char *outname)
100 {
101         errcode_t retval;
102         struct ext2_inode       inode;
103         char            buf[8192];
104         ext2_file_t     e2_file;
105         int             nbytes;
106         unsigned int    got;
107         
108         if (debugfs_read_inode(ino, &inode, cmdname))
109                 return;
110
111         retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
112         if (retval) {
113                 com_err(cmdname, retval, "while opening ext2 file");
114                 return;
115         }
116         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);
123                 if (nbytes != got)
124                         com_err(cmdname, errno, "while writing file");
125         }
126         retval = ext2fs_file_close(e2_file);
127         if (retval) {
128                 com_err(cmdname, retval, "while closing ext2 file");
129                 return;
130         }
131                 
132         if (preserve)
133                 fix_perms("dump_file", &inode, fd, outname);
134         else if (fd != 1)
135                 close(fd);
136                                     
137         return;
138 }
139
140 void do_dump(int argc, char **argv)
141 {
142         ext2_ino_t      inode;
143         int             fd;
144         int             c;
145         int             preserve = 0;
146         const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
147         char            *in_fn, *out_fn;
148         
149         reset_getopt();
150         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);
162                 return;
163         }
164
165         if (check_fs_open(argv[0]))
166                 return;
167
168         in_fn = argv[optind];
169         out_fn = argv[optind+1];
170
171         inode = string_to_inode(in_fn);
172         if (!inode) 
173                 return;
174
175         fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC | O_LARGEFILE, 0666);
176         if (fd < 0) {
177                 com_err(argv[0], errno, "while opening %s for dump_inode",
178                         out_fn);
179                 return;
180         }
181
182         dump_file(argv[0], inode, fd, preserve, out_fn);
183
184         return;
185 }
186
187 static void rdump_symlink(ext2_ino_t ino, struct ext2_inode *inode,
188                           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
235 errout:
236         free(buf);
237 }
238
239 static int rdump_dirent(struct ext2_dir_entry *, int, int, char *, void *);
240
241 static void rdump_inode(ext2_ino_t ino, struct ext2_inode *inode,
242                         const char *name, const char *dumproot)
243 {
244         char *fullname;
245
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
286 errout:
287         free(fullname);
288 }
289
290 static int rdump_dirent(struct ext2_dir_entry *dirent, int offset,
291                          int blocksize, char *buf, void *private)
292 {
293         char name[EXT2_NAME_LEN];
294         int thislen;
295         const char *dumproot = private;
296         struct ext2_inode inode;
297
298         thislen = ((dirent->name_len & 0xFF) < EXT2_NAME_LEN
299                    ? (dirent->name_len & 0xFF) : EXT2_NAME_LEN);
300         strncpy(name, dirent->name, thislen);
301         name[thislen] = 0;
302
303         if (debugfs_read_inode(dirent->inode, &inode, name))
304                 return 0;
305
306         rdump_inode(dirent->inode, &inode, name, dumproot);
307
308         return 0;
309 }
310
311 void do_rdump(int argc, char **argv)
312 {
313         ext2_ino_t ino;
314         struct ext2_inode inode;
315         struct stat st;
316         int i;
317         char *p;
318
319         if (common_args_process(argc, argv, 3, 3, "rdump",
320                                 "<directory> <native directory>", 0))
321                 return;
322
323         ino = string_to_inode(argv[1]);
324         if (!ino)
325                 return;
326
327         /* Ensure ARGV[2] is a directory. */
328         i = stat(argv[2], &st);
329         if (i == -1) {
330                 com_err("rdump", errno, "while statting %s", argv[2]);
331                 return;
332         }
333         if (!S_ISDIR(st.st_mode)) {
334                 com_err("rdump", 0, "%s is not a directory", argv[2]);
335                 return;
336         }
337
338         if (debugfs_read_inode(ino, &inode, argv[1]))
339                 return;
340
341         p = strrchr(argv[1], '/');
342         if (p)
343                 p++;
344         else
345                 p = argv[1];
346
347         rdump_inode(ino, &inode, p, argv[2]);
348 }
349
350 void do_cat(int argc, char **argv)
351 {
352         ext2_ino_t      inode;
353
354         if (common_inode_args_process(argc, argv, &inode, 0))
355                 return;
356
357         fflush(stdout);
358         fflush(stderr);
359         dump_file(argv[0], inode, 1, 0, argv[2]); 
360
361         return;
362 }
363