Home | History | Annotate | Line # | Download | only in mount_ntfs
      1  1.22     pooka /* $NetBSD: mount_ntfs.c,v 1.22 2009/09/14 12:43:46 pooka Exp $ */
      2   1.2  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (c) 1994 Christopher G. Demetriou
      5   1.1  christos  * Copyright (c) 1999 Semen Ustimenko
      6   1.1  christos  * All rights reserved.
      7   1.1  christos  *
      8   1.1  christos  * Redistribution and use in source and binary forms, with or without
      9   1.1  christos  * modification, are permitted provided that the following conditions
     10   1.1  christos  * are met:
     11   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15   1.1  christos  *    documentation and/or other materials provided with the distribution.
     16   1.1  christos  * 3. All advertising materials mentioning features or use of this software
     17   1.1  christos  *    must display the following acknowledgement:
     18   1.1  christos  *      This product includes software developed by Christopher G. Demetriou.
     19   1.1  christos  * 4. The name of the author may not be used to endorse or promote products
     20   1.1  christos  *    derived from this software without specific prior written permission
     21   1.1  christos  *
     22   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1  christos  *
     33   1.1  christos  * Id: mount_ntfs.c,v 1.1.1.1 1999/02/03 03:51:19 semenu Exp
     34   1.1  christos  */
     35   1.4       cgd 
     36   1.2  christos #include <sys/cdefs.h>
     37   1.2  christos #ifndef lint
     38  1.22     pooka __RCSID("$NetBSD: mount_ntfs.c,v 1.22 2009/09/14 12:43:46 pooka Exp $");
     39   1.2  christos #endif
     40   1.1  christos 
     41   1.1  christos #include <sys/param.h>
     42   1.1  christos #include <sys/mount.h>
     43   1.1  christos #include <sys/stat.h>
     44   1.1  christos #include <ntfs/ntfsmount.h>
     45   1.1  christos #include <err.h>
     46   1.1  christos #include <grp.h>
     47   1.1  christos #include <pwd.h>
     48   1.1  christos #include <stdio.h>
     49   1.1  christos #include <stdlib.h>
     50   1.1  christos #include <string.h>
     51   1.1  christos #include <sysexits.h>
     52   1.1  christos #include <unistd.h>
     53   1.6  christos #include <util.h>
     54   1.1  christos 
     55   1.8  jdolecek #include <mntopts.h>
     56  1.21     pooka 
     57  1.21     pooka #include "mountprog.h"
     58  1.21     pooka #include "mount_ntfs.h"
     59   1.1  christos 
     60   1.5  jdolecek static const struct mntopt mopts[] = {
     61   1.1  christos 	MOPT_STDOPTS,
     62   1.6  christos 	MOPT_GETARGS,
     63  1.15  christos 	MOPT_NULL,
     64   1.1  christos };
     65   1.1  christos 
     66  1.20     perry static void	usage(void) __dead;
     67   1.2  christos 
     68   1.5  jdolecek #ifndef MOUNT_NOMAIN
     69   1.1  christos int
     70  1.12   xtraeme main(int argc, char **argv)
     71   1.1  christos {
     72  1.21     pooka 
     73  1.21     pooka 	setprogname(argv[0]);
     74   1.5  jdolecek 	return mount_ntfs(argc, argv);
     75   1.5  jdolecek }
     76   1.5  jdolecek #endif
     77   1.5  jdolecek 
     78  1.21     pooka void
     79  1.21     pooka mount_ntfs_parseargs(int argc, char **argv,
     80  1.21     pooka 	struct ntfs_args *args, int *mntflags,
     81  1.21     pooka 	char *canon_dev, char *canon_dir)
     82   1.5  jdolecek {
     83   1.1  christos 	struct stat sb;
     84  1.21     pooka 	int c, set_gid, set_uid, set_mask;
     85  1.21     pooka 	char *dev, *dir;
     86  1.14  christos 	mntoptparse_t mp;
     87   1.1  christos 
     88  1.21     pooka 	*mntflags = set_gid = set_uid = set_mask = 0;
     89  1.21     pooka 	(void)memset(args, '\0', sizeof(*args));
     90   1.1  christos 
     91   1.1  christos 	while ((c = getopt(argc, argv, "aiu:g:m:o:")) !=  -1) {
     92   1.1  christos 		switch (c) {
     93   1.1  christos 		case 'u':
     94  1.21     pooka 			args->uid = a_uid(optarg);
     95   1.1  christos 			set_uid = 1;
     96   1.1  christos 			break;
     97   1.1  christos 		case 'g':
     98  1.21     pooka 			args->gid = a_gid(optarg);
     99   1.1  christos 			set_gid = 1;
    100   1.1  christos 			break;
    101   1.1  christos 		case 'm':
    102  1.21     pooka 			args->mode = a_mask(optarg);
    103   1.1  christos 			set_mask = 1;
    104   1.1  christos 			break;
    105   1.1  christos 		case 'i':
    106  1.21     pooka 			args->flag |= NTFS_MFLAG_CASEINS;
    107   1.1  christos 			break;
    108   1.1  christos 		case 'a':
    109  1.21     pooka 			args->flag |= NTFS_MFLAG_ALLNAMES;
    110   1.1  christos 			break;
    111   1.1  christos 		case 'o':
    112  1.21     pooka 			mp = getmntopts(optarg, mopts, mntflags, 0);
    113  1.14  christos 			if (mp == NULL)
    114  1.14  christos 				err(1, "getmntopts");
    115  1.14  christos 			freemntopts(mp);
    116   1.1  christos 			break;
    117   1.1  christos 		case '?':
    118   1.1  christos 		default:
    119   1.1  christos 			usage();
    120   1.1  christos 			break;
    121   1.1  christos 		}
    122   1.1  christos 	}
    123   1.1  christos 
    124   1.1  christos 	if (optind + 2 != argc)
    125   1.1  christos 		usage();
    126   1.1  christos 
    127   1.1  christos 	dev = argv[optind];
    128   1.1  christos 	dir = argv[optind + 1];
    129  1.11       erh 
    130  1.21     pooka 	pathadj(dev, canon_dev);
    131  1.21     pooka 	pathadj(dir, canon_dir);
    132   1.1  christos 
    133  1.22     pooka 	args->fspec = canon_dev;
    134   1.1  christos 	if (!set_gid || !set_uid || !set_mask) {
    135   1.1  christos 		if (stat(dir, &sb) == -1)
    136   1.1  christos 			err(EX_OSERR, "stat %s", dir);
    137   1.1  christos 
    138   1.1  christos 		if (!set_uid)
    139  1.21     pooka 			args->uid = sb.st_uid;
    140   1.1  christos 		if (!set_gid)
    141  1.21     pooka 			args->gid = sb.st_gid;
    142   1.1  christos 		if (!set_mask)
    143  1.21     pooka 			args->mode = sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    144   1.1  christos 	}
    145  1.21     pooka }
    146  1.21     pooka 
    147  1.21     pooka int
    148  1.21     pooka mount_ntfs(int argc, char *argv[])
    149  1.21     pooka {
    150  1.21     pooka 	struct ntfs_args args;
    151  1.21     pooka 	int mntflags;
    152  1.21     pooka 	char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
    153  1.21     pooka 
    154  1.21     pooka 	mount_ntfs_parseargs(argc, argv, &args, &mntflags,
    155  1.21     pooka 	    canon_dev, canon_dir);
    156   1.1  christos 
    157  1.21     pooka 	if (mount(MOUNT_NTFS, canon_dir, mntflags, &args, sizeof args) == -1)
    158  1.21     pooka 		err(EX_OSERR, "%s on %s", canon_dev, canon_dir);
    159   1.1  christos 
    160   1.6  christos 	if (mntflags & MNT_GETARGS) {
    161   1.6  christos 		char buf[1024];
    162   1.6  christos 		(void)snprintb(buf, sizeof(buf), NTFS_MFLAG_BITS, args.flag);
    163   1.9  christos 		printf("uid=%d, gid=%d, mode=0%o, flags=%s\n", args.uid,
    164   1.6  christos 		    args.gid, args.mode, buf);
    165   1.6  christos 	}
    166   1.1  christos 	exit (0);
    167   1.1  christos }
    168   1.1  christos 
    169   1.5  jdolecek static void
    170  1.12   xtraeme usage(void)
    171   1.1  christos {
    172  1.21     pooka 	fprintf(stderr, "usage: %s [-a] [-i] [-u user] [-g group] [-m mask] "
    173  1.21     pooka 	    "bdev dir\n", getprogname());
    174   1.1  christos 	exit(EX_USAGE);
    175   1.1  christos }
    176