zbox 0.1.4

Zbox is a zero-details, privacy-focused embeddable file system.
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
use std::sync::{Arc, RwLock};
use std::path::Path;
use std::io;

use bytes::BufMut;

use error::{Error, Result};
use base::IntoRef;
use base::crypto::{Cipher, Cost};
use content::{Store, StoreRef};
use trans::{Id, Eid, Txid, TxMgr, TxMgrRef};
use trans::cow::IntoCow;
use volume::{Volume, VolumeRef, Meta as VolumeMeta};
use super::Handle;
use super::fnode::{Fnode, FnodeRef, FileType, Version, Metadata, DirEntry,
                   Cache as FnodeCache, Reader as FnodeReader,
                   Writer as FnodeWriter};

// default cache size
const FNODE_CACHE_SIZE: usize = 16;

/// File system
#[derive(Debug)]
pub struct Fs {
    root: FnodeRef,
    fcache: FnodeCache,
    store: StoreRef,
    txmgr: TxMgrRef,
    vol: VolumeRef,
}

impl Fs {
    /// Check if fs exists
    pub fn exists(uri: &str) -> Result<bool> {
        Volume::exists(uri)
    }

    /// Create new fs
    pub fn create(
        uri: &str,
        pwd: &str,
        cost: Cost,
        cipher: Cipher,
    ) -> Result<Fs> {
        // create and initialise volume
        let mut vol = Volume::new(uri)?;
        vol.init(cost, cipher)?;
        let vol = vol.into_ref();

        // create fs components
        let txmgr = TxMgr::new(Txid::from(0), &vol).into_ref();
        let fcache = FnodeCache::new(FNODE_CACHE_SIZE, &txmgr);

        // the initial transaction, it must be successful
        let mut store_ref: Option<StoreRef> = None;
        let mut root_ref: Option<FnodeRef> = None;
        TxMgr::begin_trans(&txmgr)?.run_all(|| {
            let store_cow = Store::new(&txmgr, &vol).into_cow(&txmgr)?;
            let root_cow =
                Fnode::new(FileType::Dir, 0, &store_cow).into_cow(&txmgr)?;
            root_ref = Some(root_cow);
            store_ref = Some(store_cow);
            Ok(())
        })?;

        // write volume super block with payload
        // payload: store id + root id
        let store = store_ref.unwrap();
        let root = root_ref.unwrap();
        {
            let mut payload = Vec::with_capacity(2 * Eid::EID_SIZE);
            let store = store.read().unwrap();
            let root = root.read().unwrap();
            payload.put(store.id().as_ref());
            payload.put(root.id().as_ref());
            let mut vol = vol.write().unwrap();
            vol.write_payload(pwd, &payload)?;
        }

        Ok(Fs {
            root,
            fcache,
            store,
            txmgr,
            vol,
        })
    }

    /// Open fs
    pub fn open(uri: &str, pwd: &str) -> Result<Fs> {
        let mut vol = Volume::new(uri)?;

        // open volume
        let (last_txid, payload) = vol.open(pwd)?;
        let mut iter = payload.chunks(Eid::EID_SIZE);
        let store_id = Eid::from_slice(iter.next().unwrap());
        let root_id = Eid::from_slice(iter.next().unwrap());
        let vol = vol.into_ref();

        // create file sytem components
        let txmgr = TxMgr::new(last_txid, &vol).into_ref();
        let store = Store::load_store(&store_id, &txmgr, &vol)?;
        let root = Fnode::load_root(&root_id, &txmgr, &store, &vol)?;
        let fcache = FnodeCache::new(FNODE_CACHE_SIZE, &txmgr);

        Ok(Fs {
            root,
            fcache,
            store,
            txmgr,
            vol,
        })
    }

    /// Get volume metadata
    pub fn volume_meta(&self) -> VolumeMeta {
        let vol = self.vol.read().unwrap();
        vol.meta()
    }

    /// Reset volume password
    pub fn reset_password(
        &mut self,
        old_pwd: &str,
        new_pwd: &str,
        cost: Cost,
    ) -> Result<()> {
        let mut vol = self.vol.write().unwrap();
        vol.reset_password(old_pwd, new_pwd, cost)
    }

    /// Resolve path
    pub fn resolve(&self, path: &Path) -> Result<FnodeRef> {
        // only resolve absolute path
        if !path.has_root() {
            return Err(Error::InvalidPath);
        }

        let mut fnode = self.root.clone();

        // loop through path component and skip root
        for name in path.iter().skip(1) {
            let name = name.to_str().unwrap();
            let fc = fnode.clone();
            fnode = Fnode::child(fc, name, &self.fcache, &self.vol)?;
        }
        Ok(fnode)
    }

    // resolve path to parent fnode and child file name
    fn resolve_parent(&self, path: &Path) -> Result<(FnodeRef, String)> {
        let parent_path = path.parent().ok_or(Error::IsRoot)?;
        let file_name = path.file_name().and_then(|s| s.to_str()).ok_or(
            Error::InvalidPath,
        )?;
        let parent = self.resolve(parent_path)?;
        Ok((parent, file_name.to_string()))
    }

    /// Open fnode
    pub fn open_fnode(&mut self, path: &Path) -> Result<Handle> {
        let fnode = self.resolve(path)?;
        Ok(Handle {
            fnode,
            store: self.store.clone(),
            txmgr: self.txmgr.clone(),
            vol: self.vol.clone(),
        })
    }

    /// Create fnode
    pub fn create_fnode(
        &mut self,
        path: &Path,
        ftype: FileType,
        version_limit: u8,
    ) -> Result<FnodeRef> {
        let (parent, name) = self.resolve_parent(path)?;

        {
            let parent = parent.read().unwrap();
            if !parent.is_dir() {
                return Err(Error::NotDir);
            }
            if parent.has_child(&name) {
                return Err(Error::AlreadyExists);
            }
        }

        let mut fnode = FnodeRef::default();
        let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
        tx_handle.run_all(|| {
            fnode = Fnode::new_under(
                &parent,
                &name,
                ftype,
                version_limit,
                &self.txmgr,
            )?;
            Ok(())
        })?;

        Ok(fnode)
    }

    /// Recursively create directories along the path
    pub fn create_dir_all(&mut self, path: &Path) -> Result<()> {
        if path == Path::new("") {
            return Ok(());
        }

        match self.create_fnode(path, FileType::Dir, 0) {
            Ok(_) => return Ok(()),
            Err(ref e) if *e == Error::NotFound => {}
            Err(err) => return Err(err),
        }
        match path.parent() {
            Some(p) => self.create_dir_all(p)?,
            None => return Err(Error::IsRoot),
        }
        self.create_fnode(path, FileType::Dir, 0)?;
        Ok(())
    }

    /// Read directory entries
    pub fn read_dir(&self, path: &Path) -> Result<Vec<DirEntry>> {
        let parent = self.resolve(path)?;
        Fnode::read_dir(parent, path, &self.fcache, &self.vol)
    }

    /// Get metadata of specified path
    pub fn metadata(&self, path: &Path) -> Result<Metadata> {
        let fnode_ref = self.resolve(path)?;
        let fnode = fnode_ref.read().unwrap();
        Ok(fnode.metadata())
    }

    /// Get file version list of specified path
    pub fn history(&self, path: &Path) -> Result<Vec<Version>> {
        let fnode_ref = self.resolve(path)?;
        let fnode = fnode_ref.read().unwrap();
        if fnode.is_dir() {
            return Err(Error::IsDir);
        }
        Ok(fnode.history())
    }

    /// Copy a regular file to another
    pub fn copy(&mut self, from: &Path, to: &Path) -> Result<()> {
        let src = self.open_fnode(from)?;
        {
            let fnode = src.fnode.read().unwrap();
            if !fnode.is_file() {
                return Err(Error::NotFile);
            }
        }

        let tgt = {
            match self.open_fnode(to) {
                Ok(tgt) => {
                    // if target and source is same fnode, do nothing
                    if Arc::ptr_eq(&tgt.fnode, &src.fnode) {
                        return Ok(());
                    }

                    {
                        let fnode = tgt.fnode.read().unwrap();
                        if !fnode.is_file() {
                            return Err(Error::NotFile);
                        }
                    }
                    tgt
                }
                Err(ref err) if *err == Error::NotFound => {
                    self.create_fnode(
                        to,
                        FileType::File,
                        Fnode::DEFAULT_VERSION_LIMIT,
                    )?;
                    self.open_fnode(to)?
                }
                Err(err) => return Err(err),
            }
        };

        // tx #1: truncate target file
        let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
        tx_handle.run_all(
            || Fnode::set_len(tgt.clone(), 0, tx_handle.txid),
        )?;

        // tx #2: copy data from source to target
        let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
        tx_handle.run_all(|| {
            let mut rdr = FnodeReader::new_current(src.fnode.clone())?;
            let mut wtr = FnodeWriter::new(tgt.clone(), tx_handle.txid)?;
            io::copy(&mut rdr, &mut wtr)?;
            wtr.finish()
        })?;

        Ok(())
    }

    /// Remove a regular file
    pub fn remove_file(&mut self, path: &Path) -> Result<()> {
        let fnode_ref = self.resolve(path)?;
        {
            let fnode = fnode_ref.read().unwrap();
            if !fnode.is_file() {
                return Err(Error::NotFile);
            }
        }

        // begin and run transaction
        let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
        tx_handle.run_all(move || {
            Fnode::unlink(&fnode_ref)?;
            let mut fnode = fnode_ref.write().unwrap();
            fnode.make_mut()?.clear_vers()?;
            fnode.make_del()?;
            self.fcache.remove(fnode.id());
            Ok(())
        })?;

        Ok(())
    }

    /// Remove an existing empty directory
    pub fn remove_dir(&mut self, path: &Path) -> Result<()> {
        let fnode_ref = self.resolve(path)?;
        {
            let fnode = fnode_ref.read().unwrap();
            if !fnode.is_dir() {
                return Err(Error::NotDir);
            }
            if fnode.is_root() {
                return Err(Error::IsRoot);
            }
            if fnode.children_cnt() > 0 {
                return Err(Error::NotEmpty);
            }
        }

        // begin and run transaction
        let tx_handle = TxMgr::begin_trans(&self.txmgr)?;
        tx_handle.run_all(move || {
            Fnode::unlink(&fnode_ref)?;
            let mut fnode = fnode_ref.write().unwrap();
            fnode.make_del()?;
            self.fcache.remove(fnode.id());
            Ok(())
        })?;

        Ok(())
    }

    /// Remove an existing directory recursively
    pub fn remove_dir_all(&mut self, path: &Path) -> Result<()> {
        for child in self.read_dir(path)? {
            let child_path = child.path();
            match child.file_type() {
                FileType::File => self.remove_file(&child_path)?,
                FileType::Dir => self.remove_dir_all(&child_path)?,
            }
        }
        match self.remove_dir(path) {
            Ok(_) => Ok(()),
            Err(ref err) if *err == Error::IsRoot => Ok(()),
            Err(err) => Err(err),
        }
    }

    /// Rename a file or directory to new name
    pub fn rename(&mut self, from: &Path, to: &Path) -> Result<()> {
        if to.starts_with(from) {
            return Err(Error::InvalidArgument);
        }

        let src = self.open_fnode(from)?;
        {
            let fnode = src.fnode.read().unwrap();
            if fnode.is_root() {
                return Err(Error::IsRoot);
            }
        }

        let (tgt_parent, name) = self.resolve_parent(to)?;
        {
            let parent = tgt_parent.read().unwrap();
            if parent.has_child(&name) {
                return Err(Error::AlreadyExists);
            }
        }

        // begin and run transaction
        TxMgr::begin_trans(&self.txmgr)?.run_all(|| {
            // remove from source
            Fnode::unlink(&src.fnode)?;

            // and then add to target
            Fnode::add_child(&tgt_parent, &src.fnode, &name)
        })
    }
}

impl IntoRef for Fs {}

/// Fs reference type
pub type FsRef = Arc<RwLock<Fs>>;