blob: 592abd76522f5d16accad7418d664f794d6ff100
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 | |
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 |= MS_RDONLY; |
72 | |
73 | vol = ntfs_mount(device, flags); |
74 | if (!vol) |
75 | ret = ntfs_volume_error(errno); |
76 | |
77 | ntfs_umount(vol, FALSE); |
78 | |
79 | return ret; |
80 | } |
81 | |
82 | static void usage(void) |
83 | { |
84 | ntfs_log_info(usage_msg, EXEC_NAME, VERSION, EXEC_NAME, ntfs_home); |
85 | } |
86 | |
87 | static int parse_options(int argc, char *argv[]) |
88 | { |
89 | int c; |
90 | |
91 | static const char *sopt = "-hrw"; |
92 | static const struct option lopt[] = { |
93 | { "readonly", no_argument, NULL, 'r' }, |
94 | { "readwrite", no_argument, NULL, 'w' }, |
95 | { "help", no_argument, NULL, 'h' }, |
96 | { NULL, 0, NULL, 0 } |
97 | }; |
98 | |
99 | opterr = 0; /* We handle errors. */ |
100 | opts.probetype = PROBE_UNSET; |
101 | |
102 | while ((c = getopt_long(argc, argv, sopt, lopt, NULL)) != -1) { |
103 | switch (c) { |
104 | case 1: /* A non-option argument */ |
105 | if (!opts.device) { |
106 | opts.device = ntfs_malloc(PATH_MAX + 1); |
107 | if (!opts.device) |
108 | return -1; |
109 | |
110 | strncpy(opts.device, optarg, PATH_MAX); |
111 | opts.device[PATH_MAX] = 0; |
112 | } else { |
113 | ntfs_log_error("%s: You must specify exactly " |
114 | "one device\n", EXEC_NAME); |
115 | return -1; |
116 | } |
117 | break; |
118 | case 'h': |
119 | usage(); |
120 | exit(0); |
121 | case 'r': |
122 | opts.probetype = PROBE_READONLY; |
123 | break; |
124 | case 'w': |
125 | opts.probetype = PROBE_READWRITE; |
126 | break; |
127 | default: |
128 | ntfs_log_error("%s: Unknown option '%s'.\n", EXEC_NAME, |
129 | argv[optind - 1]); |
130 | return -1; |
131 | } |
132 | } |
133 | |
134 | if (!opts.device) { |
135 | ntfs_log_error("ERROR: %s: Device is missing\n", EXEC_NAME); |
136 | return -1; |
137 | } |
138 | |
139 | if (opts.probetype == PROBE_UNSET) { |
140 | ntfs_log_error("ERROR: %s: Probe type is missing\n", EXEC_NAME); |
141 | return -1; |
142 | } |
143 | |
144 | return 0; |
145 | } |
146 | |
147 | int main(int argc, char *argv[]) |
148 | { |
149 | int err; |
150 | |
151 | ntfs_log_set_handler(ntfs_log_handler_stderr); |
152 | |
153 | if (parse_options(argc, argv)) { |
154 | usage(); |
155 | exit(NTFS_VOLUME_SYNTAX_ERROR); |
156 | } |
157 | |
158 | err = ntfs_open(opts.device); |
159 | |
160 | free(opts.device); |
161 | exit(err); |
162 | } |
163 | |
164 |