Whamcloud - gitweb
fd2e99a9b31267f2f9484d6a82df3f81907728c0
[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 FILE *open_pager(void)
60 {
61         FILE *outfile;
62         const char *pager = getenv("PAGER");
63
64         signal(SIGPIPE, SIG_IGN);
65         if (pager) {
66                 if (strcmp(pager, "__none__") == 0) {
67                         return stdout;
68                 }
69         } else
70                 pager = "more";
71
72         outfile = popen(pager, "w");
73
74         return (outfile ? outfile : stdout);
75 }
76
77 void close_pager(FILE *stream)
78 {
79         if (stream && stream != stdout) pclose(stream);
80 }
81
82 /*
83  * This routine is used whenever a command needs to turn a string into
84  * an inode.
85  */
86 ext2_ino_t string_to_inode(char *str)
87 {
88         ext2_ino_t      ino;
89         int             len = strlen(str);
90         char            *end;
91         int             retval;
92
93         /*
94          * If the string is of the form <ino>, then treat it as an
95          * inode number.
96          */
97         if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
98                 ino = strtoul(str+1, &end, 0);
99                 if (*end=='>')
100                         return ino;
101         }
102
103         retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
104         if (retval) {
105                 com_err(str, retval, "");
106                 return 0;
107         }
108         return ino;
109 }
110
111 /*
112  * This routine returns 1 if the filesystem is not open, and prints an
113  * error message to that effect.
114  */
115 int check_fs_open(char *name)
116 {
117         if (!current_fs) {
118                 com_err(name, 0, "Filesystem not open");
119                 return 1;
120         }
121         return 0;
122 }
123
124 /*
125  * This routine returns 1 if a filesystem is open, and prints an
126  * error message to that effect.
127  */
128 int check_fs_not_open(char *name)
129 {
130         if (current_fs) {
131                 com_err(name, 0,
132                         "Filesystem %s is still open.  Close it first.\n",
133                         current_fs->device_name);
134                 return 1;
135         }
136         return 0;
137 }
138
139 /*
140  * This routine returns 1 if a filesystem is not opened read/write,
141  * and prints an error message to that effect.
142  */
143 int check_fs_read_write(char *name)
144 {
145         if (!(current_fs->flags & EXT2_FLAG_RW)) {
146                 com_err(name, 0, "Filesystem opened read/only");
147                 return 1;
148         }
149         return 0;
150 }
151
152 /*
153  * This routine returns 1 if a filesystem is doesn't have its inode
154  * and block bitmaps loaded, and prints an error message to that
155  * effect.
156  */
157 int check_fs_bitmaps(char *name)
158 {
159         if (!current_fs->block_map || !current_fs->inode_map) {
160                 com_err(name, 0, "Filesystem bitmaps not loaded");
161                 return 1;
162         }
163         return 0;
164 }
165
166 /*
167  * This function takes a __u32 time value and converts it to a string,
168  * using ctime
169  */
170 char *time_to_string(__u32 cl)
171 {
172         time_t  t = (time_t) cl;
173
174         return ctime(&t);
175 }
176
177 /*
178  * This function will convert a string to an unsigned long, printing
179  * an error message if it fails, and returning success or failure in err.
180  */
181 unsigned long parse_ulong(const char *str, const char *cmd,
182                           const char *descr, int *err)
183 {
184         char            *tmp;
185         unsigned long   ret;
186         
187         ret = strtoul(str, &tmp, 0);
188         if (*tmp == 0) {
189                 if (err)
190                         *err = 0;
191                 return ret;
192         }
193         com_err(cmd, 0, "Bad %s - %s", descr, str);
194         if (err)
195                 *err = 1;
196         else
197                 exit(1);
198         return 0;
199 }
200
201 /*
202  * This function will convert a string to a block number.  It returns
203  * 0 on success, 1 on failure.
204  */
205 int strtoblk(const char *cmd, const char *str, blk_t *ret)
206 {
207         blk_t   blk;
208         int     err;
209
210         blk = parse_ulong(str, cmd, "block number", &err);
211         *ret = blk;
212         if (err == 0 && blk == 0) {
213                 com_err(cmd, 0, "Invalid block number 0");
214                 err = 1;
215         }
216         return err;
217 }
218
219 /*
220  * This is a common helper function used by the command processing
221  * routines
222  */
223 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
224                         const char *cmd, const char *usage, int flags)
225 {
226         if (argc < min_argc || argc > max_argc) {
227                 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
228                 return 1;
229         }
230         if (flags & CHECK_FS_NOTOPEN) {
231                 if (check_fs_not_open(argv[0]))
232                         return 1;
233         } else {
234                 if (check_fs_open(argv[0]))
235                         return 1;
236         }
237         if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
238                 return 1;
239         if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
240                 return 1;
241         return 0;
242 }
243
244 /*
245  * This is a helper function used by do_stat, do_freei, do_seti, and
246  * do_testi, etc.  Basically, any command which takes a single
247  * argument which is a file/inode number specifier.
248  */
249 int common_inode_args_process(int argc, char *argv[],
250                               ext2_ino_t *inode, int flags)
251 {
252         if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
253                 return 1;
254         
255         *inode = string_to_inode(argv[1]);
256         if (!*inode) 
257                 return 1;
258         return 0;
259 }
260
261 /*
262  * This is a helper function used by do_freeb, do_setb, and do_testb
263  */
264 int common_block_args_process(int argc, char *argv[],
265                               blk_t *block, int *count)
266 {
267         int     err;
268
269         if (common_args_process(argc, argv, 2, 3, argv[0],
270                                 "<block> [count]", CHECK_FS_BITMAPS))
271                 return 1;
272
273         if (strtoblk(argv[0], argv[1], block))
274                 return 1;
275         if (argc > 2) {
276                 *count = parse_ulong(argv[2], argv[0], "count", &err);
277                 if (err)
278                         return 1;
279         }
280         return 0;
281 }
282
283 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
284                         const char *cmd)
285 {
286         int retval;
287
288         retval = ext2fs_read_inode(current_fs, ino, inode);
289         if (retval) {
290                 com_err(cmd, retval, "while reading inode %u", ino);
291                 return 1;
292         }
293         return 0;
294 }
295
296 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
297                         const char *cmd)
298 {
299         int retval;
300
301         retval = ext2fs_write_inode(current_fs, ino, inode);
302         if (retval) {
303                 com_err(cmd, retval, "while writing inode %u", ino);
304                 return 1;
305         }
306         return 0;
307 }
308