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 | |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 189 | char *inode_time_to_string(__u32 xtime, __u32 xtime_extra) |
| 190 | { |
| 191 | __s64 t = (__s32) xtime; |
| 192 | |
| 193 | t += (__s64) (xtime_extra & EXT4_EPOCH_MASK) << 32; |
| 194 | return time_to_string(t); |
| 195 | } |
| 196 | |
Theodore Ts'o | d61f617 | 2000-05-27 16:04:00 +0000 | [diff] [blame] | 197 | /* |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 198 | * This function takes a __s64 time value and converts it to a string, |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 199 | * using ctime |
| 200 | */ |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 201 | char *time_to_string(__s64 cl) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 202 | { |
Theodore Ts'o | acb79d9 | 2004-12-15 12:21:41 -0500 | [diff] [blame] | 203 | static int do_gmt = -1; |
| 204 | time_t t = (time_t) cl; |
Theodore Ts'o | d4e0b1c | 2007-08-03 18:56:01 -0400 | [diff] [blame] | 205 | const char *tz; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 206 | |
Theodore Ts'o | 8ff1a86 | 2004-11-30 19:57:20 -0500 | [diff] [blame] | 207 | if (do_gmt == -1) { |
| 208 | /* The diet libc doesn't respect the TZ environemnt variable */ |
Theodore Ts'o | 8a1da3c5 | 2012-02-14 17:01:48 -0500 | [diff] [blame] | 209 | tz = ss_safe_getenv("TZ"); |
Theodore Ts'o | acb79d9 | 2004-12-15 12:21:41 -0500 | [diff] [blame] | 210 | if (!tz) |
| 211 | tz = ""; |
Darrick J. Wong | 8a6cc1a | 2014-03-14 09:43:11 -0400 | [diff] [blame] | 212 | do_gmt = !strcmp(tz, "GMT") || !strcmp(tz, "GMT0"); |
Theodore Ts'o | 8ff1a86 | 2004-11-30 19:57:20 -0500 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return asctime((do_gmt) ? gmtime(&t) : localtime(&t)); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 218 | /* |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 219 | * Parse a string as a time. Return ((time_t)-1) if the string |
| 220 | * doesn't appear to be a sane time. |
| 221 | */ |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 222 | extern __s64 string_to_time(const char *arg) |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 223 | { |
| 224 | struct tm ts; |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 225 | __s64 ret; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 226 | char *tmp; |
| 227 | |
| 228 | if (strcmp(arg, "now") == 0) { |
| 229 | return time(0); |
| 230 | } |
Theodore Ts'o | 6854ab1 | 2012-06-11 22:02:17 -0400 | [diff] [blame] | 231 | if (arg[0] == '@') { |
| 232 | /* interpret it as an integer */ |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame] | 233 | arg++; |
| 234 | fallback: |
Theodore Ts'o | 6743215 | 2016-03-14 13:54:41 -0400 | [diff] [blame] | 235 | ret = strtoll(arg, &tmp, 0); |
Theodore Ts'o | 6854ab1 | 2012-06-11 22:02:17 -0400 | [diff] [blame] | 236 | if (*tmp) |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 237 | return -1; |
Theodore Ts'o | 6854ab1 | 2012-06-11 22:02:17 -0400 | [diff] [blame] | 238 | return ret; |
| 239 | } |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 240 | memset(&ts, 0, sizeof(ts)); |
| 241 | #ifdef HAVE_STRPTIME |
Theodore Ts'o | 40ca5c4 | 2014-01-09 14:30:43 -0500 | [diff] [blame] | 242 | tmp = strptime(arg, "%Y%m%d%H%M%S", &ts); |
| 243 | if (tmp == NULL) |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 244 | tmp = strptime(arg, "%Y%m%d%H%M", &ts); |
| 245 | if (tmp == NULL) |
| 246 | tmp = strptime(arg, "%Y%m%d", &ts); |
| 247 | if (tmp == NULL) |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame] | 248 | goto fallback; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 249 | #else |
| 250 | sscanf(arg, "%4d%2d%2d%2d%2d%2d", &ts.tm_year, &ts.tm_mon, |
| 251 | &ts.tm_mday, &ts.tm_hour, &ts.tm_min, &ts.tm_sec); |
| 252 | ts.tm_year -= 1900; |
| 253 | ts.tm_mon -= 1; |
| 254 | if (ts.tm_year < 0 || ts.tm_mon < 0 || ts.tm_mon > 11 || |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 255 | ts.tm_mday <= 0 || ts.tm_mday > 31 || ts.tm_hour > 23 || |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 256 | ts.tm_min > 59 || ts.tm_sec > 61) |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 257 | goto fallback; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 258 | #endif |
Theodore Ts'o | a2ff0f3 | 2008-03-21 09:10:09 -0400 | [diff] [blame] | 259 | ts.tm_isdst = -1; |
Andreas Dilger | 90c5b4c | 2014-05-20 16:31:10 -0600 | [diff] [blame] | 260 | /* strptime() may only update the specified fields, which does not |
| 261 | * necessarily include ts.tm_yday (%j). Calculate this if unset: |
| 262 | * |
| 263 | * Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec |
| 264 | * 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 |
| 265 | * |
| 266 | * Start with 31 days per month. Even months have only 30 days, but |
| 267 | * reverse in August, subtract one day for those months. February has |
| 268 | * only 28 days, not 30, subtract two days. Add day of month, minus |
| 269 | * one, since day is not finished yet. Leap years handled afterward. */ |
| 270 | if (ts.tm_yday == 0) |
| 271 | ts.tm_yday = (ts.tm_mon * 31) - |
| 272 | ((ts.tm_mon - (ts.tm_mon > 7)) / 2) - |
| 273 | 2 * (ts.tm_mon > 1) + ts.tm_mday - 1; |
Theodore Ts'o | 5a1d25a | 2014-01-08 22:25:04 -0500 | [diff] [blame] | 274 | ret = ts.tm_sec + ts.tm_min*60 + ts.tm_hour*3600 + ts.tm_yday*86400 + |
Theodore Ts'o | 188960e | 2013-12-09 13:55:23 -0500 | [diff] [blame] | 275 | ((__s64) ts.tm_year-70)*31536000 + |
| 276 | (((__s64) ts.tm_year-69)/4)*86400 - |
| 277 | (((__s64) ts.tm_year-1)/100)*86400 + |
| 278 | (((__s64) ts.tm_year+299)/400)*86400; |
Theodore Ts'o | a4ea6b9 | 2007-04-10 21:10:55 -0400 | [diff] [blame] | 279 | return ret; |
Theodore Ts'o | 4efae60 | 2005-09-24 21:56:38 -0400 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | /* |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 283 | * This function will convert a string to an unsigned long, printing |
| 284 | * an error message if it fails, and returning success or failure in err. |
| 285 | */ |
| 286 | unsigned long parse_ulong(const char *str, const char *cmd, |
| 287 | const char *descr, int *err) |
| 288 | { |
| 289 | char *tmp; |
| 290 | unsigned long ret; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 291 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 292 | ret = strtoul(str, &tmp, 0); |
| 293 | if (*tmp == 0) { |
Theodore Ts'o | e5b3b27 | 2002-04-01 15:42:21 -0500 | [diff] [blame] | 294 | if (err) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 295 | *err = 0; |
| 296 | return ret; |
| 297 | } |
| 298 | com_err(cmd, 0, "Bad %s - %s", descr, str); |
Theodore Ts'o | e5b3b27 | 2002-04-01 15:42:21 -0500 | [diff] [blame] | 299 | if (err) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 300 | *err = 1; |
| 301 | else |
| 302 | exit(1); |
| 303 | return 0; |
| 304 | } |
| 305 | |
| 306 | /* |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 307 | * This function will convert a string to an unsigned long long, printing |
| 308 | * an error message if it fails, and returning success or failure in err. |
| 309 | */ |
| 310 | unsigned long long parse_ulonglong(const char *str, const char *cmd, |
| 311 | const char *descr, int *err) |
| 312 | { |
| 313 | char *tmp; |
| 314 | unsigned long long ret; |
| 315 | |
| 316 | ret = strtoull(str, &tmp, 0); |
| 317 | if (*tmp == 0) { |
| 318 | if (err) |
| 319 | *err = 0; |
| 320 | return ret; |
| 321 | } |
| 322 | com_err(cmd, 0, "Bad %s - %s", descr, str); |
| 323 | if (err) |
| 324 | *err = 1; |
| 325 | else |
| 326 | exit(1); |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /* |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 331 | * This function will convert a string to a block number. It returns |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 332 | * 0 on success, 1 on failure. On failure, it outputs either an optionally |
| 333 | * specified error message or a default. |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 334 | */ |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 335 | int strtoblk(const char *cmd, const char *str, const char *errmsg, |
| 336 | blk64_t *ret) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 337 | { |
Eric Sandeen | 97a67c4 | 2012-05-27 22:11:58 -0400 | [diff] [blame] | 338 | blk64_t blk; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 339 | int err; |
| 340 | |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 341 | if (errmsg == NULL) |
| 342 | blk = parse_ulonglong(str, cmd, "block number", &err); |
| 343 | else |
| 344 | blk = parse_ulonglong(str, cmd, errmsg, &err); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 345 | *ret = blk; |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 346 | return err; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * This is a common helper function used by the command processing |
| 351 | * routines |
| 352 | */ |
| 353 | int common_args_process(int argc, char *argv[], int min_argc, int max_argc, |
| 354 | const char *cmd, const char *usage, int flags) |
| 355 | { |
| 356 | if (argc < min_argc || argc > max_argc) { |
| 357 | com_err(argv[0], 0, "Usage: %s %s", cmd, usage); |
| 358 | return 1; |
| 359 | } |
| 360 | if (flags & CHECK_FS_NOTOPEN) { |
| 361 | if (check_fs_not_open(argv[0])) |
| 362 | return 1; |
| 363 | } else { |
| 364 | if (check_fs_open(argv[0])) |
| 365 | return 1; |
| 366 | } |
| 367 | if ((flags & CHECK_FS_RW) && check_fs_read_write(argv[0])) |
| 368 | return 1; |
| 369 | if ((flags & CHECK_FS_BITMAPS) && check_fs_bitmaps(argv[0])) |
| 370 | return 1; |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * This is a helper function used by do_stat, do_freei, do_seti, and |
| 376 | * do_testi, etc. Basically, any command which takes a single |
| 377 | * argument which is a file/inode number specifier. |
| 378 | */ |
| 379 | int common_inode_args_process(int argc, char *argv[], |
| 380 | ext2_ino_t *inode, int flags) |
| 381 | { |
| 382 | if (common_args_process(argc, argv, 2, 2, argv[0], "<file>", flags)) |
| 383 | return 1; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 384 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 385 | *inode = string_to_inode(argv[1]); |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 386 | if (!*inode) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 387 | return 1; |
| 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * This is a helper function used by do_freeb, do_setb, and do_testb |
| 393 | */ |
| 394 | int common_block_args_process(int argc, char *argv[], |
Valerie Aurora Henson | 048786d | 2009-09-07 22:46:17 -0400 | [diff] [blame] | 395 | blk64_t *block, blk64_t *count) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 396 | { |
| 397 | int err; |
| 398 | |
| 399 | if (common_args_process(argc, argv, 2, 3, argv[0], |
| 400 | "<block> [count]", CHECK_FS_BITMAPS)) |
| 401 | return 1; |
| 402 | |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 403 | if (strtoblk(argv[0], argv[1], NULL, block)) |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 404 | return 1; |
Theodore Ts'o | d8d1255 | 2008-02-18 09:55:04 -0500 | [diff] [blame] | 405 | if (*block == 0) { |
| 406 | com_err(argv[0], 0, "Invalid block number 0"); |
Darrick J. Wong | 63cd76d | 2015-05-16 18:32:33 -0400 | [diff] [blame] | 407 | return 1; |
Theodore Ts'o | d8d1255 | 2008-02-18 09:55:04 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 410 | if (argc > 2) { |
Eric Whitney | a25fffa | 2013-12-30 16:43:46 -0500 | [diff] [blame] | 411 | err = strtoblk(argv[0], argv[2], "count", count); |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 412 | if (err) |
| 413 | return 1; |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
Theodore Ts'o | ea822ee | 2005-03-20 18:03:58 -0500 | [diff] [blame] | 418 | int debugfs_read_inode_full(ext2_ino_t ino, struct ext2_inode * inode, |
| 419 | const char *cmd, int bufsize) |
| 420 | { |
| 421 | int retval; |
| 422 | |
| 423 | retval = ext2fs_read_inode_full(current_fs, ino, inode, bufsize); |
| 424 | if (retval) { |
| 425 | com_err(cmd, retval, "while reading inode %u", ino); |
| 426 | return 1; |
| 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 431 | int debugfs_read_inode(ext2_ino_t ino, struct ext2_inode * inode, |
| 432 | const char *cmd) |
| 433 | { |
| 434 | int retval; |
| 435 | |
| 436 | retval = ext2fs_read_inode(current_fs, ino, inode); |
| 437 | if (retval) { |
| 438 | com_err(cmd, retval, "while reading inode %u", ino); |
| 439 | return 1; |
| 440 | } |
| 441 | return 0; |
| 442 | } |
| 443 | |
Theodore Ts'o | 16c581d | 2011-09-15 13:38:41 -0400 | [diff] [blame] | 444 | int debugfs_write_inode_full(ext2_ino_t ino, |
| 445 | struct ext2_inode *inode, |
| 446 | const char *cmd, |
| 447 | int bufsize) |
| 448 | { |
| 449 | int retval; |
| 450 | |
| 451 | retval = ext2fs_write_inode_full(current_fs, ino, |
| 452 | inode, bufsize); |
| 453 | if (retval) { |
| 454 | com_err(cmd, retval, "while writing inode %u", ino); |
| 455 | return 1; |
| 456 | } |
| 457 | return 0; |
| 458 | } |
| 459 | |
Theodore Ts'o | e1018ee | 2002-01-03 04:55:25 -0500 | [diff] [blame] | 460 | int debugfs_write_inode(ext2_ino_t ino, struct ext2_inode * inode, |
| 461 | const char *cmd) |
| 462 | { |
| 463 | int retval; |
| 464 | |
| 465 | retval = ext2fs_write_inode(current_fs, ino, inode); |
| 466 | if (retval) { |
| 467 | com_err(cmd, retval, "while writing inode %u", ino); |
| 468 | return 1; |
| 469 | } |
| 470 | return 0; |
| 471 | } |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 472 | |
Theodore Ts'o | 030970e | 2005-03-20 20:05:22 -0500 | [diff] [blame] | 473 | int debugfs_write_new_inode(ext2_ino_t ino, struct ext2_inode * inode, |
| 474 | const char *cmd) |
| 475 | { |
| 476 | int retval; |
| 477 | |
| 478 | retval = ext2fs_write_new_inode(current_fs, ino, inode); |
| 479 | if (retval) { |
| 480 | com_err(cmd, retval, "while creating inode %u", ino); |
| 481 | return 1; |
| 482 | } |
| 483 | return 0; |
| 484 | } |
Theodore Ts'o | 8bf1e91 | 2012-01-17 17:08:39 -0500 | [diff] [blame] | 485 | |
| 486 | /* |
| 487 | * Given a mode, return the ext2 file type |
| 488 | */ |
| 489 | int ext2_file_type(unsigned int mode) |
| 490 | { |
| 491 | if (LINUX_S_ISREG(mode)) |
| 492 | return EXT2_FT_REG_FILE; |
| 493 | |
| 494 | if (LINUX_S_ISDIR(mode)) |
| 495 | return EXT2_FT_DIR; |
| 496 | |
| 497 | if (LINUX_S_ISCHR(mode)) |
| 498 | return EXT2_FT_CHRDEV; |
| 499 | |
| 500 | if (LINUX_S_ISBLK(mode)) |
| 501 | return EXT2_FT_BLKDEV; |
| 502 | |
| 503 | if (LINUX_S_ISLNK(mode)) |
| 504 | return EXT2_FT_SYMLINK; |
| 505 | |
| 506 | if (LINUX_S_ISFIFO(mode)) |
| 507 | return EXT2_FT_FIFO; |
| 508 | |
| 509 | if (LINUX_S_ISSOCK(mode)) |
| 510 | return EXT2_FT_SOCK; |
| 511 | |
| 512 | return 0; |
| 513 | } |
Darrick J. Wong | 463eb92 | 2014-09-08 16:12:54 -0700 | [diff] [blame] | 514 | |
Theodore Ts'o | 25f291c | 2015-07-13 08:31:51 -0400 | [diff] [blame] | 515 | errcode_t read_list(char *str, blk64_t **list, size_t *len) |
Darrick J. Wong | 463eb92 | 2014-09-08 16:12:54 -0700 | [diff] [blame] | 516 | { |
| 517 | blk64_t *lst = *list; |
| 518 | size_t ln = *len; |
Theodore Ts'o | 25f291c | 2015-07-13 08:31:51 -0400 | [diff] [blame] | 519 | char *tok, *p = str; |
Darrick J. Wong | d4daf0d | 2014-09-18 21:47:38 -0400 | [diff] [blame] | 520 | errcode_t retval; |
Darrick J. Wong | 463eb92 | 2014-09-08 16:12:54 -0700 | [diff] [blame] | 521 | |
| 522 | while ((tok = strtok(p, ","))) { |
| 523 | blk64_t *l; |
| 524 | blk64_t x, y; |
| 525 | char *e; |
| 526 | |
| 527 | errno = 0; |
| 528 | y = x = strtoull(tok, &e, 0); |
| 529 | if (errno) |
| 530 | return errno; |
| 531 | if (*e == '-') { |
| 532 | y = strtoull(e + 1, NULL, 0); |
| 533 | if (errno) |
| 534 | return errno; |
Darrick J. Wong | d4daf0d | 2014-09-18 21:47:38 -0400 | [diff] [blame] | 535 | } else if (*e != 0) { |
| 536 | retval = EINVAL; |
| 537 | goto err; |
| 538 | } |
| 539 | if (y < x) { |
| 540 | retval = EINVAL; |
| 541 | goto err; |
| 542 | } |
Darrick J. Wong | 463eb92 | 2014-09-08 16:12:54 -0700 | [diff] [blame] | 543 | l = realloc(lst, sizeof(blk64_t) * (ln + y - x + 1)); |
Darrick J. Wong | d4daf0d | 2014-09-18 21:47:38 -0400 | [diff] [blame] | 544 | if (l == NULL) { |
| 545 | retval = ENOMEM; |
| 546 | goto err; |
| 547 | } |
Darrick J. Wong | 463eb92 | 2014-09-08 16:12:54 -0700 | [diff] [blame] | 548 | lst = l; |
| 549 | for (; x <= y; x++) |
| 550 | lst[ln++] = x; |
| 551 | p = NULL; |
| 552 | } |
| 553 | |
| 554 | *list = lst; |
| 555 | *len = ln; |
| 556 | return 0; |
Darrick J. Wong | d4daf0d | 2014-09-18 21:47:38 -0400 | [diff] [blame] | 557 | err: |
| 558 | free(lst); |
| 559 | return retval; |
Darrick J. Wong | 463eb92 | 2014-09-08 16:12:54 -0700 | [diff] [blame] | 560 | } |