summaryrefslogtreecommitdiff
path: root/src/backup.rs
blob: 675f020a4fc43b3df89d5e03d28a99a07fd39377 (plain) (blame)
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
use std::{
    fs::{create_dir_all, File},
    io::{Read, Write},
    path::PathBuf,
    time::{SystemTime, UNIX_EPOCH},
};

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::{
    config::Config,
    error::{Error, Result},
    packages::Package,
    pathinfo::PathInfo,
};

pub type BackupId = String;

#[derive(Debug, Serialize, Deserialize)]
pub struct Backup {
    pub id: String,
    pub timestamp: u64,
    packages: Vec<Package>,
    pub files: Vec<PathInfo>,
    device: String,
}

impl Backup {
    pub fn create(config: &Config, packages: Vec<Package>) -> Result<Self> {
        let mut files: Vec<PathInfo> = Vec::new();
        for dir in &config.directories {
            files.push(PathInfo::from_path(config, dir)?);
        }
        Ok(Self {
            // TODO: UUID not really needed, maybe a shorter hash
            id: Uuid::new_v4().to_string(),
            timestamp: Self::get_timestamp(),
            packages,
            files,
            device: config.device.clone(),
        })
    }

    pub fn save(&self, config: &Config) -> Result<()> {
        println!("Save Backup {:?}", self.get_location(config));
        // println!("{self:#?}");
        self.get_location(config).append_to_root(config)?;

        let backup_root = self.get_location(config).get_absolute_dir(config);
        create_dir_all(&backup_root).unwrap();
        let path = format!("{}/index.json", backup_root);
        let mut f = File::create(path).unwrap();
        f.write_all(&serde_json::to_vec(self).unwrap()).unwrap();

        for path in &self.files {
            path.save(&backup_root)?;
        }

        Ok(())
    }

    pub fn get_last(config: &Config) -> Result<Option<Self>> {
        let backup_index_root = format!("{}/index.json", config.root);
        let list: Vec<BackupLocation> = match Self::get_json_content(&backup_index_root) {
            Ok(list) => list,
            Err(err) => {
                if err.to_string() == "io: No such file or directory (os error 2)" {
                    return Ok(None);
                };
                return Err(err);
            }
        };

        Ok(Some(Self::from_index(
            config,
            &list.last().ok_or(Error::BackupNotFound)?.id,
        )?))
    }

    pub fn from_index(config: &Config, id: &BackupId) -> Result<Self> {
        let backup_index_root = format!("{}/index.json", config.root);
        let list: Vec<BackupLocation> = Self::get_json_content(&backup_index_root)?;
        let index_loc = list
            .iter()
            .find(|bl| &bl.id == id)
            .ok_or(Error::BackupNotFound)?
            .rel_location
            .clone();

        let path = format!("{}/{index_loc}/index.json", config.root);
        let index_file: Self = Self::get_json_content(&path)?;

        Ok(index_file)
    }

    pub fn get_location(&self, config: &Config) -> BackupLocation {
        let rel_location = format!("{}_{}", config.device, self.timestamp);

        BackupLocation {
            id: self.id.to_string(),
            rel_location,
        }
    }

    pub fn get_absolute_file_location(&self, config: &Config, rel_location: &str) -> String {
        let loc = self.get_location(config).get_absolute_dir(config);

        format!("{}/{}", loc, rel_location)
    }

    fn get_json_content<T: for<'a> Deserialize<'a>>(path: &str) -> Result<T> {
        let mut file = File::open(path)?;
        let mut content = String::new();
        file.read_to_string(&mut content)?;
        Ok(serde_json::from_str(&content)?)
    }

    fn get_timestamp() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BackupLocation {
    id: BackupId,
    rel_location: String,
}

impl BackupLocation {
    pub fn get_absolute_dir(&self, config: &Config) -> String {
        format!("{}/{}", config.root, self.rel_location)
    }

    pub fn append_to_root(&self, config: &Config) -> Result<()> {
        let backup_index_root = format!("{}/index.json", config.root);
        let path = PathBuf::from(&backup_index_root);
        if path.exists() {
            let mut f = File::open(&path)?;
            let mut content = String::new();
            f.read_to_string(&mut content)?;
            let mut loc: Vec<BackupLocation> = serde_json::from_str(&content)?;

            let mut f = File::create(path)?;
            loc.push(self.clone());

            f.write_all(&serde_json::to_vec(&loc)?)?;
        } else {
            create_dir_all(&config.root).unwrap();
            let mut f = File::create(backup_index_root)?;
            f.write_all(&serde_json::to_vec(&vec![self])?)?;
        };

        Ok(())
    }
}