Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 14 | #include <time.h> |
Theodore Ts'o | 2c4a540 | 2000-08-19 17:33:28 +0000 | [diff] [blame] | 15 | #include <signal.h> |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 16 | |
| 17 | #include "debugfs.h" |
| 18 | |
| 19 | FILE *open_pager(void) |
| 20 | { |
| 21 | FILE *outfile; |
| 22 | char *pager = getenv("PAGER"); |
| 23 | |
Theodore Ts'o | 2c4a540 | 2000-08-19 17:33:28 +0000 | [diff] [blame] | 24 | signal(SIGPIPE, SIG_IGN); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 25 | if (!pager) |
Theodore Ts'o | c6bd0c9 | 2000-08-14 20:37:09 +0000 | [diff] [blame] | 26 | pager = "more"; |
| 27 | |
| 28 | outfile = popen(pager, "w"); |
| 29 | if (!outfile) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | outfile = stdout; |
Theodore Ts'o | c6bd0c9 | 2000-08-14 20:37:09 +0000 | [diff] [blame] | 31 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 32 | return (outfile); |
| 33 | } |
| 34 | |
| 35 | void close_pager(FILE *stream) |
| 36 | { |
| 37 | if (stream && stream != stdout) fclose(stream); |
| 38 | } |
| 39 | |
| 40 | /* |
| 41 | * This routine is used whenever a command needs to turn a string into |
| 42 | * an inode. |
| 43 | */ |
| 44 | ino_t string_to_inode(char *str) |
| 45 | { |
| 46 | ino_t ino; |
| 47 | int len = strlen(str); |
Theodore Ts'o | 9131a75 | 2000-08-23 04:36:25 +0000 | [diff] [blame] | 48 | char *end; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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] == '>')) { |
Theodore Ts'o | 9131a75 | 2000-08-23 04:36:25 +0000 | [diff] [blame] | 56 | ino = strtoul(str+1, &end, 0); |
| 57 | if (*end=='>') |
| 58 | return ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 61 | retval = ext2fs_namei(current_fs, root, cwd, str, &ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 75 | if (!current_fs) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 88 | if (current_fs) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 89 | com_err(name, 0, |
| 90 | "Filesystem %s is still open. Close it first.\n", |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 91 | current_fs->device_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 92 | return 1; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 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 | /* |
Theodore Ts'o | d61f617 | 2000-05-27 16:04:00 +0000 | [diff] [blame] | 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 | /* |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 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 | |
| 137 | |