diff --git a/example/Makefile.am b/example/Makefile.am index 09573aff..bf1f4eef 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -1,4 +1,4 @@ -# $Id: Makefile.am,v 1.39 2006/10/21 10:55:18 gmerlin Exp $ +# $Id: Makefile.am,v 1.40 2007/08/12 00:56:09 rocky Exp $ # # Copyright (C) 2003, 2004, 2005, 2006 # Rocky Bernstein @@ -28,7 +28,7 @@ if BUILD_CD_PARANOIA paranoia_progs = paranoia paranoia2 endif noinst_PROGRAMS = audio cdchange cdtext device drives eject \ - isofile isofile2 isofuzzy isolist \ + isofile isofile2 isofuzzy isolist isolsn \ mmc1 mmc2 mmc2a mmc3 $(paranoia_progs) tracks \ sample3 sample4 udf1 udffile cdio-eject @@ -64,6 +64,7 @@ isofile_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LIBICONV) isofile2_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LIBICONV) isofuzzy_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LIBICONV) isolist_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LIBICONV) +isolsn_LDADD = $(LIBISO9660_LIBS) $(LIBCDIO_LIBS) $(LIBICONV) mmc1_DEPENDENCIES = $(LIBCDIO_DEPS) mmc1_LDADD = $(LIBCDIO_LIBS) diff --git a/example/README b/example/README index 8f40f2de..1db482bd 100644 --- a/example/README +++ b/example/README @@ -1,4 +1,4 @@ -$Id: README,v 1.27 2006/10/21 11:38:16 rocky Exp $ +$Id: README,v 1.28 2007/08/12 00:56:10 rocky Exp $ This directory contains some simple examples of the use of the libcdio library. @@ -44,6 +44,9 @@ isolist.c: A program to show using libiso9660 to list files in a directory of an ISO-9660 image and give basic iso9660 information. +isolsn.c: A program to show using libiso9660 to get the file + path for a given LSN. + mmc1.c: A program to show issuing a simple MMC command (INQUIRY). mmc2.c: A more involved MMC command to list features from diff --git a/example/isolist.c b/example/isolist.c index e8f3e5fa..8f2ce211 100644 --- a/example/isolist.c +++ b/example/isolist.c @@ -1,7 +1,7 @@ /* - $Id: isolist.c,v 1.1 2006/04/15 15:45:25 rocky Exp $ + $Id: isolist.c,v 1.2 2007/08/12 00:56:10 rocky Exp $ - Copyright (C) 2004, 2005, 2006 Rocky Bernstein + Copyright (C) 2004, 2005, 2006, 2007 Rocky Bernstein 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 diff --git a/example/isolsn.c b/example/isolsn.c new file mode 100644 index 00000000..c5034428 --- /dev/null +++ b/example/isolsn.c @@ -0,0 +1,99 @@ +/* + $Id: isolsn.c,v 1.1 2007/08/12 00:56:10 rocky Exp $ + + Copyright (C) 2004, 2005, 2006, 2007 Rocky Bernstein + + 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 get a file path + for a given LSN of an ISO-9660 image. + + If a single argument is given, it is used as the LSN to search for. + Otherwise we use a built-in default value. + + If a second argument is given, it is ISO 9660 image to use in the + listing. Otherwise a compiled-in default ISO 9660 image name (that + comes with the libcdio distribution) will be used. + */ + +/* Set up a CD-DA image to test on which is in the libcdio distribution. */ +#define ISO9660_IMAGE_PATH "../test/" +#define ISO9660_IMAGE ISO9660_IMAGE_PATH "copying.iso" + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif +#include +#include +#include + +#include + +#ifdef HAVE_STDLIB_H +#include +#endif +#ifdef HAVE_STRING_H +#include +#endif +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_SYS_TYPES_H +#include +#endif + +#define print_vd_info(title, fn) \ + if (fn(p_iso, &psz_str)) { \ + printf(title ": %s\n", psz_str); \ + } \ + free(psz_str); \ + psz_str = NULL; + + +int +main(int argc, const char *argv[]) +{ + char const *psz_fname; + iso9660_t *p_iso; + lsn_t lsn = 24; + char *psz_path = NULL; + + if (argc > 1) + lsn = strtol(argv[1], (char **)NULL, 10); + + if (argc > 2) + psz_fname = argv[2]; + else + psz_fname = ISO9660_IMAGE; + + p_iso = iso9660_open (psz_fname); + + if (NULL == p_iso) { + fprintf(stderr, "Sorry, couldn't open %s as an ISO-9660 image\n", + psz_fname); + return 1; + } + + iso9660_ifs_find_lsn_with_path (p_iso, lsn, &psz_path); + if (psz_path != NULL) { + printf("File at LSN %u is %s\n", lsn, psz_path); + free(psz_path); + } + + iso9660_close(p_iso); + return 0; +} + diff --git a/lib/iso9660/iso9660_fs.c b/lib/iso9660/iso9660_fs.c index 52939be8..06e81717 100644 --- a/lib/iso9660/iso9660_fs.c +++ b/lib/iso9660/iso9660_fs.c @@ -1,5 +1,5 @@ /* - $Id: iso9660_fs.c,v 1.39 2007/08/11 16:26:14 rocky Exp $ + $Id: iso9660_fs.c,v 1.40 2007/08/12 00:56:10 rocky Exp $ Copyright (C) 2001 Herbert Valerio Riedel Copyright (C) 2003, 2004, 2005, 2006, 2007 Rocky Bernstein @@ -50,7 +50,7 @@ #include -static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.39 2007/08/11 16:26:14 rocky Exp $"; +static const char _rcsid[] = "$Id: iso9660_fs.c,v 1.40 2007/08/12 00:56:10 rocky Exp $"; /* Implementation of iso9660_t type */ struct _iso9660_s { @@ -1374,73 +1374,15 @@ iso9660_ifs_readdir (iso9660_t *p_iso, const char psz_path[]) } } -static iso9660_stat_t * -find_fs_lsn_recurse (CdIo_t *p_cdio, const char psz_path[], lsn_t lsn, - /*out*/ char **ppsz_full_filename) -{ - CdioList_t *entlist = iso9660_fs_readdir (p_cdio, psz_path, true); - 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); - const char *psz_filename = (char *) statbuf->filename; - const unsigned int len = strlen(psz_path) + strlen(psz_filename)+2; - - if (*ppsz_full_filename != NULL) free(*ppsz_full_filename); - *ppsz_full_filename = calloc(1, len); - snprintf (*ppsz_full_filename, len, "%s%s/", psz_path, psz_filename); - - if (statbuf->type == _STAT_DIR - && strcmp ((char *) statbuf->filename, ".") - && strcmp ((char *) statbuf->filename, "..")) - _cdio_list_append (dirlist, strdup(*ppsz_full_filename)); - - 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 *psz_path_prefix = _cdio_list_node_data (entnode); - iso9660_stat_t *ret_stat; - free(ppsz_full_filename); - ret_stat = find_fs_lsn_recurse (p_cdio, psz_path_prefix, lsn, - ppsz_full_filename); - - if (NULL != ret_stat) { - _cdio_list_free (dirlist, true); - return ret_stat; - } - } - - free(ppsz_full_filename); - *ppsz_full_filename = NULL; - _cdio_list_free (dirlist, true); - return NULL; -} +typedef CdioList_t * (iso9660_readdir_t) + (void *p_image, const char * psz_path); static iso9660_stat_t * -find_ifs_lsn_recurse (iso9660_t *p_iso, const char psz_path[], lsn_t lsn, - /*out*/ char **ppsz_full_filename) +find_lsn_recurse (void *p_image, iso9660_readdir_t iso9660_readdir, + const char psz_path[], lsn_t lsn, + /*out*/ char **ppsz_full_filename) { - CdioList_t *entlist = iso9660_ifs_readdir (p_iso, psz_path); + CdioList_t *entlist = iso9660_readdir (p_image, psz_path); CdioList_t *dirlist = _cdio_list_new (); CdioListNode_t *entnode; @@ -1483,9 +1425,11 @@ find_ifs_lsn_recurse (iso9660_t *p_iso, const char psz_path[], lsn_t lsn, { char *psz_path_prefix = _cdio_list_node_data (entnode); iso9660_stat_t *ret_stat; - free(ppsz_full_filename); - ret_stat = find_ifs_lsn_recurse (p_iso, psz_path_prefix, lsn, - ppsz_full_filename); + free(*ppsz_full_filename); + *ppsz_full_filename = NULL; + ret_stat = find_lsn_recurse (p_image, iso9660_readdir, + psz_path_prefix, lsn, + ppsz_full_filename); if (NULL != ret_stat) { _cdio_list_free (dirlist, true); @@ -1493,8 +1437,10 @@ find_ifs_lsn_recurse (iso9660_t *p_iso, const char psz_path[], lsn_t lsn, } } - free(ppsz_full_filename); - *ppsz_full_filename = NULL; + if (*ppsz_full_filename != NULL) { + free(*ppsz_full_filename); + *ppsz_full_filename = NULL; + } _cdio_list_free (dirlist, true); return NULL; } @@ -1509,7 +1455,8 @@ iso9660_stat_t * iso9660_fs_find_lsn(CdIo_t *p_cdio, lsn_t i_lsn) { char *psz_full_filename = NULL; - return find_fs_lsn_recurse (p_cdio, "/", i_lsn, &psz_full_filename); + return find_lsn_recurse (p_cdio, (iso9660_readdir_t *) iso9660_fs_readdir, + "/", i_lsn, &psz_full_filename); } /*! @@ -1522,7 +1469,8 @@ iso9660_stat_t * iso9660_fs_find_lsn_with_path(CdIo_t *p_cdio, lsn_t i_lsn, /*out*/ char **ppsz_full_filename) { - return find_fs_lsn_recurse (p_cdio, "/", i_lsn, ppsz_full_filename); + return find_lsn_recurse (p_cdio, (iso9660_readdir_t *) iso9660_fs_readdir, + "/", i_lsn, ppsz_full_filename); } /*! @@ -1535,7 +1483,8 @@ iso9660_stat_t * iso9660_ifs_find_lsn(iso9660_t *p_iso, lsn_t i_lsn) { char *psz_full_filename = NULL; - return find_ifs_lsn_recurse (p_iso, "/", i_lsn, &psz_full_filename); + return find_lsn_recurse (p_iso, (iso9660_readdir_t *) iso9660_ifs_readdir, + "/", i_lsn, &psz_full_filename); } /*! @@ -1548,7 +1497,8 @@ iso9660_stat_t * iso9660_ifs_find_lsn_with_path(iso9660_t *p_iso, lsn_t i_lsn, /*out*/ char **ppsz_full_filename) { - return find_ifs_lsn_recurse (p_iso, "/", i_lsn, ppsz_full_filename); + return find_lsn_recurse (p_iso, (iso9660_readdir_t *) iso9660_ifs_readdir, + "/", i_lsn, ppsz_full_filename); } /*!