use std::time::{Duration, SystemTime};
use uuid::Uuid;
use xmltree::Element;
use crate::davpath::DavPath;
use crate::ls::*;
#[derive(Debug, Clone)]
pub struct FakeLs {}
impl FakeLs {
pub fn new() -> Box<FakeLs> {
Box::new(FakeLs {})
}
}
fn tm_limit(d: Option<Duration>) -> Duration {
match d {
None => Duration::new(120, 0),
Some(d) => {
if d.as_secs() > 120 {
Duration::new(120, 0)
} else {
d
}
}
}
}
impl DavLockSystem for FakeLs {
fn lock(
&self,
path: &DavPath,
principal: Option<&str>,
owner: Option<&Element>,
timeout: Option<Duration>,
shared: bool,
deep: bool,
) -> Result<DavLock, DavLock> {
let timeout = tm_limit(timeout);
let timeout_at = SystemTime::now() + timeout;
let d = if deep { 'I' } else { '0' };
let s = if shared { 'S' } else { 'E' };
let token = format!("opaquetoken:{}/{}/{}", Uuid::new_v4().hyphenated(), d, s);
let lock = DavLock {
token,
path: path.clone(),
principal: principal.map(|s| s.to_string()),
owner: owner.cloned(),
timeout_at: Some(timeout_at),
timeout: Some(timeout),
shared,
deep,
};
debug!("lock {} created", &lock.token);
Ok(lock)
}
fn unlock(&self, _path: &DavPath, _token: &str) -> Result<(), ()> {
Ok(())
}
fn refresh(
&self,
path: &DavPath,
token: &str,
timeout: Option<Duration>,
) -> Result<DavLock, ()> {
debug!("refresh lock {}", token);
let v: Vec<&str> = token.split('/').collect();
let deep = v.len() > 1 && v[1] == "I";
let shared = v.len() > 2 && v[2] == "S";
let timeout = tm_limit(timeout);
let timeout_at = SystemTime::now() + timeout;
let lock = DavLock {
token: token.to_string(),
path: path.clone(),
principal: None,
owner: None,
timeout_at: Some(timeout_at),
timeout: Some(timeout),
shared,
deep,
};
Ok(lock)
}
fn check(
&self,
_path: &DavPath,
_principal: Option<&str>,
_ignore_principal: bool,
_deep: bool,
_submitted_tokens: Vec<&str>,
) -> Result<(), DavLock> {
Ok(())
}
fn discover(&self, _path: &DavPath) -> Vec<DavLock> {
Vec::new()
}
fn delete(&self, _path: &DavPath) -> Result<(), ()> {
Ok(())
}
}