[48674] | 1 | /* $Id: fsw_iso9660.c 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * fsw_iso9660.c - ISO9660 file system driver code.
|
---|
| 4 | *
|
---|
| 5 | * Current limitations:
|
---|
| 6 | * - Files must be in one extent (i.e. Level 2)
|
---|
| 7 | * - No Joliet or Rock Ridge extensions
|
---|
| 8 | * - No interleaving
|
---|
| 9 | * - inode number generation strategy fails on volumes > 2 GB
|
---|
| 10 | * - No blocksizes != 2048
|
---|
| 11 | * - No High Sierra or anything else != 'CD001'
|
---|
| 12 | * - No volume sets with directories pointing at other volumes
|
---|
| 13 | * - No extended attribute records
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | /*
|
---|
[98103] | 17 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
[48674] | 18 | *
|
---|
[96407] | 19 | * This file is part of VirtualBox base platform packages, as
|
---|
| 20 | * available from https://www.virtualbox.org.
|
---|
[48674] | 21 | *
|
---|
[96407] | 22 | * This program is free software; you can redistribute it and/or
|
---|
| 23 | * modify it under the terms of the GNU General Public License
|
---|
| 24 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 25 | * License.
|
---|
| 26 | *
|
---|
| 27 | * This program is distributed in the hope that it will be useful, but
|
---|
| 28 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 30 | * General Public License for more details.
|
---|
| 31 | *
|
---|
| 32 | * You should have received a copy of the GNU General Public License
|
---|
| 33 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 34 | *
|
---|
[48674] | 35 | * The contents of this file may alternatively be used under the terms
|
---|
| 36 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 37 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 38 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[48674] | 39 | * CDDL are applicable instead of those of the GPL.
|
---|
| 40 | *
|
---|
| 41 | * You may elect to license modified versions of this file under the
|
---|
| 42 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 43 | *
|
---|
| 44 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[69498] | 45 | * ---------------------------------------------------------------------------
|
---|
[48674] | 46 | * This code is based on:
|
---|
| 47 | *
|
---|
| 48 | * Copyright (c) 2006 Christoph Pfisterer
|
---|
| 49 | *
|
---|
| 50 | * Redistribution and use in source and binary forms, with or without
|
---|
| 51 | * modification, are permitted provided that the following conditions are
|
---|
| 52 | * met:
|
---|
| 53 | *
|
---|
| 54 | * * Redistributions of source code must retain the above copyright
|
---|
| 55 | * notice, this list of conditions and the following disclaimer.
|
---|
| 56 | *
|
---|
| 57 | * * Redistributions in binary form must reproduce the above copyright
|
---|
| 58 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 59 | * documentation and/or other materials provided with the
|
---|
| 60 | * distribution.
|
---|
| 61 | *
|
---|
| 62 | * * Neither the name of Christoph Pfisterer nor the names of the
|
---|
| 63 | * contributors may be used to endorse or promote products derived
|
---|
| 64 | * from this software without specific prior written permission.
|
---|
| 65 | *
|
---|
| 66 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
| 67 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
| 68 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
| 69 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
| 70 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
| 71 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
| 72 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 73 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 74 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 75 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
| 76 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 77 | */
|
---|
| 78 |
|
---|
| 79 | #include "fsw_iso9660.h"
|
---|
| 80 |
|
---|
| 81 |
|
---|
| 82 | // functions
|
---|
| 83 |
|
---|
| 84 | static fsw_status_t fsw_iso9660_volume_mount(struct fsw_iso9660_volume *vol);
|
---|
| 85 | static void fsw_iso9660_volume_free(struct fsw_iso9660_volume *vol);
|
---|
| 86 | static fsw_status_t fsw_iso9660_volume_stat(struct fsw_iso9660_volume *vol, struct fsw_volume_stat *sb);
|
---|
| 87 |
|
---|
| 88 | static fsw_status_t fsw_iso9660_dnode_fill(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno);
|
---|
| 89 | static void fsw_iso9660_dnode_free(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno);
|
---|
| 90 | static fsw_status_t fsw_iso9660_dnode_stat(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 91 | struct fsw_dnode_stat *sb);
|
---|
| 92 | static fsw_status_t fsw_iso9660_get_extent(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 93 | struct fsw_extent *extent);
|
---|
| 94 |
|
---|
| 95 | static fsw_status_t fsw_iso9660_dir_lookup(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 96 | struct fsw_string *lookup_name, struct fsw_iso9660_dnode **child_dno);
|
---|
| 97 | static fsw_status_t fsw_iso9660_dir_read(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 98 | struct fsw_shandle *shand, struct fsw_iso9660_dnode **child_dno);
|
---|
| 99 | static fsw_status_t fsw_iso9660_read_dirrec(struct fsw_iso9660_volume *vol, struct fsw_shandle *shand, struct iso9660_dirrec_buffer *dirrec_buffer);
|
---|
| 100 |
|
---|
| 101 | static fsw_status_t fsw_iso9660_readlink(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 102 | struct fsw_string *link);
|
---|
| 103 |
|
---|
| 104 | static fsw_status_t rr_find_sp(struct iso9660_dirrec *dirrec, struct fsw_rock_ridge_susp_sp **psp);
|
---|
| 105 | static fsw_status_t rr_find_nm(struct fsw_iso9660_volume *vol, struct iso9660_dirrec *dirrec, int off, struct fsw_string *str);
|
---|
| 106 | static fsw_status_t rr_read_ce(struct fsw_iso9660_volume *vol, union fsw_rock_ridge_susp_ce *ce, fsw_u8 *begin);
|
---|
| 107 | //
|
---|
| 108 | // Dispatch Table
|
---|
| 109 | //
|
---|
| 110 |
|
---|
| 111 | struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(iso9660) = {
|
---|
| 112 | { FSW_STRING_TYPE_ISO88591, 4, 4, "iso9660" },
|
---|
| 113 | sizeof(struct fsw_iso9660_volume),
|
---|
| 114 | sizeof(struct fsw_iso9660_dnode),
|
---|
| 115 |
|
---|
| 116 | fsw_iso9660_volume_mount,
|
---|
| 117 | fsw_iso9660_volume_free,
|
---|
| 118 | fsw_iso9660_volume_stat,
|
---|
| 119 | fsw_iso9660_dnode_fill,
|
---|
| 120 | fsw_iso9660_dnode_free,
|
---|
| 121 | fsw_iso9660_dnode_stat,
|
---|
| 122 | fsw_iso9660_get_extent,
|
---|
| 123 | fsw_iso9660_dir_lookup,
|
---|
| 124 | fsw_iso9660_dir_read,
|
---|
| 125 | fsw_iso9660_readlink,
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | static fsw_status_t rr_find_sp(struct iso9660_dirrec *dirrec, struct fsw_rock_ridge_susp_sp **psp)
|
---|
| 129 | {
|
---|
| 130 | fsw_u8 *r;
|
---|
| 131 | int off = 0;
|
---|
| 132 | struct fsw_rock_ridge_susp_sp *sp;
|
---|
| 133 | r = (fsw_u8 *)((fsw_u8 *)dirrec + sizeof(*dirrec) + dirrec->file_identifier_length);
|
---|
| 134 | off = (int)(r - (fsw_u8 *)dirrec);
|
---|
| 135 | while(off < dirrec->dirrec_length)
|
---|
| 136 | {
|
---|
| 137 | if (*r == 'S')
|
---|
| 138 | {
|
---|
| 139 | sp = (struct fsw_rock_ridge_susp_sp *)r;
|
---|
| 140 | if( sp->e.sig[0] == 'S'
|
---|
| 141 | && sp->e.sig[1] == 'P'
|
---|
| 142 | && sp->magic[0] == 0xbe
|
---|
| 143 | && sp->magic[1] == 0xef)
|
---|
| 144 | {
|
---|
| 145 | *psp = sp;
|
---|
| 146 | return FSW_SUCCESS;
|
---|
| 147 | }
|
---|
| 148 | }
|
---|
| 149 | r++;
|
---|
| 150 | off = (int)(r - (fsw_u8 *)dirrec);
|
---|
| 151 | }
|
---|
| 152 | *psp = NULL;
|
---|
| 153 | return FSW_NOT_FOUND;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | static fsw_status_t rr_find_nm(struct fsw_iso9660_volume *vol, struct iso9660_dirrec *dirrec, int off, struct fsw_string *str)
|
---|
| 157 | {
|
---|
| 158 | fsw_u8 *r, *begin;
|
---|
| 159 | int fCe = 0;
|
---|
| 160 | struct fsw_rock_ridge_susp_nm *nm;
|
---|
| 161 | int limit = dirrec->dirrec_length;
|
---|
| 162 | begin = (fsw_u8 *)dirrec;
|
---|
| 163 | r = (fsw_u8 *)dirrec + off;
|
---|
| 164 | str->data = NULL;
|
---|
| 165 | str->len = 0;
|
---|
| 166 | str->size = 0;
|
---|
| 167 | str->type = 0;
|
---|
| 168 | while(off < limit)
|
---|
| 169 | {
|
---|
| 170 | if (r[0] == 'C' && r[1] == 'E' && r[2] == 28)
|
---|
| 171 | {
|
---|
| 172 | int rc;
|
---|
| 173 | int ce_off;
|
---|
| 174 | union fsw_rock_ridge_susp_ce *ce;
|
---|
| 175 | if (fCe == 0)
|
---|
| 176 | fsw_alloc_zero(ISO9660_BLOCKSIZE, (void *)&begin);
|
---|
| 177 | fCe = 1;
|
---|
| 178 | DEBUG((DEBUG_WARN, "%a:%d we found CE before NM or its continuation\n", __FILE__, __LINE__));
|
---|
| 179 | ce = (union fsw_rock_ridge_susp_ce *)r;
|
---|
| 180 | limit = ISOINT(ce->X.len);
|
---|
| 181 | ce_off = ISOINT(ce->X.offset);
|
---|
| 182 | rc = rr_read_ce(vol, ce, begin);
|
---|
| 183 | if (rc != FSW_SUCCESS)
|
---|
| 184 | {
|
---|
| 185 | fsw_free(begin);
|
---|
| 186 | return rc;
|
---|
| 187 | }
|
---|
| 188 | begin += ce_off;
|
---|
| 189 | r = begin;
|
---|
| 190 | }
|
---|
| 191 | if (r[0] == 'N' && r[1] == 'M')
|
---|
| 192 | {
|
---|
| 193 | nm = (struct fsw_rock_ridge_susp_nm *)r;
|
---|
| 194 | if( nm->e.sig[0] == 'N'
|
---|
| 195 | && nm->e.sig[1] == 'M')
|
---|
| 196 | {
|
---|
| 197 | int len = 0;
|
---|
| 198 | fsw_u8 *tmp = NULL;
|
---|
| 199 | if (nm->flags & RR_NM_CURR)
|
---|
| 200 | {
|
---|
| 201 | fsw_memdup(str->data, ".", 1);
|
---|
| 202 | str->len = 1;
|
---|
| 203 | goto done;
|
---|
| 204 | }
|
---|
| 205 | if (nm->flags & RR_NM_PARE)
|
---|
| 206 | {
|
---|
| 207 | fsw_memdup(str->data, "..", 2);
|
---|
| 208 | str->len = 2;
|
---|
| 209 | goto done;
|
---|
| 210 | }
|
---|
| 211 | len = nm->e.len - sizeof(struct fsw_rock_ridge_susp_nm) + 1;
|
---|
| 212 | fsw_alloc_zero(str->len + len, (void **)&tmp);
|
---|
| 213 | if (str->data != NULL)
|
---|
| 214 | {
|
---|
| 215 | fsw_memcpy(tmp, str->data, str->len);
|
---|
| 216 | fsw_free(str->data);
|
---|
| 217 | }
|
---|
| 218 | DEBUG((DEBUG_INFO, "dst:%p src:%p len:%d\n", tmp + str->len, &nm->name[0], len));
|
---|
| 219 | fsw_memcpy(tmp + str->len, &nm->name[0], len);
|
---|
| 220 | str->data = tmp;
|
---|
| 221 | str->len += len;
|
---|
| 222 |
|
---|
[62239] | 223 | if ((nm->flags & RR_NM_CONT) == 0)
|
---|
[48674] | 224 | goto done;
|
---|
| 225 | }
|
---|
| 226 | }
|
---|
| 227 | r++;
|
---|
| 228 | off = (int)(r - (fsw_u8 *)begin);
|
---|
| 229 | }
|
---|
| 230 | if(fCe == 1)
|
---|
| 231 | fsw_free(begin);
|
---|
| 232 | return FSW_NOT_FOUND;
|
---|
| 233 | done:
|
---|
| 234 | str->type = FSW_STRING_TYPE_ISO88591;
|
---|
| 235 | str->size = str->len;
|
---|
| 236 | if(fCe == 1)
|
---|
| 237 | fsw_free(begin);
|
---|
| 238 | return FSW_SUCCESS;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | static fsw_status_t rr_read_ce(struct fsw_iso9660_volume *vol, union fsw_rock_ridge_susp_ce *ce, fsw_u8 *begin)
|
---|
| 242 | {
|
---|
| 243 | int rc;
|
---|
| 244 | rc = vol->g.host_table->read_block(&vol->g, ISOINT(ce->X.block_loc), begin);
|
---|
| 245 | if (rc != FSW_SUCCESS)
|
---|
| 246 | return rc;
|
---|
| 247 | return FSW_SUCCESS;
|
---|
| 248 | }
|
---|
| 249 | /**
|
---|
| 250 | * Mount an ISO9660 volume. Reads the superblock and constructs the
|
---|
| 251 | * root directory dnode.
|
---|
| 252 | */
|
---|
| 253 |
|
---|
| 254 | static fsw_status_t fsw_iso9660_volume_mount(struct fsw_iso9660_volume *vol)
|
---|
| 255 | {
|
---|
| 256 | fsw_status_t status;
|
---|
| 257 | void *buffer;
|
---|
| 258 | fsw_u32 blockno;
|
---|
| 259 | struct iso9660_volume_descriptor *voldesc;
|
---|
| 260 | struct iso9660_primary_volume_descriptor *pvoldesc;
|
---|
| 261 | fsw_u32 voldesc_type;
|
---|
| 262 | int i;
|
---|
| 263 | struct fsw_string s;
|
---|
| 264 | struct iso9660_dirrec rootdir;
|
---|
| 265 | int sua_pos;
|
---|
| 266 | char *sig;
|
---|
| 267 | int skip;
|
---|
| 268 | struct fsw_rock_ridge_susp_entry *entry;
|
---|
| 269 |
|
---|
| 270 | // read through the Volume Descriptor Set
|
---|
| 271 | fsw_set_blocksize(vol, ISO9660_BLOCKSIZE, ISO9660_BLOCKSIZE);
|
---|
| 272 | blockno = ISO9660_SUPERBLOCK_BLOCKNO;
|
---|
| 273 |
|
---|
| 274 | do {
|
---|
| 275 | status = fsw_block_get(vol, blockno, 0, &buffer);
|
---|
| 276 | if (status)
|
---|
| 277 | return status;
|
---|
| 278 |
|
---|
| 279 | voldesc = (struct iso9660_volume_descriptor *)buffer;
|
---|
| 280 | voldesc_type = voldesc->volume_descriptor_type;
|
---|
| 281 | if (fsw_memeq(voldesc->standard_identifier, "CD001", 5)) {
|
---|
| 282 | // descriptor follows ISO 9660 standard
|
---|
| 283 | if (voldesc_type == 1 && voldesc->volume_descriptor_version == 1) {
|
---|
| 284 | // suitable Primary Volume Descriptor found
|
---|
| 285 | if (vol->primary_voldesc) {
|
---|
| 286 | fsw_free(vol->primary_voldesc);
|
---|
| 287 | vol->primary_voldesc = NULL;
|
---|
| 288 | }
|
---|
| 289 | status = fsw_memdup((void **)&vol->primary_voldesc, voldesc, ISO9660_BLOCKSIZE);
|
---|
| 290 | }
|
---|
| 291 | } else if (!fsw_memeq(voldesc->standard_identifier, "CD", 2)) {
|
---|
| 292 | // completely alien standard identifier, stop reading
|
---|
| 293 | voldesc_type = 255;
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | fsw_block_release(vol, blockno, buffer);
|
---|
| 297 | blockno++;
|
---|
| 298 | } while (!status && voldesc_type != 255);
|
---|
| 299 | if (status)
|
---|
| 300 | return status;
|
---|
| 301 |
|
---|
| 302 | // get information from Primary Volume Descriptor
|
---|
| 303 | if (vol->primary_voldesc == NULL)
|
---|
| 304 | return FSW_UNSUPPORTED;
|
---|
| 305 | pvoldesc = vol->primary_voldesc;
|
---|
| 306 | if (ISOINT(pvoldesc->logical_block_size) != 2048)
|
---|
| 307 | return FSW_UNSUPPORTED;
|
---|
| 308 |
|
---|
| 309 | // get volume name
|
---|
| 310 | for (i = 32; i > 0; i--)
|
---|
| 311 | if (pvoldesc->volume_identifier[i-1] != ' ')
|
---|
| 312 | break;
|
---|
| 313 | s.type = FSW_STRING_TYPE_ISO88591;
|
---|
| 314 | s.size = s.len = i;
|
---|
| 315 | s.data = pvoldesc->volume_identifier;
|
---|
| 316 | status = fsw_strdup_coerce(&vol->g.label, vol->g.host_string_type, &s);
|
---|
| 317 | if (status)
|
---|
| 318 | return status;
|
---|
| 319 |
|
---|
| 320 | // setup the root dnode
|
---|
| 321 | status = fsw_dnode_create_root(vol, ISO9660_SUPERBLOCK_BLOCKNO << ISO9660_BLOCKSIZE_BITS, &vol->g.root);
|
---|
| 322 | if (status)
|
---|
| 323 | return status;
|
---|
| 324 | fsw_memcpy(&vol->g.root->dirrec, &pvoldesc->root_directory, sizeof(struct iso9660_dirrec));
|
---|
| 325 |
|
---|
| 326 | if ( pvoldesc->escape[0] == 0x25
|
---|
| 327 | && pvoldesc->escape[1] == 0x2f
|
---|
| 328 | && ( pvoldesc->escape[2] == 0x40
|
---|
| 329 | || pvoldesc->escape[2] == 0x43
|
---|
| 330 | || pvoldesc->escape[2] == 0x45))
|
---|
| 331 | {
|
---|
| 332 | FSW_MSG_DEBUG((FSW_MSGSTR("fsw_iso9660_volume_mount: success (joliet!!!)\n")));
|
---|
| 333 | vol->fJoliet = 1;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 |
|
---|
| 337 | fsw_memcpy(&rootdir, &pvoldesc->root_directory, sizeof(rootdir));
|
---|
| 338 | sua_pos = (sizeof(struct iso9660_dirrec)) + rootdir.file_identifier_length + (rootdir.file_identifier_length % 2) - 2;
|
---|
| 339 | //int sua_size = rootdir.dirrec_length - rootdir.file_identifier_length;
|
---|
| 340 | //FSW_MSG_DEBUG((FSW_MSGSTR("fsw_iso9660_volume_mount: success (SUA(pos:%x, sz:%d)!!!)\n"), sua_pos, sua_size));
|
---|
| 341 |
|
---|
| 342 | #if 1
|
---|
| 343 | status = fsw_block_get(vol, ISOINT(rootdir.extent_location), 0, &buffer);
|
---|
| 344 | sig = (char *)buffer + sua_pos;
|
---|
| 345 | skip = 0;
|
---|
| 346 | entry = (struct fsw_rock_ridge_susp_entry *)sig;
|
---|
| 347 | if ( entry->sig[0] == 'S'
|
---|
| 348 | && entry->sig[1] == 'P')
|
---|
| 349 | {
|
---|
| 350 | struct fsw_rock_ridge_susp_sp *sp = (struct fsw_rock_ridge_susp_sp *)entry;
|
---|
| 351 | if (sp->magic[0] == 0xbe && sp->magic[1] == 0xef)
|
---|
| 352 | {
|
---|
| 353 | vol->fRockRidge = 1;
|
---|
| 354 | } else {
|
---|
| 355 | FSW_MSG_DEBUG((FSW_MSGSTR("fsw_iso9660_volume_mount: SP magic isn't valid\n")));
|
---|
| 356 | }
|
---|
| 357 | skip = sp->skip;
|
---|
| 358 | }
|
---|
| 359 | #endif
|
---|
| 360 | // release volume descriptors
|
---|
| 361 | fsw_free(vol->primary_voldesc);
|
---|
| 362 | vol->primary_voldesc = NULL;
|
---|
| 363 |
|
---|
| 364 |
|
---|
| 365 | FSW_MSG_DEBUG((FSW_MSGSTR("fsw_iso9660_volume_mount: success\n")));
|
---|
| 366 |
|
---|
| 367 | return FSW_SUCCESS;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | /**
|
---|
| 371 | * Free the volume data structure. Called by the core after an unmount or after
|
---|
| 372 | * an unsuccessful mount to release the memory used by the file system type specific
|
---|
| 373 | * part of the volume structure.
|
---|
| 374 | */
|
---|
| 375 |
|
---|
| 376 | static void fsw_iso9660_volume_free(struct fsw_iso9660_volume *vol)
|
---|
| 377 | {
|
---|
| 378 | if (vol->primary_voldesc)
|
---|
| 379 | fsw_free(vol->primary_voldesc);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | /**
|
---|
| 383 | * Get in-depth information on a volume.
|
---|
| 384 | */
|
---|
| 385 |
|
---|
| 386 | static fsw_status_t fsw_iso9660_volume_stat(struct fsw_iso9660_volume *vol, struct fsw_volume_stat *sb)
|
---|
| 387 | {
|
---|
| 388 | sb->total_bytes = 0; //(fsw_u64)vol->sb->s_blocks_count * vol->g.log_blocksize;
|
---|
| 389 | sb->free_bytes = 0;
|
---|
| 390 | return FSW_SUCCESS;
|
---|
| 391 | }
|
---|
| 392 |
|
---|
| 393 | /**
|
---|
| 394 | * Get full information on a dnode from disk. This function is called by the core
|
---|
| 395 | * whenever it needs to access fields in the dnode structure that may not
|
---|
| 396 | * be filled immediately upon creation of the dnode. In the case of iso9660, we
|
---|
| 397 | * delay fetching of the inode structure until dnode_fill is called. The size and
|
---|
| 398 | * type fields are invalid until this function has been called.
|
---|
| 399 | */
|
---|
| 400 |
|
---|
| 401 | static fsw_status_t fsw_iso9660_dnode_fill(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno)
|
---|
| 402 | {
|
---|
| 403 | // get info from the directory record
|
---|
| 404 | dno->g.size = ISOINT(dno->dirrec.data_length);
|
---|
| 405 | if (dno->dirrec.file_flags & 0x02)
|
---|
| 406 | dno->g.type = FSW_DNODE_TYPE_DIR;
|
---|
| 407 | else
|
---|
| 408 | dno->g.type = FSW_DNODE_TYPE_FILE;
|
---|
| 409 |
|
---|
| 410 | return FSW_SUCCESS;
|
---|
| 411 | }
|
---|
| 412 |
|
---|
| 413 | /**
|
---|
| 414 | * Free the dnode data structure. Called by the core when deallocating a dnode
|
---|
| 415 | * structure to release the memory used by the file system type specific part
|
---|
| 416 | * of the dnode structure.
|
---|
| 417 | */
|
---|
| 418 |
|
---|
| 419 | static void fsw_iso9660_dnode_free(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno)
|
---|
| 420 | {
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /**
|
---|
| 424 | * Get in-depth information on a dnode. The core makes sure that fsw_iso9660_dnode_fill
|
---|
| 425 | * has been called on the dnode before this function is called. Note that some
|
---|
| 426 | * data is not directly stored into the structure, but passed to a host-specific
|
---|
| 427 | * callback that converts it to the host-specific format.
|
---|
| 428 | */
|
---|
| 429 |
|
---|
| 430 | static fsw_status_t fsw_iso9660_dnode_stat(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 431 | struct fsw_dnode_stat *sb)
|
---|
| 432 | {
|
---|
| 433 | sb->used_bytes = (dno->g.size + (ISO9660_BLOCKSIZE-1)) & ~(ISO9660_BLOCKSIZE-1);
|
---|
| 434 | /*
|
---|
| 435 | sb->store_time_posix(sb, FSW_DNODE_STAT_CTIME, dno->raw->i_ctime);
|
---|
| 436 | sb->store_time_posix(sb, FSW_DNODE_STAT_ATIME, dno->raw->i_atime);
|
---|
| 437 | sb->store_time_posix(sb, FSW_DNODE_STAT_MTIME, dno->raw->i_mtime);
|
---|
| 438 | sb->store_attr_posix(sb, dno->raw->i_mode);
|
---|
| 439 | */
|
---|
| 440 |
|
---|
| 441 | return FSW_SUCCESS;
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | /**
|
---|
| 445 | * Retrieve file data mapping information. This function is called by the core when
|
---|
| 446 | * fsw_shandle_read needs to know where on the disk the required piece of the file's
|
---|
| 447 | * data can be found. The core makes sure that fsw_iso9660_dnode_fill has been called
|
---|
| 448 | * on the dnode before. Our task here is to get the physical disk block number for
|
---|
| 449 | * the requested logical block number.
|
---|
| 450 | */
|
---|
| 451 |
|
---|
| 452 | static fsw_status_t fsw_iso9660_get_extent(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 453 | struct fsw_extent *extent)
|
---|
| 454 | {
|
---|
| 455 | // Preconditions: The caller has checked that the requested logical block
|
---|
| 456 | // is within the file's size. The dnode has complete information, i.e.
|
---|
| 457 | // fsw_iso9660_dnode_read_info was called successfully on it.
|
---|
| 458 |
|
---|
| 459 | extent->type = FSW_EXTENT_TYPE_PHYSBLOCK;
|
---|
| 460 | extent->phys_start = ISOINT(dno->dirrec.extent_location);
|
---|
| 461 | extent->log_start = 0;
|
---|
| 462 | extent->log_count = (ISOINT(dno->dirrec.data_length) + (ISO9660_BLOCKSIZE-1)) >> ISO9660_BLOCKSIZE_BITS;
|
---|
| 463 | return FSW_SUCCESS;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 | /**
|
---|
| 467 | * Lookup a directory's child dnode by name. This function is called on a directory
|
---|
| 468 | * to retrieve the directory entry with the given name. A dnode is constructed for
|
---|
| 469 | * this entry and returned. The core makes sure that fsw_iso9660_dnode_fill has been called
|
---|
| 470 | * and the dnode is actually a directory.
|
---|
| 471 | */
|
---|
| 472 |
|
---|
| 473 | static fsw_status_t fsw_iso9660_dir_lookup(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 474 | struct fsw_string *lookup_name, struct fsw_iso9660_dnode **child_dno_out)
|
---|
| 475 | {
|
---|
| 476 | fsw_status_t status;
|
---|
| 477 | struct fsw_shandle shand;
|
---|
| 478 | struct iso9660_dirrec_buffer dirrec_buffer;
|
---|
| 479 | struct iso9660_dirrec *dirrec = &dirrec_buffer.dirrec;
|
---|
| 480 |
|
---|
| 481 | // Preconditions: The caller has checked that dno is a directory node.
|
---|
| 482 |
|
---|
| 483 | // setup handle to read the directory
|
---|
| 484 | status = fsw_shandle_open(dno, &shand);
|
---|
| 485 | if (status)
|
---|
| 486 | return status;
|
---|
| 487 |
|
---|
| 488 | // scan the directory for the file
|
---|
| 489 | while (1) {
|
---|
| 490 | // read next entry
|
---|
| 491 | status = fsw_iso9660_read_dirrec(vol, &shand, &dirrec_buffer);
|
---|
| 492 | if (status)
|
---|
| 493 | goto errorexit;
|
---|
| 494 | if (dirrec->dirrec_length == 0) {
|
---|
| 495 | // end of directory reached
|
---|
| 496 | status = FSW_NOT_FOUND;
|
---|
| 497 | goto errorexit;
|
---|
| 498 | }
|
---|
| 499 |
|
---|
| 500 | // skip . and ..
|
---|
| 501 | if (dirrec->file_identifier_length == 1 &&
|
---|
| 502 | (dirrec->file_identifier[0] == 0 || dirrec->file_identifier[0] == 1))
|
---|
| 503 | continue;
|
---|
| 504 |
|
---|
| 505 | // compare name
|
---|
[69429] | 506 | if (fsw_streq(lookup_name, &dirrec_buffer.name)) /// @todo compare case-insensitively
|
---|
[48674] | 507 | break;
|
---|
| 508 | }
|
---|
| 509 |
|
---|
| 510 | // setup a dnode for the child item
|
---|
| 511 | status = fsw_dnode_create(dno, dirrec_buffer.ino, FSW_DNODE_TYPE_UNKNOWN, &dirrec_buffer.name, child_dno_out);
|
---|
| 512 | if (status == FSW_SUCCESS)
|
---|
| 513 | fsw_memcpy(&(*child_dno_out)->dirrec, dirrec, sizeof(struct iso9660_dirrec));
|
---|
| 514 |
|
---|
| 515 | errorexit:
|
---|
| 516 | fsw_shandle_close(&shand);
|
---|
| 517 | return status;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | /**
|
---|
| 521 | * Get the next directory entry when reading a directory. This function is called during
|
---|
| 522 | * directory iteration to retrieve the next directory entry. A dnode is constructed for
|
---|
| 523 | * the entry and returned. The core makes sure that fsw_iso9660_dnode_fill has been called
|
---|
| 524 | * and the dnode is actually a directory. The shandle provided by the caller is used to
|
---|
| 525 | * record the position in the directory between calls.
|
---|
| 526 | */
|
---|
| 527 |
|
---|
| 528 | static fsw_status_t fsw_iso9660_dir_read(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 529 | struct fsw_shandle *shand, struct fsw_iso9660_dnode **child_dno_out)
|
---|
| 530 | {
|
---|
| 531 | fsw_status_t status;
|
---|
| 532 | struct iso9660_dirrec_buffer dirrec_buffer;
|
---|
| 533 | struct iso9660_dirrec *dirrec = &dirrec_buffer.dirrec;
|
---|
| 534 |
|
---|
| 535 | // Preconditions: The caller has checked that dno is a directory node. The caller
|
---|
| 536 | // has opened a storage handle to the directory's storage and keeps it around between
|
---|
| 537 | // calls.
|
---|
| 538 | /* (vasily) directory nodes are 4096 bytes that is two logical blocks so read dir operation
|
---|
| 539 | * should read both blocks.
|
---|
| 540 | */
|
---|
| 541 |
|
---|
| 542 | while (1) {
|
---|
| 543 | // read next entry
|
---|
| 544 | if (shand->pos >= dno->g.size)
|
---|
| 545 | return FSW_NOT_FOUND; // end of directory
|
---|
| 546 | status = fsw_iso9660_read_dirrec(vol, shand, &dirrec_buffer);
|
---|
| 547 | if (status)
|
---|
| 548 | return status;
|
---|
| 549 | if (dirrec->dirrec_length == 0)
|
---|
| 550 | {
|
---|
| 551 | // try the next block
|
---|
| 552 | shand->pos =(shand->pos & ~(vol->g.log_blocksize - 1)) + vol->g.log_blocksize;
|
---|
| 553 | continue;
|
---|
| 554 | }
|
---|
| 555 |
|
---|
| 556 | // skip . and ..
|
---|
| 557 | if (dirrec->file_identifier_length == 1 &&
|
---|
| 558 | (dirrec->file_identifier[0] == 0 || dirrec->file_identifier[0] == 1))
|
---|
| 559 | continue;
|
---|
| 560 | break;
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | // setup a dnode for the child item
|
---|
| 564 | status = fsw_dnode_create(dno, dirrec_buffer.ino, FSW_DNODE_TYPE_UNKNOWN, &dirrec_buffer.name, child_dno_out);
|
---|
| 565 | if (status == FSW_SUCCESS)
|
---|
| 566 | fsw_memcpy(&(*child_dno_out)->dirrec, dirrec, sizeof(struct iso9660_dirrec));
|
---|
| 567 |
|
---|
| 568 | return status;
|
---|
| 569 | }
|
---|
| 570 |
|
---|
| 571 | /**
|
---|
| 572 | * Read a directory entry from the directory's raw data. This internal function is used
|
---|
| 573 | * to read a raw iso9660 directory entry into memory. The shandle's position pointer is adjusted
|
---|
| 574 | * to point to the next entry.
|
---|
| 575 | */
|
---|
| 576 |
|
---|
| 577 | static fsw_status_t fsw_iso9660_read_dirrec(struct fsw_iso9660_volume *vol, struct fsw_shandle *shand, struct iso9660_dirrec_buffer *dirrec_buffer)
|
---|
| 578 | {
|
---|
| 579 | fsw_status_t status;
|
---|
| 580 | fsw_u32 i, buffer_size, remaining_size, name_len;
|
---|
| 581 | struct fsw_rock_ridge_susp_sp *sp = NULL;
|
---|
| 582 | struct iso9660_dirrec *dirrec = &dirrec_buffer->dirrec;
|
---|
| 583 | int rc;
|
---|
| 584 |
|
---|
| 585 | dirrec_buffer->ino = (ISOINT(((struct fsw_iso9660_dnode *)shand->dnode)->dirrec.extent_location)
|
---|
| 586 | << ISO9660_BLOCKSIZE_BITS)
|
---|
| 587 | + (fsw_u32)shand->pos;
|
---|
| 588 |
|
---|
| 589 | // read fixed size part of directory record
|
---|
| 590 | buffer_size = 33;
|
---|
| 591 | status = fsw_shandle_read(shand, &buffer_size, dirrec);
|
---|
| 592 | if (status)
|
---|
| 593 | {
|
---|
| 594 | DEBUG((DEBUG_INFO, "%a:%d \n", __FILE__, __LINE__));
|
---|
| 595 | return status;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | if (buffer_size < 33 || dirrec->dirrec_length == 0) {
|
---|
| 599 | // end of directory reached
|
---|
| 600 | fsw_u8 *r;
|
---|
| 601 | r = (fsw_u8 *)dirrec;
|
---|
| 602 | DEBUG((DEBUG_INFO, "%a:%d bs:%d dl:%d\n", __FILE__, __LINE__, buffer_size, dirrec->dirrec_length));
|
---|
| 603 | for(i = 0; i < buffer_size; ++i)
|
---|
| 604 | {
|
---|
| 605 | DEBUG((DEBUG_INFO, "r[%d]:%c", i, r[i]));
|
---|
| 606 | }
|
---|
| 607 | dirrec->dirrec_length = 0;
|
---|
| 608 | return FSW_SUCCESS;
|
---|
| 609 | }
|
---|
| 610 | if (dirrec->dirrec_length < 33 ||
|
---|
| 611 | dirrec->dirrec_length < 33 + dirrec->file_identifier_length)
|
---|
| 612 | return FSW_VOLUME_CORRUPTED;
|
---|
| 613 |
|
---|
| 614 | DEBUG((DEBUG_INFO, "%a:%d, dirrec_length: %d\n", __FILE__, __LINE__, dirrec->dirrec_length));
|
---|
| 615 |
|
---|
| 616 | // read variable size part of directory record
|
---|
| 617 | buffer_size = remaining_size = dirrec->dirrec_length - 33;
|
---|
| 618 | status = fsw_shandle_read(shand, &buffer_size, dirrec->file_identifier);
|
---|
| 619 | if (status)
|
---|
| 620 | return status;
|
---|
| 621 | if (buffer_size < remaining_size)
|
---|
| 622 | return FSW_VOLUME_CORRUPTED;
|
---|
| 623 |
|
---|
| 624 | if (vol->fRockRidge)
|
---|
| 625 | {
|
---|
| 626 | UINTN sp_off = sizeof(*dirrec) + dirrec->file_identifier_length;
|
---|
| 627 | rc = rr_find_sp(dirrec, &sp);
|
---|
| 628 | if ( rc == FSW_SUCCESS
|
---|
| 629 | && sp != NULL)
|
---|
| 630 | {
|
---|
| 631 | sp_off = (fsw_u8 *)&sp[1] - (fsw_u8 *)dirrec + sp->skip;
|
---|
| 632 | }
|
---|
| 633 | rc = rr_find_nm(vol, dirrec, (int)sp_off, &dirrec_buffer->name);
|
---|
| 634 | if (rc == FSW_SUCCESS)
|
---|
| 635 | return FSW_SUCCESS;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | // setup name
|
---|
| 639 | name_len = dirrec->file_identifier_length;
|
---|
| 640 | for (i = name_len - 1; i > 0; i--) {
|
---|
| 641 | if (dirrec->file_identifier[i] == ';') {
|
---|
| 642 | name_len = i; // cut the ISO9660 version number off
|
---|
| 643 | break;
|
---|
| 644 | }
|
---|
| 645 | }
|
---|
| 646 | if (name_len > 0 && dirrec->file_identifier[name_len-1] == '.')
|
---|
| 647 | name_len--; // also cut the extension separator if the extension is empty
|
---|
| 648 | dirrec_buffer->name.type = FSW_STRING_TYPE_ISO88591;
|
---|
| 649 | dirrec_buffer->name.len = dirrec_buffer->name.size = name_len;
|
---|
| 650 | dirrec_buffer->name.data = dirrec->file_identifier;
|
---|
| 651 | DEBUG((DEBUG_INFO, "%a:%d: dirrec_buffer->name.data:%a\n", __FILE__, __LINE__, dirrec_buffer->name.data));
|
---|
| 652 | return FSW_SUCCESS;
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | /**
|
---|
| 656 | * Get the target path of a symbolic link. This function is called when a symbolic
|
---|
| 657 | * link needs to be resolved. The core makes sure that the fsw_iso9660_dnode_fill has been
|
---|
| 658 | * called on the dnode and that it really is a symlink.
|
---|
| 659 | *
|
---|
| 660 | * For iso9660, the target path can be stored inline in the inode structure (in the space
|
---|
| 661 | * otherwise occupied by the block pointers) or in the inode's data. There is no flag
|
---|
| 662 | * indicating this, only the number of blocks entry (i_blocks) can be used as an
|
---|
| 663 | * indication. The check used here comes from the Linux kernel.
|
---|
| 664 | */
|
---|
| 665 |
|
---|
| 666 | static fsw_status_t fsw_iso9660_readlink(struct fsw_iso9660_volume *vol, struct fsw_iso9660_dnode *dno,
|
---|
| 667 | struct fsw_string *link_target)
|
---|
| 668 | {
|
---|
| 669 | fsw_status_t status;
|
---|
| 670 |
|
---|
| 671 | if (dno->g.size > FSW_PATH_MAX)
|
---|
| 672 | return FSW_VOLUME_CORRUPTED;
|
---|
| 673 |
|
---|
| 674 | status = fsw_dnode_readlink_data(dno, link_target);
|
---|
| 675 |
|
---|
| 676 | return status;
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 | // EOF
|
---|