mount_udf.c revision 122097
1/*
2 * Copyright (c) 1992, 1993, 1994
3 *      The Regents of the University of California.  All rights reserved.
4 * Copyright (c) 2002 Scott Long
5 *
6 * This code is derived from software contributed to Berkeley
7 * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
8 * Support code is derived from software contributed to Berkeley
9 * by Atsushi Murai (amurai@spec.co.jp).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the University of
22 *	California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 *    may be used to endorse or promote products derived from this software
25 *    without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * $FreeBSD: head/sbin/mount_udf/mount_udf.c 122097 2003-11-05 06:21:45Z scottl $
40 */
41
42/*
43 * This is just a rip-off of mount_iso9660.c.  It's been vastly simplified
44 * because UDF doesn't take any options at this time.
45 */
46
47#include <sys/cdio.h>
48#include <sys/file.h>
49#include <sys/iconv.h>
50#include <sys/param.h>
51#include <sys/linker.h>
52#include <sys/module.h>
53#include <sys/mount.h>
54#include <sys/uio.h>
55
56#include <fs/udf/udf_mount.h>
57
58#include <err.h>
59#include <errno.h>
60#include <stdlib.h>
61#include <stdio.h>
62#include <string.h>
63#include <sysexits.h>
64#include <unistd.h>
65
66#include "mntopts.h"
67
68struct mntopt mopts[] = {
69	MOPT_STDOPTS,
70	MOPT_UPDATE,
71	{ NULL, 0, 0, 0 }
72};
73
74int	set_charset(char **, char **, const char *);
75void	usage(void);
76
77int
78main(int argc, char **argv)
79{
80	struct iovec iov[12];
81	int ch, i, mntflags, opts, udf_flags;
82	char *dev, *dir, mntpath[MAXPATHLEN];
83	char *cs_disk, *cs_local;
84	int verbose;
85
86	i = mntflags = opts = udf_flags = verbose = 0;
87	cs_disk = cs_local = NULL;
88	while ((ch = getopt(argc, argv, "o:vC:")) != -1)
89		switch (ch) {
90		case 'o':
91			getmntopts(optarg, mopts, &mntflags, &opts);
92			break;
93		case 'v':
94			verbose++;
95			break;
96		case 'C':
97			if (set_charset(&cs_disk, &cs_local, optarg) == -1)
98				err(EX_OSERR, "udf_iconv");
99			udf_flags |= UDFMNT_KICONV;
100			break;
101		case '?':
102		default:
103			usage();
104		}
105	argc -= optind;
106	argv += optind;
107
108	if (argc != 2)
109		usage();
110
111	dev = argv[0];
112	dir = argv[1];
113
114	/*
115	 * Resolve the mountpoint with realpath(3) and remove unnecessary
116	 * slashes from the devicename if there are any.
117	 */
118	(void)checkpath(dir, mntpath);
119	(void)rmslashes(dev, dev);
120
121	/*
122	 * UDF file systems are not writeable.
123	 */
124	mntflags |= MNT_RDONLY;
125
126	iov[i].iov_base = "fstype";
127	iov[i++].iov_len = sizeof("fstype");
128	iov[i].iov_base = "udf";
129	iov[i].iov_len = strlen(iov[i].iov_base) + 1;
130	i++;
131	iov[i].iov_base = "fspath";
132	iov[i++].iov_len = sizeof("fspath");
133	iov[i].iov_base = mntpath;
134	iov[i++].iov_len = strlen(mntpath) + 1;
135	iov[i].iov_base = "from";
136	iov[i++].iov_len = sizeof("from");
137	iov[i].iov_base = dev;
138	iov[i++].iov_len = strlen(dev) + 1;
139	iov[i].iov_base = "flags";
140	iov[i++].iov_len = sizeof("flags");
141	iov[i].iov_base = &udf_flags;
142	iov[i++].iov_len = sizeof(udf_flags);
143	if (udf_flags & UDFMNT_KICONV) {
144		iov[i].iov_base = "cs_disk";
145		iov[i++].iov_len = sizeof("cs_disk") + 1;
146		iov[i].iov_base = cs_disk;
147		iov[i++].iov_len = strlen(cs_disk) + 1;
148		iov[i].iov_base = "cs_local";
149		iov[i++].iov_len = sizeof("cs_local") + 1;
150		iov[i].iov_base = cs_local;
151		iov[i++].iov_len = strlen(cs_local) + 1;
152	}
153	if (nmount(iov, i, mntflags) < 0)
154		err(1, "%s", dev);
155	exit(0);
156}
157
158int
159set_charset(char **cs_disk, char **cs_local, const char *localcs)
160{
161	int error;
162
163	if (modfind("udf_iconv") < 0)
164		if (kldload("udf_iconv") < 0 || modfind("udf_iconv") < 0) {
165			warnx( "cannot find or load \"udf_iconv\" kernel module");
166			return (-1);
167		}
168
169	if ((*cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
170		return (-1);
171	if ((*cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
172		return (-1);
173	strncpy(*cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
174	strncpy(*cs_local, localcs, ICONV_CSNMAXLEN);
175	error = kiconv_add_xlat16_cspair(*cs_local, *cs_disk, 0);
176	if (error)
177		return (-1);
178#if 0
179	error = kiconv_add_xlat16_cspair(*cs_disk, *cs_local, 0);
180	if (error)
181		return (-1);
182#endif
183
184	return (0);
185}
186
187void
188usage(void)
189{
190	(void)fprintf(stderr,
191		"usage: mount_udf [-v] [-o options] [-C charset] special node\n");
192	exit(EX_USAGE);
193}
194