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>
|
1997-04-26 21:21:57 +08:00
|
|
|
|
|
|
|
#include "debugfs.h"
|
|
|
|
|
|
|
|
FILE *open_pager(void)
|
|
|
|
{
|
|
|
|
FILE *outfile;
|
2001-01-11 23:26:39 +08:00
|
|
|
const char *pager = getenv("PAGER");
|
1997-04-26 21:21:57 +08:00
|
|
|
|
2000-08-20 01:33:28 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
1997-04-26 21:21:57 +08:00
|
|
|
if (!pager)
|
2000-08-15 04:37:09 +08:00
|
|
|
pager = "more";
|
|
|
|
|
|
|
|
outfile = popen(pager, "w");
|
|
|
|
if (!outfile)
|
1997-04-26 21:21:57 +08:00
|
|
|
outfile = stdout;
|
2000-08-15 04:37:09 +08:00
|
|
|
|
1997-04-26 21:21:57 +08:00
|
|
|
return (outfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
time_t t = (time_t) cl;
|
|
|
|
|
|
|
|
return ctime(&t);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|