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>
40 #include <sys/module.h>
41 #include <sys/linker.h>
42 #include <sys/iconv.h>
44 #include <vfs/ntfs/ntfsmount.h>
58 static struct mntopt mopts
[] = {
63 static gid_t
a_gid(char *);
64 static uid_t
a_uid(char *);
65 static mode_t
a_mask(char *);
66 static void usage(void) __dead2
;
69 set_charset(struct ntfs_args
*pargs
, const char *cs_local
);
72 main(int argc
, char **argv
)
74 struct ntfs_args args
;
76 int c
, mntflags
, set_gid
, set_uid
, set_mask
, error
;
77 char *dev
, *dir
, mntpath
[MAXPATHLEN
];
78 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
84 char *cs_local
= NULL
;
86 mntflags
= set_gid
= set_uid
= set_mask
= 0;
87 memset(&args
, '\0', sizeof(args
));
89 while ((c
= getopt(argc
, argv
, "aiu:g:m:o:C:")) != -1) {
92 args
.uid
= a_uid(optarg
);
96 args
.gid
= a_gid(optarg
);
100 args
.mode
= a_mask(optarg
);
104 args
.flag
|= NTFS_MFLAG_CASEINS
;
107 args
.flag
|= NTFS_MFLAG_ALLNAMES
;
110 getmntopts(optarg
, mopts
, &mntflags
, 0);
113 quirk
= kiconv_quirkcs(optarg
, KICONV_VENDOR_MICSFT
);
114 cs_local
= strdup(quirk
);
115 if (set_charset(&args
, cs_local
) == -1)
116 err(EX_OSERR
, "ntfs_iconv");
117 args
.flag
|= NTFS_MFLAG_KICONV
;
118 mntflags
|= MNT_RDONLY
;
127 if (optind
+ 2 != argc
)
131 dir
= argv
[optind
+ 1];
134 * Resolve the mountpoint with realpath(3) and remove unnecessary
135 * slashes from the devicename if there are any.
137 checkpath(dir
, mntpath
);
141 args
.export
.ex_root
= 65534; /* unchecked anyway on DOS fs */
142 if (mntflags
& MNT_RDONLY
)
143 args
.export
.ex_flags
= MNT_EXRDONLY
;
145 args
.export
.ex_flags
= 0;
146 if (!set_gid
|| !set_uid
|| !set_mask
) {
147 if (stat(mntpath
, &sb
) == -1)
148 err(EX_OSERR
, "stat %s", mntpath
);
151 args
.uid
= sb
.st_uid
;
153 args
.gid
= sb
.st_gid
;
155 args
.mode
= sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
158 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
159 error
= getvfsbyname("ntfs", &vfc
);
160 if(error
&& vfsisloadable("ntfs")) {
163 vfc
= getvfsbyname("ntfs");
164 if(!vfc
&& vfsisloadable("ntfs")) {
167 err(EX_OSERR
, "vfsload(ntfs)");
168 endvfsent(); /* clear cache */
169 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
170 error
= getvfsbyname("ntfs", &vfc
);
172 vfc
= getvfsbyname("ntfs");
175 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
180 errx(EX_OSERR
, "ntfs filesystem is not available");
182 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
183 if (mount(vfc
.vfc_name
, mntpath
, mntflags
, &args
) < 0)
185 if (mount(vfc
->vfc_index
, mntpath
, mntflags
, &args
) < 0)
187 err(EX_OSERR
, "%s", dev
);
199 if ((gr
= getgrnam(s
)) != NULL
)
202 for (gname
= s
; *s
&& isdigit(*s
); ++s
);
206 errx(EX_NOUSER
, "unknown group id: %s", gname
);
218 if ((pw
= getpwnam(s
)) != NULL
)
221 for (uname
= s
; *s
&& isdigit(*s
); ++s
);
225 errx(EX_NOUSER
, "unknown user id: %s", uname
);
237 if (*s
>= '0' && *s
<= '7') {
239 rv
= strtol(optarg
, &ep
, 8);
241 if (!done
|| rv
< 0 || *ep
)
242 errx(EX_USAGE
, "invalid file mode: %s", s
);
249 fprintf(stderr
, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] bdev dir\n");
254 set_charset(struct ntfs_args
*pargs
, const char *cs_local
)
258 if (modfind("ntfs_iconv") < 0)
259 if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) {
260 warnx( "cannot find or load \"ntfs_iconv\" kernel module");
263 strncpy(pargs
->cs_ntfs
, ENCODING_UNICODE
, ICONV_CSNMAXLEN
);
264 strncpy(pargs
->cs_local
, cs_local
, ICONV_CSNMAXLEN
);
265 error
= kiconv_add_xlat16_cspairs(pargs
->cs_ntfs
, cs_local
);