Blame example/C++/isofile.cpp

Packit dd8086
/*
Packit dd8086
  Copyright (C) 2004, 2006, 2008 Rocky Bernstein <rocky@gnu.org>
Packit dd8086
  
Packit dd8086
  This program is free software: you can redistribute it and/or modify
Packit dd8086
  it under the terms of the GNU General Public License as published by
Packit dd8086
  the Free Software Foundation, either version 3 of the License, or
Packit dd8086
  (at your option) any later version.
Packit dd8086
Packit dd8086
  This program is distributed in the hope that it will be useful,
Packit dd8086
  but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit dd8086
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit dd8086
  GNU General Public License for more details.
Packit dd8086
Packit dd8086
  You should have received a copy of the GNU General Public License
Packit dd8086
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit dd8086
*/
Packit dd8086
Packit dd8086
/* Simple program to show using libiso9660 to extract a file from an
Packit dd8086
   ISO-9660 image.
Packit dd8086
Packit dd8086
   If a single argument is given, it is used as the ISO 9660 image to
Packit dd8086
   use in the extraction. Otherwise a compiled in default ISO 9660 image
Packit dd8086
   name (that comes with the libcdio distribution) will be used.
Packit dd8086
 */
Packit dd8086
Packit dd8086
/* This is the ISO 9660 image. */
Packit dd8086
#define ISO9660_IMAGE_PATH "../../"
Packit dd8086
#define ISO9660_IMAGE ISO9660_IMAGE_PATH "test/copying.iso"
Packit dd8086
Packit dd8086
#define LOCAL_FILENAME "copying"
Packit dd8086
Packit dd8086
#include "portable.h"
Packit dd8086
Packit dd8086
#ifdef HAVE_STDIO_H
Packit dd8086
#include <stdio.h>
Packit dd8086
#endif
Packit dd8086
#ifdef HAVE_ERRNO_H
Packit dd8086
#include <errno.h>
Packit dd8086
#endif
Packit dd8086
#ifdef HAVE_STDLIB_H
Packit dd8086
#include <stdlib.h>
Packit dd8086
#endif
Packit dd8086
#ifdef HAVE_STRING_H
Packit dd8086
#include <string.h>
Packit dd8086
#endif
Packit dd8086
#ifdef HAVE_UNISTD_H
Packit dd8086
#include <unistd.h>
Packit dd8086
#endif
Packit dd8086
#ifdef HAVE_SYS_TYPES_H
Packit dd8086
#include <sys/types.h>
Packit dd8086
#endif
Packit dd8086
Packit dd8086
#include <cdio/cdio.h>
Packit dd8086
#include <cdio/iso9660.h>
Packit dd8086
Packit dd8086
#define CEILING(x, y) ((x+(y-1))/y)
Packit dd8086
Packit dd8086
#define my_exit(rc)				\
Packit dd8086
  fclose (p_outfd);				\
Packit dd8086
  free(p_statbuf);				\
Packit dd8086
  iso9660_close(p_iso);				\
Packit dd8086
  return rc;					\
Packit dd8086
Packit dd8086
int
Packit dd8086
main(int argc, const char *argv[])
Packit dd8086
{
Packit dd8086
  iso9660_stat_t *p_statbuf;
Packit dd8086
  FILE *p_outfd;
Packit dd8086
  unsigned int i;
Packit dd8086
  char const *psz_image;
Packit dd8086
  char const *psz_fname;
Packit dd8086
  iso9660_t *p_iso;
Packit dd8086
Packit dd8086
  if (argc > 3) {
Packit dd8086
    printf("usage %s [ISO9660-image.ISO [filename]]\n", argv[0]);
Packit dd8086
    printf("Extracts filename from ISO-9660-image.ISO.\n");
Packit dd8086
    return 1;
Packit dd8086
  }
Packit dd8086
  
Packit dd8086
  if (argc > 1) 
Packit dd8086
    psz_image = argv[1];
Packit dd8086
  else 
Packit dd8086
    psz_image = ISO9660_IMAGE;
Packit dd8086
Packit dd8086
  if (argc > 2) 
Packit dd8086
    psz_fname = argv[2];
Packit dd8086
  else 
Packit dd8086
    psz_fname = LOCAL_FILENAME;
Packit dd8086
Packit dd8086
  p_iso = iso9660_open (psz_image);
Packit dd8086
  
Packit dd8086
  if (NULL == p_iso) {
Packit dd8086
    fprintf(stderr, "Sorry, couldn't open ISO 9660 image %s\n", psz_image);
Packit dd8086
    return 1;
Packit dd8086
  }
Packit dd8086
Packit dd8086
  p_statbuf = iso9660_ifs_stat_translate (p_iso, psz_fname);
Packit dd8086
Packit dd8086
  if (NULL == p_statbuf) 
Packit dd8086
    {
Packit dd8086
      fprintf(stderr, 
Packit dd8086
	      "Could not get ISO-9660 file information for file %s\n",
Packit dd8086
	      psz_fname);
Packit dd8086
      iso9660_close(p_iso);
Packit dd8086
      return 2;
Packit dd8086
    }
Packit dd8086
Packit dd8086
  if (!(p_outfd = fopen (psz_fname, "wb")))
Packit dd8086
    {
Packit dd8086
      perror ("fopen()");
Packit dd8086
      free(p_statbuf);
Packit dd8086
      iso9660_close(p_iso);
Packit dd8086
      return 3;
Packit dd8086
    }
Packit dd8086
Packit dd8086
  /* Copy the blocks from the ISO-9660 filesystem to the local filesystem. */
Packit dd8086
  {
Packit dd8086
    const unsigned int i_blocks = CEILING(p_statbuf->size, ISO_BLOCKSIZE);
Packit dd8086
    for (i = 0; i < i_blocks ; i++) 
Packit dd8086
    {
Packit dd8086
      char buf[ISO_BLOCKSIZE];
Packit dd8086
      const lsn_t lsn = p_statbuf->lsn + i;
Packit dd8086
Packit dd8086
      memset (buf, 0, ISO_BLOCKSIZE);
Packit dd8086
      
Packit dd8086
      if ( ISO_BLOCKSIZE != iso9660_iso_seek_read (p_iso, buf, lsn, 1) )
Packit dd8086
      {
Packit dd8086
	fprintf(stderr, "Error reading ISO 9660 file %s at LSN %lu\n",
Packit dd8086
		psz_fname, (long unsigned int) lsn);
Packit dd8086
	my_exit(4);
Packit dd8086
      }
Packit dd8086
      
Packit dd8086
      fwrite (buf, ISO_BLOCKSIZE, 1, p_outfd);
Packit dd8086
      
Packit dd8086
      if (ferror (p_outfd))
Packit dd8086
	{
Packit dd8086
	  perror ("fwrite()");
Packit dd8086
	  my_exit(5);
Packit dd8086
	}
Packit dd8086
    }
Packit dd8086
  }
Packit dd8086
  
Packit dd8086
  fflush (p_outfd);
Packit dd8086
Packit dd8086
  /* Make sure the file size has the exact same byte size. Without the
Packit dd8086
     truncate below, the file will a multiple of ISO_BLOCKSIZE.
Packit dd8086
   */
Packit dd8086
  if (ftruncate (fileno (p_outfd), p_statbuf->size))
Packit dd8086
    perror ("ftruncate()");
Packit dd8086
Packit dd8086
  printf("Extraction of file '%s' from %s successful.\n", 
Packit dd8086
	 psz_fname, psz_image);
Packit dd8086
Packit dd8086
  my_exit(0);
Packit dd8086
}