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