Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * util.c --- utilities for the debugfs program |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 3 | * |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | ebabf2a | 2008-07-13 15:32:37 -0400 | [diff] [blame] | 9 | #define _XOPEN_SOURCE 600 /* needed for strptime */ |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 10 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 11 | #include "config.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <ctype.h> |
| 16 | #include <string.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 17 | #include <time.h> |
Theodore Ts'o | 2c4a540 | 2000-08-19 17:33:28 +0000 | [diff] [blame] | 18 | #include <signal.h> |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 19 | #ifdef HAVE_GETOPT_H |
| 20 | #include <getopt.h> |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 21 | #else |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 22 | extern int optind; |
| 23 | extern char *optarg; |
| 24 | #endif |
| 25 | #ifdef HAVE_OPTRESET |
| 26 | extern int optreset; /* defined by BSD, but not others */ |
| 27 | #endif |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 28 | |
Theodore Ts'o | 8a1da3c5 | 2012-02-14 17:01:48 -0500 | [diff] [blame] | 29 | #include "ss/ss.h" |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 30 | #include "debugfs.h" |
| 31 | |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 32 | /* |
| 33 | * This function resets the libc getopt() function, which keeps |
| 34 | * internal state. Bad design! Stupid libc API designers! No |
| 35 | * biscuit! |
| 36 | * |
| 37 | * BSD-derived getopt() functions require that optind be reset to 1 in |
| 38 | * order to reset getopt() state. This used to be generally accepted |
| 39 | * way of resetting getopt(). However, glibc's getopt() |
| 40 | * has additional getopt() state beyond optind, and requires that |
| 41 | * optind be set zero to reset its state. So the unfortunate state of |
| 42 | * affairs is that BSD-derived versions of getopt() misbehave if |
| 43 | * optind is set to 0 in order to reset getopt(), and glibc's getopt() |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 44 | * will core dump if optind is set 1 in order to reset getopt(). |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 45 | * |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 46 | * More modern versions of BSD require that optreset be set to 1 in |
| 47 | * order to reset getopt(). Sigh. Standards, anyone? |
| 48 | * |
| 49 | * We hide the hair here. |
| 50 | */ |
| 51 | void reset_getopt(void) |
| 52 | { |
Theodore Ts'o | 9c07dc0 | 2006-05-29 11:06:16 -0400 | [diff] [blame] | 53 | #if defined(__GLIBC__) || defined(__linux__) |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 54 | optind = 0; |
| 55 | #else |
| 56 | optind = 1; |
| 57 | #endif |
| 58 | #ifdef HAVE_OPTRESET |
| 59 | optreset = 1; /* Makes BSD getopt happy */ |
| 60 | #endif |
Theodore Ts'o | ec7fdb8 | 2003-12-07 13:16:25 -0500 | [diff] [blame] | 61 | } |
| 62 | |
Theodore Ts'o | 2b5ddd7 | 2004-01-24 18:54:41 -0500 | [diff] [blame] | 63 | static const char *pager_search_list[] = { "pager", "more", "less", 0 }; |
Theodore Ts'o | ec7fdb8 | 2003-12-07 13:16:25 -0500 | [diff] [blame] | 64 | static const char *pager_dir_list[] = { "/usr/bin", "/bin", 0 }; |
| 65 | |
| 66 | static const char *find_pager(char *buf) |
| 67 | { |
| 68 | const char **i, **j; |
| 69 | |
| 70 | for (i = pager_search_list; *i; i++) { |
| 71 | for (j = pager_dir_list; *j; j++) { |
| 72 | sprintf(buf, "%s/%s", *j, *i); |
| 73 | if (access(buf, X_OK) == 0) |
| 74 | return(buf); |
| 75 | } |
| 76 | } |
| 77 | return 0; |
| 78 | } |
Theodore Ts'o | 88494bb | 2003-05-13 23:03:43 -0400 | [diff] [blame] | 79 | |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 80 | FILE *open_pager(void) |
| 81 | { |
Theodore Ts'o | ec7fdb8 | 2003-12-07 13:16:25 -0500 | [diff] [blame] | 82 | FILE *outfile = 0; |
Theodore Ts'o | 8a1da3c5 | 2012-02-14 17:01:48 -0500 | [diff] [blame] | 83 | const char *pager = ss_safe_getenv("DEBUGFS_PAGER"); |
Theodore Ts'o | ec7fdb8 | 2003-12-07 13:16:25 -0500 | [diff] [blame] | 84 | char buf[80]; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 85 | |
Theodore Ts'o | 2c4a540 | 2000-08-19 17:33:28 +0000 | [diff] [blame] | 86 | signal(SIGPIPE, SIG_IGN); |
Theodore Ts'o | a7ee4e0 | 2008-03-20 10:57:55 -0400 | [diff] [blame] | 87 | if (!isatty(1)) |
| 88 | return stdout; |
Theodore Ts'o | 2b696a9 | 2003-12-25 14:28:55 -0500 | [diff] [blame] | 89 | if (!pager) |
Theodore Ts'o | 8a1da3c5 | 2012-02-14 17:01:48 -0500 | [diff] [blame] | 90 | pager = ss_safe_getenv("PAGER"); |
Theodore Ts'o | 2b696a9 | 2003-12-25 14:28:55 -0500 | [diff] [blame] | 91 | if (!pager) |
Theodore Ts'o | ec7fdb8 | 2003-12-07 13:16:25 -0500 | [diff] [blame] | 92 | pager = find_pager(buf); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 93 | if (!pager || |
Theodore Ts'o | 2b696a9 | 2003-12-25 14:28:55 -0500 | [diff] [blame] | 94 | (strcmp(pager, "__none__") == 0) || |
| 95 | ((outfile = popen(pager, "w")) == 0)) |
| 96 | return stdout; |
| 97 | return outfile; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void close_pager(FILE *stream) |
| 101 | { |
Theodore Ts'o | 571fc5a | 2001-12-02 17:23:27 +0100 | [diff] [blame] | 102 | if (stream && stream != stdout) pclose(stream); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /* |
| 106 | * This routine is used whenever a command needs to turn a string into |
| 107 | * an inode. |
| 108 | */ |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 109 | ext2_ino_t string_to_inode(char *str) |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 110 | { |
Theodore Ts'o | b044c2e | 2001-01-11 15:26:39 +0000 | [diff] [blame] | 111 | ext2_ino_t ino; |
| 112 | int len = strlen(str); |
| 113 | char *end; |
| 114 | int retval; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * If the string is of the form <ino>, then treat it as an |
| 118 | * inode number. |
| 119 | */ |
| 120 | if ((len > 2) && (str[0] == '<') && (str[len-1] == '>')) { |
Theodore Ts'o | 9131a75 | 2000-08-23 04:36:25 +0000 | [diff] [blame] | 121 | ino = strtoul(str+1, &end, 0); |
| 122 | if (*end=='>') |
| 123 | return ino; |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 126 | retval = ext2fs_namei(current_fs, root, cwd, str, &ino); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 127 | if (retval) { |
Theodore Ts'o | 2d328bb | 2008-03-17 23:17:13 -0400 | [diff] [blame] | 128 | com_err(str, retval, 0); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | return ino; |
| 132 | } |
| 133 | |
| 134 | /* |
| 135 | * This routine returns 1 if the filesystem is not open, and prints an |
| 136 | * error message to that effect. |
| 137 | */ |
| 138 | int check_fs_open(char *name) |
| 139 | { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 140 | if (!current_fs) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 141 | com_err(name, 0, "Filesystem not open"); |
| 142 | return 1; |
| 143 | } |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* |
| 148 | * This routine returns 1 if a filesystem is open, and prints an |
| 149 | * error message to that effect. |
| 150 | */ |
| 151 | int check_fs_not_open(char *name) |
| 152 | { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 153 | if (current_fs) { |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 154 | com_err(name, 0, |
| 155 | "Filesystem %s is still open. Close it first.\n", |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 156 | current_fs->device_name); |
Theodore Ts'o | 3839e65 | 1997-04-26 13:21:57 +0000 | [diff] [blame] | 157 | return 1; |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 162 | /* |
| 163 | * This routine returns 1 if a filesystem is not opened read/write, |
| 164 | * and prints an error message to that effect. |
| 165 | */ |
| 166 | int check_fs_read_write(char *name) |
| 167 | { |
| 168 | if (!(current_fs->flags & EXT2_FLAG_RW)) { |
| 169 | com_err(name, 0, "Filesystem opened read/only"); |
| 170 | return 1; |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* |
Theodore Ts'o | d61f617 | 2000-05-27 16:04:00 +0000 | [diff] [blame] | 176 | * This routine returns 1 if a filesystem is doesn't have its inode |
| 177 | * and block bitmaps loaded, and prints an error message to that |
| 178 | * effect. |
| 179 | */ |
| 180 | int check_fs_bitmaps(char *name) |
| 181 | { |
| 182 | if (!current_fs->block_map || !current_fs->inode_map) { |
| 183 | com_err(name, 0, "Filesystem bitmaps not loaded"); |
| 184 | return 1; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /* |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 190 | * This function takes a __u32 time value and converts it to a string, |
| 191 | * using ctime |
| 192 | */ |
| 193 | char *time_to_string(__u32 cl) |
| 194 | { |
Theodore Ts'o | acb79d9 | 2004-12-15 12:21:41 -0500 | [diff] [blame] | 195 | static int do_gmt = -1; |
| 196 | time_t t = (time_t) cl; |
Theodore Ts'o | d4e0b1c | 2007-08-03 18:56:01 -0400 | [diff] [blame] | 197 | const char *tz; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 198 | |
Theodore Ts'o | 8ff1a86 | 2004-11-30 19:57:20 -0500 | [diff] [blame] | 199 | if (do_gmt == -1) { |
| 200 | /* The diet libc doesn't respect the TZ environemnt variable */ |
Theodore Ts'o | 8a1da3c5 | 2012-02-14 17:01:48 -0500 | [diff] [blame] | 201 | tz = ss_safe_getenv("TZ"); |
Theodore Ts'o | acb79d9 | 2004-12-15 12:21:41 -0500 | [diff] [blame] | 202 | if (!tz) |
| 203 | tz = ""; |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame^] | 204 | do_gmt = !strcmp(tz, "GMT") | !strcmp(tz, "GMT0"); |
Theodore Ts'o | 8ff1a86 | 2004-11-30 19:57:20 -0500 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | return asctime((do_gmt) ? gmtime(&t) : localtime(&t)); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 210 | /* |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 211 | * Parse a string as a time. Return ((time_t)-1) if the string |
| 212 | * doesn't appear to be a sane time. |
| 213 | */ |
Theodore Ts'o | f404167 | 2013-12-16 18:56:36 -0500 | [diff] [blame] | 214 | time_t string_to_time(const char *arg) |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 215 | { |
| 216 | struct tm ts; |
Theodore Ts'o | a4ea6b9 | 2007-04-10 21:10:55 -0400 | [diff] [blame] | 217 | time_t ret; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 218 | char *tmp; |
| 219 | |
| 220 | if (strcmp(arg, "now") == 0) { |
| 221 | return time(0); |
| 222 | } |
Theodore Ts'o | 6854ab1 | 2012-06-11 22:02:17 -0400 | [diff] [blame] | 223 | if (arg[0] == '@') { |
| 224 | /* interpret it as an integer */ |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame^] | 225 | arg++; |
| 226 | fallback: |
| 227 | ret = strtoul(arg, &tmp, 0); |
Theodore Ts'o | 6854ab1 | 2012-06-11 22:02:17 -0400 | [diff] [blame] | 228 | if (*tmp) |
| 229 | return ((time_t) -1); |
| 230 | return ret; |
| 231 | } |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 232 | memset(&ts, 0, sizeof(ts)); |
| 233 | #ifdef HAVE_STRPTIME |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame^] | 234 | ret = strptime(arg, "%Y%m%d%H%M%S", &ts); |
| 235 | if (ret == NULL) |
| 236 | goto fallback; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 237 | #else |
| 238 | sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon, |
| 239 | &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); |
| 240 | ts.tm_year -= 1900; |
| 241 | ts.tm_mon -= 1; |
| 242 | if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 || |
| 243 | ts.tm_mday < 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || |
| 244 | ts.tm_min > 59 || ts.tm_sec > 61) |
| 245 | ts.tm_mday = 0; |
| 246 | #endif |
Theodore Ts'o | a2ff0f3 | 2008-03-21 09:10:09 -0400 | [diff] [blame] | 247 | ts.tm_isdst = -1; |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame^] | 248 | ret = ts.tm_sec + ts.tm_min*60 + ts.tm_hour*3600 + ts.tm_yday*86400 + |
| 249 | (ts.tm_year-70)*31536000 + ((ts.tm_year-69)/4)*86400 - |
| 250 | ((ts.tm_year-1)/100)*86400 + ((ts.tm_year+299)/400)*86400; |
Theodore Ts'o | a4ea6b9 | 2007-04-10 21:10:55 -0400 | [diff] [blame] | 251 | return ret; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | /* |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 255 | * This function will convert a string to an unsigned long, printing |
| 256 | * an error message if it fails, and returning success or failure in err. |
| 257 | */ |
| 258 | unsigned long parse_ulong(const char *str, const char *cmd, |
| 259 | const char *descr, int *err) |
| 260 | { |
| 261 | char *tmp; |
| 262 | unsigned long ret; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 263 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 264 | ret = strtoul(str, &tmp, 0); |
| 265 | if (*tmp == 0) { |
Theodore Ts'o | e5b3b27 | 2002-04-01 15:42:21 -0500 | [diff] [blame] | 266 | if (err) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 267 | *err = 0; |
| 268 | return ret; |
| 269 | } |
| 270 | com_err(cmd, 0, "Bad %s - %s", descr, str); |
Theodore Ts'o | e5b3b27 | 2002-04-01 15:42:21 -0500 | [diff] [blame] | 271 | if (err) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 272 | *err = 1; |
| 273 | else |
| 274 | exit(1); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | /* |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 279 | * This function will convert a string to an unsigned long long, printing |
| 280 | * an error message if it fails, and returning success or failure in err. |
| 281 | */ |
| 282 | unsigned long long parse_ulonglong(const char *str, const char *cmd, |
| 283 | const char *descr, int *err) |
| 284 | { |
| 285 | char *tmp; |
| 286 | unsigned long long ret; |
| 287 | |
| 288 | ret = strtoull(str, &tmp, 0); |
| 289 | if (*tmp == 0) { |
| 290 | if (err) |
| 291 | *err = 0; |
| 292 | return ret; |
| 293 | } |
| 294 | com_err(cmd, 0, "Bad %s - %s", descr, str); |
| 295 | if (err) |
| 296 | *err = 1; |
| 297 | else |
| 298 | exit(1); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | /* |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 303 | * This function will convert a string to a block number. It returns |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 304 | * 0 on success, 1 on failure. On failure, it outputs either an optionally |
| 305 | * specified error message or a default. |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 306 | */ |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 307 | int strtoblk(const char *cmd, const char *str, const char *errmsg, |
| 308 | blk64_t *ret) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 309 | { |
Eric Sandeen | 97a67c4 | 2012-05-27 22:11:58 -0400 | [diff] [blame] | 310 | blk64_t blk; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 311 | int err; |
| 312 | |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 313 | if (errmsg == NULL) |
| 314 | blk = parse_ulonglong(str, cmd, "block number", &err); |
| 315 | else |
| 316 | blk = parse_ulonglong(str, cmd, errmsg, &err); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 317 | *ret = blk; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 318 | return err; |
| 319 | } |
| 320 | |
| 321 | /* |
| 322 | * This is a common helper function used by the command processing |
| 323 | * routines |
| 324 | */ |
| 325 | int common_args_process(int argc, char *argv[], int min_argc, int max_argc, |
| 326 | const char *cmd, const char *usage, int flags) |
| 327 | { |
| 328 | if (argc < min_argc || argc > max_argc) { |
| 329 | com_err(argv[0], 0, "Usage: %s %s", cmd, usage); |
| 330 | return 1; |
| 331 | } |
| 332 | if (flags & CHECK_FS_NOTOPEN) { |
| 333 | if (check_fs_not_open(argv[0])) |
| 334 | return 1; |
| 335 | } else { |
| 336 | if (check_fs_open(argv[0])) |
| 337 | return 1; |
| 338 | } |
| 339 | if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0])) |
| 340 | return 1; |
| 341 | if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0])) |
| 342 | return 1; |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* |
| 347 | * This is a helper function used by do_stat, do_freei, do_seti, and |
| 348 | * do_testi, etc. Basically, any command which takes a single |
| 349 | * argument which is a file/inode number specifier. |
| 350 | */ |
| 351 | int common_inode_args_process(int argc, char *argv[], |
| 352 | ext2_ino_t *inode, int flags) |
| 353 | { |
| 354 | if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags)) |
| 355 | return 1; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 356 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 357 | *inode = string_to_inode(argv[1]); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 358 | if (!*inode) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 359 | return 1; |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * This is a helper function used by do_freeb, do_setb, and do_testb |
| 365 | */ |
| 366 | int common_block_args_process(int argc, char *argv[], |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 367 | blk64_t *block, blk64_t *count) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 368 | { |
| 369 | int err; |
| 370 | |
| 371 | if (common_args_process(argc, argv, 2, 3, argv[0], |
| 372 | "<block> [count]", CHECK_FS_BITMAPS)) |
| 373 | return 1; |
| 374 | |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 375 | if (strtoblk(argv[0], argv[1], NULL, block)) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 376 | return 1; |
Theodore Ts'o | d8d1255 | 2008-02-18 09:55:04 -0500 | [diff] [blame] | 377 | if (*block == 0) { |
| 378 | com_err(argv[0], 0, "Invalid block number 0"); |
| 379 | err = 1; |
| 380 | } |
| 381 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 382 | if (argc > 2) { |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 383 | err = strtoblk(argv[0], argv[2], "count", count); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 384 | if (err) |
| 385 | return 1; |
| 386 | } |
| 387 | return 0; |
| 388 | } |
| 389 | |
Theodore Ts'o | ea822ee | 2005-03-20 18:03:58 -0500 | [diff] [blame] | 390 | int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode, |
| 391 | const char *cmd, int bufsize) |
| 392 | { |
| 393 | int retval; |
| 394 | |
| 395 | retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize); |
| 396 | if (retval) { |
| 397 | com_err(cmd, retval, "while reading inode %u", ino); |
| 398 | return 1; |
| 399 | } |
| 400 | return 0; |
| 401 | } |
| 402 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 403 | int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode, |
| 404 | const char *cmd) |
| 405 | { |
| 406 | int retval; |
| 407 | |
| 408 | retval = ext2fs_read_inode(current_fs, ino, inode); |
| 409 | if (retval) { |
| 410 | com_err(cmd, retval, "while reading inode %u", ino); |
| 411 | return 1; |
| 412 | } |
| 413 | return 0; |
| 414 | } |
| 415 | |
Theodore Ts'o | 16c581d | 2011-09-15 13:38:41 -0400 | [diff] [blame] | 416 | int debugfs_write_inode_full(ext2_ino_t ino, |
| 417 | struct ext2_inode *inode, |
| 418 | const char *cmd, |
| 419 | int bufsize) |
| 420 | { |
| 421 | int retval; |
| 422 | |
| 423 | retval = ext2fs_write_inode_full(current_fs, ino, |
| 424 | inode, bufsize); |
| 425 | if (retval) { |
| 426 | com_err(cmd, retval, "while writing inode %u", ino); |
| 427 | return 1; |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 432 | int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode, |
| 433 | const char *cmd) |
| 434 | { |
| 435 | int retval; |
| 436 | |
| 437 | retval = ext2fs_write_inode(current_fs, ino, inode); |
| 438 | if (retval) { |
| 439 | com_err(cmd, retval, "while writing inode %u", ino); |
| 440 | return 1; |
| 441 | } |
| 442 | return 0; |
| 443 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 444 | |
Theodore Ts'o | 030970e | 2005-03-20 20:05:22 -0500 | [diff] [blame] | 445 | int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode, |
| 446 | const char *cmd) |
| 447 | { |
| 448 | int retval; |
| 449 | |
| 450 | retval = ext2fs_write_new_inode(current_fs, ino, inode); |
| 451 | if (retval) { |
| 452 | com_err(cmd, retval, "while creating inode %u", ino); |
| 453 | return 1; |
| 454 | } |
| 455 | return 0; |
| 456 | } |
Theodore Ts'o | 8bf1e91 | 2012-01-17 17:08:39 -0500 | [diff] [blame] | 457 | |
| 458 | /* |
| 459 | * Given a mode, return the ext2 file type |
| 460 | */ |
| 461 | int ext2_file_type(unsigned int mode) |
| 462 | { |
| 463 | if (LINUX_S_ISREG(mode)) |
| 464 | return EXT2_FT_REG_FILE; |
| 465 | |
| 466 | if (LINUX_S_ISDIR(mode)) |
| 467 | return EXT2_FT_DIR; |
| 468 | |
| 469 | if (LINUX_S_ISCHR(mode)) |
| 470 | return EXT2_FT_CHRDEV; |
| 471 | |
| 472 | if (LINUX_S_ISBLK(mode)) |
| 473 | return EXT2_FT_BLKDEV; |
| 474 | |
| 475 | if (LINUX_S_ISLNK(mode)) |
| 476 | return EXT2_FT_SYMLINK; |
| 477 | |
| 478 | if (LINUX_S_ISFIFO(mode)) |
| 479 | return EXT2_FT_FIFO; |
| 480 | |
| 481 | if (LINUX_S_ISSOCK(mode)) |
| 482 | return EXT2_FT_SOCK; |
| 483 | |
| 484 | return 0; |
| 485 | } |