1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! This is `dir-lock`, a library crate providing the type [`DirLock`], which is a simple file-system-based mutex.
//!
//! # Features
//!
//! The following feature is enabled by default:
//!
//! * `tokio`: Uses the [`tokio`](https://docs.rs/tokio) runtime for async operations.
//!
//! The following features can be enabled via Cargo:
//!
//! * `async-std`: Uses the [`async-std`](https://docs.rs/async-std) runtime for async operations. The `tokio` feature should be disabled when using this feature.
//! * `tokio02`: Uses the runtime of [the outdated version 0.2 of the `tokio` crate](https://docs.rs/tokio/0.2) for async operations. The `tokio` feature should be disabled when using this feature.
//! * `tokio03`: Uses the runtime of [the outdated version 0.3 of the `tokio` crate](https://docs.rs/tokio/0.3) for async operations. The `tokio` feature should be disabled when using this feature.

#![deny(missing_docs, rust_2018_idioms, unused, unused_crate_dependencies, unused_import_braces, unused_qualifications, warnings)]
#![forbid(unsafe_code)]

use {
    std::{
        fmt,
        fs::File as SyncFile,
        future::Future,
        io::{
            self,
            Read as _,
            Write as _,
        },
        mem::forget,
        num::ParseIntError,
        path::{
            Path,
            PathBuf,
        },
        sync::Arc,
        thread,
        time::Duration,
    },
    derive_more::From,
    heim::process::pid_exists,
};
#[cfg(feature = "async-std")] use async_std::{
    fs::{
        self,
        File,
    },
    io::prelude::*,
    task::{
        block_on,
        sleep,
    },
};
#[cfg(feature = "tokio02")] use tokio02::{
    self as tokio,
    time::delay_for as sleep,
};
#[cfg(feature = "tokio03")] use tokio03 as tokio;
#[cfg(any(feature = "tokio", feature = "tokio02", feature = "tokio03"))] use tokio::{
    fs::{
        self,
        File,
    },
    io::AsyncReadExt as _,
};
#[cfg(any(feature = "tokio", feature = "tokio03"))] use tokio::time::sleep;

/// A simple file-system-based mutex.
///
/// When constructing a value of this type, a directory is created at the specified path.
/// If a directory already exists, the constructor waits until it's removed.
/// Dropping a `DirLock` removes the corresponding directory.
/// Since creating a directory if it does not exist is an atomic operation on most operating systems,
/// this can be used as a quick-and-dirty cross-process mutex.
///
/// To guard against processes exiting without properly removing the lock, a file containing the current process ID is created inside the lock.
/// If no process with that ID exists, another process may claim the lock for itself.
///
/// Of course, this is still not completely fail-proof since the user or other processes could mess with the lock directory.
///
/// This type is a RAII lock guard, but unlocking a directory lock uses I/O and can error, so it is recommended to call [`drop_async`](Self::drop_async).
#[must_use = "should call the drop_async method to unlock"]
pub struct DirLock<'a>(&'a Path);

/// An error that can occur when locking or unlocking a [`DirLock`].
#[derive(Debug, From, Clone)]
#[allow(missing_docs)]
pub enum Error {
    HeimProcess(Arc<heim::process::ProcessError>),
    Io(Arc<io::Error>, Option<PathBuf>),
    #[from]
    ParseInt(ParseIntError),
}

impl From<heim::process::ProcessError> for Error {
    fn from(e: heim::process::ProcessError) -> Error {
        Error::HeimProcess(Arc::new(e))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::HeimProcess(e) => write!(f, "heim process error: {}", e),
            Error::Io(e, Some(path)) => write!(f, "I/O error at {}: {}", path.display(), e),
            Error::Io(e, None) => write!(f, "I/O error: {}", e),
            Error::ParseInt(e) => e.fmt(f),
        }
    }
}

trait IoResultExt {
    type T;

    fn at(self, path: impl AsRef<Path>) -> Self::T;
    fn at_unknown(self) -> Self::T;
}

impl IoResultExt for io::Error {
    type T = Error;

    fn at(self, path: impl AsRef<Path>) -> Error {
        Error::Io(Arc::new(self), Some(path.as_ref().to_owned()))
    }

    fn at_unknown(self) -> Error {
        Error::Io(Arc::new(self), None)
    }
}

impl<T, E: IoResultExt> IoResultExt for Result<T, E> {
    type T = Result<T, E::T>;

    fn at(self, path: impl AsRef<Path>) -> Result<T, E::T> {
        self.map_err(|e| e.at(path))
    }

    fn at_unknown(self) -> Result<T, E::T> {
        self.map_err(|e| e.at_unknown())
    }
}

impl DirLock<'_> {
    /// Acquires a directory lock at the given path, without blocking the thread.
    ///
    /// See the type-level docs for details.
    pub async fn new(path: &impl AsRef<Path>) -> Result<DirLock<'_>, Error> {
        let path = path.as_ref();
        loop {
            match fs::create_dir(path).await {
                Ok(()) => {
                    let pidfile = path.join("pid");
                    writeln!(SyncFile::create(&pidfile).at(&pidfile)?, "{}", std::process::id()).at(pidfile)?; //TODO replace SyncFile with File once format_args! is Sync
                    return Ok(DirLock(path));
                }
                Err(e) => match e.kind() {
                    io::ErrorKind::AlreadyExists => {
                        if match File::open(path.join("pid")).await {
                            Ok(mut f) => {
                                let mut buf = String::default();
                                f.read_to_string(&mut buf).await.at(path.join("pid"))?;
                                !buf.is_empty() // assume pidfile is still being written if empty //TODO check timestamp
                                && !pid_exists(buf.trim().parse()?).await?
                            }
                            Err(e) => if e.kind() == io::ErrorKind::NotFound {
                                false
                            } else {
                                return Err(e.at(path.join("pid")));
                            },
                        } {
                            DirLock(path).clean_up().await?;
                        }
                        sleep(Duration::from_secs(1)).await;
                        continue;
                    }
                    _ => { return Err(e.at(path)); }
                },
            }
        }
    }

    /// Blocks the current thread until the lock can be established.
    pub fn new_sync(path: &impl AsRef<Path>) -> Result<DirLock<'_>, Error> {
        let path = path.as_ref();
        loop {
            match std::fs::create_dir(path) {
                Ok(()) => {
                    let pidfile = path.join("pid");
                    writeln!(SyncFile::create(&pidfile).at(&pidfile)?, "{}", std::process::id()).at(pidfile)?;
                    return Ok(DirLock(path));
                }
                Err(e) => match e.kind() {
                    io::ErrorKind::AlreadyExists => {
                        if match SyncFile::open(path.join("pid")) {
                            Ok(mut f) => {
                                let mut buf = String::default();
                                f.read_to_string(&mut buf).at(path.join("pid"))?;
                                !buf.is_empty() // assume pidfile is still being written if empty //TODO check timestamp
                                && !block_on(pid_exists(buf.trim().parse()?))?
                            }
                            Err(e) => if e.kind() == io::ErrorKind::NotFound {
                                false
                            } else {
                                return Err(e.at(path.join("pid")));
                            },
                        } {
                            DirLock(path).clean_up_sync()?;
                        }
                        thread::sleep(Duration::from_secs(1));
                        continue;
                    }
                    _ => { return Err(e.at(path)); }
                },
            }
        }
    }

    /// Unlocks this lock without blocking the thread.
    pub async fn drop_async(self) -> Result<(), Error> {
        self.clean_up().await?;
        forget(self);
        Ok(())
    }

    async fn clean_up(&self) -> Result<(), Error> {
        if let Err(e) = fs::remove_file(self.0.join("pid")).await {
            if e.kind() != io::ErrorKind::NotFound {
                return Err(e.at(self.0.join("pid")));
            }
        }
        if let Err(e) = fs::remove_dir(self.0).await {
            if e.kind() != io::ErrorKind::NotFound {
                return Err(e.at(self.0));
            }
        }
        Ok(())
    }

    fn clean_up_sync(&self) -> Result<(), Error> {
        if let Err(e) = std::fs::remove_file(self.0.join("pid")) {
            if e.kind() != io::ErrorKind::NotFound {
                return Err(e.at(self.0.join("pid")));
            }
        }
        if let Err(e) = std::fs::remove_dir(self.0) {
            if e.kind() != io::ErrorKind::NotFound {
                return Err(e.at(self.0));
            }
        }
        Ok(())
    }
}

impl Drop for DirLock<'_> {
    /// Unlocks this lock, blocking the current thread while doing so.
    ///
    /// # Panics
    ///
    /// Unlocking a directory lock involves I/O. If an error occurs, this method will panic.
    /// It is recommended to use [`drop_async`](Self::drop_async) instead, which returns the error.
    fn drop(&mut self) {
        self.clean_up_sync().expect("failed to clean up dir lock");
    }
}

#[cfg(any(feature = "tokio", feature = "tokio02", feature = "tokio03"))]
fn block_on<T, E, F: Future<Output = Result<T, E>>>(fut: F) -> Result<T, Error>
where Error: From<E> {
    Ok(tokio::runtime::Runtime::new().at_unknown()?.block_on(fut)?)
}