From c9d13b0fc48da6fc3e2b9656f5d041646ac59796 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 24 Dec 2020 15:51:49 +0100 Subject: [PATCH] tape: expose check_tape_is_linux_tape_device --- src/tape/drive/linux_tape.rs | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/tape/drive/linux_tape.rs b/src/tape/drive/linux_tape.rs index 3f37b2c9..c285da26 100644 --- a/src/tape/drive/linux_tape.rs +++ b/src/tape/drive/linux_tape.rs @@ -372,23 +372,23 @@ fn tape_write_eof_mark(file: &File) -> Result<(), std::io::Error> { Ok(()) } -fn tape_is_linux_tape_device(file: &File) -> bool { +pub fn check_tape_is_linux_tape_device(file: &File) -> Result<(), Error> { - let devnum = match nix::sys::stat::fstat(file.as_raw_fd()) { - Ok(stat) => stat.st_rdev, - _ => return false, - }; + let stat = nix::sys::stat::fstat(file.as_raw_fd())?; + + let devnum = stat.st_rdev; let major = unsafe { libc::major(devnum) }; let minor = unsafe { libc::minor(devnum) }; - if major != 9 { return false; } // The st driver uses major device number 9 + if !(major != 9) { + bail!("not a tape device"); + } if (minor & 128) == 0 { - eprintln!("Detected rewinding tape. Please use non-rewinding tape devices (/dev/nstX)."); - return false; + bail!("Detected rewinding tape. Please use non-rewinding tape devices (/dev/nstX)."); } - true + Ok(()) } /// Opens a Linux tape device @@ -417,9 +417,8 @@ pub fn open_linux_tape_device( fcntl(file.as_raw_fd(), FcntlArg::F_SETFL(flags)) .into_io_result()?; - if !tape_is_linux_tape_device(&file) { - bail!("file {:?} is not a linux tape device", path); - } + check_tape_is_linux_tape_device(&file) + .map_err(|err| format_err!("device type check {:?} failed - {}", path, err))?; Ok(file) }