2003-12-24 11:09:26 +00:00
|
|
|
/*
|
2005-02-20 03:15:42 +00:00
|
|
|
$Id: iso2.c,v 1.7 2005/02/20 03:15:42 rocky Exp $
|
2003-12-24 11:09:26 +00:00
|
|
|
|
2005-01-04 04:40:22 +00:00
|
|
|
Copyright (C) 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>
|
2003-12-24 11:09:26 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
*/
|
|
|
|
|
|
2004-01-29 04:22:49 +00:00
|
|
|
/* Simple program to show using libiso9660 to extract a file from a
|
2005-02-20 03:15:42 +00:00
|
|
|
cue/bin CD-IMAGE.
|
|
|
|
|
|
|
|
|
|
This program can be compiled with either a C or C++ compiler. In
|
|
|
|
|
the distributuion we perfer C++ just to make sure we haven't broken
|
|
|
|
|
things on the C++ side.
|
2003-12-24 11:09:26 +00:00
|
|
|
*/
|
2004-03-11 01:31:32 +00:00
|
|
|
|
2003-12-24 11:09:26 +00:00
|
|
|
/* This is the CD-image with an ISO-9660 filesystem */
|
|
|
|
|
#define ISO9660_IMAGE_PATH "../"
|
|
|
|
|
#define ISO9660_IMAGE ISO9660_IMAGE_PATH "test/isofs-m1.cue"
|
|
|
|
|
|
2005-02-20 03:15:42 +00:00
|
|
|
#define ISO9660_PATH "/"
|
|
|
|
|
#define ISO9660_FILENAME "COPYING"
|
2003-12-24 11:09:26 +00:00
|
|
|
#define LOCAL_FILENAME "copying"
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
# include "config.h"
|
|
|
|
|
#endif
|
2004-03-21 00:57:03 +00:00
|
|
|
|
2004-10-31 13:58:44 +00:00
|
|
|
#include "portable.h"
|
|
|
|
|
|
2004-03-21 00:57:03 +00:00
|
|
|
#include <cdio/cdio.h>
|
|
|
|
|
#include <cdio/iso9660.h>
|
|
|
|
|
|
2004-03-20 22:44:14 +00:00
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2003-12-24 11:09:26 +00:00
|
|
|
#include <sys/types.h>
|
2004-03-20 22:44:14 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#endif
|
2004-03-21 00:57:03 +00:00
|
|
|
#ifdef HAVE_STDIO_H
|
2003-12-24 11:09:26 +00:00
|
|
|
#include <stdio.h>
|
2004-03-21 00:57:03 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_ERRNO_H
|
2003-12-24 11:09:26 +00:00
|
|
|
#include <errno.h>
|
2004-03-21 00:57:03 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_STRING_H
|
2003-12-24 11:09:26 +00:00
|
|
|
#include <string.h>
|
2004-03-21 00:57:03 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-12-24 11:09:26 +00:00
|
|
|
#include <unistd.h>
|
2004-03-21 00:57:03 +00:00
|
|
|
#endif
|
|
|
|
|
#ifdef HAVE_SYS_TYPES_H
|
2003-12-24 11:09:26 +00:00
|
|
|
#include <sys/types.h>
|
2004-03-21 00:57:03 +00:00
|
|
|
#endif
|
2003-12-24 11:09:26 +00:00
|
|
|
|
2004-03-20 22:44:14 +00:00
|
|
|
#define my_exit(rc) \
|
2004-08-27 11:34:44 +00:00
|
|
|
fclose (p_outfd); \
|
|
|
|
|
free(p_statbuf); \
|
|
|
|
|
cdio_destroy(p_cdio); \
|
2004-03-20 22:44:14 +00:00
|
|
|
return rc; \
|
|
|
|
|
|
|
|
|
|
|
2003-12-24 11:09:26 +00:00
|
|
|
int
|
|
|
|
|
main(int argc, const char *argv[])
|
|
|
|
|
{
|
2004-08-27 11:34:44 +00:00
|
|
|
iso9660_stat_t *p_statbuf;
|
|
|
|
|
FILE *p_outfd;
|
2003-12-24 11:09:26 +00:00
|
|
|
int i;
|
2005-02-20 03:15:42 +00:00
|
|
|
char const *psz_image;
|
2005-02-03 07:32:32 +00:00
|
|
|
char const *psz_fname;
|
2005-02-20 03:15:42 +00:00
|
|
|
char translated_name[256];
|
|
|
|
|
char untranslated_name[256] = ISO9660_PATH;
|
2005-02-03 07:32:32 +00:00
|
|
|
CdIo_t *p_cdio;
|
2003-12-24 11:09:26 +00:00
|
|
|
|
2005-02-03 07:32:32 +00:00
|
|
|
if (argc > 1)
|
2005-02-20 03:15:42 +00:00
|
|
|
psz_image = argv[1];
|
|
|
|
|
else
|
|
|
|
|
psz_image = ISO9660_IMAGE;
|
|
|
|
|
|
|
|
|
|
if (argc > 2)
|
|
|
|
|
psz_fname = argv[2];
|
2005-02-03 07:32:32 +00:00
|
|
|
else
|
2005-02-20 03:15:42 +00:00
|
|
|
psz_fname = ISO9660_FILENAME;
|
2005-02-03 07:32:32 +00:00
|
|
|
|
2005-02-20 03:15:42 +00:00
|
|
|
strcat(untranslated_name, psz_fname);
|
|
|
|
|
|
|
|
|
|
p_cdio = cdio_open (psz_image, DRIVER_BINCUE);
|
2004-08-27 11:34:44 +00:00
|
|
|
if (NULL == p_cdio) {
|
2005-02-03 07:32:32 +00:00
|
|
|
fprintf(stderr, "Sorry, couldn't open %s as a BIN/CUE image\n",
|
2005-02-20 03:15:42 +00:00
|
|
|
psz_image);
|
2003-12-24 11:09:26 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-20 03:15:42 +00:00
|
|
|
p_statbuf = iso9660_fs_stat (p_cdio, untranslated_name);
|
2003-12-24 11:09:26 +00:00
|
|
|
|
2004-08-27 11:34:44 +00:00
|
|
|
if (NULL == p_statbuf)
|
2003-12-24 11:09:26 +00:00
|
|
|
{
|
|
|
|
|
fprintf(stderr,
|
|
|
|
|
"Could not get ISO-9660 file information for file %s\n",
|
2005-02-20 03:15:42 +00:00
|
|
|
untranslated_name);
|
2004-08-27 11:34:44 +00:00
|
|
|
cdio_destroy(p_cdio);
|
2003-12-24 11:09:26 +00:00
|
|
|
return 2;
|
|
|
|
|
}
|
|
|
|
|
|
2005-02-20 03:15:42 +00:00
|
|
|
iso9660_name_translate(psz_fname, translated_name);
|
|
|
|
|
|
|
|
|
|
if (!(p_outfd = fopen (translated_name, "wb")))
|
2003-12-24 11:09:26 +00:00
|
|
|
{
|
|
|
|
|
perror ("fopen()");
|
2004-08-27 11:34:44 +00:00
|
|
|
cdio_destroy(p_cdio);
|
|
|
|
|
free(p_statbuf);
|
2004-03-21 00:57:03 +00:00
|
|
|
return 3;
|
2003-12-24 11:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */
|
2004-08-27 11:34:44 +00:00
|
|
|
for (i = 0; i < p_statbuf->size; i += ISO_BLOCKSIZE)
|
2003-12-24 11:09:26 +00:00
|
|
|
{
|
|
|
|
|
char buf[ISO_BLOCKSIZE];
|
|
|
|
|
|
|
|
|
|
memset (buf, 0, ISO_BLOCKSIZE);
|
|
|
|
|
|
2005-02-20 03:15:42 +00:00
|
|
|
if ( 0 != cdio_read_data_sectors (p_cdio, buf,
|
2004-08-27 11:34:44 +00:00
|
|
|
p_statbuf->lsn + (i / ISO_BLOCKSIZE),
|
2005-02-20 03:15:42 +00:00
|
|
|
ISO_BLOCKSIZE, 1) )
|
2003-12-24 11:09:26 +00:00
|
|
|
{
|
2004-02-04 10:22:01 +00:00
|
|
|
fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n",
|
2004-08-27 11:34:44 +00:00
|
|
|
(long unsigned int) p_statbuf->lsn + (i / ISO_BLOCKSIZE));
|
2004-03-20 22:44:14 +00:00
|
|
|
my_exit(4);
|
2003-12-24 11:09:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2004-08-27 11:34:44 +00:00
|
|
|
fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd);
|
2003-12-24 11:09:26 +00:00
|
|
|
|
2004-08-27 11:34:44 +00:00
|
|
|
if (ferror (p_outfd))
|
2003-12-24 11:09:26 +00:00
|
|
|
{
|
|
|
|
|
perror ("fwrite()");
|
2004-03-20 22:44:14 +00:00
|
|
|
my_exit(5);
|
2003-12-24 11:09:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2004-08-27 11:34:44 +00:00
|
|
|
fflush (p_outfd);
|
2003-12-24 11:09:26 +00:00
|
|
|
|
|
|
|
|
/* Make sure the file size has the exact same byte size. Without the
|
|
|
|
|
truncate below, the file will a multiple of ISO_BLOCKSIZE.
|
|
|
|
|
*/
|
2004-08-27 11:34:44 +00:00
|
|
|
if (ftruncate (fileno (p_outfd), p_statbuf->size))
|
2003-12-24 11:09:26 +00:00
|
|
|
perror ("ftruncate()");
|
|
|
|
|
|
2005-02-20 03:15:42 +00:00
|
|
|
printf("Extraction of file '%s' from '%s' successful.\n",
|
|
|
|
|
translated_name, untranslated_name);
|
2004-11-22 01:03:52 +00:00
|
|
|
|
2004-03-20 22:44:14 +00:00
|
|
|
my_exit(0);
|
2003-12-24 11:09:26 +00:00
|
|
|
}
|