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
use std::{
fs::{self, create_dir_all},
path::PathBuf,
str::FromStr,
};
use crossbeam_channel::Sender;
use serde::{Deserialize, Serialize};
use tempfile::TempDir;
use xvc_core::XvcRoot;
use xvc_ecs::R1NStore;
use xvc_logging::{watch, XvcOutputLine};
use super::{
XvcCachePath, XvcStorageDeleteEvent, XvcStorageGuid, XvcStorageInitEvent, XvcStorageListEvent,
XvcStorageOperations, XvcStoragePath, XvcStorageReceiveEvent, XvcStorageSendEvent,
XvcStorageTempDir, XVC_STORAGE_GUID_FILENAME,
};
use crate::{Result, XvcStorage, XvcStorageEvent};
pub fn cmd_storage_new_local(
input: std::io::StdinLock,
output_snd: &Sender<XvcOutputLine>,
xvc_root: &XvcRoot,
path: PathBuf,
name: String,
) -> Result<()> {
let remote = XvcLocalStorage {
guid: XvcStorageGuid::new(),
name,
path,
};
let (init_event, remote) = remote.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(remote.clone()),
event_e,
XvcStorageEvent::Init(init_event.clone()),
);
Ok(())
})?;
Ok(())
}
#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize)]
pub struct XvcLocalStorage {
pub guid: XvcStorageGuid,
pub name: String,
pub path: PathBuf,
}
impl XvcStorageOperations for XvcLocalStorage {
fn init(
self,
output: &Sender<XvcOutputLine>,
xvc_root: &XvcRoot,
) -> Result<(XvcStorageInitEvent, Self)> {
let guid_filename = self.path.join(XVC_STORAGE_GUID_FILENAME);
watch!(guid_filename);
if guid_filename.exists() {
let already_available_guid =
XvcStorageGuid::from_str(&fs::read_to_string(guid_filename)?)?;
output.send(XvcOutputLine::Info(format!(
"Found previous storage {} with GUID: {}",
self.path.to_string_lossy(),
already_available_guid,
)))?;
let new_self = XvcLocalStorage {
guid: already_available_guid.clone(),
..self
};
return Ok((
XvcStorageInitEvent {
guid: already_available_guid,
},
new_self,
));
}
if !self.path.exists() {
create_dir_all(&self.path)?;
}
fs::write(guid_filename, format!("{}", self.guid))?;
output.send(XvcOutputLine::Info(format!(
"Created local remote directory {} with guid: {}",
self.path.to_string_lossy(),
self.guid,
)));
Ok((
XvcStorageInitEvent {
guid: self.guid.clone(),
},
self,
))
}
fn list(
&self,
output: &Sender<XvcOutputLine>,
xvc_root: &XvcRoot,
) -> Result<XvcStorageListEvent> {
todo!()
}
fn send(
&self,
output: &Sender<XvcOutputLine>,
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 {
watch!(cache_path);
let remote_path = XvcStoragePath::from(format!("{}/{}", repo_guid, cache_path));
watch!(remote_path);
let abs_remote_path = remote_path.as_ref().to_logical_path(&self.path);
watch!(abs_remote_path);
if force && abs_remote_path.exists() {
fs::remove_file(abs_remote_path.clone())?;
}
let abs_cache_path = cache_path.to_absolute_path(xvc_root);
watch!(abs_cache_path);
let abs_remote_dir = abs_remote_path.parent().unwrap();
fs::create_dir_all(&abs_remote_dir)?;
fs::copy(&abs_cache_path, &abs_remote_path)?;
copied_paths.push(remote_path);
watch!(copied_paths.len());
output
.send(XvcOutputLine::Info(format!(
"{} -> {}",
abs_cache_path,
abs_remote_path.to_string_lossy()
)))
.unwrap();
}
Ok(XvcStorageSendEvent {
guid: self.guid.clone(),
paths: copied_paths,
})
}
fn receive(
&self,
output: &Sender<XvcOutputLine>,
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 {
watch!(cache_path);
let remote_path = XvcStoragePath::from(format!("{}/{}", repo_guid, cache_path));
watch!(remote_path);
let abs_remote_path = remote_path.as_ref().to_logical_path(&self.path);
watch!(abs_remote_path);
let abs_cache_path = temp_dir.temp_cache_path(cache_path)?;
watch!(abs_cache_path);
let abs_cache_dir = temp_dir.temp_cache_dir(cache_path)?;
watch!(abs_cache_dir);
fs::create_dir_all(&abs_cache_dir)?;
fs::copy(&abs_remote_path, &abs_cache_path)?;
watch!(abs_cache_path.exists());
copied_paths.push(remote_path);
watch!(copied_paths.len());
output
.send(XvcOutputLine::Info(format!(
"{} -> {}",
abs_remote_path.to_string_lossy(),
abs_cache_path,
)))
.unwrap();
}
Ok((
temp_dir,
XvcStorageReceiveEvent {
guid: self.guid.clone(),
paths: copied_paths,
},
))
}
fn delete(
&self,
output: &Sender<XvcOutputLine>,
xvc_root: &XvcRoot,
paths: &[XvcCachePath],
) -> Result<XvcStorageDeleteEvent> {
todo!()
}
}