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 $
35 #include <sys/param.h>
37 #include <sys/mount.h>
38 #include <sys/module.h>
39 #include <sys/linker.h>
40 #include <sys/iconv.h>
42 #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
;
66 set_charset(struct ntfs_args
*pargs
, const char *cs_local
);
69 main(int argc
, char **argv
)
71 struct ntfs_args args
;
73 int c
, mntflags
, set_gid
, set_uid
, set_mask
, error
;
74 char *dev
, *dir
, mntpath
[MAXPATHLEN
];
77 char *cs_local
= NULL
;
79 mntflags
= set_gid
= set_uid
= set_mask
= 0;
80 memset(&args
, '\0', sizeof(args
));
82 while ((c
= getopt(argc
, argv
, "aiu:g:m:o:C:")) != -1) {
85 args
.uid
= a_uid(optarg
);
89 args
.gid
= a_gid(optarg
);
93 args
.mode
= a_mask(optarg
);
97 args
.flag
|= NTFS_MFLAG_CASEINS
;
100 args
.flag
|= NTFS_MFLAG_ALLNAMES
;
103 getmntopts(optarg
, mopts
, &mntflags
, 0);
106 quirk
= kiconv_quirkcs(optarg
, KICONV_VENDOR_MICSFT
);
107 cs_local
= strdup(quirk
);
108 if (set_charset(&args
, cs_local
) == -1)
109 err(EX_OSERR
, "ntfs_iconv");
110 args
.flag
|= NTFS_MFLAG_KICONV
;
111 mntflags
|= MNT_RDONLY
;
120 if (optind
+ 2 != argc
)
124 dir
= argv
[optind
+ 1];
127 * Resolve the mountpoint with realpath(3) and remove unnecessary
128 * slashes from the devicename if there are any.
130 checkpath(dir
, mntpath
);
134 args
.export
.ex_root
= 65534; /* unchecked anyway on DOS fs */
135 if (mntflags
& MNT_RDONLY
)
136 args
.export
.ex_flags
= MNT_EXRDONLY
;
138 args
.export
.ex_flags
= 0;
139 if (!set_gid
|| !set_uid
|| !set_mask
) {
140 if (stat(mntpath
, &sb
) == -1)
141 err(EX_OSERR
, "stat %s", mntpath
);
144 args
.uid
= sb
.st_uid
;
146 args
.gid
= sb
.st_gid
;
148 args
.mode
= sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
);
151 error
= getvfsbyname("ntfs", &vfc
);
152 if(error
&& vfsisloadable("ntfs")) {
154 err(EX_OSERR
, "vfsload(ntfs)");
155 endvfsent(); /* clear cache */
156 error
= getvfsbyname("ntfs", &vfc
);
159 errx(EX_OSERR
, "ntfs filesystem is not available");
161 if (mount(vfc
.vfc_name
, mntpath
, mntflags
, &args
) < 0)
162 err(EX_OSERR
, "%s", dev
);
174 if ((gr
= getgrnam(s
)) != NULL
)
177 for (gname
= s
; *s
&& isdigit(*s
); ++s
);
181 errx(EX_NOUSER
, "unknown group id: %s", gname
);
193 if ((pw
= getpwnam(s
)) != NULL
)
196 for (uname
= s
; *s
&& isdigit(*s
); ++s
);
200 errx(EX_NOUSER
, "unknown user id: %s", uname
);
212 if (*s
>= '0' && *s
<= '7') {
214 rv
= strtol(optarg
, &ep
, 8);
216 if (!done
|| rv
< 0 || *ep
)
217 errx(EX_USAGE
, "invalid file mode: %s", s
);
224 fprintf(stderr
, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] bdev dir\n");
229 set_charset(struct ntfs_args
*pargs
, const char *cs_local
)
233 if (modfind("ntfs_iconv") < 0)
234 if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) {
235 warnx( "cannot find or load \"ntfs_iconv\" kernel module");
238 strncpy(pargs
->cs_ntfs
, ENCODING_UNICODE
, ICONV_CSNMAXLEN
);
239 strncpy(pargs
->cs_local
, cs_local
, ICONV_CSNMAXLEN
);
240 error
= kiconv_add_xlat16_cspairs(pargs
->cs_ntfs
, cs_local
);