Hi!

Wednesday, 18 December, 2002 Giuseppe Corbelli [EMAIL PROTECTED] wrote:

GC> On Tue, Dec 17, 2002 at 06:52:13PM +0300, egor duda wrote:
GC> ... 
>> As a side note, sometime ago i've sent you a patch that works on
>> WinNT/2000/XP systems and which locks the CD drive from being
>> concurrently accessed by those unfriendly applications during write.
GC> ...
GC> Recently I've done the same job in cdrdao. I did not send a patch here
GC> 'cause cdrecord is in feature freeze. But I'd like very much to have a look
GC> at your patch.

diff -Nup2 ../../cdrtools-1.11/cdrecord/Makefile ./Makefile
--- ../../cdrtools-1.11/cdrecord/Makefile       Mon Jun 18 10:39:35 2001
+++ ./Makefile  Thu Oct 11 17:53:40 2001
@@ -19,5 +19,5 @@ CFILES=               cdrecord.c cd_misc.c fifo.c iso
                drv_simul.c \
                scsi_scan.c scsi_cdr.c \
-               modes.c misc.c crc16.c
+               modes.c misc.c crc16.c wnt_lock_drive.c
 HFILES=                cdrecord.h iso9660.h auheader.h scsi_scan.h \
                scsimmc.h \
diff -Nup2 ../../cdrtools-1.11/cdrecord/cdrecord.c ./cdrecord.c
--- ../../cdrtools-1.11/cdrecord/cdrecord.c     Tue Sep  4 15:04:29 2001
+++ ./cdrecord.c        Thu Oct 11 17:53:40 2001
@@ -52,4 +52,5 @@ static        char sccsid[] =
 #include <scg/scgcmd.h>                /* XXX fuer read_buffer */
 #include "scsi_scan.h"
+#include "wnt_lock_drive.h"
 
 #include "auheader.h"
@@ -226,4 +227,5 @@ main(ac, av)
        SCSI    *scgp;
        char    errstr[80];
+       long    drive_handle = -1;
 
 #ifdef __EMX__ 
@@ -632,4 +634,11 @@ do_leadin(tracks, track);
         * Last chance to quit!
         */
+#ifdef __CYGWIN__
+       drive_handle = wnt_lock_drive (scg_scsibus (scgp), scg_target (scgp),
+                                      scg_lun (scgp));
+       if (drive_handle == 0) {
+               errmsgno(EX_BAD, "warning: cannot lock cd drive");
+       }
+#endif
        printf("Starting to write CD/DVD at speed %d in %s mode for %s session.\n",
                speed,
@@ -902,4 +911,8 @@ fix_it:
 
 restore_it:
+#ifdef __CYGWIN__
+        if (drive_handle != 0 && drive_handle != -1)
+               wnt_unlock_drive (drive_handle);
+#endif
        /*
         * Try to restore the old sector size and stop FIFO.
diff -Nup2 ../../cdrtools-1.11/cdrecord/wnt_lock_drive.c ./wnt_lock_drive.c
--- ../../cdrtools-1.11/cdrecord/wnt_lock_drive.c       Thu Jan  1 03:00:00 1970
+++ ./wnt_lock_drive.c  Thu Oct 11 18:38:56 2001
@@ -0,0 +1,150 @@
+#ifdef __CYGWIN__
+
+#include <stdio.h>
+#include <windows.h>
+
+typedef struct
+{
+       ULONG   Length;
+       UCHAR   PortNumber;
+       UCHAR   PathId;
+       UCHAR   TargetId;
+       UCHAR   Lun;
+} SCSI_ADDRESS;
+
+#define CTL_CODE( DeviceType, Function, Method, Access ) (                 \
+    ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method) \
+)
+
+#define FSCTL_LOCK_VOLUME               CTL_CODE(9, 6, 0, 0)
+#define FSCTL_UNLOCK_VOLUME             CTL_CODE(9, 7, 0, 0)
+#define IOCTL_SCSI_GET_ADDRESS          CTL_CODE(4, 0x0406, 0, 0)
+
+HANDLE
+nt_open_device (const char* device_name)
+{
+       HANDLE ret_val;
+       ret_val = CreateFile (device_name, GENERIC_READ | GENERIC_WRITE,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE,
+                             NULL, OPEN_EXISTING, 0, NULL);
+       if (ret_val != INVALID_HANDLE_VALUE)
+               return (ret_val);
+
+       ret_val = CreateFile (device_name, GENERIC_READ,
+                             FILE_SHARE_READ | FILE_SHARE_WRITE,
+                             NULL, OPEN_EXISTING, 0, NULL);
+       return (ret_val);
+}
+
+HANDLE
+find_scsi_device_by_drive (char drive_letter, BYTE* pbus,
+                          BYTE* ptarget, BYTE* plun)
+{
+       HANDLE hDevice;
+       SCSI_ADDRESS scsi_addr;
+       ULONG bytes_read;
+       char device_name [256];
+       BOOL rc;
+
+       if (!pbus || !ptarget || !plun)
+               return NULL;
+
+       sprintf (device_name, "\\\\.\\%c:", drive_letter);
+       hDevice = nt_open_device (device_name);
+       if (hDevice == INVALID_HANDLE_VALUE)
+               return NULL;
+
+       memset (&scsi_addr, '\0', sizeof (scsi_addr));
+       scsi_addr.Length = sizeof (scsi_addr);
+       rc = DeviceIoControl (hDevice, IOCTL_SCSI_GET_ADDRESS, NULL, 0,
+                             &scsi_addr, sizeof (scsi_addr), &bytes_read, NULL);
+       if (rc) {
+               *pbus = scsi_addr.PortNumber;
+               *ptarget = scsi_addr.TargetId;
+               *plun = scsi_addr.Lun;
+               return hDevice;
+       } else {
+               return NULL;
+       }
+}
+
+HANDLE
+open_scsi_cd_device (BYTE bus, BYTE target, BYTE lun)
+{
+       char buf [256];
+       char* p;
+       HANDLE hDevice;
+       BYTE cur_bus, cur_target, cur_lun; 
+
+       GetLogicalDriveStrings (sizeof (buf), buf);
+
+       for (p = buf; *p; p = strchr (p, '\0') + 1) {
+               if (GetDriveType (p) != DRIVE_CDROM)
+                       continue;
+               hDevice = find_scsi_device_by_drive (*p, &cur_bus,
+                                                        &cur_target,
+                                                        &cur_lun);
+               if (!hDevice)
+                       continue;
+               if (cur_bus != bus ||
+                   cur_target != target ||
+                   cur_lun != lun) {
+                       CloseHandle (hDevice);
+                       continue;
+               }
+               return (hDevice);
+       }
+       return NULL;
+}
+
+long int
+wnt_lock_drive (int bus, int target, int lun)
+{
+       BOOL rc;
+       DWORD bytes_read;
+       HANDLE hDevice;
+       OSVERSIONINFO os_version;
+       os_version.dwOSVersionInfoSize = sizeof (os_version);
+
+       if (!GetVersionEx (&os_version)) return 0;
+
+       if (os_version.dwPlatformId != VER_PLATFORM_WIN32_NT)
+               return -1;
+
+       hDevice = open_scsi_cd_device (bus, target, lun);
+
+       if (!hDevice) return 0;
+
+       rc = DeviceIoControl (hDevice, FSCTL_LOCK_VOLUME, 0, 0, 0, 0,
+                             &bytes_read, 0);
+       if (!rc) return 0;
+
+       return ((long) hDevice);
+}
+
+int
+wnt_unlock_drive (long handle)
+{
+       DWORD bytes_read;
+       HANDLE hDevice = (HANDLE) handle;
+       DeviceIoControl (hDevice, FSCTL_UNLOCK_VOLUME, 0, 0, 0, 0,
+                        &bytes_read, 0);
+       CloseHandle (hDevice);
+       return 1;
+}
+
+#else
+
+long int
+wnt_lock_drive (int bus, int target, int lun)
+{
+       return 0;
+}
+
+int
+wnt_unlock_drive (long handle)
+{
+       return 0;
+}
+
+#endif
diff -Nup2 ../../cdrtools-1.11/cdrecord/wnt_lock_drive.h ./wnt_lock_drive.h
--- ../../cdrtools-1.11/cdrecord/wnt_lock_drive.h       Thu Jan  1 03:00:00 1970
+++ ./wnt_lock_drive.h  Thu Oct 11 17:53:40 2001
@@ -0,0 +1,2 @@
+extern long int wnt_lock_drive (int bus, int target, int lun);
+extern int wnt_unlock_drive (long int handle);



Egor.            mailto:[EMAIL PROTECTED] ICQ 5165414 FidoNet 2:5020/496.19


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to