Whamcloud - gitweb
ChangeLog, debugfs.c, dump.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 dump_file(char *cmdname, ino_t ino, int fd, int preserve,
65                       char *outname)
66 {
67         errcode_t retval;
68         struct ext2_inode       inode;
69         struct utimbuf  ut;
70         char            buf[8192];
71         ext2_file_t     e2_file;
72         int             nbytes, got;
73         
74         retval = ext2fs_read_inode(current_fs, ino, &inode);
75         if (retval) {
76                 com_err(cmdname, retval,
77                         "while reading inode %u in dump_file", ino);
78                 return;
79         }
80
81         retval = ext2fs_file_open(current_fs, ino, 0, &e2_file);
82         if (retval) {
83                 com_err(cmdname, retval, "while opening ext2 file");
84                 return;
85         }
86         while (1) {
87                 retval = ext2fs_file_read(e2_file, buf, sizeof(buf), &got);
88                 if (retval) 
89                         com_err(cmdname, retval, "while reading ext2 file");
90                 if (got == 0)
91                         break;
92                 nbytes = write(fd, buf, got);
93                 if (nbytes != got)
94                         com_err(cmdname, errno, "while writing file");
95         }
96         retval = ext2fs_file_close(e2_file);
97         if (retval) {
98                 com_err(cmdname, retval, "while closing ext2 file");
99                 return;
100         }
101                 
102         if (preserve) {
103 #ifdef HAVE_FCHOWN
104                 if (fchown(fd, inode.i_uid, inode.i_gid) < 0)
105                         com_err("dump_file", errno,
106                                 "while changing ownership of %s", outname);
107 #else
108                 if (chown(outname, inode.i_uid, inode.i_gid) < 0)
109                         com_err("dump_file", errno,
110                                 "while changing ownership of %s", outname);
111                         
112 #endif
113                 if (fchmod(fd, mode_xlate(inode.i_mode)) < 0)
114                         com_err("dump_file", errno,
115                                 "while setting permissions of %s", outname);
116                 ut.actime = inode.i_atime;
117                 ut.modtime = inode.i_mtime;
118                 close(fd);
119                 if (utime(outname, &ut) < 0)
120                         com_err("dump_file", errno,
121                                 "while setting times on %s", outname);
122         } else if (fd != 1)
123                 close(fd);
124                                     
125         return;
126 }
127
128 void do_dump(int argc, char **argv)
129 {
130         ino_t   inode;
131         int     fd;
132         int     c;
133         int     preserve = 0;
134         const char *dump_usage = "Usage: dump_inode [-p] <file> <output_file>";
135         char    *in_fn, *out_fn;
136         
137         optind = 0;
138 #ifdef HAVE_OPTRESET
139         optreset = 1;           /* Makes BSD getopt happy */
140 #endif
141         while ((c = getopt (argc, argv, "p")) != EOF) {
142                 switch (c) {
143                 case 'p':
144                         preserve++;
145                         break;
146                 default:
147                         com_err(argv[0], 0, dump_usage);
148                         return;
149                 }
150         }
151         if (optind != argc-2) {
152                 com_err(argv[0], 0, dump_usage);
153                 return;
154         }
155
156         if (check_fs_open(argv[0]))
157                 return;
158
159         in_fn = argv[optind];
160         out_fn = argv[optind+1];
161
162         inode = string_to_inode(in_fn);
163         if (!inode) 
164                 return;
165
166         fd = open(out_fn, O_CREAT | O_WRONLY | O_TRUNC, 0666);
167         if (fd < 0) {
168                 com_err(argv[0], errno, "while opening %s for dump_inode",
169                         out_fn);
170                 return;
171         }
172
173         dump_file(argv[0], inode, fd, preserve, out_fn);
174
175         return;
176 }
177
178 void do_cat(int argc, char **argv)
179 {
180         ino_t   inode;
181
182         if (argc != 2) {
183                 com_err(argv[0], 0, "Usage: cat <file>");
184                 return;
185         }
186
187         if (check_fs_open(argv[0]))
188                 return;
189
190         inode = string_to_inode(argv[1]);
191         if (!inode) 
192                 return;
193
194         fflush(stdout);
195         fflush(stderr);
196         dump_file(argv[0], inode, 1, 0, argv[2]); 
197
198         return;
199 }
200