* Better drive detection for Linux

* cdio_add_device_list() now adds devices with symlinks only once
This commit is contained in:
gmerlin
2006-11-27 19:31:37 +00:00
parent d5fc70594a
commit efc2151bc1
5 changed files with 82 additions and 73 deletions

View File

@@ -19,7 +19,7 @@ define(RELEASE_NUM, 79cvs)
define(CDIO_VERSION_STR, 0.$1)
AC_PREREQ(2.52)
AC_REVISION([$Id: configure.ac,v 1.203 2006/11/16 00:47:28 rocky Exp $])dnl
AC_REVISION([$Id: configure.ac,v 1.204 2006/11/27 19:31:37 gmerlin Exp $])dnl
AC_INIT(libcdio, CDIO_VERSION_STR(RELEASE_NUM))
AC_CONFIG_SRCDIR(src/cd-info.c)
@@ -32,7 +32,7 @@ AM_INIT_AUTOMAKE([1.7])
AC_CANONICAL_HOST
AM_CONFIG_HEADER(config.h)
LIBCDIO_VERSION_NUM=`echo RELEASE_NUM | cut -d . -f 1`
LIBCDIO_VERSION_NUM=`echo RELEASE_NUM | cut -d . -f 1 | tr -d a-z`
AC_SUBST(LIBCDIO_VERSION_NUM)
AM_MISSING_PROG(HELP2MAN, help2man, $missing_dir)
@@ -480,7 +480,7 @@ AC_SUBST(LIBCDIO_SOURCE_PATH)
AC_CHECK_FUNCS( [bzero drand48 ftruncate geteuid getgid \
getuid getpwuid gettimeofday lstat memcpy memset \
rand seteuid setegid snprintf tzset vsnprintf] )
rand seteuid setegid snprintf tzset vsnprintf readlink] )
AC_CHECK_MEMBER([struct tm.tm_gmtoff],
[AC_DEFINE(HAVE_TM_GMTOFF, 1,

View File

@@ -1,5 +1,5 @@
/*
$Id: util.h,v 1.10 2006/03/18 00:53:20 rocky Exp $
$Id: util.h,v 1.11 2006/11/27 19:31:37 gmerlin Exp $
Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
Copyright (C) 2004, 2005, 2006 Rocky Bernstein <rocky@panix.com>
@@ -100,6 +100,8 @@ _cdio_strsplit(const char str[], char delim);
uint8_t cdio_to_bcd8(uint8_t n);
uint8_t cdio_from_bcd8(uint8_t p);
void cdio_follow_symlink (const char * src, char * dst);
#ifdef __cplusplus
}
#endif

View File

@@ -1,5 +1,5 @@
/*
$Id: _cdio_generic.c,v 1.22 2006/03/26 02:35:26 rocky Exp $
$Id: _cdio_generic.c,v 1.23 2006/11/27 19:31:37 gmerlin Exp $
Copyright (C) 2004, 2005, 2006
Rocky Bernstein <rockyb@users.sourceforge.net>
@@ -26,7 +26,7 @@
# include "config.h"
#endif
static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.22 2006/03/26 02:35:26 rocky Exp $";
static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.23 2006/11/27 19:31:37 gmerlin Exp $";
#include <stdio.h>
#include <stdlib.h>
@@ -38,6 +38,7 @@ static const char _rcsid[] = "$Id: _cdio_generic.c,v 1.22 2006/03/26 02:35:26 ro
#endif /*HAVE_UNISTD_H*/
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
@@ -221,24 +222,22 @@ cdio_add_device_list(char **device_list[], const char *drive,
{
if (NULL != drive) {
unsigned int j;
char real_device_1[PATH_MAX];
char real_device_2[PATH_MAX];
cdio_follow_symlink(drive, real_device_1);
/* Check if drive is already in list. */
for (j=0; j<*num_drives; j++) {
if (strcmp((*device_list)[j], drive) == 0) break;
cdio_follow_symlink((*device_list)[j], real_device_2);
if (strcmp(real_device_1, real_device_2) == 0) break;
}
if (j==*num_drives) {
/* Drive not in list. Add it. */
(*num_drives)++;
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 *));
*device_list = realloc(*device_list, (*num_drives) * sizeof(char *));
(*device_list)[*num_drives-1] = strdup(drive);
}
(*device_list)[*num_drives-1] = strdup(drive);
}
} else {
(*num_drives)++;
if (*device_list) {

View File

@@ -1,5 +1,5 @@
/*
$Id: gnu_linux.c,v 1.26 2006/10/21 11:38:16 rocky Exp $
$Id: gnu_linux.c,v 1.27 2006/11/27 19:31:37 gmerlin Exp $
Copyright (C) 2001 Herbert Valerio Riedel <hvr@gnu.org>
Copyright (C) 2002, 2003, 2004, 2005, 2006 Rocky Bernstein
@@ -28,7 +28,7 @@
# include "config.h"
#endif
static const char _rcsid[] = "$Id: gnu_linux.c,v 1.26 2006/10/21 11:38:16 rocky Exp $";
static const char _rcsid[] = "$Id: gnu_linux.c,v 1.27 2006/11/27 19:31:37 gmerlin Exp $";
#include <string.h>
#include <limits.h>
@@ -595,30 +595,6 @@ get_track_msf_linux(void *p_user_data, track_t i_track, msf_t *msf)
}
}
/*!
Follow symlinks until we have the real device file
(idea taken from libunieject).
*/
static void follow_symlink (const char * src, char * dst) {
char tmp_src[PATH_MAX];
char tmp_dst[PATH_MAX];
int len;
strcpy(tmp_src, src);
while(1) {
len = readlink(tmp_src, tmp_dst, PATH_MAX);
if(len < 0) {
strcpy(dst, tmp_src);
return;
}
else {
tmp_dst[len] = '\0';
strcpy(tmp_src, tmp_dst);
}
}
}
/*!
Check, if a device is mounted and return the target (=mountpoint)
@@ -642,12 +618,12 @@ static int is_mounted (const char * device, char * target) {
if(!fp) return 0;
/* Get real device */
follow_symlink(device, real_device_1);
cdio_follow_symlink(device, real_device_1);
/* Read entries */
while ( fscanf(fp, "%s %s %*s %*s %*d %*d\n", file_device, file_target) != EOF ) {
follow_symlink(file_device, real_device_2);
cdio_follow_symlink(file_device, real_device_2);
if(!strcmp(real_device_1, real_device_2)) {
strcpy(target, file_target);
fclose(fp);
@@ -1351,9 +1327,20 @@ set_arg_linux (void *p_user_data, const char key[], const char value[])
static char checklist1[][40] = {
{"cdrom"}, {"dvd"}, {""}
};
static char checklist2[][40] = {
{"?a hd?"}, {"?0 scd?"}, {"?0 sr?"}, {""}
};
static struct
{
const char * format;
int num_min;
int num_max;
}
checklist2[] =
{
{ "/dev/hd%c", 'a', 'z' },
{ "/dev/scd%d", 0, 27 },
{ "/dev/sr%d", 0, 27 },
{ /* End of array */ }
};
/* Set CD-ROM drive speed */
static driver_return_code_t
@@ -1379,15 +1366,14 @@ cdio_get_devices_linux (void)
unsigned int i;
char drive[40];
char *ret_drive;
bool exists;
char **drives = NULL;
unsigned int num_drives=0;
/* Scan the system for CD-ROM drives.
*/
for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
sprintf(drive, "/dev/%s", checklist1[i]);
if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
if ( is_cdrom_linux(drive, NULL) > 0 ) {
cdio_add_device_list(&drives, drive, &num_drives);
}
}
@@ -1407,17 +1393,11 @@ cdio_get_devices_linux (void)
/* Scan the system for CD-ROM drives.
Not always 100% reliable, so use the USE_MNTENT code above first.
*/
for ( i=0; strlen(checklist2[i]) > 0; ++i ) {
for ( i=0; checklist2[i].format; ++i ) {
unsigned int j;
char *insert;
exists = true;
for ( j=checklist2[i][1]; exists; ++j ) {
sprintf(drive, "/dev/%s", &checklist2[i][3]);
insert = strchr(drive, '?');
if ( insert != NULL ) {
*insert = j;
}
if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
for ( j=checklist2[i].num_min; j<=checklist2[i].num_max; ++j ) {
sprintf(drive, checklist2[i].format, j);
if ( (is_cdrom_linux(drive, NULL)) > 0 ) {
cdio_add_device_list(&drives, drive, &num_drives);
}
}
@@ -1439,14 +1419,13 @@ cdio_get_default_device_linux(void)
#else
unsigned int i;
char drive[40];
bool exists;
char *ret_drive;
/* Scan the system for CD-ROM drives.
*/
for ( i=0; strlen(checklist1[i]) > 0; ++i ) {
sprintf(drive, "/dev/%s", checklist1[i]);
if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
if ( is_cdrom_linux(drive, NULL) > 0 ) {
return strdup(drive);
}
}
@@ -1462,17 +1441,11 @@ cdio_get_default_device_linux(void)
/* Scan the system for CD-ROM drives.
Not always 100% reliable, so use the USE_MNTENT code above first.
*/
for ( i=0; strlen(checklist2[i]) > 0; ++i ) {
for ( i=0; checklist2[i].format; ++i ) {
unsigned int j;
char *insert;
exists = true;
for ( j=checklist2[i][1]; exists; ++j ) {
sprintf(drive, "/dev/%s", &checklist2[i][3]);
insert = strchr(drive, '?');
if ( insert != NULL ) {
*insert = j;
}
if ( (exists=is_cdrom_linux(drive, NULL)) > 0 ) {
for ( j=checklist2[i].num_min; j<=checklist2[i].num_max; ++j ) {
sprintf(drive, checklist2[i].format, j);
if ( is_cdrom_linux(drive, NULL) > 0 ) {
return(strdup(drive));
}
}

View File

@@ -1,5 +1,5 @@
/*
$Id: util.c,v 1.3 2006/03/18 00:53:20 rocky Exp $
$Id: util.c,v 1.4 2006/11/27 19:31:37 gmerlin Exp $
Copyright (C) 2000 Herbert Valerio Riedel <hvr@gnu.org>
Copyright (C) 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>
@@ -27,6 +27,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#ifdef HAVE_UNISTD_H // readlink
#include <unistd.h>
#endif
#ifdef HAVE_INTTYPES_H
#include "inttypes.h"
@@ -36,7 +41,7 @@
#include <cdio/types.h>
#include <cdio/util.h>
static const char _rcsid[] = "$Id: util.c,v 1.3 2006/03/18 00:53:20 rocky Exp $";
static const char _rcsid[] = "$Id: util.c,v 1.4 2006/11/27 19:31:37 gmerlin Exp $";
size_t
_cdio_strlenv(char **str_array)
@@ -145,6 +150,36 @@ cdio_from_bcd8(uint8_t p)
return (0xf & p)+(10*(p >> 4));
}
/*!
Follow symlinks until we have the real device file
(idea taken from libunieject).
*/
void cdio_follow_symlink (const char * src, char * dst) {
#ifdef HAVE_READLINK
char tmp_src[PATH_MAX];
char tmp_dst[PATH_MAX];
int len;
strcpy(tmp_src, src);
while(1) {
len = readlink(tmp_src, tmp_dst, PATH_MAX);
if(len < 0) {
strcpy(dst, tmp_src);
return;
}
else {
tmp_dst[len] = '\0';
strcpy(tmp_src, tmp_dst);
}
}
#else
strcpy(dst, src);
#endif
}
/*
* Local variables: