From b67c555b305aae60c9dc8f1065cdeed0d4cf81c3 Mon Sep 17 00:00:00 2001 From: Thomas Schmitt Date: Fri, 22 Sep 2006 19:29:11 +0000 Subject: [PATCH] Implemented finding matching /dev/sgN from /dev/srM or /dev/scdK --- libburn/drive.c | 56 +++++++++++++++++++++++++++++++++++++++++++------ libburn/sg.c | 34 ++++++++++++++++++++++++++++++ libburn/sg.h | 2 ++ 3 files changed, 86 insertions(+), 6 deletions(-) diff --git a/libburn/drive.c b/libburn/drive.c index 0c6ad10..7a7388c 100644 --- a/libburn/drive.c +++ b/libburn/drive.c @@ -724,7 +724,7 @@ int burn_drive_find_devno(dev_t devno, char adr[]) if(strlen(fname) >= BURN_DRIVE_ADR_LEN) return -1; -fprintf(stderr,"libburn experimental: burn_drive_find_devno( 0x%llX ) found %s\n", (long long) devno, fname); +fprintf(stderr,"libburn experimental: burn_drive_find_devno( 0x%lX ) found %s\n", (long) devno, fname); strcpy(adr, fname); return 1; @@ -732,6 +732,52 @@ fprintf(stderr,"libburn experimental: burn_drive_find_devno( 0x%llX ) found %s\n return 0; } +/* ts A60922 ticket 33 */ +/* Try to find an enumerated address with the same host,channel,target,lun + as path */ +int burn_drive_find_scsi_adr(char *path, char adr[]) +{ + char fname[4096]; + int i, ret = 0, first = 1; + int host_no, channel_no, target_no, lun_no; + int i_host_no, i_channel_no, i_target_no, i_lun_no; + + ret = sg_obtain_scsi_adr(path, &host_no, &channel_no, &target_no, + &lun_no); + if(ret <= 0) { + +fprintf(stderr,"libburn experimental: sg_obtain_scsi_adr( %s ) returns %d\n", path, ret); + + return 0; + } + +fprintf(stderr,"libburn experimental: burn_drive_find_scsi_adr( %s ) : %d,%d,%d,%d\n", path, host_no, channel_no, target_no, lun_no); + + + while (1) { + ret= sg_give_next_adr(&i, fname, sizeof(fname), first); + if(ret <= 0) + break; + first = 0; + ret = sg_obtain_scsi_adr(fname, &i_host_no, &i_channel_no, + &i_target_no, &i_lun_no); + if(ret == -1) + continue; + if(i_host_no != host_no || i_channel_no != channel_no || + i_target_no != target_no || i_lun_no != lun_no) + continue; + if(strlen(fname) >= BURN_DRIVE_ADR_LEN) + return -1; + +fprintf(stderr,"libburn experimental: burn_drive_find_scsi_adr( %s ) found %s\n", path, fname); + + strcpy(adr, fname); + return 1; + } + return 0; +} + + /* ts A60922 ticket 33 */ /** Try to convert a given existing filesystem address into a persistent drive address. */ @@ -768,11 +814,9 @@ fprintf(stderr,"libburn experimental: lstat( %s ) returns -1\n",path); ret = burn_drive_find_devno(stbuf.st_rdev, adr); if(ret > 0) return 1; - - /* >>> if SCSI device : - try to find enumerated device with same Bus,Target,Lun - */; - + ret = burn_drive_find_scsi_adr(path, adr); + if(ret > 0) + return 1; } fprintf(stderr,"libburn experimental: Nothing found for %s \n",path); diff --git a/libburn/sg.c b/libburn/sg.c index 1827a54..4c44e64 100644 --- a/libburn/sg.c +++ b/libburn/sg.c @@ -248,6 +248,9 @@ void sg_enumerate(void) close(fd); if (sid.scsi_type != TYPE_ROM) continue; +/* <<< ts A60922 (use SCSI_IOCTL_GET_IDLUN on block devices) + fprintf(stderr,"libburn experimental: SCSI triple: %d,%d,%d\n",sid.host_no,sid.scsi_id,sid.lun); +*/ enumerate_common(fname); } @@ -556,3 +559,34 @@ enum response scsi_error(struct burn_drive *d, unsigned char *sense, burn_print(1, "key:0x%x, asc:0x%x, ascq:0x%x\n", key, asc, ascq); return FAIL; } + +/* ts A60922 */ +/** Try to obtain SCSI address parameters. + @return 1 is success , 0 is failure +*/ +int sg_obtain_scsi_adr(char *path, int *host_no, int *channel_no, + int *target_no, int *lun_no) +{ + int fd, ret; + struct my_scsi_idlun { + int x; + int host_unique_id; + }; + struct my_scsi_idlun idlun; + + fd = open(path, O_RDONLY | O_NONBLOCK); + if(fd < 0) + return 0; + + /* http://www.tldp.org/HOWTO/SCSI-Generic-HOWTO/scsi_g_idlun.html */ + ret= ioctl(fd, SCSI_IOCTL_GET_IDLUN, &idlun); + + close(fd); + if(ret == -1) + return(0); + *host_no= (idlun.x>>24)&255; + *channel_no= (idlun.x>>16)&255; + *target_no= (idlun.x)&255; + *lun_no= (idlun.x>>8)&255; + return 1; +} diff --git a/libburn/sg.h b/libburn/sg.h index afb5687..cc9870a 100644 --- a/libburn/sg.h +++ b/libburn/sg.h @@ -12,6 +12,8 @@ enum response /* ts A60922 ticket 33 */ int sg_give_next_adr(int *idx, char adr[], int adr_size, int initialize); int sg_is_enumerable_adr(char *adr); +int sg_obtain_scsi_adr(char *path, int *host_no, int *channel_no, + int *target_no, int *lun_no); void sg_enumerate(void); void ata_enumerate(void);