1use std::future;
16use std::time::{Duration, SystemTime};
17
18use futures_util::FutureExt;
19use uuid::Uuid;
20use xmltree::Element;
21
22use crate::davpath::DavPath;
23use crate::ls::*;
24
25#[derive(Debug, Clone)]
27pub struct FakeLs {}
28
29impl FakeLs {
30 pub fn new() -> Box<FakeLs> {
32 Box::new(FakeLs {})
33 }
34}
35
36fn tm_limit(d: Option<Duration>) -> Duration {
37 match d {
38 None => Duration::new(120, 0),
39 Some(d) => {
40 if d.as_secs() > 120 {
41 Duration::new(120, 0)
42 } else {
43 d
44 }
45 }
46 }
47}
48
49impl DavLockSystem for FakeLs {
50 fn lock(
51 &'_ self,
52 path: &DavPath,
53 principal: Option<&str>,
54 owner: Option<&Element>,
55 timeout: Option<Duration>,
56 shared: bool,
57 deep: bool,
58 ) -> LsFuture<'_, Result<DavLock, DavLock>> {
59 let timeout = tm_limit(timeout);
60 let timeout_at = SystemTime::now() + timeout;
61
62 let d = if deep { 'I' } else { '0' };
63 let s = if shared { 'S' } else { 'E' };
64 let token = format!("opaquetoken:{}/{}/{}", Uuid::new_v4().hyphenated(), d, s);
65
66 let lock = DavLock {
67 token,
68 path: Box::new(path.clone()),
69 principal: principal.map(|s| s.to_string()),
70 owner: owner.map(|o| Box::new(o.clone())),
71 timeout_at: Some(timeout_at),
72 timeout: Some(timeout),
73 shared,
74 deep,
75 };
76 debug!("lock {} created", &lock.token);
77 future::ready(Ok(lock)).boxed()
78 }
79
80 fn unlock(&'_ self, _path: &DavPath, _token: &str) -> LsFuture<'_, Result<(), ()>> {
81 future::ready(Ok(())).boxed()
82 }
83
84 fn refresh(
85 &'_ self,
86 path: &DavPath,
87 token: &str,
88 timeout: Option<Duration>,
89 ) -> LsFuture<'_, Result<DavLock, ()>> {
90 debug!("refresh lock {token}");
91 let v: Vec<&str> = token.split('/').collect();
92 let deep = v.len() > 1 && v[1] == "I";
93 let shared = v.len() > 2 && v[2] == "S";
94
95 let timeout = tm_limit(timeout);
96 let timeout_at = SystemTime::now() + timeout;
97
98 let lock = DavLock {
99 token: token.to_string(),
100 path: Box::new(path.clone()),
101 principal: None,
102 owner: None,
103 timeout_at: Some(timeout_at),
104 timeout: Some(timeout),
105 shared,
106 deep,
107 };
108 future::ready(Ok(lock)).boxed()
109 }
110
111 fn check(
112 &'_ self,
113 _path: &DavPath,
114 _principal: Option<&str>,
115 _ignore_principal: bool,
116 _deep: bool,
117 _submitted_tokens: Vec<&str>,
118 ) -> LsFuture<'_, Result<(), DavLock>> {
119 future::ready(Ok(())).boxed()
120 }
121
122 fn discover(&'_ self, _path: &DavPath) -> LsFuture<'_, Vec<DavLock>> {
123 future::ready(Vec::new()).boxed()
124 }
125
126 fn delete(&'_ self, _path: &DavPath) -> LsFuture<'_, Result<(), ()>> {
127 future::ready(Ok(())).boxed()
128 }
129}