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