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
use crate::storage::value_storage::ValueStorage;
use crate::tokio::task::{self, JoinError};
use cfg_if::cfg_if;
use fs2::FileExt; //locking
use ockam_core::compat::boxed::Box;
use ockam_core::errcode::{Kind, Origin};
use ockam_core::{async_trait, Error, Result};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::BufReader;
use std::marker::PhantomData;
use std::path::{Path, PathBuf};

/// File Storage
/// There three files involved
///  - the actual file storing data
///
///  - a temporary file used to avoid losing data during writes. The whole data file is copied to the
///    temporary file then the temporary file is renamed
///
///  - a lock file.  It's used to control inter-process accesses to the data.
///    Before reading or writing to the data fil, a shared or exclusive lock is first acquired
///    on this file.  We don't lock over the data file directly, because doesn't play well with
///    the file rename we do
#[derive(Clone)]
pub struct FileValueStorage<V> {
    path: Box<Path>,
    temp_path: Box<Path>,
    lock_path: Box<Path>,
    _phantom_data: PhantomData<V>,
}

impl<V: Default + Serialize + for<'de> Deserialize<'de>> FileValueStorage<V> {
    /// Create and init the file storage
    pub async fn create(path: &Path) -> Result<Self> {
        let mut s = Self::new(path);
        s.init().await?;
        Ok(s)
    }

    /// Create the file storage but don't initialize it
    fn new(path: &Path) -> Self {
        let temp_path = Self::path_with_suffix(path, ".tmp");
        let lock_path = Self::path_with_suffix(path, ".lock");
        Self {
            path: path.into(),
            temp_path: temp_path.into(),
            lock_path: lock_path.into(),
            _phantom_data: PhantomData,
        }
    }

    /// Create FileStorage using file at given Path
    /// If file doesn't exist, it will be created
    async fn init(&mut self) -> Result<()> {
        // This can block, but only when first initializing and just need to write an empty vault.
        // So didn't bother to do it async
        let lock_file = Self::open_lock_file(&self.lock_path)?;
        lock_file.lock_exclusive().map_err(map_io_err)?;
        if !self.path.exists() {
            let empty = V::default();
            Self::flush_to_file(&self.path, &self.temp_path, &empty)?;
        }
        lock_file.unlock().map_err(map_io_err)?;
        Ok(())
    }

    fn load(path: &Path) -> Result<V> {
        let file = File::open(path).map_err(map_io_err)?;
        let reader = BufReader::new(file);
        Ok(serde_json::from_reader::<BufReader<File>, V>(reader)
            .map_err(|e| ValueStorageError::InvalidStorageData(e.to_string()))?)
    }

    // Flush vault to target, using temp_path as intermediary file.
    fn flush_to_file(target: &Path, temp_path: &Path, value: &V) -> Result<()> {
        let data = serde_json::to_vec(value)
            .map_err(|e| ValueStorageError::InvalidStorageData(e.to_string()))?;
        use std::io::prelude::*;
        cfg_if! {
            if #[cfg(windows)] {
                let mut file = std::fs::OpenOptions::new()
                    .write(true)
                    .create(true)
                    .open(temp_path)
                    .map_err(|_| ValueStorageError::StorageError)?;
            } else {
                use std::os::unix::fs::OpenOptionsExt;
                let mut file = std::fs::OpenOptions::new()
                    .write(true)
                    .create(true)
                    .mode(0o600)
                    .open(temp_path)
                    .map_err(|_| ValueStorageError::StorageError)?;
            }
        }
        file.write_all(&data)
            .map_err(|_| ValueStorageError::StorageError)?;
        file.flush().map_err(|_| ValueStorageError::StorageError)?;
        file.sync_all()
            .map_err(|_| ValueStorageError::StorageError)?;
        std::fs::rename(temp_path, target).map_err(|_| ValueStorageError::StorageError)?;
        Ok(())
    }
}

impl<V> FileValueStorage<V> {
    fn path_with_suffix(path: &Path, suffix: &str) -> PathBuf {
        match path.extension() {
            None => path.with_extension(suffix),
            Some(e) => path.with_extension(format!("{}{}", e.to_str().unwrap(), suffix)),
        }
    }

    fn open_lock_file(lock_path: &Path) -> Result<File> {
        std::fs::OpenOptions::new()
            .write(true)
            .read(true)
            .create(true)
            .open(lock_path)
            .map_err(map_io_err)
    }
}

#[async_trait]
impl<V: Default + for<'a> Deserialize<'a> + Serialize + Send + Sync + 'static> ValueStorage<V>
    for FileValueStorage<V>
{
    async fn update_value(&self, f: impl Fn(V) -> Result<V> + Send + Sync + 'static) -> Result<()> {
        let f = move |v: V| Ok((f(v)?, ()));
        let _ = self.modify_value(f).await?;
        Ok(())
    }

    async fn modify_value<R: Send + Sync + 'static>(
        &self,
        f: impl Fn(V) -> Result<(V, R)> + Send + Sync + 'static,
    ) -> Result<R> {
        let lock_path = self.lock_path.clone();
        let temp_path = self.temp_path.clone();
        let path = self.path.clone();
        let tr = move || -> Result<R> {
            let file = FileValueStorage::<V>::open_lock_file(&lock_path)?;
            file.lock_exclusive().map_err(map_io_err)?;
            let existing_value = FileValueStorage::<V>::load(&path)?;
            let (updated_value, result) = f(existing_value)?;
            FileValueStorage::<V>::flush_to_file(&path, &temp_path, &updated_value)?;
            // if something goes wrong it will be unlocked once the file handler get closed anyway
            file.unlock().map_err(map_io_err)?;
            Ok(result)
        };
        task::spawn_blocking(tr).await.map_err(map_join_err)?
    }

    async fn read_value<R: Send + Sync + 'static>(
        &self,
        f: impl Fn(V) -> Result<R> + Send + Sync + 'static,
    ) -> Result<R> {
        let path = self.path.clone();
        let lock_path = self.lock_path.clone();
        let tr = move || {
            let file = FileValueStorage::<V>::open_lock_file(&lock_path)?;
            file.lock_shared().map_err(map_io_err)?;
            let data = FileValueStorage::<V>::load(&path)?;
            let r = f(data)?;
            // if something goes wrong it will be unlocked once the file handler get closed anyway
            file.unlock().map_err(map_io_err)?;
            Ok(r)
        };
        task::spawn_blocking(tr).await.map_err(map_join_err)?
    }
}

fn map_join_err(err: JoinError) -> Error {
    Error::new(Origin::Application, Kind::Io, err)
}

fn map_io_err(err: std::io::Error) -> Error {
    Error::new(Origin::Application, Kind::Io, err)
}

/// Represents the failures that can occur when storing values
#[derive(Clone, Debug)]
pub enum ValueStorageError {
    /// IO error
    StorageError,
    /// Invalid Storage data
    InvalidStorageData(String),
}

impl ockam_core::compat::error::Error for ValueStorageError {}

impl core::fmt::Display for ValueStorageError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::StorageError => write!(f, "invalid storage"),
            Self::InvalidStorageData(e) => write!(f, "invalid storage data {:?}", e),
        }
    }
}

impl From<ValueStorageError> for Error {
    #[track_caller]
    fn from(err: ValueStorageError) -> Self {
        Error::new(Origin::Vault, Kind::Invalid, err)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::file_key_value_storage::tests::create_temp_file;
    use ockam_core::Result;

    #[tokio::test]
    async fn test_file_value_storage() -> Result<()> {
        let storage: FileValueStorage<Value> =
            FileValueStorage::create(create_temp_file().as_path())
                .await
                .unwrap();

        let initial = storage.read_value(Ok).await?;

        // sanity check
        assert_eq!(Value::default(), Value(0));

        // the initial value is the default value
        assert_eq!(initial, Value::default());

        // the value can be updated
        storage
            .update_value(move |_: Value| Ok(Value(10)))
            .await
            .unwrap();

        // the new value can be read again
        let updated = storage.read_value(Ok).await?;
        assert_eq!(updated, Value(10));

        Ok(())
    }

    #[derive(Serialize, Deserialize, Default, PartialEq, Eq, Debug)]
    struct Value(u8);
}