Add ISO9660::FS - the cdio portion of ISO9660 reading.

iso9600.h: another function rename to be more consistent.
This commit is contained in:
rocky
2006-03-06 21:54:56 +00:00
parent 42b0b841d0
commit 7a3ded9bd7
7 changed files with 357 additions and 40 deletions

View File

@@ -1,4 +1,4 @@
# $Id: Makefile.am,v 1.5 2006/03/06 04:48:38 rocky Exp $
# $Id: Makefile.am,v 1.6 2006/03/06 21:54:56 rocky Exp $
#
# Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com>
#
@@ -20,7 +20,7 @@
# Sample C++ programs using libcdio++ (with C++ OO wrapper)
############################################################
#
noinst_PROGRAMS = cdtext device drives eject iso1 iso3 mmc1 mmc2 tracks
noinst_PROGRAMS = cdtext device drives eject iso1 iso2 iso3 mmc1 mmc2 tracks
INCLUDES = -I$(top_srcdir)/include $(LIBCDIO_CFLAGS)
@@ -48,6 +48,10 @@ iso3_SOURCES = iso3.cpp
iso3_LDADD = $(LIBISO966PP0_LIBS) $(LIBISO9660_LIBS) \
$(LIBCDIOPP_LIBS) $(LIBICONV)
iso2_SOURCES = iso2.cpp
iso2_LDADD = $(LIBISO966PP0_LIBS) $(LIBISO9660_LIBS) \
$(LIBCDIOPP_LIBS) $(LIBICONV)
mmc1_SOURCES = mmc1.cpp
mmc1_DEPENDENCIES = $(LIBCDIO_DEPS)
mmc1_LDADD = $(LIBCDIOPP_LIBS) $(LIBCDIO_LIBS)

179
example/C++/OO/iso2.cpp Normal file
View File

@@ -0,0 +1,179 @@
/*
$Id: iso2.cpp,v 1.1 2006/03/06 21:54:56 rocky Exp $
Copyright (C) 2006 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
*/
/* Simple program to show using libiso9660 to extract a file from a
CUE/BIN CD image.
If a single argument is given, it is used as the CUE file of a CD image
to use. Otherwise a compiled-in default image name (that
comes with the libcdio distribution) will be used.
This program can be compiled with either a C or C++ compiler. In
the distribution we prefer C++ just to make sure we haven't broken
things on the C++ side.
*/
/* This is the CD-image with an ISO-9660 filesystem */
#define ISO9660_IMAGE_PATH "../../../"
#define ISO9660_IMAGE ISO9660_IMAGE_PATH "test/isofs-m1.cue"
#define ISO9660_PATH "/"
#define ISO9660_FILENAME "COPYING"
#define LOCAL_FILENAME "copying"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "portable.h"
#include <cdio++/cdio.hpp>
#include <cdio++/iso9660.hpp>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#define CEILING(x, y) ((x+(y-1))/y)
#define my_exit(rc) \
fclose (p_outfd); \
delete (p_stat); \
delete (p_iso); \
return rc; \
int
main(int argc, const char *argv[])
{
ISO9660::Stat *p_stat;
FILE *p_outfd;
unsigned int i;
char const *psz_image;
char const *psz_fname;
char translated_name[256];
char untranslated_name[256] = ISO9660_PATH;
ISO9660::FS *p_iso = new ISO9660::FS;
if (argc > 3) {
printf("usage %s [CD-ROM-or-image [filename]]\n", argv[0]);
printf("Extracts filename from CD-ROM-or-image.\n");
return 1;
}
if (argc > 1)
psz_image = argv[1];
else
psz_image = ISO9660_IMAGE;
if (argc > 2)
psz_fname = argv[2];
else
psz_fname = ISO9660_FILENAME;
strcat(untranslated_name, psz_fname);
if (!p_iso->open(psz_image, DRIVER_UNKNOWN)) {
fprintf(stderr, "Sorry, couldn't open %s\n", psz_image);
return 1;
}
p_stat = p_iso->stat(psz_fname);
if (!p_stat)
{
fprintf(stderr,
"Could not get ISO-9660 file information for file %s\n",
untranslated_name);
delete(p_iso);
return 2;
}
iso9660_name_translate(psz_fname, translated_name);
if (!(p_outfd = fopen (translated_name, "wb")))
{
perror ("fopen()");
delete (p_stat);
delete (p_iso);
return 3;
}
/* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */
{
const unsigned int i_blocks = CEILING(p_stat->p_stat->size, ISO_BLOCKSIZE);
for (i = 0; i < i_blocks; i ++)
{
char buf[ISO_BLOCKSIZE];
const lsn_t lsn = p_stat->p_stat->lsn + i;
memset (buf, 0, ISO_BLOCKSIZE);
try {
p_iso->readDataBlocks(buf, lsn, ISO_BLOCKSIZE);
}
catch ( DriverOpException e ) {
fprintf(stderr, "Error reading ISO 9660 file at lsn %lu:\n\t%s.\n",
(long unsigned int) lsn, e.get_msg());
my_exit(4);
}
fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd);
if (ferror (p_outfd))
{
perror ("fwrite()");
my_exit(5);
}
}
}
fflush (p_outfd);
/* Make sure the file size has the exact same byte size. Without the
truncate below, the file will a multiple of ISO_BLOCKSIZE.
*/
if (ftruncate (fileno (p_outfd), p_stat->p_stat->size))
perror ("ftruncate()");
printf("Extraction of file '%s' from '%s' successful.\n",
translated_name, untranslated_name);
my_exit(0);
}

View File

@@ -1,5 +1,5 @@
/* -*- C++ -*-
$Id: cdio.hpp,v 1.9 2006/03/05 06:52:15 rocky Exp $
$Id: cdio.hpp,v 1.10 2006/03/06 21:54:56 rocky Exp $
Copyright (C) 2005, 2006 Rocky Bernstein <rocky@panix.com>
@@ -174,6 +174,9 @@ class CdioDevice
{
public:
CdIo_t *p_cdio; // Make private? If so, subclasses would have to
// use their own.
CdioDevice()
{
p_cdio = (CdIo_t *) NULL;
@@ -191,8 +194,6 @@ public:
#include "mmc.hpp"
#include "read.hpp"
private:
CdIo_t *p_cdio;
};
/* Things related to devices. No class or object is needed. */

View File

@@ -1,5 +1,5 @@
/* -*- C++ -*-
$Id: iso9660.hpp,v 1.5 2006/03/06 19:39:35 rocky Exp $
$Id: iso9660.hpp,v 1.6 2006/03/06 21:54:56 rocky Exp $
Copyright (C) 2006 Rocky Bernstein <rocky@panix.com>
@@ -28,6 +28,7 @@
#define __ISO9660_HPP__
#include <cdio/iso9660.h>
#include <cdio++/cdio.hpp>
#include <string.h>
#include <list> // list class library
using namespace std;
@@ -39,24 +40,6 @@ class ISO9660
public:
/*!
Convert an ISO-9660 file name which is in the format usually stored
in a ISO 9660 directory entry into what's usually listed as the
file name in a listing. Lowercase name, and remove trailing ;1's
or .;1's and turn the other ;'s into version numbers.
@param psz_oldname the ISO-9660 filename to be translated.
@param psz_newname returned string. The caller allocates this and
it should be at least the size of psz_oldname.
@return length of the translated string is returned.
*/
int name_translate(const char *psz_oldname,
/*out*/ char *psz_newname)
{
return iso9660_name_translate(psz_oldname, psz_newname);
}
class Stat
{
public:
@@ -225,11 +208,11 @@ public:
Given a directory pointer, find the filesystem entry that contains
lsn and return information about it.
Returns stat_t of entry if we found lsn, or NULL otherwise.
Returns Stat* of entry if we found lsn, or NULL otherwise.
*/
Stat *find_lsn(lsn_t i_lsn)
{
return new Stat(iso9660_find_ifs_lsn(p_iso9660, i_lsn));
return new Stat(iso9660_ifs_find_lsn(p_iso9660, i_lsn));
};
/*!
@@ -437,7 +420,89 @@ public:
iso9660_t *p_iso9660;
};
class FS : public CdioDevice
{
public:
/*!
Given a directory pointer, find the filesystem entry that contains
lsn and return information about it.
@return Stat * of entry if we found lsn, or NULL otherwise.
Caller must free return value.
*/
Stat *find_lsn(lsn_t i_lsn)
{
return new Stat(iso9660_find_fs_lsn(p_cdio, i_lsn));
}
/*!
Read the Primary Volume Descriptor for a CD.
True is returned if read, and false if there was an error.
*/
bool read_pvd ( /*out*/ iso9660_pvd_t *p_pvd )
{
return iso9660_fs_read_pvd ( p_cdio, p_pvd );
}
/*!
Read the Super block of an ISO 9660 image. This is the
Primary Volume Descriptor (PVD) and perhaps a Supplemental Volume
Descriptor if (Joliet) extensions are acceptable.
*/
bool read_superblock (iso_extension_mask_t iso_extension_mask)
{
return iso9660_fs_read_superblock (p_cdio, iso_extension_mask);
}
/*! Read psz_path (a directory) and return a list of iso9660_stat_t
pointers for the files inside that directory. The caller must free the
returned result.
*/
bool readdir (const char psz_path[], bool b_mode2,
list< ISO9660::Stat *>& stat_list)
{
CdioList_t * p_stat_list = iso9660_fs_readdir (p_cdio, psz_path,
b_mode2);
if (p_stat_list) {
CdioListNode_t *p_entnode;
_CDIO_LIST_FOREACH (p_entnode, p_stat_list) {
iso9660_stat_t *p_statbuf =
(iso9660_stat_t *) _cdio_list_node_data (p_entnode);
stat_list.push_back(new ISO9660::Stat(p_statbuf));
}
_cdio_list_free (p_stat_list, false);
return true;
} else {
return false;
}
}
/*!
Return file status for path name psz_path. NULL is returned on
error.
If translate is true, version numbers in the ISO 9660 name are
dropped, i.e. ;1 is removed and if level 1 ISO-9660 names are
lowercased.
Mode2 is used only if translate is true and is a hack that
really should go away in libcdio sometime. If set use mode 2
reading, otherwise use mode 1 reading.
@return file status object for psz_path. NULL is returned on
error.
*/
Stat *stat (const char psz_path[], bool b_translate=false,
bool b_mode2=false)
{
if (b_translate)
return new ISO9660::Stat(iso9660_fs_stat_translate (p_cdio, psz_path,
b_mode2));
else
return new ISO9660::Stat(iso9660_fs_stat (p_cdio, psz_path));
}
};
};
#endif /* __ISO9660_HPP__ */

View File

@@ -1,5 +1,5 @@
/*
$Id: iso9660.h,v 1.86 2006/03/06 19:39:35 rocky Exp $
$Id: iso9660.h,v 1.87 2006/03/06 21:54:56 rocky Exp $
Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
Copyright (C) 2003, 2004, 2005, 2006 Rocky Bernstein <rocky@panix.com>
@@ -803,7 +803,7 @@ iso9660_dir_calc_record_size (unsigned int namelen, unsigned int su_len);
@return stat_t of entry if we found lsn, or NULL otherwise.
Caller must free return value.
*/
iso9660_stat_t *iso9660_find_fs_lsn(CdIo_t *p_cdio, lsn_t i_lsn);
iso9660_stat_t *iso9660_fs_find_lsn(CdIo_t *p_cdio, lsn_t i_lsn);
/*!
@@ -813,7 +813,7 @@ iso9660_stat_t *iso9660_find_fs_lsn(CdIo_t *p_cdio, lsn_t i_lsn);
@return stat_t of entry if we found lsn, or NULL otherwise.
Caller must free return value.
*/
iso9660_stat_t *iso9660_find_ifs_lsn(const iso9660_t *p_iso, lsn_t i_lsn);
iso9660_stat_t *iso9660_ifs_find_lsn(iso9660_t *p_iso, lsn_t i_lsn);
/*!
@@ -1012,11 +1012,12 @@ lsn_t iso9660_get_dir_extent(const iso9660_dir_t *p_idr);
bool iso9660_ifs_is_xa (const iso9660_t * p_iso);
#ifndef DO_NOT_WANT_PARANOIA_COMPATIBILITY
/** For compatibility with good ol' paranoia */
#ifndef DO_NOT_WANT_COMPATIBILITY
/** For compatibility with < 0.77 */
#define iso9660_isdchar iso9660_is_dchar
#define iso9660_isachar iso9660_is_achar
#endif /*DO_NOT_WANT_PARANOIA_COMPATIBILITY*/
#define iso9660_find_fs_lsn iso9660_fs_find_lsn
#endif /*DO_NOT_WANT_COMPATIBILITY*/
#ifdef __cplusplus
}

View File

@@ -1,5 +1,5 @@
/*
$Id: iso9660_fs.c,v 1.32 2005/11/06 00:39:37 rocky Exp $
$Id: iso9660_fs.c,v 1.33 2006/03/06 21:54:56 rocky Exp $
Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
Copyright (C) 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>
@@ -52,7 +52,7 @@
#include <stdio.h>
static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.32 2005/11/06 00:39:37 rocky Exp $";
static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.33 2006/03/06 21:54:56 rocky Exp $";
/* Implementation of iso9660_t type */
struct _iso9660_s {
@@ -987,7 +987,7 @@ _fs_stat_root (CdIo_t *p_cdio)
}
static iso9660_stat_t *
_fs_stat_iso_root (iso9660_t *p_iso)
_ifs_stat_root (iso9660_t *p_iso)
{
iso9660_stat_t *p_stat;
iso9660_dir_t *p_iso9660_dir;
@@ -1253,7 +1253,7 @@ iso9660_ifs_stat (iso9660_t *p_iso, const char psz_path[])
if (!p_iso) return NULL;
if (!psz_path) return NULL;
p_root = _fs_stat_iso_root (p_iso);
p_root = _ifs_stat_root (p_iso);
if (!p_root) return NULL;
splitpath = _cdio_strsplit (psz_path, '/');
@@ -1280,7 +1280,7 @@ iso9660_ifs_stat_translate (iso9660_t *p_iso, const char psz_path[])
if (!p_iso) return NULL;
if (!psz_path) return NULL;
p_root = _fs_stat_iso_root (p_iso);
p_root = _ifs_stat_root (p_iso);
if (NULL == p_root) return NULL;
p_psz_splitpath = _cdio_strsplit (psz_path, '/');
@@ -1483,6 +1483,60 @@ find_fs_lsn_recurse (CdIo_t *p_cdio, const char psz_path[], lsn_t lsn)
return NULL;
}
static iso9660_stat_t *
find_ifs_lsn_recurse (iso9660_t *p_iso, const char psz_path[], lsn_t lsn)
{
CdioList_t *entlist = iso9660_ifs_readdir (p_iso, psz_path);
CdioList_t *dirlist = _cdio_list_new ();
CdioListNode_t *entnode;
cdio_assert (entlist != NULL);
/* iterate over each entry in the directory */
_CDIO_LIST_FOREACH (entnode, entlist)
{
iso9660_stat_t *statbuf = _cdio_list_node_data (entnode);
char _fullname[4096] = { 0, };
char *filename = (char *) statbuf->filename;
snprintf (_fullname, sizeof (_fullname), "%s%s/", psz_path, filename);
if (statbuf->type == _STAT_DIR
&& strcmp ((char *) statbuf->filename, ".")
&& strcmp ((char *) statbuf->filename, ".."))
_cdio_list_append (dirlist, strdup (_fullname));
if (statbuf->lsn == lsn) {
unsigned int len=sizeof(iso9660_stat_t)+strlen(statbuf->filename)+1;
iso9660_stat_t *ret_stat = calloc(1, len);
memcpy(ret_stat, statbuf, len);
_cdio_list_free (entlist, true);
_cdio_list_free (dirlist, true);
return ret_stat;
}
}
_cdio_list_free (entlist, true);
/* now recurse/descend over directories encountered */
_CDIO_LIST_FOREACH (entnode, dirlist)
{
char *_fullname = _cdio_list_node_data (entnode);
iso9660_stat_t *ret_stat = find_ifs_lsn_recurse (p_iso, _fullname, lsn);
if (NULL != ret_stat) {
_cdio_list_free (dirlist, true);
return ret_stat;
}
}
_cdio_list_free (dirlist, true);
return NULL;
}
/*!
Given a directory pointer, find the filesystem entry that contains
lsn and return information about it.
@@ -1490,11 +1544,23 @@ find_fs_lsn_recurse (CdIo_t *p_cdio, const char psz_path[], lsn_t lsn)
Returns stat_t of entry if we found lsn, or NULL otherwise.
*/
iso9660_stat_t *
iso9660_find_fs_lsn(CdIo_t *p_cdio, lsn_t i_lsn)
iso9660_fs_find_lsn(CdIo_t *p_cdio, lsn_t i_lsn)
{
return find_fs_lsn_recurse (p_cdio, "/", i_lsn);
}
/*!
Given a directory pointer, find the filesystem entry that contains
lsn and return information about it.
Returns stat_t of entry if we found lsn, or NULL otherwise.
*/
iso9660_stat_t *
iso9660_ifs_find_lsn(iso9660_t *p_iso, lsn_t i_lsn)
{
return find_ifs_lsn_recurse (p_iso, "/", i_lsn);
}
/*!
Return true if ISO 9660 image has extended attrributes (XA).
*/

View File

@@ -13,7 +13,7 @@ iso9660_dir_init_new
iso9660_dir_init_new_su
iso9660_dir_to_name
iso9660_dirname_valid_p
iso9660_find_fs_lsn
iso9660_fs_find_lsn
iso9660_fs_read_pvd
iso9660_fs_read_superblock
iso9660_fs_readdir
@@ -39,6 +39,7 @@ iso9660_get_system_id
iso9660_get_volume_id
iso9660_get_volumeset_id
iso9660_get_xa_attr_str
iso9660_ifs_find_lsn
iso9660_ifs_fuzzy_read_superblock
iso9660_ifs_get_application_id
iso9660_ifs_get_joliet_level