blob: 66a0bea8d1aed2426f0dc98344b76b29088139da [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000014#include <time.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000015
16#include "debugfs.h"
17
18FILE *open_pager(void)
19{
20 FILE *outfile;
21 char *pager = getenv("PAGER");
22
23 if (!pager)
24 outfile = stdout;
25 else {
26 outfile = popen(pager, "w");
27 if (!outfile) outfile = stdout;
28 }
29 return (outfile);
30}
31
32void close_pager(FILE *stream)
33{
34 if (stream && stream != stdout) fclose(stream);
35}
36
37/*
38 * This routine is used whenever a command needs to turn a string into
39 * an inode.
40 */
41ino_t string_to_inode(char *str)
42{
43 ino_t ino;
44 int len = strlen(str);
45 int i;
46 int retval;
47
48 /*
49 * If the string is of the form <ino>, then treat it as an
50 * inode number.
51 */
52 if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
53 for (i = 1; i < len-1; i++)
54 if (!isdigit(str[i]))
55 break;
56 if (i == len-1)
57 return(atoi(str+1));
58 }
59
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000060 retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
Theodore Ts'o3839e651997-04-26 13:21:57 +000061 if (retval) {
62 com_err(str, retval, "");
63 return 0;
64 }
65 return ino;
66}
67
68/*
69 * This routine returns 1 if the filesystem is not open, and prints an
70 * error message to that effect.
71 */
72int check_fs_open(char *name)
73{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000074 if (!current_fs) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000075 com_err(name, 0, "Filesystem not open");
76 return 1;
77 }
78 return 0;
79}
80
81/*
82 * This routine returns 1 if a filesystem is open, and prints an
83 * error message to that effect.
84 */
85int check_fs_not_open(char *name)
86{
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000087 if (current_fs) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 com_err(name, 0,
89 "Filesystem %s is still open. Close it first.\n",
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000090 current_fs->device_name);
Theodore Ts'o3839e651997-04-26 13:21:57 +000091 return 1;
92 }
93 return 0;
94}
95
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000096/*
97 * This routine returns 1 if a filesystem is not opened read/write,
98 * and prints an error message to that effect.
99 */
100int check_fs_read_write(char *name)
101{
102 if (!(current_fs->flags & EXT2_FLAG_RW)) {
103 com_err(name, 0, "Filesystem opened read/only");
104 return 1;
105 }
106 return 0;
107}
108
109/*
Theodore Ts'od61f6172000-05-27 16:04:00 +0000110 * This routine returns 1 if a filesystem is doesn't have its inode
111 * and block bitmaps loaded, and prints an error message to that
112 * effect.
113 */
114int check_fs_bitmaps(char *name)
115{
116 if (!current_fs->block_map || !current_fs->inode_map) {
117 com_err(name, 0, "Filesystem bitmaps not loaded");
118 return 1;
119 }
120 return 0;
121}
122
123/*
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000124 * This function takes a __u32 time value and converts it to a string,
125 * using ctime
126 */
127char *time_to_string(__u32 cl)
128{
129 time_t t = (time_t) cl;
130
131 return ctime(&t);
132}
133
134
135
136