1997-04-26 21:21:57 +08:00
|
|
|
/*
|
|
|
|
* util.c --- utilities for the debugfs program
|
|
|
|
*
|
|
|
|
* Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
|
|
|
|
* redistributed under the terms of the GNU Public License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <string.h>
|
1997-04-29 22:53:37 +08:00
|
|
|
#include <time.h>
|
2000-08-20 01:33:28 +08:00
|
|
|
#include <signal.h>
|
util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog:
util.c (reset_getopt), debugfs.c (do_open_filesys,
do_show_super_stats), ls.c (do_list_dir), dump.c (do_dump),
htree.c (do_htree_dump, do_dx_hash), logdump.c (do_logdump):
Define and use a new function, reset_getopt(), which does whatever
is necessary to reset getopt() again. This is different for
different implementations, so the portabilty issues are a bit of a
nightmare. (Addresses Debian bug #192834)
2003-05-14 11:03:43 +08:00
|
|
|
#ifdef HAVE_GETOPT_H
|
|
|
|
#include <getopt.h>
|
|
|
|
#else
|
|
|
|
extern int optind;
|
|
|
|
extern char *optarg;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_OPTRESET
|
|
|
|
extern int optreset; /* defined by BSD, but not others */
|
|
|
|
#endif
|
1997-04-26 21:21:57 +08:00
|
|
|
|
|
|
|
#include "debugfs.h"
|
|
|
|
|
util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog:
util.c (reset_getopt), debugfs.c (do_open_filesys,
do_show_super_stats), ls.c (do_list_dir), dump.c (do_dump),
htree.c (do_htree_dump, do_dx_hash), logdump.c (do_logdump):
Define and use a new function, reset_getopt(), which does whatever
is necessary to reset getopt() again. This is different for
different implementations, so the portabilty issues are a bit of a
nightmare. (Addresses Debian bug #192834)
2003-05-14 11:03:43 +08:00
|
|
|
/*
|
|
|
|
* This function resets the libc getopt() function, which keeps
|
|
|
|
* internal state. Bad design! Stupid libc API designers! No
|
|
|
|
* biscuit!
|
|
|
|
*
|
|
|
|
* BSD-derived getopt() functions require that optind be reset to 1 in
|
|
|
|
* order to reset getopt() state. This used to be generally accepted
|
|
|
|
* way of resetting getopt(). However, glibc's getopt()
|
|
|
|
* has additional getopt() state beyond optind, and requires that
|
|
|
|
* optind be set zero to reset its state. So the unfortunate state of
|
|
|
|
* affairs is that BSD-derived versions of getopt() misbehave if
|
|
|
|
* optind is set to 0 in order to reset getopt(), and glibc's getopt()
|
|
|
|
* will core ump if optind is set 1 in order to reset getopt().
|
|
|
|
*
|
|
|
|
* More modern versions of BSD require that optreset be set to 1 in
|
|
|
|
* order to reset getopt(). Sigh. Standards, anyone?
|
|
|
|
*
|
|
|
|
* We hide the hair here.
|
|
|
|
*/
|
|
|
|
void reset_getopt(void)
|
|
|
|
{
|
|
|
|
#ifdef __GLIBC__
|
|
|
|
optind = 0;
|
|
|
|
#else
|
|
|
|
optind = 1;
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_OPTRESET
|
|
|
|
optreset = 1; /* Makes BSD getopt happy */
|
|
|
|
#endif
|
2003-12-08 02:16:25 +08:00
|
|
|
}
|
|
|
|
|
2004-01-25 07:54:41 +08:00
|
|
|
static const char *pager_search_list[] = { "pager", "more", "less", 0 };
|
2003-12-08 02:16:25 +08:00
|
|
|
static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 };
|
|
|
|
|
|
|
|
static const char *find_pager(char *buf)
|
|
|
|
{
|
|
|
|
const char **i, **j;
|
|
|
|
|
|
|
|
for (i = pager_search_list; *i; i++) {
|
|
|
|
for (j = pager_dir_list; *j; j++) {
|
|
|
|
sprintf(buf, "%s/%s", *j, *i);
|
|
|
|
if (access(buf, X_OK) == 0)
|
|
|
|
return(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
util.c, ls.c, logdump.c, htree.c, dump.c, debugfs.h, debugfs.c, ChangeLog:
util.c (reset_getopt), debugfs.c (do_open_filesys,
do_show_super_stats), ls.c (do_list_dir), dump.c (do_dump),
htree.c (do_htree_dump, do_dx_hash), logdump.c (do_logdump):
Define and use a new function, reset_getopt(), which does whatever
is necessary to reset getopt() again. This is different for
different implementations, so the portabilty issues are a bit of a
nightmare. (Addresses Debian bug #192834)
2003-05-14 11:03:43 +08:00
|
|
|
|
1997-04-26 21:21:57 +08:00
|
|
|
FILE *open_pager(void)
|
|
|
|
{
|
2003-12-08 02:16:25 +08:00
|
|
|
FILE *outfile = 0;
|
2004-04-12 05:06:58 +08:00
|
|
|
const char *pager = getenv("DEBUGFS_PAGER");
|
2003-12-08 02:16:25 +08:00
|
|
|
char buf[80];
|
1997-04-26 21:21:57 +08:00
|
|
|
|
2000-08-20 01:33:28 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2003-12-26 03:28:55 +08:00
|
|
|
if (!pager)
|
2004-04-12 05:06:58 +08:00
|
|
|
pager = getenv("PAGER");
|
2003-12-26 03:28:55 +08:00
|
|
|
if (!pager)
|
2003-12-08 02:16:25 +08:00
|
|
|
pager = find_pager(buf);
|
2003-12-26 03:28:55 +08:00
|
|
|
if (!pager ||
|
|
|
|
(strcmp(pager, "__none__") == 0) ||
|
|
|
|
((outfile = popen(pager, "w")) == 0))
|
|
|
|
return stdout;
|
|
|
|
return outfile;
|
1997-04-26 21:21:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void close_pager(FILE *stream)
|
|
|
|
{
|
2001-12-03 00:23:27 +08:00
|
|
|
if (stream && stream != stdout) pclose(stream);
|
1997-04-26 21:21:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine is used whenever a command needs to turn a string into
|
|
|
|
* an inode.
|
|
|
|
*/
|
2001-01-11 23:26:39 +08:00
|
|
|
ext2_ino_t string_to_inode(char *str)
|
1997-04-26 21:21:57 +08:00
|
|
|
{
|
2001-01-11 23:26:39 +08:00
|
|
|
ext2_ino_t ino;
|
|
|
|
int len = strlen(str);
|
|
|
|
char *end;
|
|
|
|
int retval;
|
1997-04-26 21:21:57 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the string is of the form <ino>, then treat it as an
|
|
|
|
* inode number.
|
|
|
|
*/
|
|
|
|
if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
|
2000-08-23 12:36:25 +08:00
|
|
|
ino = strtoul(str+1, &end, 0);
|
|
|
|
if (*end=='>')
|
|
|
|
return ino;
|
1997-04-26 21:21:57 +08:00
|
|
|
}
|
|
|
|
|
1997-04-29 22:53:37 +08:00
|
|
|
retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
|
1997-04-26 21:21:57 +08:00
|
|
|
if (retval) {
|
|
|
|
com_err(str, retval, "");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return ino;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine returns 1 if the filesystem is not open, and prints an
|
|
|
|
* error message to that effect.
|
|
|
|
*/
|
|
|
|
int check_fs_open(char *name)
|
|
|
|
{
|
1997-04-29 22:53:37 +08:00
|
|
|
if (!current_fs) {
|
1997-04-26 21:21:57 +08:00
|
|
|
com_err(name, 0, "Filesystem not open");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This routine returns 1 if a filesystem is open, and prints an
|
|
|
|
* error message to that effect.
|
|
|
|
*/
|
|
|
|
int check_fs_not_open(char *name)
|
|
|
|
{
|
1997-04-29 22:53:37 +08:00
|
|
|
if (current_fs) {
|
1997-04-26 21:21:57 +08:00
|
|
|
com_err(name, 0,
|
|
|
|
"Filesystem %s is still open. Close it first.\n",
|
1997-04-29 22:53:37 +08:00
|
|
|
current_fs->device_name);
|
1997-04-26 21:21:57 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-04-29 22:53:37 +08:00
|
|
|
/*
|
|
|
|
* This routine returns 1 if a filesystem is not opened read/write,
|
|
|
|
* and prints an error message to that effect.
|
|
|
|
*/
|
|
|
|
int check_fs_read_write(char *name)
|
|
|
|
{
|
|
|
|
if (!(current_fs->flags & EXT2_FLAG_RW)) {
|
|
|
|
com_err(name, 0, "Filesystem opened read/only");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-05-28 00:04:00 +08:00
|
|
|
/*
|
|
|
|
* This routine returns 1 if a filesystem is doesn't have its inode
|
|
|
|
* and block bitmaps loaded, and prints an error message to that
|
|
|
|
* effect.
|
|
|
|
*/
|
|
|
|
int check_fs_bitmaps(char *name)
|
|
|
|
{
|
|
|
|
if (!current_fs->block_map || !current_fs->inode_map) {
|
|
|
|
com_err(name, 0, "Filesystem bitmaps not loaded");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-04-29 22:53:37 +08:00
|
|
|
/*
|
|
|
|
* This function takes a __u32 time value and converts it to a string,
|
|
|
|
* using ctime
|
|
|
|
*/
|
|
|
|
char *time_to_string(__u32 cl)
|
|
|
|
{
|
2004-12-16 01:21:41 +08:00
|
|
|
static int do_gmt = -1;
|
|
|
|
time_t t = (time_t) cl;
|
|
|
|
char * tz;
|
1997-04-29 22:53:37 +08:00
|
|
|
|
2004-12-01 08:57:20 +08:00
|
|
|
if (do_gmt == -1) {
|
|
|
|
/* The diet libc doesn't respect the TZ environemnt variable */
|
2004-12-16 01:21:41 +08:00
|
|
|
tz = getenv("TZ");
|
|
|
|
if (!tz)
|
|
|
|
tz = "";
|
|
|
|
do_gmt = !strcmp(tz, "GMT");
|
2004-12-01 08:57:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return asctime((do_gmt) ? gmtime(&t) : localtime(&t));
|
1997-04-29 22:53:37 +08:00
|
|
|
}
|
|
|
|
|
2005-09-25 09:56:38 +08:00
|
|
|
/*
|
|
|
|
* Parse a string as a time. Return ((time_t)-1) if the string
|
|
|
|
* doesn't appear to be a sane time.
|
|
|
|
*/
|
|
|
|
extern time_t string_to_time(const char *arg)
|
|
|
|
{
|
|
|
|
struct tm ts;
|
|
|
|
unsigned long ret;
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
if (strcmp(arg, "now") == 0) {
|
|
|
|
return time(0);
|
|
|
|
}
|
|
|
|
memset(&ts, 0, sizeof(ts));
|
|
|
|
#ifdef HAVE_STRPTIME
|
|
|
|
strptime(arg, "%Y%m%d%H%M%S", &ts);
|
|
|
|
#else
|
|
|
|
sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon,
|
|
|
|
&ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec);
|
|
|
|
ts.tm_year -= 1900;
|
|
|
|
ts.tm_mon -= 1;
|
|
|
|
if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 ||
|
|
|
|
ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 ||
|
|
|
|
ts.tm_min > 59 || ts.tm_sec > 61)
|
|
|
|
ts.tm_mday = 0;
|
|
|
|
#endif
|
|
|
|
if (ts.tm_mday == 0) {
|
|
|
|
/* Try it as an integer... */
|
|
|
|
|
|
|
|
ret = strtoul(arg, &tmp, 0);
|
|
|
|
if (*tmp)
|
|
|
|
return ((time_t) -1);
|
|
|
|
}
|
|
|
|
return mktime(&ts);
|
|
|
|
}
|
|
|
|
|
2002-01-03 17:55:25 +08:00
|
|
|
/*
|
|
|
|
* This function will convert a string to an unsigned long, printing
|
|
|
|
* an error message if it fails, and returning success or failure in err.
|
|
|
|
*/
|
|
|
|
unsigned long parse_ulong(const char *str, const char *cmd,
|
|
|
|
const char *descr, int *err)
|
|
|
|
{
|
|
|
|
char *tmp;
|
|
|
|
unsigned long ret;
|
|
|
|
|
|
|
|
ret = strtoul(str, &tmp, 0);
|
|
|
|
if (*tmp == 0) {
|
2002-04-02 04:42:21 +08:00
|
|
|
if (err)
|
2002-01-03 17:55:25 +08:00
|
|
|
*err = 0;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
com_err(cmd, 0, "Bad %s - %s", descr, str);
|
2002-04-02 04:42:21 +08:00
|
|
|
if (err)
|
2002-01-03 17:55:25 +08:00
|
|
|
*err = 1;
|
|
|
|
else
|
|
|
|
exit(1);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function will convert a string to a block number. It returns
|
|
|
|
* 0 on success, 1 on failure.
|
|
|
|
*/
|
|
|
|
int strtoblk(const char *cmd, const char *str, blk_t *ret)
|
|
|
|
{
|
|
|
|
blk_t blk;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
blk = parse_ulong(str, cmd, "block number", &err);
|
|
|
|
*ret = blk;
|
|
|
|
if (err == 0 && blk == 0) {
|
|
|
|
com_err(cmd, 0, "Invalid block number 0");
|
|
|
|
err = 1;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a common helper function used by the command processing
|
|
|
|
* routines
|
|
|
|
*/
|
|
|
|
int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
|
|
|
|
const char *cmd, const char *usage, int flags)
|
|
|
|
{
|
|
|
|
if (argc < min_argc || argc > max_argc) {
|
|
|
|
com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (flags & CHECK_FS_NOTOPEN) {
|
|
|
|
if (check_fs_not_open(argv[0]))
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
if (check_fs_open(argv[0]))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
|
|
|
|
return 1;
|
|
|
|
if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
1997-04-29 22:53:37 +08:00
|
|
|
|
2002-01-03 17:55:25 +08:00
|
|
|
/*
|
|
|
|
* This is a helper function used by do_stat, do_freei, do_seti, and
|
|
|
|
* do_testi, etc. Basically, any command which takes a single
|
|
|
|
* argument which is a file/inode number specifier.
|
|
|
|
*/
|
|
|
|
int common_inode_args_process(int argc, char *argv[],
|
|
|
|
ext2_ino_t *inode, int flags)
|
|
|
|
{
|
|
|
|
if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
|
|
|
|
return 1;
|
1997-04-29 22:53:37 +08:00
|
|
|
|
2002-01-03 17:55:25 +08:00
|
|
|
*inode = string_to_inode(argv[1]);
|
|
|
|
if (!*inode)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a helper function used by do_freeb, do_setb, and do_testb
|
|
|
|
*/
|
|
|
|
int common_block_args_process(int argc, char *argv[],
|
|
|
|
blk_t *block, int *count)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (common_args_process(argc, argv, 2, 3, argv[0],
|
|
|
|
"<block> [count]", CHECK_FS_BITMAPS))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if (strtoblk(argv[0], argv[1], block))
|
|
|
|
return 1;
|
|
|
|
if (argc > 2) {
|
2002-05-12 10:13:20 +08:00
|
|
|
*count = parse_ulong(argv[2], argv[0], "count", &err);
|
2002-01-03 17:55:25 +08:00
|
|
|
if (err)
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-21 07:03:58 +08:00
|
|
|
int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode,
|
|
|
|
const char *cmd, int bufsize)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize);
|
|
|
|
if (retval) {
|
|
|
|
com_err(cmd, retval, "while reading inode %u", ino);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-01-03 17:55:25 +08:00
|
|
|
int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
|
|
|
|
const char *cmd)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = ext2fs_read_inode(current_fs, ino, inode);
|
|
|
|
if (retval) {
|
|
|
|
com_err(cmd, retval, "while reading inode %u", ino);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
|
|
|
|
const char *cmd)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = ext2fs_write_inode(current_fs, ino, inode);
|
|
|
|
if (retval) {
|
|
|
|
com_err(cmd, retval, "while writing inode %u", ino);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1997-04-29 22:53:37 +08:00
|
|
|
|
2005-03-21 09:05:22 +08:00
|
|
|
int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode,
|
|
|
|
const char *cmd)
|
|
|
|
{
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = ext2fs_write_new_inode(current_fs, ino, inode);
|
|
|
|
if (retval) {
|
|
|
|
com_err(cmd, retval, "while creating inode %u", ino);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|