xref: /netbsd/sbin/mount_ntfs/mount_ntfs.c (revision bf9ec67e)
1 /* $NetBSD: mount_ntfs.c,v 1.5 2000/10/30 20:57:00 jdolecek Exp $ */
2 
3 /*
4  * Copyright (c) 1994 Christopher G. Demetriou
5  * Copyright (c) 1999 Semen Ustimenko
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by Christopher G. Demetriou.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Id: mount_ntfs.c,v 1.1.1.1 1999/02/03 03:51:19 semenu Exp
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __RCSID("$NetBSD: mount_ntfs.c,v 1.5 2000/10/30 20:57:00 jdolecek Exp $");
39 #endif
40 
41 #include <sys/cdefs.h>
42 #include <sys/param.h>
43 #define NTFS
44 #include <sys/mount.h>
45 #include <sys/stat.h>
46 #include <ntfs/ntfsmount.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <grp.h>
50 #include <pwd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <sysexits.h>
55 #include <unistd.h>
56 
57 #include "mntopts.h"
58 #include <fattr.h>
59 
60 static const struct mntopt mopts[] = {
61 	MOPT_STDOPTS,
62 	{ NULL }
63 };
64 
65 #ifndef __dead2
66 #define __dead2 __attribute__((__noreturn__))
67 #endif
68 
69 static void	usage __P((void)) __dead2;
70 int main __P((int, char **));
71 int mount_ntfs __P((int argc, char **argv));
72 
73 #ifndef MOUNT_NOMAIN
74 int
75 main(argc, argv)
76 	int argc;
77 	char **argv;
78 {
79 	return mount_ntfs(argc, argv);
80 }
81 #endif
82 
83 int
84 mount_ntfs(argc, argv)
85 	int argc;
86 	char **argv;
87 {
88 	struct ntfs_args args;
89 	struct stat sb;
90 	int c, mntflags, set_gid, set_uid, set_mask;
91 	char *dev, *dir, ndir[MAXPATHLEN+1];
92 #ifdef __FreeBSD__
93 #if __FreeBSD_version >= 300000
94 	struct vfsconf vfc;
95 #else
96 	struct vfsconf *vfc;
97 #endif
98 #endif
99 
100 	mntflags = set_gid = set_uid = set_mask = 0;
101 	(void)memset(&args, '\0', sizeof(args));
102 
103 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
104 		switch (c) {
105 		case 'u':
106 			args.uid = a_uid(optarg);
107 			set_uid = 1;
108 			break;
109 		case 'g':
110 			args.gid = a_gid(optarg);
111 			set_gid = 1;
112 			break;
113 		case 'm':
114 			args.mode = a_mask(optarg);
115 			set_mask = 1;
116 			break;
117 		case 'i':
118 			args.flag |= NTFS_MFLAG_CASEINS;
119 			break;
120 		case 'a':
121 			args.flag |= NTFS_MFLAG_ALLNAMES;
122 			break;
123 		case 'o':
124 			getmntopts(optarg, mopts, &mntflags, 0);
125 			break;
126 		case '?':
127 		default:
128 			usage();
129 			break;
130 		}
131 	}
132 
133 	if (optind + 2 != argc)
134 		usage();
135 
136 	dev = argv[optind];
137 	dir = argv[optind + 1];
138 	if (dir[0] != '/') {
139 		warnx("\"%s\" is a relative path", dir);
140 		if (getcwd(ndir, sizeof(ndir)) == NULL)
141 			err(EX_OSERR, "getcwd");
142 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
143 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
144 		dir = ndir;
145 		warnx("using \"%s\" instead", dir);
146 	}
147 
148 	args.fspec = dev;
149 	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
150 	if (mntflags & MNT_RDONLY)
151 		args.export.ex_flags = MNT_EXRDONLY;
152 	else
153 		args.export.ex_flags = 0;
154 	if (!set_gid || !set_uid || !set_mask) {
155 		if (stat(dir, &sb) == -1)
156 			err(EX_OSERR, "stat %s", dir);
157 
158 		if (!set_uid)
159 			args.uid = sb.st_uid;
160 		if (!set_gid)
161 			args.gid = sb.st_gid;
162 		if (!set_mask)
163 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
164 	}
165 #ifdef __FreeBSD__
166 #if __FreeBSD_version >= 300000
167 	c = getvfsbyname("ntfs", &vfc);
168 	if(c && vfsisloadable("ntfs")) {
169 		if(vfsload("ntfs"))
170 #else
171 	vfc = getvfsbyname("ntfs");
172 	if(!vfc && vfsisloadable("ntfs")) {
173 		if(vfsload("ntfs"))
174 #endif
175 			err(EX_OSERR, "vfsload(ntfs)");
176 		endvfsent();	/* clear cache */
177 #if __FreeBSD_version >= 300000
178 		c = getvfsbyname("ntfs", &vfc);
179 #else
180 		vfc = getvfsbyname("ntfs");
181 #endif
182 	}
183 #if __FreeBSD_version >= 300000
184 	if (c)
185 #else
186 	if (!vfc)
187 #endif
188 		errx(EX_OSERR, "ntfs filesystem is not available");
189 
190 #if __FreeBSD_version >= 300000
191 	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
192 #else
193 	if (mount(vfc->vfc_index, dir, mntflags, &args) < 0)
194 #endif
195 #else
196 	if (mount(MOUNT_NTFS, dir, mntflags, &args) < 0)
197 #endif
198 		err(EX_OSERR, "%s on %s", dev, dir);
199 
200 	exit (0);
201 }
202 
203 static void
204 usage()
205 {
206 	fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] bdev dir\n");
207 	exit(EX_USAGE);
208 }
209