2 * util.c --- utilities for the debugfs program
4 * Copyright (C) 1993, 1994 Theodore Ts'o. This file may be
5 * redistributed under the terms of the GNU Public License.
19 FILE *open_pager(void)
22 const char *pager = getenv("PAGER");
24 signal(SIGPIPE, SIG_IGN);
26 if (strcmp(pager, "__none__") == 0) {
32 outfile = popen(pager, "w");
34 return (outfile ? outfile : stdout);
37 void close_pager(FILE *stream)
39 if (stream && stream != stdout) pclose(stream);
43 * This routine is used whenever a command needs to turn a string into
46 ext2_ino_t string_to_inode(char *str)
49 int len = strlen(str);
54 * If the string is of the form <ino>, then treat it as an
57 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
58 ino = strtoul(str+1, &end, 0);
63 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
65 com_err(str, retval, "");
72 * This routine returns 1 if the filesystem is not open, and prints an
73 * error message to that effect.
75 int check_fs_open(char *name)
78 com_err(name, 0, "Filesystem not open");
85 * This routine returns 1 if a filesystem is open, and prints an
86 * error message to that effect.
88 int check_fs_not_open(char *name)
92 "Filesystem %s is still open. Close it first.\n",
93 current_fs->device_name);
100 * This routine returns 1 if a filesystem is not opened read/write,
101 * and prints an error message to that effect.
103 int check_fs_read_write(char *name)
105 if (!(current_fs->flags & EXT2_FLAG_RW)) {
106 com_err(name, 0, "Filesystem opened read/only");
113 * This routine returns 1 if a filesystem is doesn't have its inode
114 * and block bitmaps loaded, and prints an error message to that
117 int check_fs_bitmaps(char *name)
119 if (!current_fs->block_map || !current_fs->inode_map) {
120 com_err(name, 0, "Filesystem bitmaps not loaded");
127 * This function takes a __u32 time value and converts it to a string,
130 char *time_to_string(__u32 cl)
132 time_t t = (time_t) cl;
138 * This function will convert a string to an unsigned long, printing
139 * an error message if it fails, and returning success or failure in err.
141 unsigned long parse_ulong(const char *str, const char *cmd,
142 const char *descr, int *err)
147 ret = strtoul(str, &tmp, 0);
153 com_err(cmd, 0, "Bad %s - %s", descr, str);
162 * This function will convert a string to a block number. It returns
163 * 0 on success, 1 on failure.
165 int strtoblk(const char *cmd, const char *str, blk_t *ret)
170 blk = parse_ulong(str, cmd, "block number", &err);
172 if (err == 0 && blk == 0) {
173 com_err(cmd, 0, "Invalid block number 0");
180 * This is a common helper function used by the command processing
183 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
184 const char *cmd, const char *usage, int flags)
186 if (argc < min_argc || argc > max_argc) {
187 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
190 if (flags & CHECK_FS_NOTOPEN) {
191 if (check_fs_not_open(argv[0]))
194 if (check_fs_open(argv[0]))
197 if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
199 if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
205 * This is a helper function used by do_stat, do_freei, do_seti, and
206 * do_testi, etc. Basically, any command which takes a single
207 * argument which is a file/inode number specifier.
209 int common_inode_args_process(int argc, char *argv[],
210 ext2_ino_t *inode, int flags)
212 if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
215 *inode = string_to_inode(argv[1]);
222 * This is a helper function used by do_freeb, do_setb, and do_testb
224 int common_block_args_process(int argc, char *argv[],
225 blk_t *block, int *count)
229 if (common_args_process(argc, argv, 2, 3, argv[0],
230 "<block> [count]", CHECK_FS_BITMAPS))
233 if (strtoblk(argv[0], argv[1], block))
236 *count = parse_ulong(argv[0], argv[2], "count", &err);
243 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
248 retval = ext2fs_read_inode(current_fs, ino, inode);
250 com_err(cmd, retval, "while reading inode %u", ino);
256 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
261 retval = ext2fs_write_inode(current_fs, ino, inode);
263 com_err(cmd, retval, "while writing inode %u", ino);