mount_udf.c revision 1.8 1 /* $NetBSD: mount_udf.c,v 1.8 2007/01/17 21:59:50 hubertf Exp $ */
2
3 /*
4 * Copyright (c) 2006 Reinoud Zandijk
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed for the
18 * NetBSD Project. See http://www.NetBSD.org/ for
19 * information about NetBSD.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36
37 #include <sys/cdefs.h>
38 #ifndef lint
39 __RCSID("$NetBSD: mount_udf.c,v 1.8 2007/01/17 21:59:50 hubertf Exp $");
40 #endif /* not lint */
41
42
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/stat.h>
46
47 #include <assert.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <errno.h>
51 #include <grp.h>
52 #include <pwd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <util.h>
59
60
61 /* mount specific options */
62 #include <fs/udf/udf_mount.h>
63 #include <mntopts.h>
64 #include <fattr.h>
65
66
67 /* options to pass on to the `mount' call */
68 static const struct mntopt mopts[] = {
69 MOPT_STDOPTS, /* `normal' options */
70 MOPT_ASYNC, /* default */
71 MOPT_NOATIME, /* dont update access times */
72 MOPT_UPDATE, /* not yet supported */
73 MOPT_GETARGS, /* printing */
74 MOPT_NULL,
75 };
76
77
78 /* prototypes */
79 int mount_udf(int argc, char **argv);
80 static void usage(void) __attribute__((__noreturn__));
81
82
83 /* code */
84
85 static void
86 usage(void)
87 {
88 (void)fprintf(stderr, "Usage: %s [-g gid] [-o options] [-s session] "
89 "[-t gmtoff] [-u uid] special node\n", getprogname());
90 exit(EXIT_FAILURE);
91 }
92
93
94 /* copied from mount_msdos; is it necessary still? */
95 #ifndef MOUNT_NOMAIN
96 int
97 main(int argc, char **argv)
98 {
99 return mount_udf(argc, argv);
100 }
101 #endif
102
103
104 /* main routine */
105 int
106 mount_udf(int argc, char **argv)
107 {
108 struct udf_args args;
109 struct tm *tm;
110 time_t now;
111 uid_t anon_uid, nobody_uid;
112 gid_t anon_gid, nobody_gid;
113 char *dev, *dir;
114 int ch, mntflags, set_gmtoff;
115 uint32_t sector_size;
116 mntoptparse_t mp;
117
118 /* set program name for error messages */
119 setprogname(argv[0]);
120
121 /* initialise */
122 (void)memset(&args, 0, sizeof(args));
123
124 set_gmtoff = mntflags = 0;
125 sector_size = 0;
126
127 /* get nobody */
128 nobody_uid = anon_uid = a_uid("nobody");
129 nobody_gid = anon_gid = a_gid("nobody");
130
131 /* NEVER EVER allow nobody_uid:nobody_gid to be 0:0 */
132 assert(nobody_uid != 0);
133 assert(nobody_gid != 0);
134
135 while ((ch = getopt(argc, argv, "cg:o:s:t:u:")) != -1) {
136 switch (ch) {
137 #ifdef notyet
138 case 'c' :
139 args.udfmflags |= UDFMNT_CLOSESESSION;
140 break;
141 #endif
142 case 'g' :
143 /* convert groupname or numeric equiv. */
144 anon_gid = a_gid(optarg);
145 break;
146 case 'u' :
147 /* convert username or numeric equiv. */
148 anon_uid = a_uid(optarg);
149 break;
150 case 'o' :
151 /* process generic mount options */
152 mp = getmntopts(optarg, mopts, &mntflags, 0);
153 if (mp == NULL)
154 err(EXIT_FAILURE, "getmntopts");
155 freemntopts(mp);
156 break;
157 case 's' :
158 args.sessionnr = a_num(optarg, "session number");
159 break;
160 case 't' :
161 args.gmtoff = a_num(optarg, "gmtoff");
162 set_gmtoff = 1;
163 break;
164 default :
165 usage();
166 /* NOTREACHED */
167 }
168 }
169
170 if (optind + 2 != argc)
171 usage();
172
173 if (!set_gmtoff) {
174 /* use user's time zone as default */
175 (void)time(&now);
176 tm = localtime(&now);
177 args.gmtoff = tm->tm_gmtoff;
178 }
179
180 /* get device and directory specifier */
181 dev = argv[optind];
182 dir = argv[optind + 1];
183
184 args.version = UDFMNT_VERSION;
185 args.fspec = dev;
186 args.anon_uid = anon_uid;
187 args.anon_gid = anon_gid;
188 args.nobody_uid = nobody_uid;
189 args.nobody_gid = nobody_gid;
190 args.sector_size = sector_size; /* invalid */
191
192 /* mount it! :) */
193 if (mount(MOUNT_UDF, dir, mntflags, &args) == -1)
194 err(EXIT_FAILURE, "Cannot mount %s on %s", dev, dir);
195
196 if (mntflags & MNT_GETARGS) {
197 char buf[1024];
198
199 (void)snprintb(buf, sizeof(buf), UDFMNT_BITS,
200 (uint64_t)args.udfmflags);
201 (void)printf("gmtoffset=%d, sessionnr=%d, flags=%s\n",
202 args.gmtoff, args.sessionnr, buf);
203 }
204
205 return EXIT_SUCCESS;
206 }
207