2 /* $NetBSD: cd9660.c,v 1.5 1997/06/26 19:11:33 drochner Exp $ */
5 * Copyright (C) 1996 Wolfgang Solfrank.
6 * Copyright (C) 1996 TooLs GmbH.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by TooLs GmbH.
20 * 4. The name of TooLs GmbH may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Stand-alone ISO9660 file reading package.
38 * Note: This doesn't support Rock Ridge extensions, extended attributes,
39 * blocksizes other than 2048 bytes, multi-extent files, etc.
41 #include <sys/param.h>
43 #include <sys/dirent.h>
44 #include <isofs/cd9660/iso.h>
48 static int cd9660_open(const char *path, struct open_file *f);
49 static int cd9660_close(struct open_file *f);
50 static int cd9660_read(struct open_file *f, void *buf, size_t size, size_t *resid);
51 static int cd9660_write(struct open_file *f, void *buf, size_t size, size_t *resid);
52 static off_t cd9660_seek(struct open_file *f, off_t offset, int where);
53 static int cd9660_stat(struct open_file *f, struct stat *sb);
54 static int cd9660_readdir(struct open_file *f, struct dirent *d);
56 struct fs_ops cd9660_fsops = {
68 int f_isdir; /* nonzero if file is directory */
69 off_t f_off; /* Current offset within file */
70 daddr_t f_bno; /* Starting block number */
71 off_t f_size; /* Size of file */
72 daddr_t f_buf_blkno; /* block number of data block */
73 char *f_buf; /* buffer for data block */
77 char namlen [ISODCL( 1, 1)]; /* 711 */
78 char extlen [ISODCL( 2, 2)]; /* 711 */
79 char block [ISODCL( 3, 6)]; /* 732 */
80 char parent [ISODCL( 7, 8)]; /* 722 */
84 #define PTSIZE(pp) roundup(PTFIXSZ + isonum_711((pp)->namlen), 2)
86 #define cdb2devb(bno) ((bno) * ISO_DEFAULT_BLOCK_SIZE / DEV_BSIZE)
88 /* XXX these should be in the system headers */
93 return (*p << 8)|p[1];
100 return (*p << 24)|(p[1] << 16)|(p[2] << 8)|p[3];
106 struct iso_directory_record *dp;
112 for (i = isonum_711(dp->name_len); --i >= 0; path++, cp++) {
113 if (!*path || *path == '/')
115 if (toupper(*path) == *cp)
119 if (*path && *path != '/')
122 * Allow stripping of trailing dots and the version number.
123 * Note that this will find the first instead of the last version
126 if (i >= 0 && (*cp == ';' || *cp == '.')) {
127 /* This is to prevent matching of numeric extensions */
128 if (*cp == '.' && cp[1] != ';')
131 if (*++cp != ';' && (*cp < '0' || *cp > '9'))
144 struct iso_primary_descriptor *vd;
145 size_t buf_size, read, dsize, off;
147 struct iso_directory_record rec;
148 struct iso_directory_record *dp = 0;
151 /* First find the volume descriptor */
152 buf = malloc(buf_size = ISO_DEFAULT_BLOCK_SIZE);
154 for (bno = 16;; bno++) {
156 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ, cdb2devb(bno),
157 ISO_DEFAULT_BLOCK_SIZE, buf, &read);
160 if (read != ISO_DEFAULT_BLOCK_SIZE) {
165 if (bcmp(vd->id, ISO_STANDARD_ID, sizeof vd->id) != 0)
167 if (isonum_711(vd->type) == ISO_VD_END)
169 if (isonum_711(vd->type) == ISO_VD_PRIMARY)
172 if (isonum_723(vd->logical_block_size) != ISO_DEFAULT_BLOCK_SIZE)
175 rec = *(struct iso_directory_record *) vd->root_directory_record;
176 if (*path == '/') path++; /* eat leading '/' */
179 bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
180 dsize = isonum_733(rec.size);
184 while (off < dsize) {
185 if ((off % ISO_DEFAULT_BLOCK_SIZE) == 0) {
187 rc = f->f_dev->dv_strategy
188 (f->f_devdata, F_READ,
189 cdb2devb(bno + boff),
190 ISO_DEFAULT_BLOCK_SIZE,
194 if (read != ISO_DEFAULT_BLOCK_SIZE) {
199 dp = (struct iso_directory_record *) buf;
201 if (isonum_711(dp->length) == 0) {
202 /* skip to next block, if any */
203 off = boff * ISO_DEFAULT_BLOCK_SIZE;
207 if (dirmatch(path, dp))
210 dp = (struct iso_directory_record *)
211 ((char *) dp + isonum_711(dp->length));
212 off += isonum_711(dp->length);
220 while (*path && *path != '/') /* look for next component */
222 if (*path) path++; /* skip '/' */
225 /* allocate file system specific data structure */
226 fp = malloc(sizeof(struct file));
227 bzero(fp, sizeof(struct file));
228 f->f_fsdata = (void *)fp;
230 fp->f_isdir = (isonum_711(rec.flags) & 2) != 0;
232 fp->f_bno = isonum_733(rec.extent) + isonum_711(rec.ext_attr_length);
233 fp->f_size = isonum_733(rec.size);
250 struct file *fp = (struct file *)f->f_fsdata;
259 buf_read_file(f, buf_p, size_p)
264 struct file *fp = (struct file *)f->f_fsdata;
265 daddr_t blkno, blkoff;
269 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE + fp->f_bno;
270 blkoff = fp->f_off % ISO_DEFAULT_BLOCK_SIZE;
272 if (blkno != fp->f_buf_blkno) {
273 if (fp->f_buf == (char *)0)
274 fp->f_buf = malloc(ISO_DEFAULT_BLOCK_SIZE);
277 rc = f->f_dev->dv_strategy(f->f_devdata, F_READ,
278 cdb2devb(blkno), ISO_DEFAULT_BLOCK_SIZE, fp->f_buf, &read);
281 if (read != ISO_DEFAULT_BLOCK_SIZE)
284 fp->f_buf_blkno = blkno;
287 *buf_p = fp->f_buf + blkoff;
288 *size_p = ISO_DEFAULT_BLOCK_SIZE - blkoff;
290 if (*size_p > fp->f_size - fp->f_off)
291 *size_p = fp->f_size - fp->f_off;
296 cd9660_read(f, start, size, resid)
302 struct file *fp = (struct file *)f->f_fsdata;
304 size_t buf_size, csize;
309 if (fp->f_off < 0 || fp->f_off >= fp->f_size)
312 rc = buf_read_file(f, &buf, &buf_size);
316 csize = size > buf_size ? buf_size : size;
317 bcopy(buf, addr, csize);
329 cd9660_readdir(struct open_file *f, struct dirent *d)
331 struct file *fp = (struct file *)f->f_fsdata;
332 struct iso_directory_record *ep;
333 size_t buf_size, reclen, namelen;
338 if (fp->f_off >= fp->f_size)
340 error = buf_read_file(f, &buf, &buf_size);
343 ep = (struct iso_directory_record *)buf;
345 if (isonum_711(ep->length) == 0) {
348 /* skip to next block, if any */
349 blkno = fp->f_off / ISO_DEFAULT_BLOCK_SIZE;
350 fp->f_off = (blkno + 1) * ISO_DEFAULT_BLOCK_SIZE;
354 namelen = isonum_711(ep->name_len);
355 if (namelen == 1 && ep->name[0] == 1)
357 reclen = sizeof(struct dirent) - (MAXNAMLEN+1) + namelen + 1;
358 reclen = (reclen + 3) & ~3;
360 d->d_fileno = isonum_733(ep->extent);
361 d->d_reclen = reclen;
362 if (isonum_711(ep->flags) & 2)
366 d->d_namlen = namelen;
368 if (isonum_711(ep->name_len) == 1 && ep->name[0] == 0)
369 strcpy(d->d_name, ".");
370 else if (isonum_711(ep->name_len) == 1 && ep->name[0] == 1)
371 strcpy(d->d_name, "..");
373 bcopy(ep->name, d->d_name, d->d_namlen);
374 d->d_name[d->d_namlen] = 0;
376 fp->f_off += isonum_711(ep->length);
381 cd9660_write(f, start, size, resid)
391 cd9660_seek(f, offset, where)
396 struct file *fp = (struct file *)f->f_fsdata;
406 fp->f_off = fp->f_size - offset;
419 struct file *fp = (struct file *)f->f_fsdata;
421 /* only important stuff */
422 sb->st_mode = S_IRUSR | S_IRGRP | S_IROTH;
424 sb->st_mode |= S_IFDIR;
426 sb->st_mode |= S_IFREG;
427 sb->st_uid = sb->st_gid = 0;
428 sb->st_size = fp->f_size;