[Ocfs2-tools-commits] smushran commits r508 - in trunk/debugfs.ocfs2: . include

svn-commits at oss.oracle.com svn-commits at oss.oracle.com
Wed Dec 22 19:45:56 CST 2004


Author: smushran
Date: 2004-12-22 19:45:54 -0600 (Wed, 22 Dec 2004)
New Revision: 508

Modified:
   trunk/debugfs.ocfs2/commands.c
   trunk/debugfs.ocfs2/include/main.h
   trunk/debugfs.ocfs2/include/utils.h
   trunk/debugfs.ocfs2/main.c
   trunk/debugfs.ocfs2/utils.c
Log:
debugfs now supports -f cmd_file to allow batch execution of cmds

Modified: trunk/debugfs.ocfs2/commands.c
===================================================================
--- trunk/debugfs.ocfs2/commands.c	2004-12-22 22:08:13 UTC (rev 507)
+++ trunk/debugfs.ocfs2/commands.c	2004-12-23 01:45:54 UTC (rev 508)
@@ -60,8 +60,6 @@
 static void do_chroot (char **args);
 static void do_slotmap (char **args);
 
-extern gboolean allow_write;
-
 dbgfs_gbls gbls;
 
 static Command commands[] = {
@@ -136,13 +134,20 @@
 	/* Move empty strings to the end */
 	crunch_strsplit(args);
 
+	/* ignore commented line */
+	if (!strncmp(args[0], "#", 1))
+		goto bail;
+
 	command = find_command (args[0]);
 
+	fflush(stdout);
+
 	if (command)
 		command->func (args);
 	else
-		fprintf(stderr, "%s: Unrecognized command\n", args[0]);
+		fprintf(stderr, "%s: command not found\n", args[0]);
 
+bail:
 	g_strfreev (args);
 }
 
@@ -153,7 +158,7 @@
 static int check_device_open(void)
 {
 	if (!gbls.fs) {
-		fprintf(stderr, "Device not open\n");
+		fprintf(stderr, "No device open\n");
 		return -1;
 	}
 
@@ -388,7 +393,7 @@
 		return ;
 	}
 
-	flags = allow_write ? OCFS2_FLAG_RW : OCFS2_FLAG_RO;
+	flags = gbls.allow_write ? OCFS2_FLAG_RW : OCFS2_FLAG_RO;
 	ret = ocfs2_open(dev, flags, 0, 0, &gbls.fs);
 	if (ret) {
 		gbls.fs = NULL;
@@ -544,7 +549,7 @@
 		}
 	}
 
-	ls_opts.out = open_pager();
+	ls_opts.out = open_pager(gbls.interactive);
 	ret = ocfs2_dir_iterate(gbls.fs, blkno, 0, NULL,
 				dump_dir_entry, (void *)&ls_opts);
 	if (ret)
@@ -648,7 +653,7 @@
 	if (check_device_open())
 		goto bail;
 
-	out = open_pager ();
+	out = open_pager(gbls.interactive);
 	in = gbls.fs->fs_super;
 	sb = OCFS2_RAW_SB(gbls.fs->fs_super);
 	dump_super_block(out, sb);
@@ -686,7 +691,7 @@
 
 	inode = (ocfs2_dinode *)buf;
 
-	out = open_pager();
+	out = open_pager(gbls.interactive);
 	dump_inode(out, inode);
 
 	if ((inode->i_flags & OCFS2_LOCAL_ALLOC_FL))
@@ -856,7 +861,7 @@
 		goto bail;
 	}
 
-	out = open_pager ();
+	out = open_pager(gbls.interactive);
 	read_journal (out, logbuf, (uint64_t)len);
 	close_pager (out);
 
@@ -884,7 +889,7 @@
 		return ;
 
 	buf = gbls.blockbuf;
-	out = open_pager();
+	out = open_pager(gbls.interactive);
 	while (blkno) {
 		ret = ocfs2_read_group_desc(gbls.fs, blkno, buf);
 		if (ret) {
@@ -928,7 +933,7 @@
 
 	eb = (ocfs2_extent_block *)buf;
 
-	out = open_pager();
+	out = open_pager(gbls.interactive);
 	dump_extent_block(out, eb);
 	dump_extent_list(out, &eb->h_list);
 	close_pager(out);
@@ -957,7 +962,7 @@
 		goto bail;
 	}
 
-	out = open_pager ();
+	out = open_pager(gbls.interactive);
 	dump_slots (out, buf, len);
 	close_pager (out);
 

Modified: trunk/debugfs.ocfs2/include/main.h
===================================================================
--- trunk/debugfs.ocfs2/include/main.h	2004-12-22 22:08:13 UTC (rev 507)
+++ trunk/debugfs.ocfs2/include/main.h	2004-12-23 01:45:54 UTC (rev 508)
@@ -48,7 +48,7 @@
 #include <linux/kdev_t.h>
 #include <inttypes.h>
 #include <utime.h>
-
+#include <getopt.h>
 #include <glib.h>
 
 #include <readline/readline.h>
@@ -68,6 +68,8 @@
 
 typedef struct _dbgfs_glbs {
 	char *progname;
+	int allow_write;
+	int interactive;
 	char *device;
 	ocfs2_filesys *fs;
 	char *cwd;
@@ -82,6 +84,12 @@
 	uint64_t jrnl_blkno[256];
 } dbgfs_gbls;
 
+typedef struct _dbgfs_opts {
+	int allow_write;
+	char *cmd_file;
+	char *device;
+} dbgfs_opts;
+
 #define DBGFS_FATAL(fmt, arg...)	({ fprintf(stderr, "ERROR at %s, %d: " fmt ".  EXITING!!!\n", \
 						   __FILE__, __LINE__, ##arg);  \
 					   raise (SIGTERM);	\

Modified: trunk/debugfs.ocfs2/include/utils.h
===================================================================
--- trunk/debugfs.ocfs2/include/utils.h	2004-12-22 22:08:13 UTC (rev 507)
+++ trunk/debugfs.ocfs2/include/utils.h	2004-12-23 01:45:54 UTC (rev 508)
@@ -37,7 +37,7 @@
 void get_publish_flag (__u32 flag, GString *str);
 void get_journal_blktyp (__u32 jtype, GString *str);
 void get_tag_flag (__u32 flags, GString *str);
-FILE *open_pager(void);
+FILE *open_pager(int interactive);
 void close_pager(FILE *stream);
 int inodestr_to_inode(char *str, uint64_t *blkno);
 errcode_t string_to_inode(ocfs2_filesys *fs, uint64_t root_blkno,

Modified: trunk/debugfs.ocfs2/main.c
===================================================================
--- trunk/debugfs.ocfs2/main.c	2004-12-22 22:08:13 UTC (rev 507)
+++ trunk/debugfs.ocfs2/main.c	2004-12-23 01:45:54 UTC (rev 508)
@@ -27,11 +27,6 @@
 
 #define PROMPT "debugfs: "
 
-static void  usage         (char *progname);
-static void  print_version (char *progname);
-static char *get_line      (void);
-
-gboolean allow_write = FALSE;
 extern dbgfs_gbls gbls;
 
 /*
@@ -42,9 +37,10 @@
 {
 	g_print ("Usage: %s [OPTION]... [DEVICE]\n", progname);
 	g_print ("Options:\n");
-	g_print ("  -V, --version  g_print version information and exit\n");
-	g_print ("  -?, --help     display this help and exit\n");
-	g_print ("  -w, --write    turn on write support\n");
+	g_print ("  -f, --file <cmd_file>	Execute commands in cmd_file\n");
+	g_print ("  -V, --version		Display version\n");
+	g_print ("  -?, --help			Display this help\n");
+	g_print ("  -w, --write			Enable writes\n");
 	exit (0);
 }					/* usage */
 
@@ -58,18 +54,89 @@
 }					/* print_version */
 
 /*
+ * get_options()
+ *
+ */
+static void get_options(int argc, char **argv, dbgfs_opts *opts)
+{
+	int c;
+	static struct option long_options[] = {
+		{ "file", 1, 0, 'f' },
+		{ "version", 0, 0, 'V' },
+		{ "help", 0, 0, '?' },
+		{ "write", 0, 0, '?' },
+		{ 0, 0, 0, 0}
+	};
+
+	while (1) {
+		c = getopt_long(argc, argv, "f:V?w", long_options, NULL);
+		if (c == -1)
+			break;
+
+		switch (c) {
+		case 'f':
+			opts->cmd_file = strdup(optarg);
+			if (!strlen(opts->cmd_file)) {
+				usage(gbls.progname);
+				exit(1);
+			}
+			break;
+
+		case 'w':
+			opts->allow_write = 1;
+			break;
+
+		case 'h':
+			usage(gbls.progname);
+			exit(0);
+			break;
+
+		case 'V':
+			print_version(gbls.progname);
+			exit(0);
+			break;
+
+		default:
+			usage(gbls.progname);
+			break;
+		}
+	}
+
+	if (optind < argc)
+		opts->device = strdup(argv[optind]);
+
+	return ;
+}
+
+/*
  * get_line()
  *
  */
-static char * get_line (void)
+static char * get_line (FILE *stream)
 {
 	char *line;
+	static char buf[1024];
+	int i;
 
-	line = readline (PROMPT);
+	if (stream) {
+		while (1) {
+			if (!fgets(buf, sizeof(buf), stream))
+				return NULL;
+			line = buf;
+			i = strlen(line);
+			if (i)
+				buf[i - 1] = '\0';
+			g_strchug(line);
+			if (strlen(line))
+				break;
+		}
+	} else {
+		line = readline (PROMPT);
 
-	if (line && *line) {
-		g_strchug(line);
-		add_history (line);
+		if (line && *line) {
+			g_strchug(line);
+			add_history (line);
+		}
 	}
 
 	return line;
@@ -81,11 +148,9 @@
  */
 int main (int argc, char **argv)
 {
-	int i;
 	char *line;
-	char *device = NULL;
-	char *arg;
-	gboolean seen_device = FALSE;
+	dbgfs_opts opts;
+	FILE *cmd = NULL;
 
 	initialize_ocfs_error_table();
 
@@ -100,49 +165,41 @@
 	INSTALL_SIGNAL(SIGTERM);
 	INSTALL_SIGNAL(SIGINT);
 
+	memset(&opts, 0, sizeof(opts));
 	memset(&gbls, 0, sizeof(gbls));
 
 	gbls.progname = basename(argv[0]);
 
-	for (i = 1; i < argc; i++) {
-		arg = argv[i];
-		if ((strcmp (arg, "--write") == 0) ||
-		    (strcmp (arg, "-w") == 0)) {
-			allow_write = TRUE;
-		} else if ((strcmp (arg, "--version") == 0) ||
-			   (strcmp (arg, "-V") == 0)) {
-			print_version (gbls.progname);
-			exit (0);
-		} else if ((strcmp (arg, "--help") == 0) ||
-			   (strcmp (arg, "-?") == 0)) {
-			usage (argv[0]);
-			exit (0);
-		} else if (!seen_device) {
-			device = g_strdup (arg);
-			seen_device = TRUE;
-		} else {
-			usage (argv[0]);
-			exit (1);
-		}
-	}
+	get_options(argc, argv, &opts);
+	gbls.allow_write = opts.allow_write;
+	if (!opts.cmd_file)
+		gbls.interactive++;
 
 	print_version (gbls.progname);
 
-	if (device) {
-		line = g_strdup_printf ("open %s", device);
+	if (opts.device) {
+		line = g_strdup_printf ("open %s", opts.device);
 		do_command (line);
 		g_free (line);
 	}
 
+	if (opts.cmd_file) {
+		cmd = fopen(opts.cmd_file, "r");
+		if (!cmd) {
+			com_err(argv[0], errno, "'%s'", opts.cmd_file);
+			goto bail;
+		}
+	}
+
 	while (1) {
-		line = get_line ();
+		line = get_line(cmd);
 
 		if (line) {
-			if (!isatty (0))
-				printf ("%s\n", line);
-
+			if (!gbls.interactive)
+				fprintf (stdout, "%s%s\n", PROMPT, line);
 			do_command (line);
-			free (line);
+			if (gbls.interactive)
+				free (line);
 		} else {
 			printf ("\n");
 			raise (SIGTERM);
@@ -151,5 +208,11 @@
 	}
 
 bail:
+	if (cmd)
+		fclose(cmd);
+	if (opts.cmd_file)
+		free(opts.cmd_file);
+	if (opts.device)
+		free(opts.device);
 	return 0;
 }					/* main */

Modified: trunk/debugfs.ocfs2/utils.c
===================================================================
--- trunk/debugfs.ocfs2/utils.c	2004-12-22 22:08:13 UTC (rev 507)
+++ trunk/debugfs.ocfs2/utils.c	2004-12-23 01:45:54 UTC (rev 508)
@@ -207,20 +207,22 @@
  * redistributed under the terms of the GNU Public License.
  *
  */
-FILE *open_pager(void)
+FILE *open_pager(int interactive)
 {
-	FILE *outfile;
+	FILE *outfile = NULL;
 	const char *pager = getenv("PAGER");
 
-	signal(SIGPIPE, SIG_IGN);
-	if (pager) {
-		if (strcmp(pager, "__none__") == 0) {
-			return stdout;
-		}
-	} else
-		pager = "more";
+	if (interactive) {
+		signal(SIGPIPE, SIG_IGN);
+		if (pager) {
+			if (strcmp(pager, "__none__") == 0) {
+				return stdout;
+			}
+		} else
+			pager = "more";
 
-	outfile = popen(pager, "w");
+		outfile = popen(pager, "w");
+	}
 
 	return (outfile ? outfile : stdout);
 }



More information about the Ocfs2-tools-commits mailing list