usr.sbin/fstyp: Fix incorrect pfs_type test in ondisk inode
[dragonfly.git] / sbin / mount_cd9660 / mount_cd9660.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1992, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley
6 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
7 * Support code is derived from software contributed to Berkeley
8 * by Atsushi Murai (amurai@spec.co.jp).
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
dc71b7ab 18 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)mount_cd9660.c 8.7 (Berkeley) 5/1/95
1de703da
MD
35 *
36 * @(#) Copyright (c) 1992, 1993, 1994 The Regents of the University of California. All rights reserved.
37 * @(#)mount_cd9660.c 8.7 (Berkeley) 5/1/95
38 * $FreeBSD: src/sbin/mount_cd9660/mount_cd9660.c,v 1.15.2.3 2001/03/14 12:05:01 bp Exp $
984263bc
MD
39 */
40
984263bc 41#include <sys/cdio.h>
984263bc
MD
42#include <sys/param.h>
43#include <sys/mount.h>
d35760ee
AP
44#include <sys/iconv.h>
45#include <sys/linker.h>
46#include <sys/module.h>
47#include <vfs/isofs/cd9660/cd9660_mount.h>
984263bc
MD
48
49#include <err.h>
50#include <errno.h>
2c3b1d1b 51#include <fcntl.h>
2dc55a02 52#include <mntopts.h>
984263bc
MD
53#include <stdlib.h>
54#include <stdio.h>
55#include <string.h>
d35760ee 56#include <locale.h>
984263bc
MD
57#include <sysexits.h>
58#include <unistd.h>
59
984263bc
MD
60struct mntopt mopts[] = {
61 MOPT_STDOPTS,
62 MOPT_UPDATE,
63 { "extatt", 0, ISOFSMNT_EXTATT, 1 },
64 { "gens", 0, ISOFSMNT_GENS, 1 },
65 { "rrip", 1, ISOFSMNT_NORRIP, 1 },
66 { "joliet", 1, ISOFSMNT_NOJOLIET, 1 },
67 { "strictjoliet", 1, ISOFSMNT_BROKENJOLIET, 1 },
e7adb9bc 68 MOPT_NULL
984263bc
MD
69};
70
e7adb9bc
MD
71static int get_ssector(const char *dev);
72static void usage(void);
d35760ee 73int set_charset(struct iso_args *args, const char *cs_local, const char *cs_disk);
984263bc
MD
74
75int
76main(int argc, char **argv)
77{
78 struct iso_args args;
79 int ch, mntflags, opts;
80 char *dev, *dir, mntpath[MAXPATHLEN];
81 struct vfsconf vfc;
82 int error, verbose;
d35760ee
AP
83 const char *quirk;
84 char *cs_local = NULL;
984263bc
MD
85
86 mntflags = opts = verbose = 0;
87 memset(&args, 0, sizeof args);
88 args.ssector = -1;
e1483b1c 89 while ((ch = getopt(argc, argv, "begjo:rs:C:v")) != -1)
984263bc
MD
90 switch (ch) {
91 case 'b':
92 opts |= ISOFSMNT_BROKENJOLIET;
93 break;
94 case 'e':
95 opts |= ISOFSMNT_EXTATT;
96 break;
97 case 'g':
98 opts |= ISOFSMNT_GENS;
99 break;
100 case 'j':
101 opts |= ISOFSMNT_NOJOLIET;
102 break;
e1483b1c
AP
103 case 'C':
104 quirk = kiconv_quirkcs(optarg, KICONV_VENDOR_MICSFT);
105 cs_local = strdup(quirk);
106 if (set_charset(&args, cs_local, NULL) == -1)
107 err(EX_OSERR, "cd9660_iconv");
d35760ee
AP
108 opts |= ISOFSMNT_KICONV;
109 break;
984263bc
MD
110 case 'o':
111 getmntopts(optarg, mopts, &mntflags, &opts);
112 break;
113 case 'r':
114 opts |= ISOFSMNT_NORRIP;
115 break;
116 case 's':
117 args.ssector = atoi(optarg);
118 break;
119 case 'v':
120 verbose++;
121 break;
122 case '?':
123 default:
124 usage();
125 }
126 argc -= optind;
127 argv += optind;
128
129 if (argc != 2)
130 usage();
131
132 dev = argv[0];
133 dir = argv[1];
134
135 /*
136 * Resolve the mountpoint with realpath(3) and remove unnecessary
137 * slashes from the devicename if there are any.
138 */
7cee7052
SW
139 checkpath(dir, mntpath);
140 rmslashes(dev, dev);
984263bc
MD
141
142#define DEFAULT_ROOTUID -2
143 /*
144 * ISO 9660 filesystems are not writeable.
145 */
146 mntflags |= MNT_RDONLY;
147 args.export.ex_flags = MNT_EXRDONLY;
148 args.fspec = dev;
149 args.export.ex_root = DEFAULT_ROOTUID;
150 args.flags = opts;
151
152 if (args.ssector == -1) {
153 /*
154 * The start of the session has not been specified on
155 * the command line. If we can successfully read the
156 * TOC of a CD-ROM, use the last data track we find.
157 * Otherwise, just use 0, in order to mount the very
158 * first session. This is compatible with the
159 * historic behaviour of mount_cd9660(8). If the user
160 * has specified -s <ssector> above, we don't get here
161 * and leave the user's will.
162 */
163 if ((args.ssector = get_ssector(dev)) == -1) {
164 if (verbose)
165 printf("could not determine starting sector, "
166 "using very first session\n");
167 args.ssector = 0;
168 } else if (verbose)
169 printf("using starting sector %d\n", args.ssector);
170 }
171
172 error = getvfsbyname("cd9660", &vfc);
173 if (error && vfsisloadable("cd9660")) {
174 if (vfsload("cd9660"))
175 err(EX_OSERR, "vfsload(cd9660)");
176 endvfsent(); /* flush cache */
177 error = getvfsbyname("cd9660", &vfc);
178 }
179 if (error)
180 errx(1, "cd9660 filesystem is not available");
181
182 if (mount(vfc.vfc_name, mntpath, mntflags, &args) < 0)
183 err(1, "%s", args.fspec);
184 exit(0);
185}
186
d35760ee
AP
187int
188set_charset(struct iso_args *args, const char *cs_local, const char *cs_disk)
189{
190 int error;
191 if (modfind("cd9660_iconv") < 0) {
192 if (kldload("cd9660_iconv") < 0 || modfind("cd9660_iconv") < 0)
193 {
194 warnx("cannot find or load \"cd9660_iconv\" kernel module");
195 return (-1);
196 }
197 }
198 snprintf(args->cs_local, ICONV_CSNMAXLEN, "%s", cs_local);
199 error = kiconv_add_xlat16_cspairs(ENCODING_UNICODE, cs_local);
200 if (error)
201 return (-1);
202 if (!cs_disk)
203 cs_disk = strdup(ENCODING_UNICODE);
204 snprintf(args->cs_disk, ICONV_CSNMAXLEN, "%s", cs_disk);
205 error = kiconv_add_xlat16_cspairs(cs_disk, cs_local);
206 if (error)
207 return (-1);
208 return (0);
209}
210
211
e7adb9bc 212static void
984263bc
MD
213usage(void)
214{
7cee7052 215 fprintf(stderr,
5bdb9719 216 "usage: mount_cd9660 [-begjrv] [-C charset] [-o options] [-s startsector] special node\n");
984263bc
MD
217 exit(EX_USAGE);
218}
219
e7adb9bc 220static int
984263bc
MD
221get_ssector(const char *dev)
222{
223 struct ioc_toc_header h;
224 struct ioc_read_toc_entry t;
225 struct cd_toc_entry toc_buffer[100];
226 int fd, ntocentries, i;
227
228 if ((fd = open(dev, O_RDONLY)) == -1)
229 return -1;
230 if (ioctl(fd, CDIOREADTOCHEADER, &h) == -1) {
231 close(fd);
232 return -1;
233 }
234
235 ntocentries = h.ending_track - h.starting_track + 1;
236 if (ntocentries > 100) {
237 /* unreasonable, only 100 allowed */
238 close(fd);
239 return -1;
240 }
241 t.address_format = CD_LBA_FORMAT;
242 t.starting_track = 0;
243 t.data_len = ntocentries * sizeof(struct cd_toc_entry);
244 t.data = toc_buffer;
245
246 if (ioctl(fd, CDIOREADTOCENTRYS, (char *) &t) == -1) {
247 close(fd);
248 return -1;
249 }
250 close(fd);
251
252 for (i = ntocentries - 1; i >= 0; i--)
253 if ((toc_buffer[i].control & 4) != 0)
254 /* found a data track */
255 break;
256 if (i < 0)
257 return -1;
258
259 return ntohl(toc_buffer[i].addr.lba);
260}