2003-08-31 06:59:23 +00:00
|
|
|
|
/*
|
2003-09-06 14:50:50 +00:00
|
|
|
|
$Id: iso9660_fs.c,v 1.7 2003/09/06 14:50:50 rocky Exp $
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
|
|
|
|
|
|
Copyright (C) 2003 Rocky Bernstein <rocky@panix.com>
|
|
|
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
|
# include "config.h"
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2003-08-31 09:11:25 +00:00
|
|
|
|
#ifdef HAVE_STRING_H
|
2003-08-31 06:59:23 +00:00
|
|
|
|
#include <string.h>
|
2003-08-31 09:11:25 +00:00
|
|
|
|
#endif
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
#include <cdio/cdio.h>
|
|
|
|
|
|
#include <cdio/iso9660.h>
|
2003-08-31 08:32:40 +00:00
|
|
|
|
#include <cdio/util.h>
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
/* Private headers */
|
2003-08-31 08:32:40 +00:00
|
|
|
|
#include "cdio_assert.h"
|
|
|
|
|
|
#include "bytesex.h"
|
|
|
|
|
|
#include "ds.h"
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
2003-09-05 22:48:16 +00:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
2003-09-06 14:50:50 +00:00
|
|
|
|
static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.7 2003/09/06 14:50:50 rocky Exp $";
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
static void
|
2003-08-31 15:52:56 +00:00
|
|
|
|
_idr2statbuf (const iso9660_dir_t *idr, iso9660_stat_t *buf, bool is_mode2)
|
2003-08-31 06:59:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
iso9660_xa_t *xa_data = NULL;
|
2003-08-31 15:52:56 +00:00
|
|
|
|
uint8_t dir_len= iso9660_get_dir_len(idr);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
memset ((void *) buf, 0, sizeof (iso9660_stat_t));
|
|
|
|
|
|
|
2003-08-31 15:52:56 +00:00
|
|
|
|
if (!dir_len) return;
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
2003-08-31 15:52:56 +00:00
|
|
|
|
buf->type = (idr->flags & ISO_DIRECTORY) ? _STAT_DIR : _STAT_FILE;
|
|
|
|
|
|
buf->lsn = from_733 (idr->extent);
|
|
|
|
|
|
buf->size = from_733 (idr->size);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
buf->secsize = _cdio_len2blocks (buf->size, ISO_BLOCKSIZE);
|
|
|
|
|
|
|
2003-08-31 15:52:56 +00:00
|
|
|
|
cdio_assert (dir_len >= sizeof (iso9660_dir_t));
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
2003-08-31 15:52:56 +00:00
|
|
|
|
if (is_mode2) {
|
|
|
|
|
|
int su_length = iso9660_get_dir_len(idr) - sizeof (iso9660_dir_t);
|
|
|
|
|
|
su_length -= idr->name_len;
|
|
|
|
|
|
|
|
|
|
|
|
if (su_length % 2)
|
|
|
|
|
|
su_length--;
|
|
|
|
|
|
|
|
|
|
|
|
if (su_length < 0 || su_length < sizeof (iso9660_xa_t))
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
xa_data = (void *) (((char *) idr) + (iso9660_get_dir_len(idr) - su_length));
|
|
|
|
|
|
|
|
|
|
|
|
if (xa_data->signature[0] != 'X'
|
|
|
|
|
|
|| xa_data->signature[1] != 'A')
|
2003-08-31 06:59:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
cdio_warn ("XA signature not found in ISO9660's system use area;"
|
2003-08-31 15:52:56 +00:00
|
|
|
|
" ignoring XA attributes for this file entry.");
|
2003-08-31 06:59:23 +00:00
|
|
|
|
cdio_debug ("%d %d %d, '%c%c' (%d, %d)", iso9660_get_dir_len(idr),
|
|
|
|
|
|
idr->name_len,
|
|
|
|
|
|
su_length,
|
|
|
|
|
|
xa_data->signature[0], xa_data->signature[1],
|
|
|
|
|
|
xa_data->signature[0], xa_data->signature[1]);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2003-08-31 15:52:56 +00:00
|
|
|
|
buf->xa = *xa_data;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-08-31 06:59:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
|
|
_idr2name (const iso9660_dir_t *idr)
|
|
|
|
|
|
{
|
|
|
|
|
|
char namebuf[256] = { 0, };
|
|
|
|
|
|
|
|
|
|
|
|
if (!iso9660_get_dir_len(idr))
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
cdio_assert (iso9660_get_dir_len(idr) >= sizeof (iso9660_dir_t));
|
|
|
|
|
|
|
|
|
|
|
|
/* (idr->flags & ISO_DIRECTORY) */
|
|
|
|
|
|
|
|
|
|
|
|
if (idr->name[0] == '\0')
|
|
|
|
|
|
strcpy (namebuf, ".");
|
|
|
|
|
|
else if (idr->name[0] == '\1')
|
|
|
|
|
|
strcpy (namebuf, "..");
|
|
|
|
|
|
else
|
|
|
|
|
|
strncpy (namebuf, idr->name, idr->name_len);
|
|
|
|
|
|
|
|
|
|
|
|
return strdup (namebuf);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2003-09-05 22:48:16 +00:00
|
|
|
|
_fs_stat_root (const CdIo *cdio, iso9660_stat_t *buf, bool is_mode2)
|
2003-08-31 06:59:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
char block[ISO_BLOCKSIZE] = { 0, };
|
|
|
|
|
|
const iso9660_pvd_t *pvd = (void *) █
|
|
|
|
|
|
const iso9660_dir_t *idr = (void *) pvd->root_directory_record;
|
|
|
|
|
|
|
2003-08-31 14:26:06 +00:00
|
|
|
|
if (is_mode2) {
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (cdio_read_mode2_sector (cdio, &block, ISO_PVD_SECTOR, false))
|
2003-08-31 14:26:06 +00:00
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
} else {
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (cdio_read_mode1_sector (cdio, &block, ISO_PVD_SECTOR, false))
|
2003-08-31 14:26:06 +00:00
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
}
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
2003-08-31 15:52:56 +00:00
|
|
|
|
_idr2statbuf (idr, buf, is_mode2);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2003-09-05 22:48:16 +00:00
|
|
|
|
_fs_stat_traverse (const CdIo *cdio, const iso9660_stat_t *_root,
|
|
|
|
|
|
char **splitpath, iso9660_stat_t *buf, bool is_mode2)
|
2003-08-31 06:59:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
|
uint8_t *_dirbuf = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (!splitpath[0])
|
|
|
|
|
|
{
|
|
|
|
|
|
*buf = *_root;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (_root->type == _STAT_FILE)
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
|
|
cdio_assert (_root->type == _STAT_DIR);
|
|
|
|
|
|
|
|
|
|
|
|
if (_root->size != ISO_BLOCKSIZE * _root->secsize)
|
|
|
|
|
|
{
|
|
|
|
|
|
cdio_warn ("bad size for ISO9660 directory (%ud) should be (%d)!",
|
|
|
|
|
|
(unsigned) _root->size, ISO_BLOCKSIZE * _root->secsize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_dirbuf = _cdio_malloc (_root->secsize * ISO_BLOCKSIZE);
|
|
|
|
|
|
|
2003-08-31 15:52:56 +00:00
|
|
|
|
if (is_mode2) {
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (cdio_read_mode2_sectors (cdio, _dirbuf, _root->lsn, false,
|
2003-08-31 15:52:56 +00:00
|
|
|
|
_root->secsize))
|
|
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
} else {
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (cdio_read_mode1_sectors (cdio, _dirbuf, _root->lsn, false,
|
2003-08-31 15:52:56 +00:00
|
|
|
|
_root->secsize))
|
|
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-08-31 06:59:23 +00:00
|
|
|
|
while (offset < (_root->secsize * ISO_BLOCKSIZE))
|
|
|
|
|
|
{
|
|
|
|
|
|
const iso9660_dir_t *idr
|
|
|
|
|
|
= (void *) &_dirbuf[offset];
|
|
|
|
|
|
iso9660_stat_t _stat;
|
|
|
|
|
|
|
|
|
|
|
|
char *_name;
|
|
|
|
|
|
|
|
|
|
|
|
if (!iso9660_get_dir_len(idr))
|
|
|
|
|
|
{
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_name = _idr2name (idr);
|
2003-08-31 15:52:56 +00:00
|
|
|
|
_idr2statbuf (idr, &_stat, is_mode2);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
if (!strcmp (splitpath[0], _name))
|
|
|
|
|
|
{
|
2003-09-05 22:48:16 +00:00
|
|
|
|
int retval = _fs_stat_traverse (cdio, &_stat, &splitpath[1], buf,
|
2003-08-31 15:52:56 +00:00
|
|
|
|
is_mode2);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
free (_name);
|
|
|
|
|
|
free (_dirbuf);
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
free (_name);
|
|
|
|
|
|
|
|
|
|
|
|
offset += iso9660_get_dir_len(idr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cdio_assert (offset == (_root->secsize * ISO_BLOCKSIZE));
|
|
|
|
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
|
|
free (_dirbuf);
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-09-06 14:50:50 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Get file status for pathname into stat. As with libc's stat, 0 is returned
|
|
|
|
|
|
if no error and -1 on error.
|
|
|
|
|
|
*/
|
2003-08-31 06:59:23 +00:00
|
|
|
|
int
|
2003-09-06 14:50:50 +00:00
|
|
|
|
iso9660_fs_stat (const CdIo *cdio, const char pathname[],
|
|
|
|
|
|
/*out*/ iso9660_stat_t *stat, bool is_mode2)
|
2003-08-31 06:59:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
iso9660_stat_t _root;
|
|
|
|
|
|
int retval;
|
|
|
|
|
|
char **splitpath;
|
|
|
|
|
|
|
2003-09-05 22:48:16 +00:00
|
|
|
|
cdio_assert (cdio != NULL);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
cdio_assert (pathname != NULL);
|
2003-09-06 14:50:50 +00:00
|
|
|
|
cdio_assert (stat != NULL);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
2003-09-05 22:48:16 +00:00
|
|
|
|
_fs_stat_root (cdio, &_root, is_mode2);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
splitpath = _cdio_strsplit (pathname, '/');
|
2003-09-06 14:50:50 +00:00
|
|
|
|
retval = _fs_stat_traverse (cdio, &_root, splitpath, stat, is_mode2);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
_cdio_strfreev (splitpath);
|
|
|
|
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void * /* list of char* -- caller must free it */
|
2003-09-05 22:48:16 +00:00
|
|
|
|
iso9660_fs_readdir (const CdIo *cdio, const char pathname[], bool is_mode2)
|
2003-08-31 06:59:23 +00:00
|
|
|
|
{
|
|
|
|
|
|
iso9660_stat_t _stat;
|
|
|
|
|
|
|
2003-09-05 22:48:16 +00:00
|
|
|
|
cdio_assert (cdio != NULL);
|
2003-08-31 06:59:23 +00:00
|
|
|
|
cdio_assert (pathname != NULL);
|
|
|
|
|
|
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (iso9660_fs_stat (cdio, pathname, &_stat, is_mode2))
|
2003-08-31 06:59:23 +00:00
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
if (_stat.type != _STAT_DIR)
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
unsigned offset = 0;
|
|
|
|
|
|
uint8_t *_dirbuf = NULL;
|
|
|
|
|
|
CdioList *retval = _cdio_list_new ();
|
|
|
|
|
|
|
|
|
|
|
|
if (_stat.size != ISO_BLOCKSIZE * _stat.secsize)
|
|
|
|
|
|
{
|
|
|
|
|
|
cdio_warn ("bad size for ISO9660 directory (%ud) should be (%d)!",
|
|
|
|
|
|
(unsigned) _stat.size, ISO_BLOCKSIZE * _stat.secsize);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_dirbuf = _cdio_malloc (_stat.secsize * ISO_BLOCKSIZE);
|
|
|
|
|
|
|
2003-08-31 14:26:06 +00:00
|
|
|
|
if (is_mode2) {
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (cdio_read_mode2_sectors (cdio, _dirbuf, _stat.lsn, false,
|
2003-08-31 06:59:23 +00:00
|
|
|
|
_stat.secsize))
|
2003-08-31 14:26:06 +00:00
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
} else {
|
2003-09-05 22:48:16 +00:00
|
|
|
|
if (cdio_read_mode1_sectors (cdio, _dirbuf, _stat.lsn, false,
|
2003-08-31 14:26:06 +00:00
|
|
|
|
_stat.secsize))
|
|
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
}
|
2003-08-31 06:59:23 +00:00
|
|
|
|
|
|
|
|
|
|
while (offset < (_stat.secsize * ISO_BLOCKSIZE))
|
|
|
|
|
|
{
|
|
|
|
|
|
const iso9660_dir_t *idr = (void *) &_dirbuf[offset];
|
|
|
|
|
|
|
|
|
|
|
|
if (!iso9660_get_dir_len(idr))
|
|
|
|
|
|
{
|
|
|
|
|
|
offset++;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_cdio_list_append (retval, _idr2name (idr));
|
|
|
|
|
|
|
|
|
|
|
|
offset += iso9660_get_dir_len(idr);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cdio_assert (offset == (_stat.secsize * ISO_BLOCKSIZE));
|
|
|
|
|
|
|
|
|
|
|
|
free (_dirbuf);
|
|
|
|
|
|
return retval;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-09-05 22:48:16 +00:00
|
|
|
|
static bool
|
|
|
|
|
|
find_fs_lsn_recurse (const CdIo *cdio, const char pathname[],
|
|
|
|
|
|
/*out*/ iso9660_stat_t *statbuf, lsn_t lsn)
|
|
|
|
|
|
{
|
|
|
|
|
|
CdioList *entlist = iso9660_fs_readdir (cdio, pathname, true);
|
|
|
|
|
|
CdioList *dirlist = _cdio_list_new ();
|
|
|
|
|
|
CdioListNode *entnode;
|
|
|
|
|
|
|
|
|
|
|
|
cdio_assert (entlist != NULL);
|
|
|
|
|
|
|
|
|
|
|
|
/* iterate over each entry in the directory */
|
|
|
|
|
|
|
|
|
|
|
|
_CDIO_LIST_FOREACH (entnode, entlist)
|
|
|
|
|
|
{
|
|
|
|
|
|
char *_name = _cdio_list_node_data (entnode);
|
|
|
|
|
|
char _fullname[4096] = { 0, };
|
|
|
|
|
|
|
|
|
|
|
|
snprintf (_fullname, sizeof (_fullname), "%s%s", pathname, _name);
|
|
|
|
|
|
|
|
|
|
|
|
if (iso9660_fs_stat (cdio, _fullname, statbuf, true))
|
|
|
|
|
|
cdio_assert_not_reached ();
|
|
|
|
|
|
|
|
|
|
|
|
strncat (_fullname, "/", sizeof (_fullname));
|
|
|
|
|
|
|
|
|
|
|
|
if (statbuf->type == _STAT_DIR
|
|
|
|
|
|
&& strcmp (_name, ".")
|
|
|
|
|
|
&& strcmp (_name, ".."))
|
|
|
|
|
|
_cdio_list_append (dirlist, strdup (_fullname));
|
|
|
|
|
|
|
|
|
|
|
|
if (statbuf->lsn == lsn) {
|
|
|
|
|
|
_cdio_list_free (entlist, true);
|
|
|
|
|
|
_cdio_list_free (dirlist, true);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_cdio_list_free (entlist, true);
|
|
|
|
|
|
|
|
|
|
|
|
/* now recurse/descend over directories encountered */
|
|
|
|
|
|
|
|
|
|
|
|
_CDIO_LIST_FOREACH (entnode, dirlist)
|
|
|
|
|
|
{
|
|
|
|
|
|
char *_fullname = _cdio_list_node_data (entnode);
|
|
|
|
|
|
|
|
|
|
|
|
if (find_fs_lsn_recurse (cdio, _fullname, statbuf, lsn)) {
|
|
|
|
|
|
_cdio_list_free (dirlist, true);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_cdio_list_free (dirlist, true);
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
Given a directory pointer, find the filesystem entry that contains
|
|
|
|
|
|
lsn and return information about it in stat.
|
|
|
|
|
|
|
|
|
|
|
|
Returns true if we found an entry with the lsn and false if not.
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool
|
|
|
|
|
|
iso9660_find_fs_lsn(const CdIo *cdio, lsn_t lsn, /*out*/ iso9660_stat_t *stat)
|
|
|
|
|
|
{
|
|
|
|
|
|
return find_fs_lsn_recurse (cdio, "/", stat, lsn);
|
|
|
|
|
|
}
|
|
|
|
|
|
|