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/cdefs.h>
36 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/module.h>
40 #include <sys/linker.h>
41 #include <sys/iconv.h>
43 #include <vfs/ntfs/ntfsmount.h>
56 static struct mntopt mopts[] = {
61 static gid_t a_gid(char *);
62 static uid_t a_uid(char *);
63 static mode_t a_mask(char *);
64 static void usage(void) __dead2;
67 set_charset(struct ntfs_args *pargs, const char *cs_local);
70 main(int argc, char **argv)
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 || defined(__DragonFly__)
82 char *cs_local = NULL;
84 mntflags = set_gid = set_uid = set_mask = 0;
85 memset(&args, '\0', sizeof(args));
87 while ((c = getopt(argc, argv, "aiu:g:m:o:C:")) != -1) {
90 args.uid = a_uid(optarg);
94 args.gid = a_gid(optarg);
98 args.mode = a_mask(optarg);
102 args.flag |= NTFS_MFLAG_CASEINS;
105 args.flag |= NTFS_MFLAG_ALLNAMES;
108 getmntopts(optarg, mopts, &mntflags, 0);
111 quirk = kiconv_quirkcs(optarg, KICONV_VENDOR_MICSFT);
112 cs_local = strdup(quirk);
113 if (set_charset(&args, cs_local) == -1)
114 err(EX_OSERR, "ntfs_iconv");
115 args.flag |= NTFS_MFLAG_KICONV;
116 mntflags |= MNT_RDONLY;
125 if (optind + 2 != argc)
129 dir = argv[optind + 1];
132 * Resolve the mountpoint with realpath(3) and remove unnecessary
133 * slashes from the devicename if there are any.
135 checkpath(dir, mntpath);
139 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
140 if (mntflags & MNT_RDONLY)
141 args.export.ex_flags = MNT_EXRDONLY;
143 args.export.ex_flags = 0;
144 if (!set_gid || !set_uid || !set_mask) {
145 if (stat(mntpath, &sb) == -1)
146 err(EX_OSERR, "stat %s", mntpath);
149 args.uid = sb.st_uid;
151 args.gid = sb.st_gid;
153 args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
156 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
157 error = getvfsbyname("ntfs", &vfc);
158 if(error && vfsisloadable("ntfs")) {
161 vfc = getvfsbyname("ntfs");
162 if(!vfc && vfsisloadable("ntfs")) {
165 err(EX_OSERR, "vfsload(ntfs)");
166 endvfsent(); /* clear cache */
167 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
168 error = getvfsbyname("ntfs", &vfc);
170 vfc = getvfsbyname("ntfs");
173 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
178 errx(EX_OSERR, "ntfs filesystem is not available");
180 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
181 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
183 if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
185 err(EX_OSERR, "%s", dev);
197 if ((gr = getgrnam(s)) != NULL)
200 for (gname = s; *s && isdigit(*s); ++s);
204 errx(EX_NOUSER, "unknown group id: %s", gname);
216 if ((pw = getpwnam(s)) != NULL)
219 for (uname = s; *s && isdigit(*s); ++s);
223 errx(EX_NOUSER, "unknown user id: %s", uname);
235 if (*s >= '0' && *s <= '7') {
237 rv = strtol(optarg, &ep, 8);
239 if (!done || rv < 0 || *ep)
240 errx(EX_USAGE, "invalid file mode: %s", s);
247 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] bdev dir\n");
252 set_charset(struct ntfs_args *pargs, const char *cs_local)
256 if (modfind("ntfs_iconv") < 0)
257 if (kldload("ntfs_iconv") < 0 || modfind("ntfs_iconv") < 0) {
258 warnx( "cannot find or load \"ntfs_iconv\" kernel module");
261 strncpy(pargs->cs_ntfs, ENCODING_UNICODE, ICONV_CSNMAXLEN);
262 strncpy(pargs->cs_local, cs_local, ICONV_CSNMAXLEN);
263 error = kiconv_add_xlat16_cspairs(pargs->cs_ntfs, cs_local);