Whamcloud - gitweb
Enhance debugfs's stat command so it can dump extended attributes
[tools/e2fsprogs.git] / debugfs / util.c
1 /*
2  * util.c --- utilities for the debugfs program
3  * 
4  * Copyright (C) 1993, 1994 Theodore Ts'o.  This file may be
5  * redistributed under the terms of the GNU Public License.
6  *
7  */
8
9 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #include <signal.h>
16 #ifdef HAVE_GETOPT_H
17 #include <getopt.h>
18 #else 
19 extern int optind;
20 extern char *optarg;
21 #endif
22 #ifdef HAVE_OPTRESET
23 extern int optreset;            /* defined by BSD, but not others */
24 #endif
25
26 #include "debugfs.h"
27
28 /*
29  * This function resets the libc getopt() function, which keeps
30  * internal state.  Bad design!  Stupid libc API designers!  No
31  * biscuit!
32  *
33  * BSD-derived getopt() functions require that optind be reset to 1 in
34  * order to reset getopt() state.  This used to be generally accepted
35  * way of resetting getopt().  However, glibc's getopt()
36  * has additional getopt() state beyond optind, and requires that
37  * optind be set zero to reset its state.  So the unfortunate state of
38  * affairs is that BSD-derived versions of getopt() misbehave if
39  * optind is set to 0 in order to reset getopt(), and glibc's getopt()
40  * will core ump if optind is set 1 in order to reset getopt().
41  * 
42  * More modern versions of BSD require that optreset be set to 1 in
43  * order to reset getopt().   Sigh.  Standards, anyone?
44  *
45  * We hide the hair here.
46  */
47 void reset_getopt(void)
48 {
49 #ifdef __GLIBC__
50         optind = 0;
51 #else
52         optind = 1;
53 #endif
54 #ifdef HAVE_OPTRESET
55         optreset = 1;           /* Makes BSD getopt happy */
56 #endif
57 }
58
59 static const char *pager_search_list[] = { "pager", "more", "less", 0 };
60 static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
61
62 static const char *find_pager(char *buf)
63 {
64         const char **i, **j;
65
66         for (i = pager_search_list; *i; i++) {
67                 for (j = pager_dir_list; *j; j++) {
68                         sprintf(buf, "%s/%s", *j, *i);
69                         if (access(buf, X_OK) == 0)
70                                 return(buf);
71                 }
72         }
73         return 0;
74 }
75
76 FILE *open_pager(void)
77 {
78         FILE *outfile = 0;
79         const char *pager = getenv("DEBUGFS_PAGER");
80         char buf[80];
81
82         signal(SIGPIPE, SIG_IGN);
83         if (!pager)
84                 pager = getenv("PAGER");
85         if (!pager)
86                 pager = find_pager(buf);
87         if (!pager || 
88             (strcmp(pager, "__none__") == 0) ||
89             ((outfile = popen(pager, "w")) == 0))
90                 return stdout;
91         return outfile;
92 }
93
94 void close_pager(FILE *stream)
95 {
96         if (stream && stream != stdout) pclose(stream);
97 }
98
99 /*
100  * This routine is used whenever a command needs to turn a string into
101  * an inode.
102  */
103 ext2_ino_t string_to_inode(char *str)
104 {
105         ext2_ino_t      ino;
106         int             len = strlen(str);
107         char            *end;
108         int             retval;
109
110         /*
111          * If the string is of the form <ino>, then treat it as an
112          * inode number.
113          */
114         if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
115                 ino = strtoul(str+1, &end, 0);
116                 if (*end=='>')
117                         return ino;
118         }
119
120         retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
121         if (retval) {
122                 com_err(str, retval, "");
123                 return 0;
124         }
125         return ino;
126 }
127
128 /*
129  * This routine returns 1 if the filesystem is not open, and prints an
130  * error message to that effect.
131  */
132 int check_fs_open(char *name)
133 {
134         if (!current_fs) {
135                 com_err(name, 0, "Filesystem not open");
136                 return 1;
137         }
138         return 0;
139 }
140
141 /*
142  * This routine returns 1 if a filesystem is open, and prints an
143  * error message to that effect.
144  */
145 int check_fs_not_open(char *name)
146 {
147         if (current_fs) {
148                 com_err(name, 0,
149                         "Filesystem %s is still open.  Close it first.\n",
150                         current_fs->device_name);
151                 return 1;
152         }
153         return 0;
154 }
155
156 /*
157  * This routine returns 1 if a filesystem is not opened read/write,
158  * and prints an error message to that effect.
159  */
160 int check_fs_read_write(char *name)
161 {
162         if (!(current_fs->flags & EXT2_FLAG_RW)) {
163                 com_err(name, 0, "Filesystem opened read/only");
164                 return 1;
165         }
166         return 0;
167 }
168
169 /*
170  * This routine returns 1 if a filesystem is doesn't have its inode
171  * and block bitmaps loaded, and prints an error message to that
172  * effect.
173  */
174 int check_fs_bitmaps(char *name)
175 {
176         if (!current_fs->block_map || !current_fs->inode_map) {
177                 com_err(name, 0, "Filesystem bitmaps not loaded");
178                 return 1;
179         }
180         return 0;
181 }
182
183 /*
184  * This function takes a __u32 time value and converts it to a string,
185  * using ctime
186  */
187 char *time_to_string(__u32 cl)
188 {
189         static int      do_gmt = -1;
190         time_t          t = (time_t) cl;
191         char *          tz;
192
193         if (do_gmt == -1) {
194                 /* The diet libc doesn't respect the TZ environemnt variable */
195                 tz = getenv("TZ");
196                 if (!tz)
197                         tz = "";
198                 do_gmt = !strcmp(tz, "GMT");
199         }
200
201         return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
202 }
203
204 /*
205  * This function will convert a string to an unsigned long, printing
206  * an error message if it fails, and returning success or failure in err.
207  */
208 unsigned long parse_ulong(const char *str, const char *cmd,
209                           const char *descr, int *err)
210 {
211         char            *tmp;
212         unsigned long   ret;
213         
214         ret = strtoul(str, &tmp, 0);
215         if (*tmp == 0) {
216                 if (err)
217                         *err = 0;
218                 return ret;
219         }
220         com_err(cmd, 0, "Bad %s - %s", descr, str);
221         if (err)
222                 *err = 1;
223         else
224                 exit(1);
225         return 0;
226 }
227
228 /*
229  * This function will convert a string to a block number.  It returns
230  * 0 on success, 1 on failure.
231  */
232 int strtoblk(const char *cmd, const char *str, blk_t *ret)
233 {
234         blk_t   blk;
235         int     err;
236
237         blk = parse_ulong(str, cmd, "block number", &err);
238         *ret = blk;
239         if (err == 0 && blk == 0) {
240                 com_err(cmd, 0, "Invalid block number 0");
241                 err = 1;
242         }
243         return err;
244 }
245
246 /*
247  * This is a common helper function used by the command processing
248  * routines
249  */
250 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
251                         const char *cmd, const char *usage, int flags)
252 {
253         if (argc < min_argc || argc > max_argc) {
254                 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
255                 return 1;
256         }
257         if (flags & CHECK_FS_NOTOPEN) {
258                 if (check_fs_not_open(argv[0]))
259                         return 1;
260         } else {
261                 if (check_fs_open(argv[0]))
262                         return 1;
263         }
264         if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
265                 return 1;
266         if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
267                 return 1;
268         return 0;
269 }
270
271 /*
272  * This is a helper function used by do_stat, do_freei, do_seti, and
273  * do_testi, etc.  Basically, any command which takes a single
274  * argument which is a file/inode number specifier.
275  */
276 int common_inode_args_process(int argc, char *argv[],
277                               ext2_ino_t *inode, int flags)
278 {
279         if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
280                 return 1;
281         
282         *inode = string_to_inode(argv[1]);
283         if (!*inode) 
284                 return 1;
285         return 0;
286 }
287
288 /*
289  * This is a helper function used by do_freeb, do_setb, and do_testb
290  */
291 int common_block_args_process(int argc, char *argv[],
292                               blk_t *block, int *count)
293 {
294         int     err;
295
296         if (common_args_process(argc, argv, 2, 3, argv[0],
297                                 "<block> [count]", CHECK_FS_BITMAPS))
298                 return 1;
299
300         if (strtoblk(argv[0], argv[1], block))
301                 return 1;
302         if (argc > 2) {
303                 *count = parse_ulong(argv[2], argv[0], "count", &err);
304                 if (err)
305                         return 1;
306         }
307         return 0;
308 }
309
310 int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
311                         const char *cmd, int bufsize)
312 {
313         int retval;
314
315         retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
316         if (retval) {
317                 com_err(cmd, retval, "while reading inode %u", ino);
318                 return 1;
319         }
320         return 0;
321 }
322
323 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
324                         const char *cmd)
325 {
326         int retval;
327
328         retval = ext2fs_read_inode(current_fs, ino, inode);
329         if (retval) {
330                 com_err(cmd, retval, "while reading inode %u", ino);
331                 return 1;
332         }
333         return 0;
334 }
335
336 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
337                         const char *cmd)
338 {
339         int retval;
340
341         retval = ext2fs_write_inode(current_fs, ino, inode);
342         if (retval) {
343                 com_err(cmd, retval, "while writing inode %u", ino);
344                 return 1;
345         }
346         return 0;
347 }
348