Home | History | Annotate | Line # | Download | only in mount_ntfs
mount_ntfs.c revision 1.6
      1 /* $NetBSD: mount_ntfs.c,v 1.6 2002/09/21 18:43:36 christos 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.6 2002/09/21 18:43:36 christos 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 #include <util.h>
     57 
     58 #include "mntopts.h"
     59 #include <fattr.h>
     60 
     61 static const struct mntopt mopts[] = {
     62 	MOPT_STDOPTS,
     63 	MOPT_GETARGS,
     64 	{ NULL }
     65 };
     66 
     67 #ifndef __dead2
     68 #define __dead2 __attribute__((__noreturn__))
     69 #endif
     70 
     71 static void	usage __P((void)) __dead2;
     72 int main __P((int, char **));
     73 int mount_ntfs __P((int argc, char **argv));
     74 
     75 #ifndef MOUNT_NOMAIN
     76 int
     77 main(argc, argv)
     78 	int argc;
     79 	char **argv;
     80 {
     81 	return mount_ntfs(argc, argv);
     82 }
     83 #endif
     84 
     85 int
     86 mount_ntfs(argc, argv)
     87 	int argc;
     88 	char **argv;
     89 {
     90 	struct ntfs_args args;
     91 	struct stat sb;
     92 	int c, mntflags, set_gid, set_uid, set_mask;
     93 	char *dev, *dir, ndir[MAXPATHLEN+1];
     94 #ifdef __FreeBSD__
     95 #if __FreeBSD_version >= 300000
     96 	struct vfsconf vfc;
     97 #else
     98 	struct vfsconf *vfc;
     99 #endif
    100 #endif
    101 
    102 	mntflags = set_gid = set_uid = set_mask = 0;
    103 	(void)memset(&args, '\0', sizeof(args));
    104 
    105 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
    106 		switch (c) {
    107 		case 'u':
    108 			args.uid = a_uid(optarg);
    109 			set_uid = 1;
    110 			break;
    111 		case 'g':
    112 			args.gid = a_gid(optarg);
    113 			set_gid = 1;
    114 			break;
    115 		case 'm':
    116 			args.mode = a_mask(optarg);
    117 			set_mask = 1;
    118 			break;
    119 		case 'i':
    120 			args.flag |= NTFS_MFLAG_CASEINS;
    121 			break;
    122 		case 'a':
    123 			args.flag |= NTFS_MFLAG_ALLNAMES;
    124 			break;
    125 		case 'o':
    126 			getmntopts(optarg, mopts, &mntflags, 0);
    127 			break;
    128 		case '?':
    129 		default:
    130 			usage();
    131 			break;
    132 		}
    133 	}
    134 
    135 	if (optind + 2 != argc)
    136 		usage();
    137 
    138 	dev = argv[optind];
    139 	dir = argv[optind + 1];
    140 	if (dir[0] != '/') {
    141 		warnx("\"%s\" is a relative path", dir);
    142 		if (getcwd(ndir, sizeof(ndir)) == NULL)
    143 			err(EX_OSERR, "getcwd");
    144 		strncat(ndir, "/", sizeof(ndir) - strlen(ndir) - 1);
    145 		strncat(ndir, dir, sizeof(ndir) - strlen(ndir) - 1);
    146 		dir = ndir;
    147 		warnx("using \"%s\" instead", dir);
    148 	}
    149 
    150 	args.fspec = dev;
    151 	args.export.ex_root = 65534;	/* unchecked anyway on DOS fs */
    152 	if (mntflags & MNT_RDONLY)
    153 		args.export.ex_flags = MNT_EXRDONLY;
    154 	else
    155 		args.export.ex_flags = 0;
    156 	if (!set_gid || !set_uid || !set_mask) {
    157 		if (stat(dir, &sb) == -1)
    158 			err(EX_OSERR, "stat %s", dir);
    159 
    160 		if (!set_uid)
    161 			args.uid = sb.st_uid;
    162 		if (!set_gid)
    163 			args.gid = sb.st_gid;
    164 		if (!set_mask)
    165 			args.mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    166 	}
    167 #ifdef __FreeBSD__
    168 #if __FreeBSD_version >= 300000
    169 	c = getvfsbyname("ntfs", &vfc);
    170 	if(c && vfsisloadable("ntfs")) {
    171 		if(vfsload("ntfs"))
    172 #else
    173 	vfc = getvfsbyname("ntfs");
    174 	if(!vfc && vfsisloadable("ntfs")) {
    175 		if(vfsload("ntfs"))
    176 #endif
    177 			err(EX_OSERR, "vfsload(ntfs)");
    178 		endvfsent();	/* clear cache */
    179 #if __FreeBSD_version >= 300000
    180 		c = getvfsbyname("ntfs", &vfc);
    181 #else
    182 		vfc = getvfsbyname("ntfs");
    183 #endif
    184 	}
    185 #if __FreeBSD_version >= 300000
    186 	if (c)
    187 #else
    188 	if (!vfc)
    189 #endif
    190 		errx(EX_OSERR, "ntfs filesystem is not available");
    191 
    192 #if __FreeBSD_version >= 300000
    193 	if (mount(vfc.vfc_name, dir, mntflags, &args) < 0)
    194 #else
    195 	if (mount(vfc->vfc_index, dir, mntflags, &args) < 0)
    196 #endif
    197 #else
    198 	if (mount(MOUNT_NTFS, dir, mntflags, &args) < 0)
    199 #endif
    200 		err(EX_OSERR, "%s on %s", dev, dir);
    201 
    202 	if (mntflags & MNT_GETARGS) {
    203 		char buf[1024];
    204 		(void)snprintb(buf, sizeof(buf), NTFS_MFLAG_BITS, args.flag);
    205 		printf("uid=%d, gid=%d, mode=%d, flags=%s\n", args.uid,
    206 		    args.gid, args.mode, buf);
    207 	}
    208 	exit (0);
    209 }
    210 
    211 static void
    212 usage()
    213 {
    214 	fprintf(stderr, "usage: mount_ntfs [-a] [-i] [-u user] [-g group] [-m mask] bdev dir\n");
    215 	exit(EX_USAGE);
    216 }
    217