xref: /freebsd/sbin/mount_udf/mount_udf.c (revision b3e76948)
18a16b7a1SPedro F. Giffuni /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
451a7b740SScott Long  * Copyright (c) 1992, 1993, 1994
551a7b740SScott Long  *      The Regents of the University of California.  All rights reserved.
651a7b740SScott Long  * Copyright (c) 2002 Scott Long
751a7b740SScott Long  *
851a7b740SScott Long  * This code is derived from software contributed to Berkeley
951a7b740SScott Long  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
1051a7b740SScott Long  * Support code is derived from software contributed to Berkeley
1151a7b740SScott Long  * by Atsushi Murai (amurai@spec.co.jp).
1251a7b740SScott Long  *
1351a7b740SScott Long  * Redistribution and use in source and binary forms, with or without
1451a7b740SScott Long  * modification, are permitted provided that the following conditions
1551a7b740SScott Long  * are met:
1651a7b740SScott Long  * 1. Redistributions of source code must retain the above copyright
1751a7b740SScott Long  *    notice, this list of conditions and the following disclaimer.
1851a7b740SScott Long  * 2. Redistributions in binary form must reproduce the above copyright
1951a7b740SScott Long  *    notice, this list of conditions and the following disclaimer in the
2051a7b740SScott Long  *    documentation and/or other materials provided with the distribution.
21fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
2251a7b740SScott Long  *    may be used to endorse or promote products derived from this software
2351a7b740SScott Long  *    without specific prior written permission.
2451a7b740SScott Long  *
2551a7b740SScott Long  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2651a7b740SScott Long  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2751a7b740SScott Long  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2851a7b740SScott Long  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2951a7b740SScott Long  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3051a7b740SScott Long  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3151a7b740SScott Long  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3251a7b740SScott Long  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3351a7b740SScott Long  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3451a7b740SScott Long  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3551a7b740SScott Long  * SUCH DAMAGE.
3651a7b740SScott Long  */
3751a7b740SScott Long 
3851a7b740SScott Long /*
3951a7b740SScott Long  * This is just a rip-off of mount_iso9660.c.  It's been vastly simplified
4051a7b740SScott Long  * because UDF doesn't take any options at this time.
4151a7b740SScott Long  */
4251a7b740SScott Long 
4351a7b740SScott Long #include <sys/cdio.h>
4451a7b740SScott Long #include <sys/file.h>
45cc2c948fSScott Long #include <sys/iconv.h>
4651a7b740SScott Long #include <sys/param.h>
47cc2c948fSScott Long #include <sys/linker.h>
48cc2c948fSScott Long #include <sys/module.h>
4951a7b740SScott Long #include <sys/mount.h>
50eddb9a0dSMaxime Henrion #include <sys/uio.h>
5151a7b740SScott Long 
52cc2c948fSScott Long #include <fs/udf/udf_mount.h>
53cc2c948fSScott Long 
5451a7b740SScott Long #include <err.h>
5551a7b740SScott Long #include <errno.h>
5651a7b740SScott Long #include <stdlib.h>
5751a7b740SScott Long #include <stdio.h>
5851a7b740SScott Long #include <string.h>
5951a7b740SScott Long #include <sysexits.h>
6051a7b740SScott Long #include <unistd.h>
6151a7b740SScott Long 
6251a7b740SScott Long #include "mntopts.h"
6351a7b740SScott Long 
641efe3c6bSEd Schouten static struct mntopt mopts[] = {
6551a7b740SScott Long 	MOPT_STDOPTS,
6651a7b740SScott Long 	MOPT_UPDATE,
6746b7a14bSXin LI 	MOPT_END
6851a7b740SScott Long };
6951a7b740SScott Long 
70cc2c948fSScott Long int	set_charset(char **, char **, const char *);
7151a7b740SScott Long void	usage(void);
7251a7b740SScott Long 
7351a7b740SScott Long int
main(int argc,char ** argv)7451a7b740SScott Long main(int argc, char **argv)
7551a7b740SScott Long {
7606704176SJung-uk Kim 	char mntpath[MAXPATHLEN];
7706704176SJung-uk Kim 	char fstype[] = "udf";
7806704176SJung-uk Kim 	struct iovec *iov;
7906704176SJung-uk Kim 	char *cs_disk, *cs_local, *dev, *dir;
80ca001f0bSChristian Brueffer 	int ch, iovlen, mntflags, udf_flags, verbose;
8151a7b740SScott Long 
82ca001f0bSChristian Brueffer 	iovlen = mntflags = udf_flags = verbose = 0;
83cc2c948fSScott Long 	cs_disk = cs_local = NULL;
8406704176SJung-uk Kim 	iov = NULL;
85cc2c948fSScott Long 	while ((ch = getopt(argc, argv, "o:vC:")) != -1)
8651a7b740SScott Long 		switch (ch) {
8751a7b740SScott Long 		case 'o':
887039dfb3SJung-uk Kim 			getmntopts(optarg, mopts, &mntflags, NULL);
8951a7b740SScott Long 			break;
9051a7b740SScott Long 		case 'v':
9151a7b740SScott Long 			verbose++;
9251a7b740SScott Long 			break;
93cc2c948fSScott Long 		case 'C':
94cc2c948fSScott Long 			if (set_charset(&cs_disk, &cs_local, optarg) == -1)
95cc2c948fSScott Long 				err(EX_OSERR, "udf_iconv");
96cc2c948fSScott Long 			udf_flags |= UDFMNT_KICONV;
97cc2c948fSScott Long 			break;
9851a7b740SScott Long 		case '?':
9951a7b740SScott Long 		default:
10051a7b740SScott Long 			usage();
10151a7b740SScott Long 		}
10251a7b740SScott Long 	argc -= optind;
10351a7b740SScott Long 	argv += optind;
10451a7b740SScott Long 
10551a7b740SScott Long 	if (argc != 2)
10651a7b740SScott Long 		usage();
10751a7b740SScott Long 
10851a7b740SScott Long 	dev = argv[0];
10951a7b740SScott Long 	dir = argv[1];
11051a7b740SScott Long 
11151a7b740SScott Long 	/*
11251a7b740SScott Long 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
11351a7b740SScott Long 	 * slashes from the devicename if there are any.
11451a7b740SScott Long 	 */
115d3250014SJaakko Heinonen 	if (checkpath(dir, mntpath) != 0)
116d3250014SJaakko Heinonen 		err(EX_USAGE, "%s", mntpath);
11751a7b740SScott Long 	(void)rmslashes(dev, dev);
11851a7b740SScott Long 
11951a7b740SScott Long 	/*
12051a7b740SScott Long 	 * UDF file systems are not writeable.
12151a7b740SScott Long 	 */
12251a7b740SScott Long 	mntflags |= MNT_RDONLY;
12351a7b740SScott Long 
12406704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
12506704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
12606704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
12706704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "flags", &udf_flags, sizeof(udf_flags));
128cc2c948fSScott Long 	if (udf_flags & UDFMNT_KICONV) {
12906704176SJung-uk Kim 		build_iovec(&iov, &iovlen, "cs_disk", cs_disk, (size_t)-1);
13006704176SJung-uk Kim 		build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
131cc2c948fSScott Long 	}
132ca001f0bSChristian Brueffer 	if (nmount(iov, iovlen, mntflags) < 0)
133c3210a83SMaxime Henrion 		err(1, "%s", dev);
13451a7b740SScott Long 	exit(0);
13551a7b740SScott Long }
13651a7b740SScott Long 
137cc2c948fSScott Long int
set_charset(char ** cs_disk,char ** cs_local,const char * localcs)138cc2c948fSScott Long set_charset(char **cs_disk, char **cs_local, const char *localcs)
139cc2c948fSScott Long {
140cc2c948fSScott Long 	int error;
141cc2c948fSScott Long 
142cc2c948fSScott Long 	if (modfind("udf_iconv") < 0)
143cc2c948fSScott Long 		if (kldload("udf_iconv") < 0 || modfind("udf_iconv") < 0) {
144cc2c948fSScott Long 			warnx( "cannot find or load \"udf_iconv\" kernel module");
145cc2c948fSScott Long 			return (-1);
146cc2c948fSScott Long 		}
147cc2c948fSScott Long 
148cc2c948fSScott Long 	if ((*cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
149cc2c948fSScott Long 		return (-1);
150cc2c948fSScott Long 	if ((*cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
151cc2c948fSScott Long 		return (-1);
152cc2c948fSScott Long 	strncpy(*cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
153cc2c948fSScott Long 	strncpy(*cs_local, localcs, ICONV_CSNMAXLEN);
1540f4e4130SMax Khon 	error = kiconv_add_xlat16_cspairs(*cs_disk, *cs_local);
155cc2c948fSScott Long 	if (error)
156cc2c948fSScott Long 		return (-1);
157cc2c948fSScott Long 
158cc2c948fSScott Long 	return (0);
159cc2c948fSScott Long }
160cc2c948fSScott Long 
16151a7b740SScott Long void
usage(void)16251a7b740SScott Long usage(void)
16351a7b740SScott Long {
16451a7b740SScott Long 	(void)fprintf(stderr,
165cc2c948fSScott Long 		"usage: mount_udf [-v] [-o options] [-C charset] special node\n");
16651a7b740SScott Long 	exit(EX_USAGE);
16751a7b740SScott Long }
168