]>
git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - debugfs/util.c
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.
9 #define _XOPEN_SOURCE 600 /* needed for strptime */
25 extern int optreset
; /* defined by BSD, but not others */
31 * This function resets the libc getopt() function, which keeps
32 * internal state. Bad design! Stupid libc API designers! No
35 * BSD-derived getopt() functions require that optind be reset to 1 in
36 * order to reset getopt() state. This used to be generally accepted
37 * way of resetting getopt(). However, glibc's getopt()
38 * has additional getopt() state beyond optind, and requires that
39 * optind be set zero to reset its state. So the unfortunate state of
40 * affairs is that BSD-derived versions of getopt() misbehave if
41 * optind is set to 0 in order to reset getopt(), and glibc's getopt()
42 * will core dump if optind is set 1 in order to reset getopt().
44 * More modern versions of BSD require that optreset be set to 1 in
45 * order to reset getopt(). Sigh. Standards, anyone?
47 * We hide the hair here.
49 void reset_getopt(void)
51 #if defined(__GLIBC__) || defined(__linux__)
57 optreset
= 1; /* Makes BSD getopt happy */
61 static const char *pager_search_list
[] = { "pager", "more", "less", 0 };
62 static const char *pager_dir_list
[] = { "/usr/bin", "/bin", 0 };
64 static const char *find_pager(char *buf
)
68 for (i
= pager_search_list
; *i
; i
++) {
69 for (j
= pager_dir_list
; *j
; j
++) {
70 sprintf(buf
, "%s/%s", *j
, *i
);
71 if (access(buf
, X_OK
) == 0)
78 FILE *open_pager(void)
81 const char *pager
= getenv("DEBUGFS_PAGER");
84 signal(SIGPIPE
, SIG_IGN
);
88 pager
= getenv("PAGER");
90 pager
= find_pager(buf
);
92 (strcmp(pager
, "__none__") == 0) ||
93 ((outfile
= popen(pager
, "w")) == 0))
98 void close_pager(FILE *stream
)
100 if (stream
&& stream
!= stdout
) pclose(stream
);
104 * This routine is used whenever a command needs to turn a string into
107 ext2_ino_t
string_to_inode(char *str
)
110 int len
= strlen(str
);
115 * If the string is of the form <ino>, then treat it as an
118 if ((len
> 2) && (str
[0] == '<') && (str
[len
-1] == '>')) {
119 ino
= strtoul(str
+1, &end
, 0);
124 retval
= ext2fs_namei(current_fs
, root
, cwd
, str
, &ino
);
126 com_err(str
, retval
, 0);
133 * This routine returns 1 if the filesystem is not open, and prints an
134 * error message to that effect.
136 int check_fs_open(char *name
)
139 com_err(name
, 0, "Filesystem not open");
146 * This routine returns 1 if a filesystem is open, and prints an
147 * error message to that effect.
149 int check_fs_not_open(char *name
)
153 "Filesystem %s is still open. Close it first.\n",
154 current_fs
->device_name
);
161 * This routine returns 1 if a filesystem is not opened read/write,
162 * and prints an error message to that effect.
164 int check_fs_read_write(char *name
)
166 if (!(current_fs
->flags
& EXT2_FLAG_RW
)) {
167 com_err(name
, 0, "Filesystem opened read/only");
174 * This routine returns 1 if a filesystem is doesn't have its inode
175 * and block bitmaps loaded, and prints an error message to that
178 int check_fs_bitmaps(char *name
)
180 if (!current_fs
->block_map
|| !current_fs
->inode_map
) {
181 com_err(name
, 0, "Filesystem bitmaps not loaded");
188 * This function takes a __u32 time value and converts it to a string,
191 char *time_to_string(__u32 cl
)
193 static int do_gmt
= -1;
194 time_t t
= (time_t) cl
;
198 /* The diet libc doesn't respect the TZ environemnt variable */
202 do_gmt
= !strcmp(tz
, "GMT");
205 return asctime((do_gmt
) ? gmtime(&t
) : localtime(&t
));
209 * Parse a string as a time. Return ((time_t)-1) if the string
210 * doesn't appear to be a sane time.
212 extern time_t string_to_time(const char *arg
)
218 if (strcmp(arg
, "now") == 0) {
221 memset(&ts
, 0, sizeof(ts
));
223 strptime(arg
, "%Y%m%d%H%M%S", &ts
);
225 sscanf(arg
, "%4d%2d%2d%2d%2d%2d", &ts
.tm_year
, &ts
.tm_mon
,
226 &ts
.tm_mday
, &ts
.tm_hour
, &ts
.tm_min
, &ts
.tm_sec
);
229 if (ts
.tm_year
< 0 || ts
.tm_mon
< 0 || ts
.tm_mon
> 11 ||
230 ts
.tm_mday
< 0 || ts
.tm_mday
> 31 || ts
.tm_hour
> 23 ||
231 ts
.tm_min
> 59 || ts
.tm_sec
> 61)
236 if (ts
.tm_mday
== 0 || ret
== ((time_t) -1)) {
237 /* Try it as an integer... */
238 ret
= strtoul(arg
, &tmp
, 0);
240 return ((time_t) -1);
246 * This function will convert a string to an unsigned long, printing
247 * an error message if it fails, and returning success or failure in err.
249 unsigned long parse_ulong(const char *str
, const char *cmd
,
250 const char *descr
, int *err
)
255 ret
= strtoul(str
, &tmp
, 0);
261 com_err(cmd
, 0, "Bad %s - %s", descr
, str
);
270 * This function will convert a string to an unsigned long long, printing
271 * an error message if it fails, and returning success or failure in err.
273 unsigned long long parse_ulonglong(const char *str
, const char *cmd
,
274 const char *descr
, int *err
)
277 unsigned long long ret
;
279 ret
= strtoull(str
, &tmp
, 0);
285 com_err(cmd
, 0, "Bad %s - %s", descr
, str
);
294 * This function will convert a string to a block number. It returns
295 * 0 on success, 1 on failure.
297 int strtoblk(const char *cmd
, const char *str
, blk64_t
*ret
)
302 blk
= parse_ulonglong(str
, cmd
, "block number", &err
);
305 com_err(cmd
, 0, "Invalid block number: %s", str
);
310 * This is a common helper function used by the command processing
313 int common_args_process(int argc
, char *argv
[], int min_argc
, int max_argc
,
314 const char *cmd
, const char *usage
, int flags
)
316 if (argc
< min_argc
|| argc
> max_argc
) {
317 com_err(argv
[0], 0, "Usage: %s %s", cmd
, usage
);
320 if (flags
& CHECK_FS_NOTOPEN
) {
321 if (check_fs_not_open(argv
[0]))
324 if (check_fs_open(argv
[0]))
327 if ((flags
& CHECK_FS_RW
) && check_fs_read_write(argv
[0]))
329 if ((flags
& CHECK_FS_BITMAPS
) && check_fs_bitmaps(argv
[0]))
335 * This is a helper function used by do_stat, do_freei, do_seti, and
336 * do_testi, etc. Basically, any command which takes a single
337 * argument which is a file/inode number specifier.
339 int common_inode_args_process(int argc
, char *argv
[],
340 ext2_ino_t
*inode
, int flags
)
342 if (common_args_process(argc
, argv
, 2, 2, argv
[0], "<file>", flags
))
345 *inode
= string_to_inode(argv
[1]);
352 * This is a helper function used by do_freeb, do_setb, and do_testb
354 int common_block_args_process(int argc
, char *argv
[],
355 blk64_t
*block
, blk64_t
*count
)
359 if (common_args_process(argc
, argv
, 2, 3, argv
[0],
360 "<block> [count]", CHECK_FS_BITMAPS
))
363 if (strtoblk(argv
[0], argv
[1], block
))
366 com_err(argv
[0], 0, "Invalid block number 0");
371 *count
= parse_ulong(argv
[2], argv
[0], "count", &err
);
378 int debugfs_read_inode_full(ext2_ino_t ino
, struct ext2_inode
* inode
,
379 const char *cmd
, int bufsize
)
383 retval
= ext2fs_read_inode_full(current_fs
, ino
, inode
, bufsize
);
385 com_err(cmd
, retval
, "while reading inode %u", ino
);
391 int debugfs_read_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
396 retval
= ext2fs_read_inode(current_fs
, ino
, inode
);
398 com_err(cmd
, retval
, "while reading inode %u", ino
);
404 int debugfs_write_inode_full(ext2_ino_t ino
,
405 struct ext2_inode
*inode
,
411 retval
= ext2fs_write_inode_full(current_fs
, ino
,
414 com_err(cmd
, retval
, "while writing inode %u", ino
);
420 int debugfs_write_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
425 retval
= ext2fs_write_inode(current_fs
, ino
, inode
);
427 com_err(cmd
, retval
, "while writing inode %u", ino
);
433 int debugfs_write_new_inode(ext2_ino_t ino
, struct ext2_inode
* inode
,
438 retval
= ext2fs_write_new_inode(current_fs
, ino
, inode
);
440 com_err(cmd
, retval
, "while creating inode %u", ino
);