Whamcloud - gitweb
Add new debugfs commands and arguments which make it easier to
[tools/e2fsprogs.git] / debugfs / util.c
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 #include <stdio.h>
10 #include <unistd.h>
11 #include <stdlib.h>
12 #include <ctype.h>
13 #include <string.h>
14 #include <time.h>
15 #include <signal.h>
16
17 #include "debugfs.h"
18
19 FILE *open_pager(void)
20 {
21         FILE *outfile;
22         const char *pager = getenv("PAGER");
23
24         signal(SIGPIPE, SIG_IGN);
25         if (!pager)
26                 pager = "more";
27
28         outfile = popen(pager, "w");
29         if (!outfile)
30                 outfile = stdout;
31
32         return (outfile);
33 }
34
35 void close_pager(FILE *stream)
36 {
37         if (stream && stream != stdout) pclose(stream);
38 }
39
40 /*
41  * This routine is used whenever a command needs to turn a string into
42  * an inode.
43  */
44 ext2_ino_t string_to_inode(char *str)
45 {
46         ext2_ino_t      ino;
47         int             len = strlen(str);
48         char            *end;
49         int             retval;
50
51         /*
52          * If the string is of the form <ino>, then treat it as an
53          * inode number.
54          */
55         if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) {
56                 ino = strtoul(str+1, &end, 0);
57                 if (*end=='>')
58                         return ino;
59         }
60
61         retval = ext2fs_namei(current_fs, root, cwd, str, &ino);
62         if (retval) {
63                 com_err(str, retval, "");
64                 return 0;
65         }
66         return ino;
67 }
68
69 /*
70  * This routine returns 1 if the filesystem is not open, and prints an
71  * error message to that effect.
72  */
73 int check_fs_open(char *name)
74 {
75         if (!current_fs) {
76                 com_err(name, 0, "Filesystem not open");
77                 return 1;
78         }
79         return 0;
80 }
81
82 /*
83  * This routine returns 1 if a filesystem is open, and prints an
84  * error message to that effect.
85  */
86 int check_fs_not_open(char *name)
87 {
88         if (current_fs) {
89                 com_err(name, 0,
90                         "Filesystem %s is still open.  Close it first.\n",
91                         current_fs->device_name);
92                 return 1;
93         }
94         return 0;
95 }
96
97 /*
98  * This routine returns 1 if a filesystem is not opened read/write,
99  * and prints an error message to that effect.
100  */
101 int check_fs_read_write(char *name)
102 {
103         if (!(current_fs->flags & EXT2_FLAG_RW)) {
104                 com_err(name, 0, "Filesystem opened read/only");
105                 return 1;
106         }
107         return 0;
108 }
109
110 /*
111  * This routine returns 1 if a filesystem is doesn't have its inode
112  * and block bitmaps loaded, and prints an error message to that
113  * effect.
114  */
115 int check_fs_bitmaps(char *name)
116 {
117         if (!current_fs->block_map || !current_fs->inode_map) {
118                 com_err(name, 0, "Filesystem bitmaps not loaded");
119                 return 1;
120         }
121         return 0;
122 }
123
124 /*
125  * This function takes a __u32 time value and converts it to a string,
126  * using ctime
127  */
128 char *time_to_string(__u32 cl)
129 {
130         time_t  t = (time_t) cl;
131
132         return ctime(&t);
133 }
134
135 /*
136  * This function will convert a string to an unsigned long, printing
137  * an error message if it fails, and returning success or failure in err.
138  */
139 unsigned long parse_ulong(const char *str, const char *cmd,
140                           const char *descr, int *err)
141 {
142         char            *tmp;
143         unsigned long   ret;
144         
145         ret = strtoul(str, &tmp, 0);
146         if (*tmp == 0) {
147                 if (*err)
148                         *err = 0;
149                 return ret;
150         }
151         com_err(cmd, 0, "Bad %s - %s", descr, str);
152         if (*err)
153                 *err = 1;
154         else
155                 exit(1);
156         return 0;
157 }
158
159 /*
160  * This function will convert a string to a block number.  It returns
161  * 0 on success, 1 on failure.
162  */
163 int strtoblk(const char *cmd, const char *str, blk_t *ret)
164 {
165         blk_t   blk;
166         int     err;
167
168         blk = parse_ulong(str, cmd, "block number", &err);
169         *ret = blk;
170         if (err == 0 && blk == 0) {
171                 com_err(cmd, 0, "Invalid block number 0");
172                 err = 1;
173         }
174         return err;
175 }
176
177 /*
178  * This is a common helper function used by the command processing
179  * routines
180  */
181 int common_args_process(int argc, char *argv[], int min_argc, int max_argc,
182                         const char *cmd, const char *usage, int flags)
183 {
184         if (argc < min_argc || argc > max_argc) {
185                 com_err(argv[0], 0, "Usage: %s %s", cmd, usage);
186                 return 1;
187         }
188         if (flags & CHECK_FS_NOTOPEN) {
189                 if (check_fs_not_open(argv[0]))
190                         return 1;
191         } else {
192                 if (check_fs_open(argv[0]))
193                         return 1;
194         }
195         if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0]))
196                 return 1;
197         if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0]))
198                 return 1;
199         return 0;
200 }
201
202 /*
203  * This is a helper function used by do_stat, do_freei, do_seti, and
204  * do_testi, etc.  Basically, any command which takes a single
205  * argument which is a file/inode number specifier.
206  */
207 int common_inode_args_process(int argc, char *argv[],
208                               ext2_ino_t *inode, int flags)
209 {
210         if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags))
211                 return 1;
212         
213         *inode = string_to_inode(argv[1]);
214         if (!*inode) 
215                 return 1;
216         return 0;
217 }
218
219 /*
220  * This is a helper function used by do_freeb, do_setb, and do_testb
221  */
222 int common_block_args_process(int argc, char *argv[],
223                               blk_t *block, int *count)
224 {
225         int     err;
226
227         if (common_args_process(argc, argv, 2, 3, argv[0],
228                                 "<block> [count]", CHECK_FS_BITMAPS))
229                 return 1;
230
231         if (strtoblk(argv[0], argv[1], block))
232                 return 1;
233         if (argc > 2) {
234                 *count = parse_ulong(argv[0], argv[2], "count", &err);
235                 if (err)
236                         return 1;
237         }
238         return 0;
239 }
240
241 int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode,
242                         const char *cmd)
243 {
244         int retval;
245
246         retval = ext2fs_read_inode(current_fs, ino, inode);
247         if (retval) {
248                 com_err(cmd, retval, "while reading inode %u", ino);
249                 return 1;
250         }
251         return 0;
252 }
253
254 int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode,
255                         const char *cmd)
256 {
257         int retval;
258
259         retval = ext2fs_write_inode(current_fs, ino, inode);
260         if (retval) {
261                 com_err(cmd, retval, "while writing inode %u", ino);
262                 return 1;
263         }
264         return 0;
265 }
266