mount_msdos.c revision 1.46 1 1.46 pooka /* $NetBSD: mount_msdos.c,v 1.46 2008/08/05 20:57:45 pooka Exp $ */
2 1.10 cgd
3 1.1 cgd /*
4 1.5 cgd * Copyright (c) 1994 Christopher G. Demetriou
5 1.5 cgd * All rights reserved.
6 1.23 cgd *
7 1.5 cgd * Redistribution and use in source and binary forms, with or without
8 1.5 cgd * modification, are permitted provided that the following conditions
9 1.5 cgd * are met:
10 1.5 cgd * 1. Redistributions of source code must retain the above copyright
11 1.5 cgd * notice, this list of conditions and the following disclaimer.
12 1.5 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.5 cgd * notice, this list of conditions and the following disclaimer in the
14 1.5 cgd * documentation and/or other materials provided with the distribution.
15 1.5 cgd * 3. All advertising materials mentioning features or use of this software
16 1.5 cgd * must display the following acknowledgement:
17 1.23 cgd * This product includes software developed for the
18 1.26 grant * NetBSD Project. See http://www.NetBSD.org/ for
19 1.23 cgd * information about NetBSD.
20 1.5 cgd * 4. The name of the author may not be used to endorse or promote products
21 1.23 cgd * derived from this software without specific prior written permission.
22 1.23 cgd *
23 1.5 cgd * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 1.5 cgd * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 1.5 cgd * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 1.5 cgd * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 1.5 cgd * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 1.5 cgd * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 1.5 cgd * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.5 cgd * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 1.5 cgd * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 1.5 cgd * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 1.23 cgd *
34 1.23 cgd * <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35 1.1 cgd */
36 1.2 mycroft
37 1.17 lukem #include <sys/cdefs.h>
38 1.2 mycroft #ifndef lint
39 1.46 pooka __RCSID("$NetBSD: mount_msdos.c,v 1.46 2008/08/05 20:57:45 pooka Exp $");
40 1.2 mycroft #endif /* not lint */
41 1.2 mycroft
42 1.7 cgd #include <sys/param.h>
43 1.1 cgd #include <sys/mount.h>
44 1.6 cgd #include <sys/stat.h>
45 1.19 fvdl #include <msdosfs/msdosfsmount.h>
46 1.6 cgd #include <err.h>
47 1.6 cgd #include <grp.h>
48 1.6 cgd #include <pwd.h>
49 1.5 cgd #include <stdio.h>
50 1.5 cgd #include <stdlib.h>
51 1.5 cgd #include <string.h>
52 1.31 itojun #include <time.h>
53 1.7 cgd #include <unistd.h>
54 1.25 christos #include <util.h>
55 1.1 cgd
56 1.27 jdolecek #include <mntopts.h>
57 1.8 cgd
58 1.46 pooka #include "mountprog.h"
59 1.45 pooka #include "mount_msdos.h"
60 1.45 pooka
61 1.24 jdolecek static const struct mntopt mopts[] = {
62 1.8 cgd MOPT_STDOPTS,
63 1.21 jdolecek MOPT_ASYNC,
64 1.21 jdolecek MOPT_SYNC,
65 1.16 cgd MOPT_UPDATE,
66 1.30 jdolecek MOPT_GETARGS,
67 1.39 christos MOPT_NULL,
68 1.8 cgd };
69 1.8 cgd
70 1.44 perry static void usage(void) __dead;
71 1.8 cgd
72 1.24 jdolecek #ifndef MOUNT_NOMAIN
73 1.1 cgd int
74 1.36 xtraeme main(int argc, char **argv)
75 1.1 cgd {
76 1.45 pooka
77 1.45 pooka setprogname(argv[0]);
78 1.24 jdolecek return mount_msdos(argc, argv);
79 1.24 jdolecek }
80 1.24 jdolecek #endif
81 1.24 jdolecek
82 1.45 pooka void
83 1.45 pooka mount_msdos_parseargs(int argc, char **argv,
84 1.45 pooka struct msdosfs_args *args, int *mntflags,
85 1.45 pooka char *canon_dev, char *canon_dir)
86 1.24 jdolecek {
87 1.6 cgd struct stat sb;
88 1.45 pooka int c, set_gid, set_uid, set_mask, set_dirmask, set_gmtoff;
89 1.45 pooka char *dev, *dir;
90 1.31 itojun time_t now;
91 1.31 itojun struct tm *tm;
92 1.38 christos mntoptparse_t mp;
93 1.1 cgd
94 1.45 pooka *mntflags = set_gid = set_uid = set_mask = set_dirmask = set_gmtoff = 0;
95 1.45 pooka (void)memset(args, '\0', sizeof(*args));
96 1.1 cgd
97 1.31 itojun while ((c = getopt(argc, argv, "Gsl9u:g:m:M:o:t:")) != -1) {
98 1.1 cgd switch (c) {
99 1.12 leo case 'G':
100 1.45 pooka args->flags |= MSDOSFSMNT_GEMDOSFS;
101 1.12 leo break;
102 1.11 ws case 's':
103 1.45 pooka args->flags |= MSDOSFSMNT_SHORTNAME;
104 1.11 ws break;
105 1.11 ws case 'l':
106 1.45 pooka args->flags |= MSDOSFSMNT_LONGNAME;
107 1.11 ws break;
108 1.11 ws case '9':
109 1.45 pooka args->flags |= MSDOSFSMNT_NOWIN95;
110 1.11 ws break;
111 1.6 cgd case 'u':
112 1.45 pooka args->uid = a_uid(optarg);
113 1.6 cgd set_uid = 1;
114 1.6 cgd break;
115 1.6 cgd case 'g':
116 1.45 pooka args->gid = a_gid(optarg);
117 1.6 cgd set_gid = 1;
118 1.1 cgd break;
119 1.6 cgd case 'm':
120 1.45 pooka args->mask = a_mask(optarg);
121 1.6 cgd set_mask = 1;
122 1.6 cgd break;
123 1.29 jdolecek case 'M':
124 1.45 pooka args->dirmask = a_mask(optarg);
125 1.29 jdolecek set_dirmask = 1;
126 1.29 jdolecek break;
127 1.8 cgd case 'o':
128 1.45 pooka mp = getmntopts(optarg, mopts, mntflags, 0);
129 1.38 christos if (mp == NULL)
130 1.38 christos err(1, "getmntopts");
131 1.38 christos freemntopts(mp);
132 1.8 cgd break;
133 1.31 itojun case 't':
134 1.45 pooka args->gmtoff = atoi(optarg);
135 1.31 itojun set_gmtoff = 1;
136 1.31 itojun break;
137 1.6 cgd case '?':
138 1.1 cgd default:
139 1.6 cgd usage();
140 1.6 cgd break;
141 1.1 cgd }
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd if (optind + 2 != argc)
145 1.6 cgd usage();
146 1.1 cgd
147 1.29 jdolecek if (set_mask && !set_dirmask) {
148 1.45 pooka args->dirmask = args->mask;
149 1.29 jdolecek set_dirmask = 1;
150 1.29 jdolecek } else if (set_dirmask && !set_mask) {
151 1.45 pooka args->mask = args->dirmask;
152 1.29 jdolecek set_mask = 1;
153 1.29 jdolecek }
154 1.29 jdolecek
155 1.1 cgd dev = argv[optind];
156 1.1 cgd dir = argv[optind + 1];
157 1.35 erh
158 1.46 pooka pathadj(dev, canon_dev);
159 1.46 pooka pathadj(dir, canon_dir);
160 1.1 cgd
161 1.45 pooka args->fspec = dev;
162 1.6 cgd if (!set_gid || !set_uid || !set_mask) {
163 1.6 cgd if (stat(dir, &sb) == -1)
164 1.6 cgd err(1, "stat %s", dir);
165 1.6 cgd
166 1.6 cgd if (!set_uid)
167 1.45 pooka args->uid = sb.st_uid;
168 1.6 cgd if (!set_gid)
169 1.45 pooka args->gid = sb.st_gid;
170 1.29 jdolecek if (!set_mask) {
171 1.45 pooka args->mask = args->dirmask =
172 1.29 jdolecek sb.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO);
173 1.29 jdolecek }
174 1.31 itojun }
175 1.31 itojun
176 1.31 itojun if (!set_gmtoff) {
177 1.31 itojun /* use user's time zone as default */
178 1.31 itojun time(&now);
179 1.31 itojun tm = localtime(&now);
180 1.45 pooka args->gmtoff = tm->tm_gmtoff;
181 1.31 itojun
182 1.6 cgd }
183 1.45 pooka args->flags |= MSDOSFSMNT_VERSIONED;
184 1.45 pooka args->version = MSDOSFSMNT_VERSION;
185 1.45 pooka }
186 1.45 pooka
187 1.45 pooka int
188 1.45 pooka mount_msdos(int argc, char **argv)
189 1.45 pooka {
190 1.45 pooka struct msdosfs_args args;
191 1.45 pooka char canon_dev[MAXPATHLEN], canon_dir[MAXPATHLEN];
192 1.45 pooka int mntflags;
193 1.45 pooka
194 1.45 pooka mount_msdos_parseargs(argc, argv, &args, &mntflags,
195 1.45 pooka canon_dev, canon_dir);
196 1.6 cgd
197 1.45 pooka if (mount(MOUNT_MSDOS, canon_dir, mntflags, &args, sizeof args) == -1)
198 1.45 pooka err(1, "%s on %s", canon_dev, canon_dir);
199 1.25 christos
200 1.25 christos if (mntflags & MNT_GETARGS) {
201 1.25 christos char buf[1024];
202 1.25 christos (void)snprintb(buf, sizeof(buf), MSDOSFSMNT_BITS, args.flags);
203 1.29 jdolecek printf("uid=%d, gid=%d, mask=0%o, dirmask=0%o, flags=%s\n",
204 1.29 jdolecek args.uid, args.gid, args.mask, args.dirmask, buf);
205 1.25 christos }
206 1.6 cgd
207 1.6 cgd exit (0);
208 1.6 cgd }
209 1.6 cgd
210 1.24 jdolecek static void
211 1.36 xtraeme usage(void)
212 1.6 cgd {
213 1.9 mycroft
214 1.45 pooka fprintf(stderr, "usage: %s [-9Gls] [-g gid] [-M mask] [-m mask] "
215 1.45 pooka "[-o options]\n\t[-t gmtoff] [-u uid] special mountpath\n",
216 1.45 pooka getprogname());
217 1.6 cgd exit(1);
218 1.1 cgd }
219