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