2 * Copyright (c) 1994 Christopher G. Demetriou
3 * Copyright (c) 1999 Semen Ustimenko
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Christopher G. Demetriou.
17 * 4. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * $FreeBSD: src/sbin/mount_ntfs/mount_ntfs.c,v 1.3.2.2 2001/10/12 22:08:43 semenu Exp $
32 * $DragonFly: src/sbin/mount_ntfs/mount_ntfs.c,v 1.3 2003/08/08 04:18:40 dillon Exp $
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <vfs/ntfs/ntfsmount.h>
55 static struct mntopt mopts[] = {
60 static gid_t a_gid __P((char *));
61 static uid_t a_uid __P((char *));
62 static mode_t a_mask __P((char *));
63 static void usage __P((void)) __dead2;
65 static void load_u2wtable __P((struct ntfs_args *, char *));
72 struct ntfs_args args;
74 int c, mntflags, set_gid, set_uid, set_mask, error;
75 char *dev, *dir, mntpath[MAXPATHLEN];
76 #if __FreeBSD_version >= 300000
82 mntflags = set_gid = set_uid = set_mask = 0;
83 (void)memset(&args, '\0', sizeof(args));
85 while ((c = getopt(argc, argv, "aiu:g:m:o:W:")) != -1) {
88 args.uid = a_uid(optarg);
92 args.gid = a_gid(optarg);
96 args.mode = a_mask(optarg);
100 args.flag |= NTFS_MFLAG_CASEINS;
103 args.flag |= NTFS_MFLAG_ALLNAMES;
106 getmntopts(optarg, mopts, &mntflags, 0);
109 load_u2wtable(&args, optarg);
110 args.flag |= NTFSMNT_U2WTABLE;
119 if (optind + 2 != argc)
123 dir = argv[optind + 1];
126 * Resolve the mountpoint with realpath(3) and remove unnecessary
127 * slashes from the devicename if there are any.
129 (void)checkpath(dir, mntpath);
130 (void)rmslashes(dev, dev);
133 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
134 if (mntflags & MNT_RDONLY)
135 args.export.ex_flags = MNT_EXRDONLY;
137 args.export.ex_flags = 0;
138 if (!set_gid || !set_uid || !set_mask) {
139 if (stat(mntpath, &sb) == -1)
140 err(EX_OSERR, "stat %s", mntpath);
143 args.uid = sb.st_uid;
145 args.gid = sb.st_gid;
147 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
150 #if __FreeBSD_version >= 300000
151 error = getvfsbyname("ntfs", &vfc);
152 if(error && vfsisloadable("ntfs")) {
155 vfc = getvfsbyname("ntfs");
156 if(!vfc && vfsisloadable("ntfs")) {
159 err(EX_OSERR, "vfsload(ntfs)");
160 endvfsent(); /* clear cache */
161 #if __FreeBSD_version >= 300000
162 error = getvfsbyname("ntfs", &vfc);
164 vfc = getvfsbyname("ntfs");
167 #if __FreeBSD_version >= 300000
172 errx(EX_OSERR, "ntfs filesystem is not available");
174 #if __FreeBSD_version >= 300000
175 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
177 if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
179 err(EX_OSERR, "%s", dev);
192 if ((gr = getgrnam(s)) != NULL)
195 for (gname = s; *s && isdigit(*s); ++s);
199 errx(EX_NOUSER, "unknown group id: %s", gname);
212 if ((pw = getpwnam(s)) != NULL)
215 for (uname = s; *s && isdigit(*s); ++s);
219 errx(EX_NOUSER, "unknown user id: %s", uname);
232 if (*s >= '0' && *s <= '7') {
234 rv = strtol(optarg, &ep, 8);
236 if (!done || rv < 0 || *ep)
237 errx(EX_USAGE, "invalid file mode: %s", s);
244 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n");
249 load_u2wtable (pargs, name)
250 struct ntfs_args *pargs;
262 snprintf(buf, sizeof(buf), "/usr/libdata/msdosfs/%s", name);
266 if ((f = fopen(fn, "r")) == NULL)
267 err(EX_NOINPUT, "%s", fn);
269 for (i = 0; i < 16; i++) {
271 if (p != NULL) free(p);
272 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
273 errx(EX_DATAERR, "can't read u2w table row %d near line %d", i, line);
274 while (isspace((unsigned char)*s))
276 } while (*s == '\0');
277 if (sscanf(s, "%i%i%i%i%i%i%i%i",
278 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
279 errx(EX_DATAERR, "u2w table: missing item(s) in row %d, line %d", i, line);
280 for (j = 0; j < 8; j++)
281 pargs->u2w[i * 8 + j] = code[j];
283 for (i = 0; i < 16; i++) {
286 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
287 errx(EX_DATAERR, "can't read d2u table row %d near line %d", i, line);
288 while (isspace((unsigned char)*s))
290 } while (*s == '\0');
291 if (sscanf(s, "%i%i%i%i%i%i%i%i",
292 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
293 errx(EX_DATAERR, "d2u table: missing item(s) in row %d, line %d", i, line);
294 for (j = 0; j < 8; j++)
295 /* pargs->d2u[i * 8 + j] = code[j] */;
297 for (i = 0; i < 16; i++) {
300 if ((p = s = fparseln(f, NULL, &line, NULL, 0)) == NULL)
301 errx(EX_DATAERR, "can't read u2d table row %d near line %d", i, line);
302 while (isspace((unsigned char)*s))
304 } while (*s == '\0');
305 if (sscanf(s, "%i%i%i%i%i%i%i%i",
306 code, code + 1, code + 2, code + 3, code + 4, code + 5, code + 6, code + 7) != 8)
307 errx(EX_DATAERR, "u2d table: missing item(s) in row %d, line %d", i, line);
308 for (j = 0; j < 8; j++)
309 /* pargs->u2d[i * 8 + j] = code[j] */;