2 * Copyright (c) 1993, 1994
3 * The Regents of the University of California. All rights reserved.
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).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)cd9660_rrip.c 8.6 (Berkeley) 12/5/94
39 * $FreeBSD: src/sys/isofs/cd9660/cd9660_rrip.c,v 1.17 1999/08/28 00:46:06 peter Exp $
40 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_rrip.c,v 1.9 2005/02/03 09:20:27 joerg Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/kernel.h>
51 #include "cd9660_node.h"
52 #include "cd9660_rrip.h"
55 typedef int rrt_func_t (void *, ISO_RRIP_ANALYZE *ana);
60 void (*func2) (struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana);
64 static int cd9660_rrip_altname (ISO_RRIP_ALTNAME *p,
65 ISO_RRIP_ANALYZE *ana);
66 static int cd9660_rrip_attr (ISO_RRIP_ATTR *p, ISO_RRIP_ANALYZE *ana);
67 static int cd9660_rrip_cont (ISO_RRIP_CONT *p, ISO_RRIP_ANALYZE *ana);
68 static void cd9660_rrip_defattr (struct iso_directory_record *isodir,
69 ISO_RRIP_ANALYZE *ana);
70 static void cd9660_rrip_defname (struct iso_directory_record *isodir,
71 ISO_RRIP_ANALYZE *ana);
72 static void cd9660_rrip_deftstamp (struct iso_directory_record *isodir,
73 ISO_RRIP_ANALYZE *ana);
74 static int cd9660_rrip_device (ISO_RRIP_DEVICE *p,
75 ISO_RRIP_ANALYZE *ana);
76 static int cd9660_rrip_extref (ISO_RRIP_EXTREF *p,
77 ISO_RRIP_ANALYZE *ana);
78 static int cd9660_rrip_idflag (ISO_RRIP_IDFLAG *p,
79 ISO_RRIP_ANALYZE *ana);
80 static int cd9660_rrip_loop (struct iso_directory_record *isodir,
81 ISO_RRIP_ANALYZE *ana,
83 static int cd9660_rrip_pclink (ISO_RRIP_CLINK *p,
84 ISO_RRIP_ANALYZE *ana);
85 static int cd9660_rrip_reldir (ISO_RRIP_RELDIR *p,
86 ISO_RRIP_ANALYZE *ana);
87 static int cd9660_rrip_slink (ISO_RRIP_SLINK *p,
88 ISO_RRIP_ANALYZE *ana);
89 static int cd9660_rrip_stop (ISO_SUSP_HEADER *p,
90 ISO_RRIP_ANALYZE *ana);
91 static int cd9660_rrip_tstamp (ISO_RRIP_TSTAMP *p,
92 ISO_RRIP_ANALYZE *ana);
95 * POSIX file attribute
98 cd9660_rrip_attr(ISO_RRIP_ATTR *p, ISO_RRIP_ANALYZE *ana)
100 ana->inop->inode.iso_mode = isonum_733(p->mode);
101 ana->inop->inode.iso_uid = isonum_733(p->uid);
102 ana->inop->inode.iso_gid = isonum_733(p->gid);
103 ana->inop->inode.iso_links = isonum_733(p->links);
104 ana->fields &= ~ISO_SUSP_ATTR;
105 return ISO_SUSP_ATTR;
109 cd9660_rrip_defattr(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana)
111 /* But this is a required field! */
112 printf("RRIP without PX field?\n");
113 cd9660_defattr(isodir,ana->inop,NULL,ISO_FTYPE_RRIP);
120 cd9660_rrip_slink(ISO_RRIP_SLINK *p, ISO_RRIP_ANALYZE *ana)
122 ISO_RRIP_SLINK_COMPONENT *pcomp;
123 ISO_RRIP_SLINK_COMPONENT *pcompe;
124 int error, len, wlen, cont;
125 char *outbuf, *inbuf, *freebuf;
127 pcomp = (ISO_RRIP_SLINK_COMPONENT *)p->component;
128 pcompe = (ISO_RRIP_SLINK_COMPONENT *)((char *)p + isonum_711(p->h.length));
130 outbuf = ana->outbuf;
135 * Gathering a Symbolic name from each component with path
139 pcomp = (ISO_RRIP_SLINK_COMPONENT *)((char *)pcomp + ISO_RRIP_SLSIZ
140 + isonum_711(pcomp->clen))) {
143 if (len < ana->maxlen) {
153 switch (*pcomp->cflag) {
155 case ISO_SUSP_CFLAG_CURRENT:
156 /* Inserting Current */
160 case ISO_SUSP_CFLAG_PARENT:
161 /* Inserting Parent */
165 case ISO_SUSP_CFLAG_ROOT:
166 /* Inserting slash for ROOT */
167 /* start over from beginning(?) */
172 case ISO_SUSP_CFLAG_VOLROOT:
173 /* Inserting a mount point i.e. "/cdrom" */
177 error = cache_fullpath(NULL, ana->imp->im_mountp->mnt_ncp,
181 wlen = strlen(inbuf);
184 case ISO_SUSP_CFLAG_HOST:
185 /* Inserting hostname i.e. "kurt.tools.de" */
187 wlen = strlen(hostname);
190 case ISO_SUSP_CFLAG_CONTINUE:
194 /* Inserting component */
195 wlen = isonum_711(pcomp->clen);
199 printf("RRIP with incorrect flags?");
200 wlen = ana->maxlen + 1;
204 if (len + wlen > ana->maxlen)
207 bcopy(inbuf,outbuf,wlen);
211 if (freebuf != NULL) {
212 free(freebuf, M_TEMP);
216 ana->outbuf = outbuf;
220 if (!isonum_711(p->flags)) {
221 ana->fields &= ~ISO_SUSP_SLINK;
222 return ISO_SUSP_SLINK;
228 free(freebuf, M_TEMP);
231 ana->outbuf -= *ana->outlen;
240 cd9660_rrip_altname(ISO_RRIP_ALTNAME *p, ISO_RRIP_ANALYZE *ana)
251 case ISO_SUSP_CFLAG_CURRENT:
252 /* Inserting Current */
256 case ISO_SUSP_CFLAG_PARENT:
257 /* Inserting Parent */
261 case ISO_SUSP_CFLAG_HOST:
262 /* Inserting hostname i.e. "kurt.tools.de" */
264 wlen = strlen(hostname);
267 case ISO_SUSP_CFLAG_CONTINUE:
271 /* Inserting component */
272 wlen = isonum_711(p->h.length) - 5;
273 inbuf = (char *)p + 5;
277 printf("RRIP with incorrect NM flags?\n");
278 wlen = ana->maxlen + 1;
282 if ((*ana->outlen += wlen) > ana->maxlen) {
283 /* treat as no name field */
284 ana->fields &= ~ISO_SUSP_ALTNAME;
285 ana->outbuf -= *ana->outlen - wlen;
290 bcopy(inbuf,ana->outbuf,wlen);
294 ana->fields &= ~ISO_SUSP_ALTNAME;
295 return ISO_SUSP_ALTNAME;
301 cd9660_rrip_defname(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana)
303 isofntrans(isodir->name,isonum_711(isodir->name_len),
304 ana->outbuf,ana->outlen,
305 1,isonum_711(isodir->flags)&4, ana->imp->joliet_level);
306 switch (*ana->outbuf) {
313 /* outlen is 1 already */
314 strcpy(ana->outbuf,"..");
320 * Parent or Child Link
323 cd9660_rrip_pclink(ISO_RRIP_CLINK *p, ISO_RRIP_ANALYZE *ana)
325 *ana->inump = isonum_733(p->dir_loc) << ana->imp->im_bshift;
326 ana->fields &= ~(ISO_SUSP_CLINK|ISO_SUSP_PLINK);
327 return *p->h.type == 'C' ? ISO_SUSP_CLINK : ISO_SUSP_PLINK;
331 * Relocated directory
334 cd9660_rrip_reldir(ISO_RRIP_RELDIR *p, ISO_RRIP_ANALYZE *ana)
336 /* special hack to make caller aware of RE field */
339 return ISO_SUSP_RELDIR|ISO_SUSP_ALTNAME|ISO_SUSP_CLINK|ISO_SUSP_PLINK;
343 cd9660_rrip_tstamp(ISO_RRIP_TSTAMP *p, ISO_RRIP_ANALYZE *ana)
349 /* Check a format of time stamp (7bytes/17bytes) */
350 if (!(*p->flags&ISO_SUSP_TSTAMP_FORM17)) {
351 if (*p->flags&ISO_SUSP_TSTAMP_CREAT)
354 if (*p->flags&ISO_SUSP_TSTAMP_MODIFY) {
355 cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_mtime,
359 bzero(&ana->inop->inode.iso_mtime,sizeof(struct timespec));
361 if (*p->flags&ISO_SUSP_TSTAMP_ACCESS) {
362 cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_atime,
366 ana->inop->inode.iso_atime = ana->inop->inode.iso_mtime;
368 if (*p->flags&ISO_SUSP_TSTAMP_ATTR)
369 cd9660_tstamp_conv7(ptime,&ana->inop->inode.iso_ctime,
372 ana->inop->inode.iso_ctime = ana->inop->inode.iso_mtime;
375 if (*p->flags&ISO_SUSP_TSTAMP_CREAT)
378 if (*p->flags&ISO_SUSP_TSTAMP_MODIFY) {
379 cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_mtime);
382 bzero(&ana->inop->inode.iso_mtime,sizeof(struct timespec));
384 if (*p->flags&ISO_SUSP_TSTAMP_ACCESS) {
385 cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_atime);
388 ana->inop->inode.iso_atime = ana->inop->inode.iso_mtime;
390 if (*p->flags&ISO_SUSP_TSTAMP_ATTR)
391 cd9660_tstamp_conv17(ptime,&ana->inop->inode.iso_ctime);
393 ana->inop->inode.iso_ctime = ana->inop->inode.iso_mtime;
396 ana->fields &= ~ISO_SUSP_TSTAMP;
397 return ISO_SUSP_TSTAMP;
401 cd9660_rrip_deftstamp(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana)
403 cd9660_deftstamp(isodir,ana->inop,NULL,ISO_FTYPE_RRIP);
410 cd9660_rrip_device(ISO_RRIP_DEVICE *p, ISO_RRIP_ANALYZE *ana)
414 high = isonum_733(p->dev_t_high);
415 low = isonum_733(p->dev_t_low);
418 ana->inop->inode.iso_rdev = makeudev(umajor(low), uminor(low));
420 ana->inop->inode.iso_rdev = makeudev(high, uminor(low));
421 ana->fields &= ~ISO_SUSP_DEVICE;
422 return ISO_SUSP_DEVICE;
429 cd9660_rrip_idflag(ISO_RRIP_IDFLAG *p, ISO_RRIP_ANALYZE *ana)
431 ana->fields &= isonum_711(p->flags)|~0xff; /* don't touch high bits */
432 /* special handling of RE field */
433 if (ana->fields&ISO_SUSP_RELDIR)
434 return cd9660_rrip_reldir(/* XXX */ (ISO_RRIP_RELDIR *)p,ana);
436 return ISO_SUSP_IDFLAG;
440 * Continuation pointer
443 cd9660_rrip_cont(ISO_RRIP_CONT *p, ISO_RRIP_ANALYZE *ana)
445 ana->iso_ce_blk = isonum_733(p->location);
446 ana->iso_ce_off = isonum_733(p->offset);
447 ana->iso_ce_len = isonum_733(p->length);
448 return ISO_SUSP_CONT;
455 cd9660_rrip_stop(ISO_SUSP_HEADER *p, ISO_RRIP_ANALYZE *ana)
457 return ISO_SUSP_STOP;
461 * Extension reference
464 cd9660_rrip_extref(ISO_RRIP_EXTREF *p, ISO_RRIP_ANALYZE *ana)
466 if (isonum_711(p->len_id) != 10
467 || bcmp((char *)p + 8,"RRIP_1991A",10)
468 || isonum_711(p->version) != 1)
470 ana->fields &= ~ISO_SUSP_EXTREF;
471 return ISO_SUSP_EXTREF;
475 cd9660_rrip_loop(struct iso_directory_record *isodir, ISO_RRIP_ANALYZE *ana,
479 ISO_SUSP_HEADER *phead;
480 ISO_SUSP_HEADER *pend;
481 struct buf *bp = NULL;
487 * Note: If name length is odd,
488 * it will be padding 1 byte after the name
490 pwhead = isodir->name + isonum_711(isodir->name_len);
491 if (!(isonum_711(isodir->name_len)&1))
493 isochar(isodir->name, pwhead, ana->imp->joliet_level, &c);
495 /* If it's not the '.' entry of the root dir obey SP field */
496 if (c != 0 || isonum_733(isodir->extent) != ana->imp->root_extent)
497 pwhead += ana->imp->rr_skip;
499 pwhead += ana->imp->rr_skip0;
501 phead = (ISO_SUSP_HEADER *)pwhead;
502 pend = (ISO_SUSP_HEADER *)((char *)isodir + isonum_711(isodir->length));
508 * Note: "pend" should be more than one SUSP header
510 while (pend >= phead + 1) {
511 if (isonum_711(phead->version) == 1) {
512 for (ptable = table; ptable->func; ptable++) {
513 if (*phead->type == *ptable->type
514 && phead->type[1] == ptable->type[1]) {
515 result |= ptable->func(phead,ana);
522 if (result&ISO_SUSP_STOP) {
523 result &= ~ISO_SUSP_STOP;
526 /* plausibility check */
527 if (isonum_711(phead->length) < sizeof(*phead))
531 * Hopefully this works with newer versions, too
533 phead = (ISO_SUSP_HEADER *)((char *)phead + isonum_711(phead->length));
536 if (ana->fields && ana->iso_ce_len) {
537 if (ana->iso_ce_blk >= ana->imp->volume_space_size
538 || ana->iso_ce_off + ana->iso_ce_len > ana->imp->logical_block_size
539 || bread(ana->imp->im_devvp,
541 (ana->imp->im_bshift - DEV_BSHIFT),
542 ana->imp->logical_block_size, &bp))
543 /* what to do now? */
545 phead = (ISO_SUSP_HEADER *)(bp->b_data + ana->iso_ce_off);
546 pend = (ISO_SUSP_HEADER *) ((char *)phead + ana->iso_ce_len);
553 * If we don't find the Basic SUSP stuffs, just set default value
554 * (attribute/time stamp)
556 for (ptable = table; ptable->func2; ptable++)
557 if (!(ptable->result&result))
558 ptable->func2(isodir,ana);
567 * XXX the casts are bogus but will do for now.
569 #define BC (rrt_func_t *)
570 static RRIP_TABLE rrip_table_analyze[] = {
571 { "PX", BC cd9660_rrip_attr, cd9660_rrip_defattr, ISO_SUSP_ATTR },
572 { "TF", BC cd9660_rrip_tstamp, cd9660_rrip_deftstamp, ISO_SUSP_TSTAMP },
573 { "PN", BC cd9660_rrip_device, 0, ISO_SUSP_DEVICE },
574 { "RR", BC cd9660_rrip_idflag, 0, ISO_SUSP_IDFLAG },
575 { "CE", BC cd9660_rrip_cont, 0, ISO_SUSP_CONT },
576 { "ST", BC cd9660_rrip_stop, 0, ISO_SUSP_STOP },
581 cd9660_rrip_analyze(struct iso_directory_record *isodir, struct iso_node *inop,
584 ISO_RRIP_ANALYZE analyze;
588 analyze.fields = ISO_SUSP_ATTR|ISO_SUSP_TSTAMP|ISO_SUSP_DEVICE;
590 return cd9660_rrip_loop(isodir,&analyze,rrip_table_analyze);
594 * Get Alternate Name.
596 static RRIP_TABLE rrip_table_getname[] = {
597 { "NM", BC cd9660_rrip_altname, cd9660_rrip_defname, ISO_SUSP_ALTNAME },
598 { "CL", BC cd9660_rrip_pclink, 0, ISO_SUSP_CLINK|ISO_SUSP_PLINK },
599 { "PL", BC cd9660_rrip_pclink, 0, ISO_SUSP_CLINK|ISO_SUSP_PLINK },
600 { "RE", BC cd9660_rrip_reldir, 0, ISO_SUSP_RELDIR },
601 { "RR", BC cd9660_rrip_idflag, 0, ISO_SUSP_IDFLAG },
602 { "CE", BC cd9660_rrip_cont, 0, ISO_SUSP_CONT },
603 { "ST", BC cd9660_rrip_stop, 0, ISO_SUSP_STOP },
608 cd9660_rrip_getname(struct iso_directory_record *isodir, char *outbuf,
609 u_short *outlen, ino_t *inump, struct iso_mnt *imp)
611 ISO_RRIP_ANALYZE analyze;
615 analyze.outbuf = outbuf;
616 analyze.outlen = outlen;
617 analyze.maxlen = NAME_MAX;
618 analyze.inump = inump;
620 analyze.fields = ISO_SUSP_ALTNAME|ISO_SUSP_RELDIR|ISO_SUSP_CLINK|ISO_SUSP_PLINK;
623 isochar(isodir->name, isodir->name + isonum_711(isodir->name_len),
624 imp->joliet_level, &c);
625 tab = rrip_table_getname;
626 if (c == 0 || c == 1) {
627 cd9660_rrip_defname(isodir,&analyze);
629 analyze.fields &= ~ISO_SUSP_ALTNAME;
633 return cd9660_rrip_loop(isodir,&analyze,tab);
639 static RRIP_TABLE rrip_table_getsymname[] = {
640 { "SL", BC cd9660_rrip_slink, 0, ISO_SUSP_SLINK },
641 { "RR", BC cd9660_rrip_idflag, 0, ISO_SUSP_IDFLAG },
642 { "CE", BC cd9660_rrip_cont, 0, ISO_SUSP_CONT },
643 { "ST", BC cd9660_rrip_stop, 0, ISO_SUSP_STOP },
648 cd9660_rrip_getsymname(struct iso_directory_record *isodir, char *outbuf,
649 u_short *outlen, struct iso_mnt *imp)
651 ISO_RRIP_ANALYZE analyze;
653 analyze.outbuf = outbuf;
654 analyze.outlen = outlen;
656 analyze.maxlen = MAXPATHLEN;
657 analyze.cont = 1; /* don't start with a slash */
659 analyze.fields = ISO_SUSP_SLINK;
661 return (cd9660_rrip_loop(isodir,&analyze,rrip_table_getsymname)&ISO_SUSP_SLINK);
664 static RRIP_TABLE rrip_table_extref[] = {
665 { "ER", BC cd9660_rrip_extref, 0, ISO_SUSP_EXTREF },
666 { "CE", BC cd9660_rrip_cont, 0, ISO_SUSP_CONT },
667 { "ST", BC cd9660_rrip_stop, 0, ISO_SUSP_STOP },
672 * Check for Rock Ridge Extension and return offset of its fields.
673 * Note: We insist on the ER field.
676 cd9660_rrip_offset(struct iso_directory_record *isodir, struct iso_mnt *imp)
679 ISO_RRIP_ANALYZE analyze;
682 p = (ISO_RRIP_OFFSET *)(isodir->name + 1);
683 if (bcmp(p,"SP\7\1\276\357",6)) {
684 /* Maybe, it's a CDROM XA disc? */
686 p = (ISO_RRIP_OFFSET *)((char *)p + 15);
687 if (bcmp(p,"SP\7\1\276\357",6))
692 analyze.fields = ISO_SUSP_EXTREF;
693 if (!(cd9660_rrip_loop(isodir,&analyze,rrip_table_extref)&ISO_SUSP_EXTREF))
696 return isonum_711(p->skip);