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