fdisk - Use heads = 255 on file images
[dragonfly.git] / sbin / mount_ntfs / mount_ntfs.c
blob2d4ac9f8b0df89606fc5be0f85e017ce798ad251
1 /*
2 * Copyright (c) 1994 Christopher G. Demetriou
3 * Copyright (c) 1999 Semen Ustimenko
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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>
38 #define NTFS
39 #include <sys/mount.h>
40 #include <sys/module.h>
41 #include <sys/linker.h>
42 #include <sys/iconv.h>
43 #include <sys/stat.h>
44 #include <vfs/ntfs/ntfsmount.h>
45 #include <ctype.h>
46 #include <err.h>
47 #include <grp.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sysexits.h>
53 #include <unistd.h>
54 #include <libutil.h>
56 #include "mntopts.h"
58 static struct mntopt mopts[] = {
59 MOPT_STDOPTS,
60 MOPT_NULL
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;
68 int
69 set_charset(struct ntfs_args *pargs, const char *cs_local);
71 int
72 main(int argc, char **argv)
74 struct ntfs_args args;
75 struct stat sb;
76 int c, mntflags, set_gid, set_uid, set_mask, error;
77 char *dev, *dir, mntpath[MAXPATHLEN];
78 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
79 struct vfsconf vfc;
80 #else
81 struct vfsconf *vfc;
82 #endif
83 const char *quirk;
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) {
90 switch (c) {
91 case 'u':
92 args.uid = a_uid(optarg);
93 set_uid = 1;
94 break;
95 case 'g':
96 args.gid = a_gid(optarg);
97 set_gid = 1;
98 break;
99 case 'm':
100 args.mode = a_mask(optarg);
101 set_mask = 1;
102 break;
103 case 'i':
104 args.flag |= NTFS_MFLAG_CASEINS;
105 break;
106 case 'a':
107 args.flag |= NTFS_MFLAG_ALLNAMES;
108 break;
109 case 'o':
110 getmntopts(optarg, mopts, &mntflags, 0);
111 break;
112 case 'C':
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;
119 break;
120 case '?':
121 default:
122 usage();
123 break;
127 if (optind + 2 != argc)
128 usage();
130 dev = argv[optind];
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);
138 rmslashes(dev, dev);
140 args.fspec = dev;
141 args.export.ex_root = 65534; /* unchecked anyway on DOS fs */
142 if (mntflags & MNT_RDONLY)
143 args.export.ex_flags = MNT_EXRDONLY;
144 else
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);
150 if (!set_uid)
151 args.uid = sb.st_uid;
152 if (!set_gid)
153 args.gid = sb.st_gid;
154 if (!set_mask)
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")) {
161 if(vfsload("ntfs"))
162 #else
163 vfc = getvfsbyname("ntfs");
164 if(!vfc && vfsisloadable("ntfs")) {
165 if(vfsload("ntfs"))
166 #endif
167 err(EX_OSERR, "vfsload(ntfs)");
168 endvfsent(); /* clear cache */
169 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
170 error = getvfsbyname("ntfs", &vfc);
171 #else
172 vfc = getvfsbyname("ntfs");
173 #endif
175 #if __FreeBSD_version >= 300000 || defined(__DragonFly__)
176 if (error)
177 #else
178 if (!vfc)
179 #endif
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)
184 #else
185 if (mount(vfc->vfc_index, mntpath, mntflags, &args) < 0)
186 #endif
187 err(EX_OSERR, "%s", dev);
189 exit (0);
192 gid_t
193 a_gid(char *s)
195 struct group *gr;
196 char *gname;
197 gid_t gid;
199 if ((gr = getgrnam(s)) != NULL)
200 gid = gr->gr_gid;
201 else {
202 for (gname = s; *s && isdigit(*s); ++s);
203 if (!*s)
204 gid = atoi(gname);
205 else
206 errx(EX_NOUSER, "unknown group id: %s", gname);
208 return (gid);
211 uid_t
212 a_uid(char *s)
214 struct passwd *pw;
215 char *uname;
216 uid_t uid;
218 if ((pw = getpwnam(s)) != NULL)
219 uid = pw->pw_uid;
220 else {
221 for (uname = s; *s && isdigit(*s); ++s);
222 if (!*s)
223 uid = atoi(uname);
224 else
225 errx(EX_NOUSER, "unknown user id: %s", uname);
227 return (uid);
230 mode_t
231 a_mask(char *s)
233 int done, rv=0;
234 char *ep;
236 done = 0;
237 if (*s >= '0' && *s <= '7') {
238 done = 1;
239 rv = strtol(optarg, &ep, 8);
241 if (!done || rv < 0 || *ep)
242 errx(EX_USAGE, "invalid file mode: %s", s);
243 return (rv);
246 void
247 usage(void)
249 fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] [-C charset] bdev dir\n");
250 exit(EX_USAGE);
254 set_charset(struct ntfs_args *pargs, const char *cs_local)
256 int error;
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");
261 return (-1);
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);
266 if (error)
267 return (-1);
269 return (0);