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