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