fedimint_portalloc/
data.rs

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
mod dto;

use std::path::PathBuf;
use std::time::Duration;
use std::{fs, thread};

use anyhow::{bail, Result};
use fs2::FileExt;
use tracing::{debug, info, warn};

use crate::util;

/// Root directory where we keep the lock & data file
pub struct DataDir {
    path: PathBuf,
    lock_file: fs::File,
}

impl DataDir {
    pub fn new(path: impl Into<PathBuf>) -> Result<Self> {
        let path = path.into();
        ensure_root_exists(&path)?;

        let lock = util::open_lock_file(&path)?;

        Ok(Self {
            path,
            lock_file: lock,
        })
    }

    pub fn with_lock<T>(&mut self, f: impl FnOnce(&mut LockedRoot) -> Result<T>) -> Result<T> {
        f(&mut LockedRoot::new(&self.path, &mut self.lock_file)?)
    }
}

fn ensure_root_exists(dir: &PathBuf) -> Result<()> {
    if !dir.try_exists()? {
        info!(dir = %dir.display(), "Creating root dir");
        fs::create_dir_all(dir)?;
    }
    Ok(())
}

/// A handle passed to `with_lock` argument after root was acquired
pub struct LockedRoot<'a> {
    path: &'a PathBuf,
    lock_file: &'a mut fs::File,
    locked: bool,
}

impl<'a> Drop for LockedRoot<'a> {
    fn drop(&mut self) {
        if self.locked {
            let Ok(()) = self.lock_file.unlock() else {
                warn!("Failed to release the lock file");
                return;
            };
            self.locked = false;
        }
    }
}

impl<'a> LockedRoot<'a> {
    fn new(path: &'a PathBuf, lock_file: &'a mut fs::File) -> Result<Self> {
        let mut locked_root = Self {
            path,
            lock_file,
            locked: false,
        };
        locked_root.lock()?;
        Ok(locked_root)
    }

    fn lock(&mut self) -> Result<()> {
        debug!(path = %self.path.display(), "Acquiring lock...");
        if self.lock_file.try_lock_exclusive().is_err() {
            info!("Lock taken, waiting...");
            self.lock_file.lock_exclusive()?;
            info!("Acquired lock after wait");
        };
        debug!("Acquired lock");
        self.locked = true;
        Ok(())
    }

    fn unlock(&mut self) -> Result<()> {
        self.ensure_locked()?;
        debug!(path = %self.path.display(), "Releasing lock...");
        self.lock_file.unlock()?;
        self.locked = false;
        Ok(())
    }

    fn data_file_path(&self) -> PathBuf {
        self.path.join("fm-portalloc.json")
    }

    fn ensure_locked(&self) -> anyhow::Result<()> {
        if !self.locked {
            bail!("LockedRoot no longer valid");
        }
        Ok(())
    }

    pub fn r#yield(&mut self, duration: Duration) -> Result<()> {
        self.unlock()?;
        thread::sleep(duration);
        self.lock()?;
        Ok(())
    }
    pub fn load_data(&self, now: u64) -> Result<dto::RootData> {
        self.ensure_locked()?;
        let path = self.data_file_path();
        if !path.try_exists()? {
            return Ok(Default::default());
        }
        let root_data: dto::RootData = serde_json::from_reader::<_, _>(std::fs::File::open(path)?)?;
        Ok(root_data.reclaim(now))
    }

    pub fn store_data(&mut self, data: &dto::RootData) -> Result<()> {
        util::store_json_pretty_to_file(&self.data_file_path(), data)
    }
}