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.9 2008/11/02 21:52:46 swildner 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(char *);
61 static uid_t
a_uid(char *);
62 static mode_t
a_mask(char *);
63 static void usage(void) __dead2
;
65 static void load_u2wtable(struct ntfs_args
*, char *);
68 main(int argc
, char **argv
)
70 struct ntfs_args args
;
72 int c
, mntflags
, set_gid
, set_uid
, set_mask
, error
;
73 char *dev
, *dir
, mntpath
[MAXPATHLEN
];
74 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
80 mntflags
= set_gid
= set_uid
= set_mask
= 0;
81 memset(&args
, '\0', sizeof(args
));
83 while ((c
= getopt(argc
, argv
, "aiu:g:m:o:W:")) != -1) {
86 args
.uid
= a_uid(optarg
);
90 args
.gid
= a_gid(optarg
);
94 args
.mode
= a_mask(optarg
);
98 args
.flag
|= NTFS_MFLAG_CASEINS
;
101 args
.flag
|= NTFS_MFLAG_ALLNAMES
;
104 getmntopts(optarg
, mopts
, &mntflags
, 0);
107 load_u2wtable(&args
, optarg
);
108 args
.flag
|= NTFSMNT_U2WTABLE
;
117 if (optind
+ 2 != argc
)
121 dir
= argv
[optind
+ 1];
124 * Resolve the mountpoint with realpath(3) and remove unnecessary
125 * slashes from the devicename if there are any.
127 checkpath(dir
, mntpath
);
131 args
.export
.ex_root
= 65534; /* unchecked anyway on DOS fs */
132 if (mntflags
& MNT_RDONLY
)
133 args
.export
.ex_flags
= MNT_EXRDONLY
;
135 args
.export
.ex_flags
= 0;
136 if (!set_gid
|| !set_uid
|| !set_mask
) {
137 if (stat(mntpath
, &sb
) == -1)
138 err(EX_OSERR
, "stat %s", mntpath
);
141 args
.uid
= sb
.st_uid
;
143 args
.gid
= sb
.st_gid
;
145 args
.mode
= sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
148 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
149 error
= getvfsbyname("ntfs", &vfc
);
150 if(error
&& vfsisloadable("ntfs")) {
153 vfc
= getvfsbyname("ntfs");
154 if(!vfc
&& vfsisloadable("ntfs")) {
157 err(EX_OSERR
, "vfsload(ntfs)");
158 endvfsent(); /* clear cache */
159 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
160 error
= getvfsbyname("ntfs", &vfc
);
162 vfc
= getvfsbyname("ntfs");
165 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
170 errx(EX_OSERR
, "ntfs filesystem is not available");
172 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
173 if (mount(vfc
.vfc_name
, mntpath
, mntflags
, &args
) < 0)
175 if (mount(vfc
->vfc_index
, mntpath
, mntflags
, &args
) < 0)
177 err(EX_OSERR
, "%s", dev
);
189 if ((gr
= getgrnam(s
)) != NULL
)
192 for (gname
= s
; *s
&& isdigit(*s
); ++s
);
196 errx(EX_NOUSER
, "unknown group id: %s", gname
);
208 if ((pw
= getpwnam(s
)) != NULL
)
211 for (uname
= s
; *s
&& isdigit(*s
); ++s
);
215 errx(EX_NOUSER
, "unknown user id: %s", uname
);
227 if (*s
>= '0' && *s
<= '7') {
229 rv
= strtol(optarg
, &ep
, 8);
231 if (!done
|| rv
< 0 || *ep
)
232 errx(EX_USAGE
, "invalid file mode: %s", s
);
239 fprintf(stderr
, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-W u2wtable] bdev dir\n");
244 load_u2wtable (struct ntfs_args
*pargs
, char *name
)
255 snprintf(buf
, sizeof(buf
), "/usr/libdata/msdosfs/%s", name
);
259 if ((f
= fopen(fn
, "r")) == NULL
)
260 err(EX_NOINPUT
, "%s", fn
);
262 for (i
= 0; i
< 16; i
++) {
264 if (p
!= NULL
) free(p
);
265 if ((p
= s
= fparseln(f
, NULL
, &line
, NULL
, 0)) == NULL
)
266 errx(EX_DATAERR
, "can't read u2w table row %d near line %zu", i
, line
);
267 while (isspace((unsigned char)*s
))
269 } while (*s
== '\0');
270 if (sscanf(s
, "%i%i%i%i%i%i%i%i",
271 code
, code
+ 1, code
+ 2, code
+ 3, code
+ 4, code
+ 5, code
+ 6, code
+ 7) != 8)
272 errx(EX_DATAERR
, "u2w table: missing item(s) in row %d, line %zu", i
, line
);
273 for (j
= 0; j
< 8; j
++)
274 pargs
->u2w
[i
* 8 + j
] = code
[j
];
276 for (i
= 0; i
< 16; i
++) {
279 if ((p
= s
= fparseln(f
, NULL
, &line
, NULL
, 0)) == NULL
)
280 errx(EX_DATAERR
, "can't read d2u table row %d near line %zu", i
, line
);
281 while (isspace((unsigned char)*s
))
283 } while (*s
== '\0');
284 if (sscanf(s
, "%i%i%i%i%i%i%i%i",
285 code
, code
+ 1, code
+ 2, code
+ 3, code
+ 4, code
+ 5, code
+ 6, code
+ 7) != 8)
286 errx(EX_DATAERR
, "d2u table: missing item(s) in row %d, line %zu", i
, line
);
287 for (j
= 0; j
< 8; j
++)
288 /* pargs->d2u[i * 8 + j] = code[j] */;
290 for (i
= 0; i
< 16; i
++) {
293 if ((p
= s
= fparseln(f
, NULL
, &line
, NULL
, 0)) == NULL
)
294 errx(EX_DATAERR
, "can't read u2d table row %d near line %zu", i
, line
);
295 while (isspace((unsigned char)*s
))
297 } while (*s
== '\0');
298 if (sscanf(s
, "%i%i%i%i%i%i%i%i",
299 code
, code
+ 1, code
+ 2, code
+ 3, code
+ 4, code
+ 5, code
+ 6, code
+ 7) != 8)
300 errx(EX_DATAERR
, "u2d table: missing item(s) in row %d, line %zu", i
, line
);
301 for (j
= 0; j
< 8; j
++)
302 /* pargs->u2d[i * 8 + j] = code[j] */;