Blame test/testisocd.c

Packit dd8086
/*
Packit dd8086
  Copyright (C) 2003-2008, 2011, 2017 Rocky Bernstein
Packit dd8086
  <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
/* Tests reading ISO 9660 info from a CD.  */
Packit dd8086
#include "portable.h"
Packit dd8086
#include "cdio_assert.h"
Packit dd8086
Packit dd8086
#ifdef HAVE_SYS_TYPES_H
Packit dd8086
#include <sys/types.h>
Packit dd8086
#endif
Packit dd8086
#ifdef HAVE_STDLIB_H
Packit dd8086
#include <stdlib.h>
Packit dd8086
#endif
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_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
Packit dd8086
#include <cdio/cdio.h>
Packit dd8086
#include <cdio/iso9660.h>
Packit dd8086
#include <cdio/cd_types.h>
Packit dd8086
Packit dd8086
#define SKIP_TEST_RC 77
Packit dd8086
Packit dd8086
int
Packit dd8086
main(int argc, const char *argv[])
Packit dd8086
{
Packit dd8086
  char **ppsz_cd_drives = NULL; /* All drives with an ISO9660 filesystem. */
Packit dd8086
  driver_id_t driver_id;  /* Driver ID found */
Packit dd8086
  char *psz_drive;        /* Name of drive */
Packit dd8086
  CdIo_t *p_cdio;
Packit dd8086
Packit dd8086
  /* See if we can find a device with a loaded CD-DA in it. If successful
Packit dd8086
     drive_id will be set.  */
Packit dd8086
  ppsz_cd_drives =
Packit dd8086
    cdio_get_devices_with_cap_ret(NULL, CDIO_FS_ANAL_ISO9660_ANY,
Packit dd8086
				  true, &driver_id);
Packit dd8086
Packit dd8086
  if (ppsz_cd_drives && ppsz_cd_drives[0]) {
Packit dd8086
    /* Found such a CD-ROM with an ISO 9660 filesystem. Use the first drive in
Packit dd8086
       the list. */
Packit dd8086
    psz_drive = strdup(ppsz_cd_drives[0]);
Packit dd8086
    /* Don't need a list of CD's with CD-DA's any more. */
Packit dd8086
    cdio_free_device_list(ppsz_cd_drives);
Packit dd8086
    ppsz_cd_drives = NULL;
Packit dd8086
  } else {
Packit dd8086
    printf("-- Unable find or access a CD-ROM drive with an ISO-9660 "
Packit dd8086
	   "filesystem.\n");
Packit dd8086
    if (ppsz_cd_drives)
Packit dd8086
      cdio_free_device_list(ppsz_cd_drives);
Packit dd8086
    exit(SKIP_TEST_RC);
Packit dd8086
  }
Packit dd8086
Packit dd8086
  p_cdio = cdio_open (psz_drive, driver_id);
Packit dd8086
  if (!p_cdio) {
Packit dd8086
    fprintf(stderr, "Sorry, couldn't open %s\n", psz_drive);
Packit dd8086
    free(psz_drive);
Packit dd8086
    return 1;
Packit dd8086
  } else {
Packit dd8086
    /* You make get different results looking up "/" versus "/." and the
Packit dd8086
       latter may give more complete information. "/" will take information
Packit dd8086
       from the PVD only, whereas "/." will force a directory read of "/" and
Packit dd8086
       find "." and in that Rock-Ridge information might be found which fills
Packit dd8086
       in more stat information that iso9660_fs_find_lsn also will find.
Packit dd8086
       . Ideally iso9660_fs_stat should be fixed. */
Packit dd8086
    iso9660_stat_t *p_statbuf = iso9660_fs_stat (p_cdio, "/.");
Packit dd8086
Packit dd8086
    free(psz_drive);
Packit dd8086
    if (NULL == p_statbuf) {
Packit dd8086
      fprintf(stderr,
Packit dd8086
	      "Could not get ISO-9660 file information for file /.\n");
Packit dd8086
      cdio_destroy(p_cdio);
Packit dd8086
      exit(2);
Packit dd8086
    } else {
Packit dd8086
      /* Now try getting the statbuf another way */
Packit dd8086
Packit dd8086
      char buf[ISO_BLOCKSIZE];
Packit dd8086
      char *psz_path = NULL;
Packit dd8086
      const lsn_t i_lsn = p_statbuf->lsn;
Packit dd8086
      iso9660_stat_t *p_statbuf2 = iso9660_fs_find_lsn (p_cdio, i_lsn);
Packit dd8086
      iso9660_stat_t *p_statbuf3 =
Packit dd8086
	iso9660_fs_find_lsn_with_path (p_cdio, i_lsn, &psz_path);
Packit dd8086
      int rc=0;
Packit dd8086
      const unsigned int statbuf_test_size = 100;
Packit dd8086
Packit dd8086
      /* Compare the two statbufs. */
Packit dd8086
      if (p_statbuf->lsn != p_statbuf2->lsn ||
Packit dd8086
	  p_statbuf->size != p_statbuf2->size ||
Packit dd8086
	  p_statbuf->type != p_statbuf2->type) {
Packit dd8086
	  fprintf(stderr, "File stat information between fs_stat and "
Packit dd8086
		  "fs_find_lsn isn't the same\n");
Packit dd8086
	  rc=3;
Packit dd8086
	  goto exit;
Packit dd8086
      }
Packit dd8086
Packit dd8086
      cdio_assert(statbuf_test_size < sizeof(iso9660_stat_t));
Packit dd8086
      if (0 != memcmp(p_statbuf3, p_statbuf2, statbuf_test_size)) {
Packit dd8086
	  fprintf(stderr, "File stat information between fs_find_lsn and "
Packit dd8086
		  "fs_find_lsn_with_path isn't the same\n");
Packit dd8086
	  rc=4;
Packit dd8086
	  goto exit;
Packit dd8086
      }
Packit dd8086
Packit dd8086
      if (psz_path != NULL) {
Packit dd8086
	if (0 != strncmp("/./", psz_path, strlen("/./"))) {
Packit dd8086
	  fprintf(stderr, "Path returned for fs_find_lsn_with_path "
Packit dd8086
		  "is not correct should be /./, is %s\n", psz_path);
Packit dd8086
	  free(psz_path);
Packit dd8086
	  rc=5;
Packit dd8086
	  goto exit;
Packit dd8086
	}
Packit dd8086
      } else {
Packit dd8086
	fprintf(stderr, "Path returned for fs_find_lsn_with_path is NULL\n");
Packit dd8086
	rc=6;
Packit dd8086
	goto exit;
Packit dd8086
      }
Packit dd8086
Packit dd8086
      /* Try reading from the directory. */
Packit dd8086
      memset (buf, 0, ISO_BLOCKSIZE);
Packit dd8086
      if ( 0 != cdio_read_data_sectors (p_cdio, buf, i_lsn, ISO_BLOCKSIZE, 1) )
Packit dd8086
	{
Packit dd8086
	  fprintf(stderr, "Error reading ISO 9660 file at lsn %lu\n",
Packit dd8086
		  (long unsigned int) p_statbuf->lsn);
Packit dd8086
	  rc=7;
Packit dd8086
	}
Packit dd8086
    exit:
Packit dd8086
      iso9660_stat_free(p_statbuf);
Packit dd8086
      iso9660_stat_free(p_statbuf2);
Packit dd8086
      iso9660_stat_free(p_statbuf3);
Packit dd8086
      cdio_destroy(p_cdio);
Packit dd8086
      exit(rc);
Packit dd8086
    }
Packit dd8086
  }
Packit dd8086
Packit dd8086
  exit(0);
Packit dd8086
}