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