Treat Windows CDROM Drive as Block File?

350 Views Asked by At

I'm attempting to use a Python module (python-dvdvideo to be exact) to clone an ISO image. The provided class works fine if I pass it a filepath to an ISO file that is already on my computer, but it throws an exception if I attempt to pass it the drive letter of my CDROM drive instead.

After quickly inspecting the library's code, I determined that the class is expecting either a regular file or a block special device file, as shown here:

def __init__(self, filename):
    s = os.stat(filename)
        if stat.S_ISREG(s.st_mode):
            f = self.File(filename)
        elif stat.S_ISBLK(s.st_mode):
            f = DvdCssFile(filename)
        else:
            raise RuntimeError

This leads me to my question: Is there a way to treat a Windows CDROM drive as either of these? I'm vaguely familiar with how Linux works in this regard (it treats a CDROM drive as a block device file under /dev/*), but not with how Windows sees drives.

1

There are 1 best solutions below

0
On

While trying to do something similar I found this thread useful. Based on the information there (and also here) I created this, which shows you the basics:

import os

driveName = "D"

# Get Window raw block device name from logical drive
# Adapted from https://stackoverflow.com/a/6523306/1209004
deviceName = "\\\\.\\" + driveName + ":" 

# Open as file object
# Adapted from https://stackoverflow.com/q/7135398/1209004
d = os.fdopen(os.open(deviceName, os.O_RDONLY|os.O_BINARY), 'rb+')

# Read data
data = d.read()

# Close file object
d.close()

# Write data to an output file
fOut = open('data.bin','wb')
fOut.write(data)
fOut.close()

One thing I noted is that compared against dedicated imaging tools like IsoBuster the data read in this way may be incomplete. Also, it doesn't appear to work to access data sessions on an 'enhanced' audio CD. So use with caution.