Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide better error message when there's no CD in the drive #507

Merged
merged 1 commit into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions whipper/command/cd.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ def do(self):

utils.load_device(self.device)
utils.unmount_device(self.device)
# Exit and inform the user if there's no CD in the disk drive
if drive.get_cdrom_drive_status(self.device): # rc == 1 means no disc
raise OSError("no CD detected, please insert one and retry")

# first, read the normal TOC, which is fast
self.ittoc = self.program.getFastToc(self.runner, self.device)
Expand Down
27 changes: 27 additions & 0 deletions whipper/common/drive.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# along with whipper. If not, see <http://www.gnu.org/licenses/>.

import os
from fcntl import ioctl

import logging
logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -69,3 +70,29 @@ def getDeviceInfo(path):
_, vendor, model, release = device.get_hwinfo()

return vendor, model, release


def get_cdrom_drive_status(drive_path):
"""
Get the status of the disc drive.

Drive status possibilities returned by CDROM_DRIVE_STATUS ioctl:
- CDS_NO_INFO = 0 (if not implemented)
- CDS_NO_DISC = 1
- CDS_TRAY_OPEN = 2
- CDS_DRIVE_NOT_READY = 3
- CDS_DISC_OK = 4

Documentation here:
- https://www.kernel.org/doc/Documentation/ioctl/cdrom.txt
- https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/uapi/linux/cdrom.h # noqa: E501

:param drive_path: path to the disc drive
:type device: str
:returns: return code of the 'CDROM_DRIVE_STATUS' ioctl
:rtype: int
"""
fd = os.open(drive_path, os.O_RDONLY | os.O_NONBLOCK)
rc = ioctl(fd, 0x5326) # AKA 'CDROM_DRIVE_STATUS'
os.close(fd)
return rc