• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * ntfs-3g.probe - Probe NTFS volume mountability
3  *
4  * Copyright (c) 2007-2009 Szabolcs Szakacsits
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the NTFS-3G
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21 
22 #include "config.h"
23 
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30 #ifdef HAVE_ERRNO_H
31 #include <errno.h>
32 #endif
33 #include <getopt.h>
34 
35 #include "compat.h"
36 #include "volume.h"
37 #include "misc.h"
38 
39 typedef enum {
40 	PROBE_UNSET,
41 	PROBE_READONLY,
42 	PROBE_READWRITE
43 } probe_t;
44 
45 static struct options {
46 	probe_t  probetype;
47 	char	 *device;
48 } opts;
49 
50 static const char *EXEC_NAME = "ntfs-3g.probe";
51 
52 static const char *usage_msg =
53 "\n"
54 "%s %s - Probe NTFS volume mountability\n"
55 "\n"
56 "Copyright (C) 2007 Szabolcs Szakacsits\n"
57 "\n"
58 "Usage:    %s <--readonly|--readwrite> <device|image_file>\n"
59 "\n"
60 "Example:  ntfs-3g.probe --readwrite /dev/sda1\n"
61 "\n"
62 "%s";
63 
ntfs_open(const char * device)64 static int ntfs_open(const char *device)
65 {
66 	ntfs_volume *vol;
67 	unsigned long flags = 0;
68 	int ret = NTFS_VOLUME_OK;
69 
70 	if (opts.probetype == PROBE_READONLY)
71 		flags |= NTFS_MNT_RDONLY;
72 
73 	vol = ntfs_mount(device, flags);
74 	if (!vol)
75 		ret = ntfs_volume_error(errno);
76 
77 	if (ret == 0 && ntfs_umount(vol, FALSE) == -1)
78 		ret = ntfs_volume_error(errno);
79 
80 	return ret;
81 }
82 
usage(void)83 static void usage(void)
84 {
85 	ntfs_log_info(usage_msg, EXEC_NAME, VERSION, EXEC_NAME, ntfs_home);
86 }
87 
parse_options(int argc,char * argv[])88 static int parse_options(int argc, char *argv[])
89 {
90 	int c;
91 
92 	static const char *sopt = "-hrw";
93 	static const struct option lopt[] = {
94 		{ "readonly",	no_argument,	NULL, 'r' },
95 		{ "readwrite",	no_argument,	NULL, 'w' },
96 		{ "help",	no_argument,	NULL, 'h' },
97 		{ NULL,		0,		NULL,  0  }
98 	};
99 
100 	opterr = 0; /* We handle errors. */
101 	opts.probetype = PROBE_UNSET;
102 
103 	while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) {
104 		switch (c) {
105 		case 1:	/* A non-option argument */
106 			if (!opts.device) {
107 				opts.device = ntfs_malloc(PATH_MAX + 1);
108 				if (!opts.device)
109 					return -1;
110 
111 				strncpy(opts.device, optarg, PATH_MAX);
112 				opts.device[PATH_MAX] = 0;
113 			} else {
114 				ntfs_log_error("%s: You must specify exactly "
115 					       "one device\n", EXEC_NAME);
116 				return -1;
117 			}
118 			break;
119 		case 'h':
120 			usage();
121 			exit(0);
122 		case 'r':
123 			opts.probetype = PROBE_READONLY;
124 			break;
125 		case 'w':
126 			opts.probetype = PROBE_READWRITE;
127 			break;
128 		default:
129 			ntfs_log_error("%s: Unknown option '%s'.\n", EXEC_NAME,
130 				       argv[optind - 1]);
131 			return -1;
132 		}
133 	}
134 
135 	if (!opts.device) {
136 		ntfs_log_error("ERROR: %s: Device is missing\n", EXEC_NAME);
137 		return -1;
138 	}
139 
140 	if (opts.probetype == PROBE_UNSET) {
141 		ntfs_log_error("ERROR: %s: Probe type is missing\n", EXEC_NAME);
142 		return -1;
143 	}
144 
145 	return 0;
146 }
147 
main(int argc,char * argv[])148 int main(int argc, char *argv[])
149 {
150 	int err;
151 
152 	ntfs_log_set_handler(ntfs_log_handler_stderr);
153 
154 	if (parse_options(argc, argv)) {
155 		usage();
156 		exit(NTFS_VOLUME_SYNTAX_ERROR);
157 	}
158 
159 	err = ntfs_open(opts.device);
160 
161 	free(opts.device);
162 	if (err)
163 		exit(err);
164 	return (0);
165 }
166 
167