use crate::{expect, from_str, ProcResult};
use std::io::BufRead;
#[cfg(feature = "serde1")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum LockType {
FLock,
Posix,
ODF,
Other(String),
}
impl LockType {
pub fn as_str(&self) -> &str {
match self {
LockType::FLock => "FLOCK",
LockType::Posix => "POSIX",
LockType::ODF => "ODF",
LockType::Other(s) => s.as_ref(),
}
}
}
impl From<&str> for LockType {
fn from(s: &str) -> LockType {
match s {
"FLOCK" => LockType::FLock,
"OFDLCK" => LockType::ODF,
"POSIX" => LockType::Posix,
x => LockType::Other(x.to_string()),
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum LockMode {
Advisory,
Mandatory,
Other(String),
}
impl LockMode {
pub fn as_str(&self) -> &str {
match self {
LockMode::Advisory => "ADVISORY",
LockMode::Mandatory => "MANDATORY",
LockMode::Other(s) => s.as_ref(),
}
}
}
impl From<&str> for LockMode {
fn from(s: &str) -> LockMode {
match s {
"ADVISORY" => LockMode::Advisory,
"MANDATORY" => LockMode::Mandatory,
x => LockMode::Other(x.to_string()),
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub enum LockKind {
Read,
Write,
Other(String),
}
impl LockKind {
pub fn as_str(&self) -> &str {
match self {
LockKind::Read => "READ",
LockKind::Write => "WRITE",
LockKind::Other(s) => s.as_ref(),
}
}
}
impl From<&str> for LockKind {
fn from(s: &str) -> LockKind {
match s {
"READ" => LockKind::Read,
"WRITE" => LockKind::Write,
x => LockKind::Other(x.to_string()),
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Lock {
pub lock_type: LockType,
pub mode: LockMode,
pub kind: LockKind,
pub pid: Option<i32>,
pub devmaj: u32,
pub devmin: u32,
pub inode: u64,
pub offset_first: u64,
pub offset_last: Option<u64>,
}
impl Lock {
fn from_line(line: &str) -> ProcResult<Lock> {
let mut s = line.split_whitespace();
let _ = expect!(s.next());
let typ = {
let t = expect!(s.next());
if t == "->" {
From::from(expect!(s.next()))
} else {
From::from(t)
}
};
let mode = From::from(expect!(s.next()));
let kind = From::from(expect!(s.next()));
let pid = expect!(s.next());
let disk_inode = expect!(s.next());
let offset_first = from_str!(u64, expect!(s.next()));
let offset_last = expect!(s.next());
let mut dis = disk_inode.split(':');
let devmaj = from_str!(u32, expect!(dis.next()), 16);
let devmin = from_str!(u32, expect!(dis.next()), 16);
let inode = from_str!(u64, expect!(dis.next()));
Ok(Lock {
lock_type: typ,
mode,
kind,
pid: if pid == "-1" { None } else { Some(from_str!(i32, pid)) },
devmaj,
devmin,
inode,
offset_first,
offset_last: if offset_last == "EOF" {
None
} else {
Some(from_str!(u64, offset_last))
},
})
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))]
pub struct Locks(pub Vec<Lock>);
impl super::FromBufRead for Locks {
fn from_buf_read<R: BufRead>(r: R) -> ProcResult<Self> {
let mut v = Vec::new();
for line in r.lines() {
let line = line?;
v.push(Lock::from_line(&line)?);
}
Ok(Locks(v))
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_blocked() {
let data = r#"1: POSIX ADVISORY WRITE 723 00:14:16845 0 EOF
2: FLOCK ADVISORY WRITE 652 00:14:16763 0 EOF
3: FLOCK ADVISORY WRITE 1594 fd:00:396528 0 EOF
4: FLOCK ADVISORY WRITE 1594 fd:00:396527 0 EOF
5: FLOCK ADVISORY WRITE 2851 fd:00:529372 0 EOF
6: POSIX ADVISORY WRITE 1280 00:14:16200 0 0
6: -> POSIX ADVISORY WRITE 1281 00:14:16200 0 0
6: -> POSIX ADVISORY WRITE 1279 00:14:16200 0 0
6: -> POSIX ADVISORY WRITE 1282 00:14:16200 0 0
6: -> POSIX ADVISORY WRITE 1283 00:14:16200 0 0
7: OFDLCK ADVISORY READ -1 00:06:1028 0 EOF
8: FLOCK ADVISORY WRITE 6471 fd:00:529426 0 EOF
9: FLOCK ADVISORY WRITE 6471 fd:00:529424 0 EOF
10: FLOCK ADVISORY WRITE 6471 fd:00:529420 0 EOF
11: FLOCK ADVISORY WRITE 6471 fd:00:529418 0 EOF
12: POSIX ADVISORY WRITE 1279 00:14:23553 0 EOF
13: FLOCK ADVISORY WRITE 6471 fd:00:393838 0 EOF
14: POSIX ADVISORY WRITE 655 00:14:16146 0 EOF"#;
for line in data.lines() {
super::Lock::from_line(line.trim()).unwrap();
}
}
}