From 4090a07081122e2027c6aa84ec8235f5c0c54cd8 Mon Sep 17 00:00:00 2001 From: szaka Date: Sat, 15 Dec 2007 09:27:00 +0000 Subject: [PATCH] add ntfs-3g.probe utility which probes read-only or read-write mountability --- src/Makefile.am | 57 ++++++++------- src/ntfs-3g.probe.c | 166 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 194 insertions(+), 29 deletions(-) create mode 100644 src/ntfs-3g.probe.c diff --git a/src/Makefile.am b/src/Makefile.am index 5bc02646..fc74bbdf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,38 +1,37 @@ -MAINTAINERCLEANFILES=\ - Makefile.in +MAINTAINERCLEANFILES = Makefile.in -rootbin_PROGRAMS=\ - ntfs-3g -rootsbin_DATA=#Create directory -man_MANS=\ - ntfs-3g.8 - -ntfs_3g_LDADD =\ - $(top_builddir)/libntfs-3g/libntfs-3g.la -if REALLYSTATIC -ntfs_3g_LDFLAGS=\ - $(AM_LDFLAGS) \ - -all-static -endif -ntfs_3g_CFLAGS=\ - $(AM_CFLAGS) \ - -DFUSE_USE_VERSION=26 \ - -I$(top_srcdir)/include/ntfs-3g -ntfs_3g_SOURCES=\ - ntfs-3g.c \ - utils.c \ - utils.h if FUSE_INTERNAL -ntfs_3g_CFLAGS+=\ - -I$(top_srcdir)/include/fuse-lite +FUSE_CFLAGS = -I$(top_srcdir)/include/fuse-lite +FUSE_LIBS = else -ntfs_3g_CFLAGS+=\ - $(FUSE_MODULE_CFLAGS) -ntfs_3g_LDADD+=\ - $(FUSE_MODULE_LIBS) +FUSE_CFLAGS = $(FUSE_MODULE_CFLAGS) +FUSE_LIBS = $(FUSE_MODULE_LIBS) endif +bin_PROGRAMS = ntfs-3g.probe +rootbin_PROGRAMS = ntfs-3g +rootsbin_DATA = #Create directory +man_MANS = ntfs-3g.8 + +ntfs_3g_LDADD = $(FUSE_LIBS) $(top_builddir)/libntfs-3g/libntfs-3g.la +if REALLYSTATIC +ntfs_3g_LDFLAGS = $(AM_LDFLAGS) -all-static +endif +ntfs_3g_CFLAGS = \ + $(AM_CFLAGS) \ + -DFUSE_USE_VERSION=26 \ + $(FUSE_CFLAGS) \ + -I$(top_srcdir)/include/ntfs-3g +ntfs_3g_SOURCES = ntfs-3g.c utils.c utils.h + +ntfs_3g_probe_LDADD = $(top_builddir)/libntfs-3g/libntfs-3g.la +if REALLYSTATIC +ntfs_3g_probe_LDFLAGS = $(AM_LDFLAGS) -all-static +endif +ntfs_3g_probe_CFLAGS = $(AM_CFLAGS) -I$(top_srcdir)/include/ntfs-3g +ntfs_3g_probe_SOURCES = ntfs-3g.probe.c utils.c utils.h + if RUN_LDCONFIG install-exec-hook: $(LDCONFIG) diff --git a/src/ntfs-3g.probe.c b/src/ntfs-3g.probe.c new file mode 100644 index 00000000..a4088038 --- /dev/null +++ b/src/ntfs-3g.probe.c @@ -0,0 +1,166 @@ +/** + * ntfs-3g.probe - Probe NTFS volume mountability + * + * Copyright (c) 2007 Szabolcs Szakacsits + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program (in the main directory of the NTFS-3G + * distribution in the file COPYING); if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_ERRNO_H +#include +#endif +#include + +#include "volume.h" +#include "utils.h" +#include "misc.h" + +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +typedef enum { + PROBE_UNSET, + PROBE_READONLY, + PROBE_READWRITE +} probe_t; + +static struct options { + probe_t probetype; + char *device; +} opts; + +static const char *EXEC_NAME = "ntfs-3g.probe"; + +static const char *usage_msg = +"\n" +"%s %s - Probe NTFS volume mountability\n" +"\n" +"Copyright (C) 2007 Szabolcs Szakacsits\n" +"\n" +"Usage: %s <--readonly|--readwrite> \n" +"\n" +"Example: ntfs-3g.probe --readwrite /dev/sda1\n" +"\n" +"%s"; + +static int ntfs_open(const char *device) +{ + ntfs_volume *vol; + unsigned long flags = 0; + int ret = NTFS_VOLUME_OK; + + if (opts.probetype == PROBE_READONLY) + flags |= MS_RDONLY; + + vol = ntfs_mount(device, flags); + if (!vol) + ret = ntfs_volume_error(errno); + + ntfs_umount(vol, FALSE); + + return ret; +} + +static void usage(void) +{ + ntfs_log_info(usage_msg, EXEC_NAME, VERSION, EXEC_NAME, ntfs_home); +} + +static int parse_options(int argc, char *argv[]) +{ + int c; + + static const char *sopt = "-hrw"; + static const struct option lopt[] = { + { "readonly", no_argument, NULL, 'r' }, + { "readwrite", no_argument, NULL, 'w' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } + }; + + opterr = 0; /* We handle errors. */ + opts.probetype = PROBE_UNSET; + + while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { + switch (c) { + case 1: /* A non-option argument */ + if (!opts.device) { + opts.device = ntfs_malloc(PATH_MAX + 1); + if (!opts.device) + return -1; + + strcpy(opts.device, optarg); + } else { + ntfs_log_error("%s: You must specify exactly " + "one device\n", EXEC_NAME); + return -1; + } + break; + case 'h': + usage(); + exit(0); + case 'r': + opts.probetype = PROBE_READONLY; + break; + case 'w': + opts.probetype = PROBE_READWRITE; + break; + default: + ntfs_log_error("%s: Unknown option '%s'.\n", EXEC_NAME, + argv[optind - 1]); + return -1; + } + } + + if (!opts.device) { + ntfs_log_error("ERROR: %s: Device is missing\n", EXEC_NAME); + return -1; + } + + if (opts.probetype == PROBE_UNSET) { + ntfs_log_error("ERROR: %s: Probe type is missing\n", EXEC_NAME); + return -1; + } + + return 0; +} + +int main(int argc, char *argv[]) +{ + int err; + + ntfs_log_set_handler(ntfs_log_handler_stderr); + + if (parse_options(argc, argv)) { + usage(); + exit(NTFS_VOLUME_SYNTAX_ERROR); + } + + err = ntfs_open(opts.device); + + free(opts.device); + exit(err); +} +