2003-03-29 17:32:00 +00:00
|
|
|
|
/*
|
2004-06-26 00:39:00 +00:00
|
|
|
|
$Id: _cdio_generic.c,v 1.19 2004/06/26 00:39:00 rocky Exp $
|
2003-03-29 17:32:00 +00:00
|
|
|
|
|
|
|
|
|
|
Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
|
2004-03-20 22:46:56 +00:00
|
|
|
|
Copyright (C) 2002, 2003, 2004 Rocky Bernstein <rocky@panix.com>
|
2003-03-29 17:32:00 +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
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/* This file contains Linux-specific code and implements low-level
|
|
|
|
|
|
control of the CD drive.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
|
# include "config.h"
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2004-06-26 00:39:00 +00:00
|
|
|
|
static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.19 2004/06/26 00:39:00 rocky Exp $";
|
2003-03-29 17:32:00 +00:00
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
#include <errno.h>
|
2004-06-19 19:15:15 +00:00
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
#endif /*HAVE_UNISTD_H*/
|
|
|
|
|
|
|
2003-03-29 17:32:00 +00:00
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
2003-04-22 12:09:08 +00:00
|
|
|
|
#include <cdio/sector.h>
|
|
|
|
|
|
#include <cdio/util.h>
|
2004-06-19 19:15:15 +00:00
|
|
|
|
#include <cdio/logging.h>
|
2003-04-07 11:24:04 +00:00
|
|
|
|
#include "cdio_assert.h"
|
|
|
|
|
|
#include "cdio_private.h"
|
|
|
|
|
|
#include "_cdio_stdio.h"
|
|
|
|
|
|
|
2003-03-30 13:01:22 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Eject media -- there's nothing to do here. We always return 2.
|
|
|
|
|
|
Should we also free resources?
|
|
|
|
|
|
*/
|
|
|
|
|
|
int
|
|
|
|
|
|
cdio_generic_bogus_eject_media (void *user_data) {
|
|
|
|
|
|
/* Sort of a stub here. Perhaps log a message? */
|
|
|
|
|
|
return 2;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-29 17:32:00 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Release and free resources associated with cd.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
cdio_generic_free (void *user_data)
|
|
|
|
|
|
{
|
2004-05-31 12:29:09 +00:00
|
|
|
|
generic_img_private_t *env = user_data;
|
2003-03-29 17:32:00 +00:00
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
if (NULL == env) return;
|
|
|
|
|
|
free (env->source_name);
|
2003-03-29 17:32:00 +00:00
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
if (env->fd >= 0)
|
|
|
|
|
|
close (env->fd);
|
2003-03-29 17:32:00 +00:00
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
free (env);
|
2003-03-29 17:32:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2003-03-30 13:01:22 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Initialize CD device.
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool
|
|
|
|
|
|
cdio_generic_init (void *user_data)
|
|
|
|
|
|
{
|
2004-05-31 12:29:09 +00:00
|
|
|
|
generic_img_private_t *_env = user_data;
|
|
|
|
|
|
if (_env->init) {
|
2004-06-25 21:10:36 +00:00
|
|
|
|
cdio_warn ("init called more than once");
|
2003-03-30 13:01:22 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
_env->fd = open (_env->source_name, O_RDONLY, 0);
|
2003-03-30 13:01:22 +00:00
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
if (_env->fd < 0)
|
2003-03-30 13:01:22 +00:00
|
|
|
|
{
|
2004-05-31 12:29:09 +00:00
|
|
|
|
cdio_warn ("open (%s): %s", _env->source_name, strerror (errno));
|
2003-03-30 13:01:22 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
_env->init = true;
|
|
|
|
|
|
_env->toc_init = false;
|
2003-03-30 13:01:22 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2004-06-26 00:39:00 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Reads a single form1 sector from cd device into data starting
|
|
|
|
|
|
from lsn. Returns 0 if no error.
|
|
|
|
|
|
*/
|
|
|
|
|
|
int
|
|
|
|
|
|
cdio_generic_read_form1_sector (void * user_data, void *data, lsn_t lsn)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (0 > cdio_generic_lseek(user_data, CDIO_CD_FRAMESIZE*lsn, SEEK_SET))
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
if (0 > cdio_generic_read(user_data, data, CDIO_CD_FRAMESIZE))
|
|
|
|
|
|
return -1;
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-03-29 17:32:00 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Reads into buf the next size bytes.
|
|
|
|
|
|
Returns -1 on error.
|
2004-05-13 01:50:10 +00:00
|
|
|
|
Is in fact libc's lseek().
|
2003-03-29 17:32:00 +00:00
|
|
|
|
*/
|
|
|
|
|
|
off_t
|
|
|
|
|
|
cdio_generic_lseek (void *user_data, off_t offset, int whence)
|
|
|
|
|
|
{
|
2004-05-31 12:29:09 +00:00
|
|
|
|
generic_img_private_t *_env = user_data;
|
|
|
|
|
|
return lseek(_env->fd, offset, whence);
|
2003-03-29 17:32:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
Reads into buf the next size bytes.
|
|
|
|
|
|
Returns -1 on error.
|
|
|
|
|
|
Is in fact libc's read().
|
|
|
|
|
|
*/
|
|
|
|
|
|
ssize_t
|
|
|
|
|
|
cdio_generic_read (void *user_data, void *buf, size_t size)
|
|
|
|
|
|
{
|
2004-05-31 12:29:09 +00:00
|
|
|
|
generic_img_private_t *_env = user_data;
|
|
|
|
|
|
return read(_env->fd, buf, size);
|
2003-03-29 17:32:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2003-04-07 11:24:04 +00:00
|
|
|
|
/*!
|
|
|
|
|
|
Release and free resources associated with stream or disk image.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
2004-02-07 18:53:02 +00:00
|
|
|
|
cdio_generic_stdio_free (void *user_data)
|
2003-03-29 17:32:00 +00:00
|
|
|
|
{
|
2004-05-31 12:29:09 +00:00
|
|
|
|
generic_img_private_t *_env = user_data;
|
2003-04-07 11:24:04 +00:00
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
if (NULL == _env) return;
|
|
|
|
|
|
if (NULL != _env->source_name)
|
|
|
|
|
|
free (_env->source_name);
|
2003-04-07 11:24:04 +00:00
|
|
|
|
|
2004-05-31 12:29:09 +00:00
|
|
|
|
if (_env->data_source)
|
|
|
|
|
|
cdio_stdio_destroy (_env->data_source);
|
2003-03-29 17:32:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2003-06-07 16:49:50 +00:00
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
Return true if source_name could be a device containing a CD-ROM.
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool
|
|
|
|
|
|
cdio_is_device_generic(const char *source_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct stat buf;
|
|
|
|
|
|
if (0 != stat(source_name, &buf)) {
|
2003-06-12 04:46:27 +00:00
|
|
|
|
cdio_warn ("Can't get file status for %s:\n%s", source_name,
|
2003-06-07 16:49:50 +00:00
|
|
|
|
strerror(errno));
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode));
|
|
|
|
|
|
}
|
2003-06-12 04:46:27 +00:00
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
Like above, but don't give a warning device doesn't exist.
|
|
|
|
|
|
*/
|
|
|
|
|
|
bool
|
|
|
|
|
|
cdio_is_device_quiet_generic(const char *source_name)
|
|
|
|
|
|
{
|
|
|
|
|
|
struct stat buf;
|
|
|
|
|
|
if (0 != stat(source_name, &buf)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
return (S_ISBLK(buf.st_mode) || S_ISCHR(buf.st_mode));
|
|
|
|
|
|
}
|
2003-09-28 17:14:20 +00:00
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
|
Add/allocate a drive to the end of drives.
|
|
|
|
|
|
Use cdio_free_device_list() to free this device_list.
|
|
|
|
|
|
*/
|
|
|
|
|
|
void
|
|
|
|
|
|
cdio_add_device_list(char **device_list[], const char *drive, int *num_drives)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (NULL != drive) {
|
|
|
|
|
|
unsigned int j;
|
2004-03-20 22:46:56 +00:00
|
|
|
|
|
|
|
|
|
|
/* Check if drive is already in list. */
|
2003-09-28 17:14:20 +00:00
|
|
|
|
for (j=0; j<*num_drives; j++) {
|
|
|
|
|
|
if (strcmp((*device_list)[j], drive) == 0) break;
|
|
|
|
|
|
}
|
2004-03-20 22:46:56 +00:00
|
|
|
|
|
2003-09-28 17:14:20 +00:00
|
|
|
|
if (j==*num_drives) {
|
2004-03-20 22:46:56 +00:00
|
|
|
|
/* Drive not in list. Add it. */
|
2003-09-28 17:14:20 +00:00
|
|
|
|
(*num_drives)++;
|
2004-03-20 22:46:56 +00:00
|
|
|
|
if (*device_list) {
|
|
|
|
|
|
*device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
/* num_drives should be 0. Add assert? */
|
|
|
|
|
|
*device_list = malloc((*num_drives) * sizeof(char *));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2003-09-28 17:14:20 +00:00
|
|
|
|
(*device_list)[*num_drives-1] = strdup(drive);
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
(*num_drives)++;
|
2004-03-20 22:46:56 +00:00
|
|
|
|
if (*device_list) {
|
|
|
|
|
|
*device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
*device_list = malloc((*num_drives) * sizeof(char *));
|
|
|
|
|
|
}
|
2003-09-28 17:14:20 +00:00
|
|
|
|
(*device_list)[*num_drives-1] = NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|