[PATCH] Make tux3fuse more user friendly

Tero Roponen tero.roponen at gmail.com
Sat Jan 5 06:06:52 PST 2013


This patch cleans up the command line parsing in
tux3fuse making it somewhat more user friendly.

$ ./tux3fuse
Usage: ./tux3fuse [options] <volume> <mount-point>

Options:
	-f	Stay in foreground
	-d	Stay in foreground, display FUSE messages

$ ./tux3fuse tux3.imgg mnt
Volume not found: tux3.imgg: No such file or directory

$ ./tux3fuse tux3.img mntt
fuse: bad mount point `mntt': No such file or directory

$ ./tux3fuse tux3.img mnt
Running in background
$

Signed-off-by: Tero Roponen <tero.roponen at gmail.com>
---
 user/tux3fuse.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 59 insertions(+), 4 deletions(-)

diff --git a/user/tux3fuse.c b/user/tux3fuse.c
index 10f1bcb..9eb7e30 100644
--- a/user/tux3fuse.c
+++ b/user/tux3fuse.c
@@ -973,17 +973,68 @@ static struct fuse_lowlevel_ops tux3_ops = {
 	/* .poll */
 };
 
+enum {
+	/* FUSE handles this option internally */
+	FUSE_OPT_KEY_INTERNAL,
+};
+
+static struct fuse_opt tux3fuse_options[] = {
+	FUSE_OPT_KEY("-d", FUSE_OPT_KEY_INTERNAL),
+	FUSE_OPT_KEY("-f", FUSE_OPT_KEY_INTERNAL),
+	FUSE_OPT_END
+};
+
+static int tux3fuse_parse_options(void *data, const char *arg,
+	int key, struct fuse_args *outargs)
+{
+	/*
+	 * We take the first two NONOPT options as
+	 * the volume name and the mount point.
+	 */
+	if (key == FUSE_OPT_KEY_NONOPT) {
+		char **volname = data;
+		if (!*volname) {
+			*volname = canonicalize_file_name(arg);
+			if (!*volname) {
+				printf("Volume not found: %s: %s\n",
+					arg, strerror(errno));
+				return -1;
+			}
+			return 0; /* We handled this option */
+		} else {
+			return 1; /* Pass mount point to FUSE */
+		}
+	} else if (key == FUSE_OPT_KEY_INTERNAL) {
+		return 1; /* Pass this option to FUSE */
+	}
+
+	printf("Unhandled option: %s\n", arg);
+	return -1;
+}
+
 int main(int argc, char *argv[])
 {
-	struct fuse_args args = FUSE_ARGS_INIT(argc - 1, argv + 1);
+	struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
 	struct fuse_chan *fc;
 	struct fuse_session *fs;
 	char *mountpoint;
 	int foreground;
 	int err = -1;
 
-	if (argc < 3)
-		error("usage: %s <volname> <mountpoint> [fuse opts]", argv[0]);
+	char *volname = NULL;
+
+	if (argc < 3) {
+		printf("Usage: %s [options] <volume> <mount-point>\n\n",
+			argv[0]);
+		printf("Options:\n");
+		printf("\t-f\tStay in foreground\n");
+		printf("\t-d\tStay in foreground, display FUSE messages\n");
+		return 1;
+	}
+
+	if (fuse_opt_parse(&args, &volname, tux3fuse_options,
+		tux3fuse_parse_options) == -1)
+		goto error;
 
 	if (fuse_parse_cmdline(&args, &mountpoint, NULL, &foreground) == -1)
 		goto error;
@@ -993,12 +1044,16 @@ int main(int argc, char *argv[])
 		goto error;
 
 	struct tux3fuse tux3fuse = {
-		.volname = canonicalize_file_name(argv[1]),
+		.volname = volname,
 	};
+
 	fs = fuse_lowlevel_new(&args, &tux3_ops, sizeof(tux3_ops), &tux3fuse);
 	if (fs) {
 		if (fuse_set_signal_handlers(fs) != -1) {
 			fuse_session_add_chan(fs, fc);
+
+			if (!foreground)
+				printf("Running in background\n");
 			fuse_daemonize(foreground);
 
 			err = fuse_session_loop(fs);
-- 
1.7.12.464.g83379df





More information about the Tux3 mailing list