tape/drive: add test_device_path_lock

this makes it possible to detect if the drive was locked in a
non-blocking manner

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2021-02-18 15:40:20 +01:00 committed by Dietmar Maurer
parent 1917ea3ce1
commit 33c06b3388
1 changed files with 25 additions and 0 deletions

View File

@ -496,3 +496,28 @@ fn lock_device_path(device_path: &str) -> Result<DeviceLockGuard, Error> {
Ok(DeviceLockGuard(file))
}
// Same logic as lock_device_path, but uses a timeout of 0, making it
// non-blocking, and returning if the file is locked or not
fn test_device_path_lock(device_path: &str) -> Result<bool, Error> {
let lock_name = crate::tools::systemd::escape_unit(device_path, true);
let mut path = std::path::PathBuf::from("/var/lock");
path.push(lock_name);
let timeout = std::time::Duration::new(0, 0);
let mut file = std::fs::OpenOptions::new().create(true).append(true).open(path)?;
match proxmox::tools::fs::lock_file(&mut file, true, Some(timeout)) {
// file was not locked, continue
Ok(()) => {},
// file was locked, return true
Err(err) if err.kind() == std::io::ErrorKind::WouldBlock => return Ok(true),
Err(err) => bail!("{}", err),
}
let backup_user = crate::backup::backup_user()?;
fchown(file.as_raw_fd(), Some(backup_user.uid), Some(backup_user.gid))?;
Ok(false)
}