xref: /freebsd/sbin/mount_udf/mount_udf.c (revision 8a16b7a1)
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  * $FreeBSD$
3851a7b740SScott Long  */
3951a7b740SScott Long 
4051a7b740SScott Long /*
4151a7b740SScott Long  * This is just a rip-off of mount_iso9660.c.  It's been vastly simplified
4251a7b740SScott Long  * because UDF doesn't take any options at this time.
4351a7b740SScott Long  */
4451a7b740SScott Long 
4551a7b740SScott Long #include <sys/cdio.h>
4651a7b740SScott Long #include <sys/file.h>
47cc2c948fSScott Long #include <sys/iconv.h>
4851a7b740SScott Long #include <sys/param.h>
49cc2c948fSScott Long #include <sys/linker.h>
50cc2c948fSScott Long #include <sys/module.h>
5151a7b740SScott Long #include <sys/mount.h>
52eddb9a0dSMaxime Henrion #include <sys/uio.h>
5351a7b740SScott Long 
54cc2c948fSScott Long #include <fs/udf/udf_mount.h>
55cc2c948fSScott Long 
5651a7b740SScott Long #include <err.h>
5751a7b740SScott Long #include <errno.h>
5851a7b740SScott Long #include <stdlib.h>
5951a7b740SScott Long #include <stdio.h>
6051a7b740SScott Long #include <string.h>
6151a7b740SScott Long #include <sysexits.h>
6251a7b740SScott Long #include <unistd.h>
6351a7b740SScott Long 
6451a7b740SScott Long #include "mntopts.h"
6551a7b740SScott Long 
661efe3c6bSEd Schouten static struct mntopt mopts[] = {
6751a7b740SScott Long 	MOPT_STDOPTS,
6851a7b740SScott Long 	MOPT_UPDATE,
6946b7a14bSXin LI 	MOPT_END
7051a7b740SScott Long };
7151a7b740SScott Long 
72cc2c948fSScott Long int	set_charset(char **, char **, const char *);
7351a7b740SScott Long void	usage(void);
7451a7b740SScott Long 
7551a7b740SScott Long int
7651a7b740SScott Long main(int argc, char **argv)
7751a7b740SScott Long {
7806704176SJung-uk Kim 	char mntpath[MAXPATHLEN];
7906704176SJung-uk Kim 	char fstype[] = "udf";
8006704176SJung-uk Kim 	struct iovec *iov;
8106704176SJung-uk Kim 	char *cs_disk, *cs_local, *dev, *dir;
82ca001f0bSChristian Brueffer 	int ch, iovlen, mntflags, udf_flags, verbose;
8351a7b740SScott Long 
84ca001f0bSChristian Brueffer 	iovlen = mntflags = udf_flags = verbose = 0;
85cc2c948fSScott Long 	cs_disk = cs_local = NULL;
8606704176SJung-uk Kim 	iov = NULL;
87cc2c948fSScott Long 	while ((ch = getopt(argc, argv, "o:vC:")) != -1)
8851a7b740SScott Long 		switch (ch) {
8951a7b740SScott Long 		case 'o':
907039dfb3SJung-uk Kim 			getmntopts(optarg, mopts, &mntflags, NULL);
9151a7b740SScott Long 			break;
9251a7b740SScott Long 		case 'v':
9351a7b740SScott Long 			verbose++;
9451a7b740SScott Long 			break;
95cc2c948fSScott Long 		case 'C':
96cc2c948fSScott Long 			if (set_charset(&cs_disk, &cs_local, optarg) == -1)
97cc2c948fSScott Long 				err(EX_OSERR, "udf_iconv");
98cc2c948fSScott Long 			udf_flags |= UDFMNT_KICONV;
99cc2c948fSScott Long 			break;
10051a7b740SScott Long 		case '?':
10151a7b740SScott Long 		default:
10251a7b740SScott Long 			usage();
10351a7b740SScott Long 		}
10451a7b740SScott Long 	argc -= optind;
10551a7b740SScott Long 	argv += optind;
10651a7b740SScott Long 
10751a7b740SScott Long 	if (argc != 2)
10851a7b740SScott Long 		usage();
10951a7b740SScott Long 
11051a7b740SScott Long 	dev = argv[0];
11151a7b740SScott Long 	dir = argv[1];
11251a7b740SScott Long 
11351a7b740SScott Long 	/*
11451a7b740SScott Long 	 * Resolve the mountpoint with realpath(3) and remove unnecessary
11551a7b740SScott Long 	 * slashes from the devicename if there are any.
11651a7b740SScott Long 	 */
117d3250014SJaakko Heinonen 	if (checkpath(dir, mntpath) != 0)
118d3250014SJaakko Heinonen 		err(EX_USAGE, "%s", mntpath);
11951a7b740SScott Long 	(void)rmslashes(dev, dev);
12051a7b740SScott Long 
12151a7b740SScott Long 	/*
12251a7b740SScott Long 	 * UDF file systems are not writeable.
12351a7b740SScott Long 	 */
12451a7b740SScott Long 	mntflags |= MNT_RDONLY;
12551a7b740SScott Long 
12606704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
12706704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
12806704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
12906704176SJung-uk Kim 	build_iovec(&iov, &iovlen, "flags", &udf_flags, sizeof(udf_flags));
130cc2c948fSScott Long 	if (udf_flags & UDFMNT_KICONV) {
13106704176SJung-uk Kim 		build_iovec(&iov, &iovlen, "cs_disk", cs_disk, (size_t)-1);
13206704176SJung-uk Kim 		build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
133cc2c948fSScott Long 	}
134ca001f0bSChristian Brueffer 	if (nmount(iov, iovlen, mntflags) < 0)
135c3210a83SMaxime Henrion 		err(1, "%s", dev);
13651a7b740SScott Long 	exit(0);
13751a7b740SScott Long }
13851a7b740SScott Long 
139cc2c948fSScott Long int
140cc2c948fSScott Long set_charset(char **cs_disk, char **cs_local, const char *localcs)
141cc2c948fSScott Long {
142cc2c948fSScott Long 	int error;
143cc2c948fSScott Long 
144cc2c948fSScott Long 	if (modfind("udf_iconv") < 0)
145cc2c948fSScott Long 		if (kldload("udf_iconv") < 0 || modfind("udf_iconv") < 0) {
146cc2c948fSScott Long 			warnx( "cannot find or load \"udf_iconv\" kernel module");
147cc2c948fSScott Long 			return (-1);
148cc2c948fSScott Long 		}
149cc2c948fSScott Long 
150cc2c948fSScott Long 	if ((*cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
151cc2c948fSScott Long 		return (-1);
152cc2c948fSScott Long 	if ((*cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
153cc2c948fSScott Long 		return (-1);
154cc2c948fSScott Long 	strncpy(*cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
155cc2c948fSScott Long 	strncpy(*cs_local, localcs, ICONV_CSNMAXLEN);
1560f4e4130SMax Khon 	error = kiconv_add_xlat16_cspairs(*cs_disk, *cs_local);
157cc2c948fSScott Long 	if (error)
158cc2c948fSScott Long 		return (-1);
159cc2c948fSScott Long 
160cc2c948fSScott Long 	return (0);
161cc2c948fSScott Long }
162cc2c948fSScott Long 
16351a7b740SScott Long void
16451a7b740SScott Long usage(void)
16551a7b740SScott Long {
16651a7b740SScott Long 	(void)fprintf(stderr,
167cc2c948fSScott Long 		"usage: mount_udf [-v] [-o options] [-C charset] special node\n");
16851a7b740SScott Long 	exit(EX_USAGE);
16951a7b740SScott Long }
170