xvc_storage/storage/
local.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
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
//! Local storage implementation
use std::{
    fs::{self, create_dir_all},
    path::PathBuf,
    str::FromStr,
};

use serde::{Deserialize, Serialize};

use xvc_core::XvcRoot;
use xvc_ecs::R1NStore;
use xvc_logging::{info, watch, XvcOutputSender};

use super::{
    XvcCachePath, XvcStorageDeleteEvent, XvcStorageGuid, XvcStorageInitEvent, XvcStorageListEvent,
    XvcStorageOperations, XvcStoragePath, XvcStorageReceiveEvent, XvcStorageSendEvent,
    XvcStorageTempDir, XVC_STORAGE_GUID_FILENAME,
};
use crate::{Error, Result, XvcStorage, XvcStorageEvent};

/// Entry point for `xvc storage new local` command
pub fn cmd_storage_new_local(
    _input: std::io::StdinLock,
    output_snd: &XvcOutputSender,
    xvc_root: &XvcRoot,
    path: PathBuf,
    name: String,
) -> Result<()> {
    let mut storage = XvcLocalStorage {
        guid: XvcStorageGuid::new(),
        name,
        path,
    };
    let init_event = storage.init(output_snd, xvc_root)?;

    xvc_root.with_r1nstore_mut(|store: &mut R1NStore<XvcStorage, XvcStorageEvent>| {
        let store_e = xvc_root.new_entity();
        let event_e = xvc_root.new_entity();
        store.insert(
            store_e,
            XvcStorage::Local(storage.clone()),
            event_e,
            XvcStorageEvent::Init(init_event.clone()),
        );
        Ok(())
    })?;

    Ok(())
}

/// A local storage is a directory that is on the same machine as the repository.
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
pub struct XvcLocalStorage {
    /// GUID of the storage
    pub guid: XvcStorageGuid,
    /// Name of the storage
    pub name: String,
    /// Path to the storage
    pub path: PathBuf,
}

impl XvcLocalStorage {
    fn storage_path(&self, repo_guid: &str, cache_path: &XvcCachePath) -> XvcStoragePath {
        XvcStoragePath::from(format!("{}/{}", repo_guid, cache_path))
    }
}

impl XvcStorageOperations for XvcLocalStorage {
    fn init(
        &mut self,
        output: &XvcOutputSender,
        _xvc_root: &XvcRoot,
    ) -> Result<XvcStorageInitEvent> {
        let guid_filename = self.path.join(XVC_STORAGE_GUID_FILENAME);
        // If guid filename exists, we can report a reinit and exit.

        if guid_filename.exists() {
            let already_available_guid =
                XvcStorageGuid::from_str(&fs::read_to_string(guid_filename)?)?;
            info!(
                output,
                "Found previous storage {} with GUID: {}",
                self.path.to_string_lossy(),
                already_available_guid
            );

            self.guid = already_available_guid.clone();

            return Ok(XvcStorageInitEvent {
                guid: already_available_guid,
            });
        }

        if !self.path.exists() {
            create_dir_all(&self.path)?;
        }

        fs::write(guid_filename, format!("{}", self.guid))?;
        info!(
            output,
            "Created local storage directory {} with guid: {}",
            self.path.to_string_lossy(),
            self.guid
        );

        Ok(XvcStorageInitEvent {
            guid: self.guid.clone(),
        })
    }

    fn list(&self, _output: &XvcOutputSender, _xvc_root: &XvcRoot) -> Result<XvcStorageListEvent> {
        todo!()
    }

    fn send(
        &self,
        output: &XvcOutputSender,
        xvc_root: &XvcRoot,
        paths: &[XvcCachePath],
        force: bool,
    ) -> Result<XvcStorageSendEvent> {
        let repo_guid = xvc_root
            .config()
            .guid()
            .ok_or_else(|| crate::Error::NoRepositoryGuidFound)?;
        let mut copied_paths = Vec::<XvcStoragePath>::new();

        for cache_path in paths {
            let storage_path = self.storage_path(&repo_guid, cache_path);
            let abs_storage_path = storage_path.as_ref().to_logical_path(&self.path);
            if force && abs_storage_path.exists() {
                fs::remove_file(abs_storage_path.clone())?;
            } else {
                info!(output, "[SKIPPED] {}", storage_path)
            }
            let abs_cache_path = cache_path.to_absolute_path(xvc_root);
            let abs_storage_dir = abs_storage_path.parent().unwrap();
            fs::create_dir_all(abs_storage_dir)?;
            fs::copy(&abs_cache_path, &abs_storage_path)?;
            copied_paths.push(storage_path);
            info!(
                output,
                "{} -> {}",
                abs_cache_path,
                abs_storage_path.to_string_lossy()
            );
        }

        Ok(XvcStorageSendEvent {
            guid: self.guid.clone(),
            paths: copied_paths,
        })
    }

    fn receive(
        &self,
        output: &XvcOutputSender,
        xvc_root: &XvcRoot,
        paths: &[XvcCachePath],
        _force: bool,
    ) -> Result<(XvcStorageTempDir, XvcStorageReceiveEvent)> {
        let repo_guid = xvc_root
            .config()
            .guid()
            .ok_or_else(|| crate::Error::NoRepositoryGuidFound)?;
        let mut copied_paths = Vec::<XvcStoragePath>::new();
        let temp_dir = XvcStorageTempDir::new()?;

        for cache_path in paths {
            let storage_path = self.storage_path(&repo_guid, cache_path);
            let abs_storage_path = storage_path.as_ref().to_logical_path(&self.path);
            let abs_cache_path = temp_dir.temp_cache_path(cache_path)?;
            let abs_cache_dir = temp_dir.temp_cache_dir(cache_path)?;
            fs::create_dir_all(&abs_cache_dir)?;
            fs::copy(&abs_storage_path, &abs_cache_path)?;
            copied_paths.push(storage_path);
            info!(
                output,
                "{} -> {}",
                abs_storage_path.to_string_lossy(),
                abs_cache_path
            );
        }

        Ok((
            temp_dir,
            XvcStorageReceiveEvent {
                guid: self.guid.clone(),
                paths: copied_paths,
            },
        ))
    }

    fn delete(
        &self,
        output: &XvcOutputSender,
        xvc_root: &XvcRoot,
        paths: &[XvcCachePath],
    ) -> Result<XvcStorageDeleteEvent> {
        let repo_guid = xvc_root
            .config()
            .guid()
            .ok_or_else(|| crate::Error::NoRepositoryGuidFound)?;
        let mut deleted_paths = Vec::<XvcStoragePath>::new();

        for cache_path in paths {
            let storage_path = self.storage_path(&repo_guid, cache_path);
            let abs_storage_path = storage_path.as_ref().to_logical_path(&self.path);
            fs::remove_file(&abs_storage_path)?;
            info!(output, "[DELETE] {}", abs_storage_path.to_string_lossy());
            deleted_paths.push(storage_path);
        }

        Ok(XvcStorageDeleteEvent {
            guid: self.guid.clone(),
            paths: deleted_paths,
        })
    }

    fn share(
        &self,
        _output: &XvcOutputSender,
        _xvc_root: &XvcRoot,
        _path: &XvcCachePath,
        _period: std::time::Duration,
    ) -> Result<super::XvcStorageExpiringShareEvent> {
        Err(Error::StorageDoesNotSupportSignedUrls)
    }
}