revault_lockbox_api 0.0.36

reVault lockbox API to create and manage lockboxes
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
use crate::{Error, Result};
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::fs::{self, File, OpenOptions};
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::thread;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};

const DEFAULT_LOCK_TIMEOUT: Duration = Duration::from_secs(30);
const LOCK_POLL_INTERVAL: Duration = Duration::from_millis(100);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// Diagnostic scope recorded by a cross-process write lock.
pub enum FileLockScope {
    /// Lock protects a `.lbox` archive.
    Lockbox,
    /// Lock protects a local vault directory.
    Vault,
    /// Lock exclusively owns mandatory transaction recovery.
    Recovery,
}

impl FileLockScope {
    fn as_str(self) -> &'static str {
        match self {
            Self::Lockbox => "lockbox",
            Self::Vault => "vault",
            Self::Recovery => "transaction recovery",
        }
    }
}

#[derive(Debug)]
/// RAII write lock shared by threads and cooperating processes.
///
/// Lockbox and recovery scopes lock the archive inode. Vault scope retains
/// its separate coordination sidecar. Archive locks are released on close.
pub struct ScopedFileLock {
    lock_path: PathBuf,
    archive: Option<File>,
    file: Option<std::sync::Arc<VaultLockHandle>>,
}

#[cfg(unix)]
type VaultLockHandle = File;

#[cfg(not(unix))]
#[derive(Debug)]
struct VaultLockHandle(PathBuf);

#[cfg(not(unix))]
impl Drop for VaultLockHandle {
    fn drop(&mut self) {
        let _ = fs::remove_file(&self.0);
    }
}

impl ScopedFileLock {
    /// Holds a shared lock on an existing archive without requiring write access.
    pub fn read_archive(path: &Path) -> Result<Self> {
        Ok(Self::archive_guard(super::archive_lock::open(path, false)?))
    }

    /// Reads archive bytes through the locked handle, including after a rename.
    pub fn read_archive_bytes(&self) -> Result<Vec<u8>> {
        let mut file = self
            .archive
            .as_ref()
            .ok_or_else(|| Error::InvalidOperation("not an archive lock".into()))?;
        let mut bytes = Vec::new();
        file.seek(SeekFrom::Start(0))
            .map_err(|err| Error::Io(err.to_string()))?;
        file.read_to_end(&mut bytes)
            .map_err(|err| Error::Io(err.to_string()))?;
        Ok(bytes)
    }

    /// Exclusively locks a privately staged archive before publishing its name.
    /// Keep this guard alive through publication and all associated updates.
    pub fn lock_archive(file: File) -> Result<Self> {
        super::archive_lock::acquire(&file, Path::new("<staged archive>"), true, Instant::now())?;
        Ok(Self::archive_guard(file))
    }

    fn archive_guard(file: File) -> Self {
        Self {
            lock_path: PathBuf::new(),
            archive: Some(file),
            file: None,
        }
    }

    /// Acquires the lock for `target`, waiting up to the configured timeout.
    pub fn acquire(target: &Path, scope: FileLockScope) -> Result<Self> {
        if scope != FileLockScope::Vault {
            return Ok(Self::archive_guard(super::archive_lock::open(
                target, true,
            )?));
        }
        let lock_path = lock_path_for(target);
        if let Some(file) = VAULT_HANDLES.with(|handles| {
            handles
                .borrow()
                .get(&lock_path)
                .and_then(std::sync::Weak::upgrade)
        }) {
            return Ok(Self {
                lock_path,
                archive: None,
                file: Some(file),
            });
        }
        let timeout = lock_timeout();
        let started = Instant::now();
        loop {
            match try_acquire(target, &lock_path, scope) {
                Ok(lock) => {
                    if let Some(file) = &lock.file {
                        VAULT_HANDLES.with(|handles| {
                            handles
                                .borrow_mut()
                                .insert(lock_path.clone(), std::sync::Arc::downgrade(file));
                        });
                    }
                    return Ok(lock);
                }
                Err(AcquireFailure::Busy(owner)) => {
                    if started.elapsed() >= timeout {
                        return Err(timeout_error(target, scope, timeout, owner.as_deref()));
                    }
                    thread::sleep(LOCK_POLL_INTERVAL);
                }
                Err(AcquireFailure::Io(err)) => {
                    return Err(Error::Io(err));
                }
            }
        }
    }
}

impl Drop for ScopedFileLock {
    fn drop(&mut self) {
        if self
            .file
            .as_ref()
            .is_some_and(|file| std::sync::Arc::strong_count(file) == 1)
        {
            VAULT_HANDLES.with(|handles| {
                handles.borrow_mut().remove(&self.lock_path);
            });
        }
        // The last owner releases the kernel lock (Unix) or coordination file
        // (other platforms), even when the handle moved to another thread.
    }
}

enum AcquireFailure {
    Busy(Option<String>),
    Io(String),
}

#[cfg(unix)]
fn try_acquire(
    target: &Path,
    lock_path: &Path,
    scope: FileLockScope,
) -> std::result::Result<ScopedFileLock, AcquireFailure> {
    use std::os::fd::AsRawFd;

    if let Some(parent) = lock_path.parent() {
        fs::create_dir_all(parent).map_err(|err| AcquireFailure::Io(err.to_string()))?;
    }
    let mut file = OpenOptions::new()
        .read(true)
        .write(true)
        .create(true)
        .truncate(false)
        .open(lock_path)
        .map_err(|err| AcquireFailure::Io(format!("open {}: {err}", lock_path.display())))?;
    // SAFETY: flock operates on a valid file descriptor owned by `file`.
    let rc = unsafe { libc::flock(file.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) };
    if rc == 0 {
        write_owner_metadata(&mut file, target, scope)
            .map_err(|err| AcquireFailure::Io(format!("write {}: {err}", lock_path.display())))?;
        return Ok(ScopedFileLock {
            lock_path: lock_path.to_path_buf(),
            archive: None,
            file: Some(std::sync::Arc::new(file)),
        });
    }
    let err = std::io::Error::last_os_error();
    if err.raw_os_error() == Some(libc::EWOULDBLOCK) || err.raw_os_error() == Some(libc::EAGAIN) {
        return Err(AcquireFailure::Busy(read_owner_metadata(lock_path)));
    }
    Err(AcquireFailure::Io(err.to_string()))
}

#[cfg(not(unix))]
fn try_acquire(
    target: &Path,
    lock_path: &Path,
    scope: FileLockScope,
) -> std::result::Result<ScopedFileLock, AcquireFailure> {
    if let Some(parent) = lock_path.parent() {
        fs::create_dir_all(parent).map_err(|err| AcquireFailure::Io(err.to_string()))?;
    }
    let mut file = match OpenOptions::new()
        .write(true)
        .create_new(true)
        .open(lock_path)
    {
        Ok(file) => file,
        Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => {
            if lock_file_is_stale(lock_path) {
                let _ = fs::remove_file(lock_path);
                return Err(AcquireFailure::Busy(None));
            }
            return Err(AcquireFailure::Busy(read_owner_metadata(lock_path)));
        }
        Err(err) => {
            return Err(AcquireFailure::Io(format!(
                "create {}: {err}",
                lock_path.display()
            )));
        }
    };
    write_owner_metadata(&mut file, target, scope)
        .map_err(|err| AcquireFailure::Io(format!("write {}: {err}", lock_path.display())))?;
    Ok(ScopedFileLock {
        lock_path: lock_path.to_path_buf(),
        archive: None,
        file: Some(std::sync::Arc::new(VaultLockHandle(
            lock_path.to_path_buf(),
        ))),
    })
}

thread_local! {
    static VAULT_HANDLES: RefCell<BTreeMap<PathBuf, std::sync::Weak<VaultLockHandle>>> = const { RefCell::new(BTreeMap::new()) };
}

/// Returns the hidden sidecar path used to coordinate writes to `target`.
pub fn lock_path_for(target: &Path) -> PathBuf {
    let Some(file_name) = target.file_name() else {
        let mut path = OsString::from(target.as_os_str());
        path.push(".lock");
        return PathBuf::from(path);
    };
    let mut lock_name = OsString::from(".");
    lock_name.push(file_name);
    lock_name.push(".lock");
    target.with_file_name(lock_name)
}

pub(crate) fn lock_timeout() -> Duration {
    std::env::var("LOCKBOX_LOCK_TIMEOUT_MS")
        .ok()
        .and_then(|value| value.parse::<u64>().ok())
        .map(Duration::from_millis)
        .unwrap_or(DEFAULT_LOCK_TIMEOUT)
}

fn write_owner_metadata(
    file: &mut File,
    target: &Path,
    scope: FileLockScope,
) -> std::io::Result<()> {
    file.set_len(0)?;
    file.seek(SeekFrom::Start(0))?;
    let now_ms = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis();
    let exe = std::env::current_exe()
        .ok()
        .map(|path| path.display().to_string())
        .unwrap_or_else(|| "unknown".to_string());
    let user = std::env::var("USER")
        .or_else(|_| std::env::var("USERNAME"))
        .unwrap_or_else(|_| "unknown".to_string());
    writeln!(file, "scope={}", scope.as_str())?;
    writeln!(file, "target={}", target.display())?;
    writeln!(file, "pid={}", std::process::id())?;
    writeln!(file, "user={user}")?;
    writeln!(file, "exe={exe}")?;
    writeln!(file, "created_unix_ms={now_ms}")?;
    file.sync_data()
}

fn read_owner_metadata(path: &Path) -> Option<String> {
    let mut text = String::new();
    File::open(path).ok()?.read_to_string(&mut text).ok()?;
    let pid = metadata_value(&text, "pid").unwrap_or("unknown");
    let created = metadata_value(&text, "created_unix_ms")
        .and_then(|value| value.parse::<u128>().ok())
        .map(format_unix_millis_datetime)
        .or_else(|| metadata_value(&text, "created_unix_ms").map(str::to_string))
        .unwrap_or_else(|| "unknown".to_string());
    Some(format!(" by pid {pid} since {created}"))
}

fn timeout_error(
    target: &Path,
    scope: FileLockScope,
    timeout: Duration,
    owner: Option<&str>,
) -> Error {
    let message = format!(
        "{} {} is locked{}; timed out after {}s",
        scope.as_str(),
        target.display(),
        owner.unwrap_or(""),
        timeout.as_secs()
    );
    if scope == FileLockScope::Recovery {
        Error::RecoveryInProgress(message)
    } else {
        Error::LockUnavailable(message)
    }
}

fn metadata_value<'a>(text: &'a str, key: &str) -> Option<&'a str> {
    text.lines()
        .find_map(|line| line.strip_prefix(key)?.strip_prefix('='))
}

fn format_unix_millis_datetime(unix_ms: u128) -> String {
    let rounded_seconds = ((unix_ms + 500) / 1000).min(i64::MAX as u128) as i64;
    let days = rounded_seconds.div_euclid(86_400);
    let seconds_of_day = rounded_seconds.rem_euclid(86_400);
    let (year, month, day) = civil_from_days(days);
    let hour = seconds_of_day / 3_600;
    let minute = (seconds_of_day % 3_600) / 60;
    let second = seconds_of_day % 60;
    format!("{year:04}-{month:02}-{day:02} {hour:02}:{minute:02}:{second:02}")
}

fn civil_from_days(days_since_unix_epoch: i64) -> (i64, u32, u32) {
    let z = days_since_unix_epoch + 719_468;
    let era = if z >= 0 { z } else { z - 146_096 } / 146_097;
    let day_of_era = z - era * 146_097;
    let year_of_era =
        (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365;
    let year = year_of_era + era * 400;
    let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100);
    let month_prime = (5 * day_of_year + 2) / 153;
    let day = day_of_year - (153 * month_prime + 2) / 5 + 1;
    let month = month_prime + if month_prime < 10 { 3 } else { -9 };
    let year = year + if month <= 2 { 1 } else { 0 };
    (year, month as u32, day as u32)
}

#[cfg(not(unix))]
fn lock_file_is_stale(path: &Path) -> bool {
    let Ok(text) = fs::read_to_string(path) else {
        return false;
    };
    let Some(pid) = metadata_value(&text, "pid").and_then(|pid| pid.parse::<u32>().ok()) else {
        return false;
    };
    !process_exists(pid)
}

#[cfg(windows)]
fn process_exists(pid: u32) -> bool {
    use windows_sys::Win32::Foundation::{CloseHandle, GetLastError, ERROR_INVALID_PARAMETER};
    use windows_sys::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION};

    // SAFETY: `OpenProcess` receives a numeric PID and returns an owned handle
    // that is closed below when successful.
    let handle = unsafe { OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid) };
    if !handle.is_null() {
        // SAFETY: `handle` was returned successfully by `OpenProcess` and is
        // closed exactly once here.
        unsafe { CloseHandle(handle) };
        return true;
    }

    // Access can be denied for protected processes that still exist. Treat
    // every failure except the documented invalid-PID case conservatively as
    // evidence that the process may still be alive.
    // SAFETY: `GetLastError` reads the calling thread's last-error value and
    // does not dereference pointers or require additional invariants.
    (unsafe { GetLastError() }) != ERROR_INVALID_PARAMETER
}

#[cfg(not(any(unix, windows)))]
fn process_exists(_pid: u32) -> bool {
    // Avoid deleting another process's lock on platforms where no reliable
    // native process query is implemented.
    true
}

#[cfg(test)]
mod tests {
    use super::{format_unix_millis_datetime, lock_path_for};
    use std::path::Path;

    #[test]
    fn lock_file_is_hidden_beside_target() {
        assert_eq!(
            lock_path_for(Path::new("/tmp/secrets.lbox")),
            Path::new("/tmp/.secrets.lbox.lock")
        );
        assert_eq!(
            lock_path_for(Path::new("relative.lbox")),
            Path::new(".relative.lbox.lock")
        );
    }

    #[test]
    fn unix_millis_format_is_human_readable_datetime() {
        assert_eq!(format_unix_millis_datetime(0), "1970-01-01 00:00:00");
        assert_eq!(
            format_unix_millis_datetime(1_704_067_201_234),
            "2024-01-01 00:00:01"
        );
        assert_eq!(
            format_unix_millis_datetime(1_704_067_201_500),
            "2024-01-01 00:00:02"
        );
    }

    #[cfg(windows)]
    #[test]
    fn current_process_exists() {
        assert!(super::process_exists(std::process::id()));
    }
}