194663Sscottl/*
294663Sscottl * Copyright (c) 1992, 1993, 1994
394663Sscottl *      The Regents of the University of California.  All rights reserved.
494663Sscottl * Copyright (c) 2002 Scott Long
594663Sscottl *
694663Sscottl * This code is derived from software contributed to Berkeley
794663Sscottl * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
894663Sscottl * Support code is derived from software contributed to Berkeley
994663Sscottl * by Atsushi Murai (amurai@spec.co.jp).
1094663Sscottl *
1194663Sscottl * Redistribution and use in source and binary forms, with or without
1294663Sscottl * modification, are permitted provided that the following conditions
1394663Sscottl * are met:
1494663Sscottl * 1. Redistributions of source code must retain the above copyright
1594663Sscottl *    notice, this list of conditions and the following disclaimer.
1694663Sscottl * 2. Redistributions in binary form must reproduce the above copyright
1794663Sscottl *    notice, this list of conditions and the following disclaimer in the
1894663Sscottl *    documentation and/or other materials provided with the distribution.
1994663Sscottl * 4. Neither the name of the University nor the names of its contributors
2094663Sscottl *    may be used to endorse or promote products derived from this software
2194663Sscottl *    without specific prior written permission.
2294663Sscottl *
2394663Sscottl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2494663Sscottl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2594663Sscottl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2694663Sscottl * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2794663Sscottl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2894663Sscottl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2994663Sscottl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3094663Sscottl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3194663Sscottl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3294663Sscottl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3394663Sscottl * SUCH DAMAGE.
3494663Sscottl *
3594663Sscottl * $FreeBSD$
3694663Sscottl */
3794663Sscottl
3894663Sscottl/*
3994663Sscottl * This is just a rip-off of mount_iso9660.c.  It's been vastly simplified
4094663Sscottl * because UDF doesn't take any options at this time.
4194663Sscottl */
4294663Sscottl
4394663Sscottl#include <sys/cdio.h>
4494663Sscottl#include <sys/file.h>
45122097Sscottl#include <sys/iconv.h>
4694663Sscottl#include <sys/param.h>
47122097Sscottl#include <sys/linker.h>
48122097Sscottl#include <sys/module.h>
4994663Sscottl#include <sys/mount.h>
50101829Smux#include <sys/uio.h>
5194663Sscottl
52122097Sscottl#include <fs/udf/udf_mount.h>
53122097Sscottl
5494663Sscottl#include <err.h>
5594663Sscottl#include <errno.h>
5694663Sscottl#include <stdlib.h>
5794663Sscottl#include <stdio.h>
5894663Sscottl#include <string.h>
5994663Sscottl#include <sysexits.h>
6094663Sscottl#include <unistd.h>
6194663Sscottl
6294663Sscottl#include "mntopts.h"
6394663Sscottl
64227081Sedstatic struct mntopt mopts[] = {
6594663Sscottl	MOPT_STDOPTS,
6694663Sscottl	MOPT_UPDATE,
67147242Sdelphij	MOPT_END
6894663Sscottl};
6994663Sscottl
70122097Sscottlint	set_charset(char **, char **, const char *);
7194663Sscottlvoid	usage(void);
7294663Sscottl
7394663Sscottlint
7494663Sscottlmain(int argc, char **argv)
7594663Sscottl{
76247861Sjkim	char mntpath[MAXPATHLEN];
77247861Sjkim	char fstype[] = "udf";
78247861Sjkim	struct iovec *iov;
79247861Sjkim	char *cs_disk, *cs_local, *dev, *dir;
80261741Sbrueffer	int ch, iovlen, mntflags, udf_flags, verbose;
8194663Sscottl
82261741Sbrueffer	iovlen = mntflags = udf_flags = verbose = 0;
83122097Sscottl	cs_disk = cs_local = NULL;
84247861Sjkim	iov = NULL;
85122097Sscottl	while ((ch = getopt(argc, argv, "o:vC:")) != -1)
8694663Sscottl		switch (ch) {
8794663Sscottl		case 'o':
88247856Sjkim			getmntopts(optarg, mopts, &mntflags, NULL);
8994663Sscottl			break;
9094663Sscottl		case 'v':
9194663Sscottl			verbose++;
9294663Sscottl			break;
93122097Sscottl		case 'C':
94122097Sscottl			if (set_charset(&cs_disk, &cs_local, optarg) == -1)
95122097Sscottl				err(EX_OSERR, "udf_iconv");
96122097Sscottl			udf_flags |= UDFMNT_KICONV;
97122097Sscottl			break;
9894663Sscottl		case '?':
9994663Sscottl		default:
10094663Sscottl			usage();
10194663Sscottl		}
10294663Sscottl	argc -= optind;
10394663Sscottl	argv += optind;
10494663Sscottl
10594663Sscottl	if (argc != 2)
10694663Sscottl		usage();
10794663Sscottl
10894663Sscottl	dev = argv[0];
10994663Sscottl	dir = argv[1];
11094663Sscottl
11194663Sscottl	/*
11294663Sscottl	 * Resolve the mountpoint with realpath(3) and remove unnecessary
11394663Sscottl	 * slashes from the devicename if there are any.
11494663Sscottl	 */
115230226Sjh	if (checkpath(dir, mntpath) != 0)
116230226Sjh		err(EX_USAGE, "%s", mntpath);
11794663Sscottl	(void)rmslashes(dev, dev);
11894663Sscottl
11994663Sscottl	/*
120102231Strhodes	 * UDF file systems are not writeable.
12194663Sscottl	 */
12294663Sscottl	mntflags |= MNT_RDONLY;
12394663Sscottl
124247861Sjkim	build_iovec(&iov, &iovlen, "fstype", fstype, (size_t)-1);
125247861Sjkim	build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1);
126247861Sjkim	build_iovec(&iov, &iovlen, "from", dev, (size_t)-1);
127247861Sjkim	build_iovec(&iov, &iovlen, "flags", &udf_flags, sizeof(udf_flags));
128122097Sscottl	if (udf_flags & UDFMNT_KICONV) {
129247861Sjkim		build_iovec(&iov, &iovlen, "cs_disk", cs_disk, (size_t)-1);
130247861Sjkim		build_iovec(&iov, &iovlen, "cs_local", cs_local, (size_t)-1);
131122097Sscottl	}
132261741Sbrueffer	if (nmount(iov, iovlen, mntflags) < 0)
13398265Smux		err(1, "%s", dev);
13494663Sscottl	exit(0);
13594663Sscottl}
13694663Sscottl
137122097Sscottlint
138122097Sscottlset_charset(char **cs_disk, char **cs_local, const char *localcs)
139122097Sscottl{
140122097Sscottl	int error;
141122097Sscottl
142122097Sscottl	if (modfind("udf_iconv") < 0)
143122097Sscottl		if (kldload("udf_iconv") < 0 || modfind("udf_iconv") < 0) {
144122097Sscottl			warnx( "cannot find or load \"udf_iconv\" kernel module");
145122097Sscottl			return (-1);
146122097Sscottl		}
147122097Sscottl
148122097Sscottl	if ((*cs_disk = malloc(ICONV_CSNMAXLEN)) == NULL)
149122097Sscottl		return (-1);
150122097Sscottl	if ((*cs_local = malloc(ICONV_CSNMAXLEN)) == NULL)
151122097Sscottl		return (-1);
152122097Sscottl	strncpy(*cs_disk, ENCODING_UNICODE, ICONV_CSNMAXLEN);
153122097Sscottl	strncpy(*cs_local, localcs, ICONV_CSNMAXLEN);
154123293Sfjoe	error = kiconv_add_xlat16_cspairs(*cs_disk, *cs_local);
155122097Sscottl	if (error)
156122097Sscottl		return (-1);
157122097Sscottl
158122097Sscottl	return (0);
159122097Sscottl}
160122097Sscottl
16194663Sscottlvoid
16294663Sscottlusage(void)
16394663Sscottl{
16494663Sscottl	(void)fprintf(stderr,
165122097Sscottl		"usage: mount_udf [-v] [-o options] [-C charset] special node\n");
16694663Sscottl	exit(EX_USAGE);
16794663Sscottl}
168