mount_udf.c revision 1.5 1 /* $NetBSD: mount_udf.c,v 1.5 2006/03/21 21:11:42 christos 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.5 2006/03/21 21:11:42 christos Exp $");
40 #endif /* not lint */
41
42
43 /* main includes; strip me! */
44 #include <sys/cdefs.h>
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/stat.h>
48 #include <ctype.h>
49 #include <err.h>
50 #include <grp.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57 #include <util.h>
58 #include <assert.h>
59 #include <pwd.h>
60 #include <errno.h>
61 #include <grp.h>
62
63
64 /* mount specific options */
65 #include <fs/udf/udf_mount.h>
66 #include <mntopts.h>
67 #include <fattr.h>
68
69
70 /* options to pass on to the `mount' call */
71 static const struct mntopt mopts[] = {
72 MOPT_STDOPTS, /* `normal' options */
73 MOPT_ASYNC, /* default */
74 MOPT_UPDATE, /* not yet supported */
75 MOPT_GETARGS, /* printing */
76 { NULL }
77 };
78
79
80 /* prototypes */
81 int mount_udf(int argc, char **argv);
82 static void usage(void) __attribute__((__noreturn__));
83
84
85 /* code */
86
87 static void
88 usage(void)
89 {
90 (void)fprintf(stderr, "Usage: %s [-g gid] [-o options] [-s session] "
91 "[-t gmtoff] [-u uid] special node\n", getprogname());
92 exit(EXIT_FAILURE);
93 }
94
95
96 /* copied from mount_msdos; is it necessary still? */
97 #ifndef MOUNT_NOMAIN
98 int
99 main(int argc, char **argv)
100 {
101 return mount_udf(argc, argv);
102 }
103 #endif
104
105
106 /* main routine */
107 int
108 mount_udf(int argc, char **argv)
109 {
110 struct udf_args args;
111 struct tm *tm;
112 time_t now;
113 uid_t anon_uid, nobody_uid;
114 gid_t anon_gid, nobody_gid;
115 char *dev, *dir;
116 int ch, mntflags, set_gmtoff;
117 uint32_t sector_size;
118 mntoptparse_t mp;
119
120 /* set program name for error messages */
121 setprogname(argv[0]);
122
123 /* initialise */
124 (void)memset(&args, 0, sizeof(args));
125
126 set_gmtoff = mntflags = 0;
127 sector_size = 0;
128
129 /* get nobody */
130 nobody_uid = anon_uid = a_uid("nobody");
131 nobody_gid = anon_gid = a_gid("nobody");
132
133 /* NEVER EVER allow nobody_uid:nobody_gid to be 0:0 */
134 assert(nobody_uid != 0);
135 assert(nobody_gid != 0);
136
137 while ((ch = getopt(argc, argv, "cg:o:s:t:u:")) != -1) {
138 switch (ch) {
139 #ifdef notyet
140 case 'c' :
141 args.udfmflags |= UDFMNT_CLOSESESSION;
142 break;
143 #endif
144 case 'g' :
145 /* convert groupname or numeric equiv. */
146 anon_gid = a_gid(optarg);
147 break;
148 case 'u' :
149 /* convert username or numeric equiv. */
150 anon_uid = a_uid(optarg);
151 break;
152 case 'o' :
153 /* process generic mount options */
154 mp = getmntopts(optarg, mopts, &mntflags, 0);
155 if (mp == NULL)
156 err(EXIT_FAILURE, "getmntopts");
157 freemntopts(mp);
158 break;
159 case 's' :
160 args.sessionnr = a_num(optarg, "session number");
161 break;
162 case 't' :
163 args.gmtoff = a_num(optarg, "gmtoff");
164 set_gmtoff = 1;
165 break;
166 default :
167 usage();
168 /* NOTREACHED */
169 }
170 }
171
172 if (optind + 2 != argc)
173 usage();
174
175 if (!set_gmtoff) {
176 /* use user's time zone as default */
177 (void)time(&now);
178 tm = localtime(&now);
179 args.gmtoff = tm->tm_gmtoff;
180 }
181
182 /* get device and directory specifier */
183 dev = argv[optind];
184 dir = argv[optind + 1];
185
186 args.version = UDFMNT_VERSION;
187 args.fspec = dev;
188 args.anon_uid = anon_uid;
189 args.anon_gid = anon_gid;
190 args.nobody_uid = nobody_uid;
191 args.nobody_gid = nobody_gid;
192 args.sector_size = sector_size; /* invalid */
193
194 /* mount it! :) */
195 if (mount(MOUNT_UDF, dir, mntflags, &args) == -1)
196 err(EXIT_FAILURE, "Cannot mount %s on %s", dev, dir);
197
198 if (mntflags & MNT_GETARGS) {
199 char buf[1024];
200
201 (void)snprintb(buf, sizeof(buf), UDFMNT_BITS,
202 (uint64_t)args.udfmflags);
203 (void)printf("gmtoffset=%d, sessionnr=%d, flags=%s\n",
204 args.gmtoff, args.sessionnr, buf);
205 }
206
207 return EXIT_SUCCESS;
208 }
209