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
use super::{
file_system::{normalise_path_separator, upload_file_to_net},
metadata::FileMeta,
ProcessedFiles,
};
use crate::{api::app::consts::*, Error, Result, Safe};
use log::{debug, info};
use std::{collections::BTreeMap, fs, path::Path};
pub type FilesMap = BTreeMap<String, FileItem>;
pub type FileItem = BTreeMap<String, String>;
pub trait GetAttr {
fn getattr(&self, key: &str) -> Result<&str>;
}
impl GetAttr for FileItem {
fn getattr(&self, key: &str) -> Result<&str> {
match self.get(key) {
Some(v) => Ok(v),
None => Err(Error::EntryNotFound(format!("key not found: {}", key))),
}
}
}
#[allow(clippy::too_many_arguments)]
pub(crate) async fn add_or_update_file_item(
safe: &mut Safe,
file_name: &str,
file_name_for_map: &str,
file_path: &Path,
file_meta: &FileMeta,
file_link: Option<&str>,
name_exists: bool,
dry_run: bool,
files_map: &mut FilesMap,
processed_files: &mut ProcessedFiles,
) -> bool {
match gen_new_file_item(safe, file_path, file_meta, file_link, dry_run).await {
Ok(new_file_item) => {
let content_added_sign = if name_exists {
CONTENT_UPDATED_SIGN.to_string()
} else {
CONTENT_ADDED_SIGN.to_string()
};
debug!("New FileItem item: {:?}", new_file_item);
debug!("New FileItem item inserted as {:?}", file_name);
files_map.insert(file_name_for_map.to_string(), new_file_item.clone());
processed_files.insert(
file_name.to_string(),
(
content_added_sign,
new_file_item
.get(PREDICATE_LINK)
.unwrap_or(&String::default())
.to_string(),
),
);
true
}
Err(err) => {
processed_files.insert(
file_name.to_string(),
(CONTENT_ERROR_SIGN.to_string(), format!("<{}>", err)),
);
info!("Skipping file \"{}\": {:?}", file_link.unwrap_or(""), err);
false
}
}
}
async fn gen_new_file_item(
safe: &mut Safe,
file_path: &Path,
file_meta: &FileMeta,
link: Option<&str>,
dry_run: bool,
) -> Result<FileItem> {
let mut file_item = file_meta.to_file_item();
if file_meta.is_file() {
let xorurl = match link {
None => upload_file_to_net(safe, file_path, dry_run).await?,
Some(link) => link.to_string(),
};
file_item.insert(PREDICATE_LINK.to_string(), xorurl);
} else if file_meta.is_symlink() {
let result = fs::metadata(&file_path);
let symlink_target_type = match result {
Ok(meta) => {
if meta.is_dir() {
"dir"
} else {
"file"
}
}
Err(_) => "unknown",
};
let target_path = match link {
Some(target) => target.to_string(),
None => {
let target_path = fs::read_link(&file_path).map_err(|e| {
Error::FileSystemError(format!(
"Unable to read link: {}. {:#?}",
file_path.display(),
e
))
})?;
normalise_path_separator(&target_path.display().to_string())
}
};
file_item.insert("symlink_target".to_string(), target_path);
file_item.insert(
"symlink_target_type".to_string(),
symlink_target_type.to_string(),
);
}
Ok(file_item)
}