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
use super::{object::Objects, package::Packages, *};
use crate::{extra::persist::Pot, GID, UID};
use rustbreak::PathDatabase;
use snafu::ResultExt;
use std::{
    fs,
    os::unix::{self, prelude::PermissionsExt},
    path::PathBuf,
};

#[derive(Debug)]
pub struct LocalBackend {
    path: PathBuf,
    db: PathDatabase<(Objects, Packages), Pot>,
    objects: Objects,
    packages: Packages,
}

impl ReadBackend for LocalBackend {
    type Source = PathBuf;

    fn open(path: PathBuf) -> StoreResult<Self> {
        let db = PathDatabase::load_from_path(path.clone()).context(RustbreakLoadSnafu)?;
        let (objects, packages) = db.get_data(false).context(RustbreakLoadDataSnafu)?;

        Ok(Self {
            path,
            db,
            objects,
            packages,
        })
    }

    fn objects(&self) -> &Objects {
        &self.objects
    }

    fn packages(&self) -> &Packages {
        &self.packages
    }
}

impl WriteBackend for LocalBackend {
    type Source = PathBuf;

    fn init(path: PathBuf) -> StoreResult<Self> {
        let db = PathDatabase::create_at_path(path.clone(), (Objects::new(), Packages::new()))
            .context(RustbreakCreateSnafu)?;

        let mut perm = fs::metadata(&path).context(IoSnafu)?.permissions();
        perm.set_mode(0o644);
        fs::set_permissions(&path, perm).context(IoSnafu)?;
        unix::fs::chown(&path, UID, GID).context(IoSnafu)?;

        Ok(Self {
            path,
            db,
            objects: Objects::new(),
            packages: Packages::new(),
        })
    }

    fn objects_mut(&mut self) -> &mut Objects {
        &mut self.objects
    }

    fn packages_mut(&mut self) -> &mut Packages {
        &mut self.packages
    }

    fn flush(self) -> StoreResult<()> {
        self.db
            .put_data((self.objects, self.packages), true)
            .context(RustbreakSaveDataSnafu)?;
        unix::fs::chown(self.path, UID, GID).context(IoSnafu)?;

        Ok(())
    }
}