@anysphere/file-service 0.0.0-c0e75c6f → 0.0.0-dacf38fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +0 -1
- package/package.json +7 -7
- package/src/lib.rs +52 -84
- package/src/merkle_tree/local_construction.rs +14 -18
- package/src/merkle_tree/mod.rs +120 -223
package/index.d.ts
CHANGED
|
@@ -11,6 +11,5 @@ export class MerkleClient {
|
|
|
11
11
|
getSubtreeHash(path: string): Promise<string>
|
|
12
12
|
getNumEmbeddableFiles(): Promise<number>
|
|
13
13
|
getAllFiles(): Promise<Array<string>>
|
|
14
|
-
getNextFileToEmbed(): Promise<Array<string>>
|
|
15
14
|
getHashesForFiles(files: Array<string>): Promise<Array<string>>
|
|
16
15
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anysphere/file-service",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-dacf38fa",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"napi": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"version": "napi version"
|
|
36
36
|
},
|
|
37
37
|
"optionalDependencies": {
|
|
38
|
-
"@anysphere/file-service-win32-x64-msvc": "0.0.0-
|
|
39
|
-
"@anysphere/file-service-darwin-x64": "0.0.0-
|
|
40
|
-
"@anysphere/file-service-linux-x64-gnu": "0.0.0-
|
|
41
|
-
"@anysphere/file-service-darwin-arm64": "0.0.0-
|
|
42
|
-
"@anysphere/file-service-win32-arm64-msvc": "0.0.0-
|
|
43
|
-
"@anysphere/file-service-darwin-universal": "0.0.0-
|
|
38
|
+
"@anysphere/file-service-win32-x64-msvc": "0.0.0-dacf38fa",
|
|
39
|
+
"@anysphere/file-service-darwin-x64": "0.0.0-dacf38fa",
|
|
40
|
+
"@anysphere/file-service-linux-x64-gnu": "0.0.0-dacf38fa",
|
|
41
|
+
"@anysphere/file-service-darwin-arm64": "0.0.0-dacf38fa",
|
|
42
|
+
"@anysphere/file-service-win32-arm64-msvc": "0.0.0-dacf38fa",
|
|
43
|
+
"@anysphere/file-service-darwin-universal": "0.0.0-dacf38fa"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/lib.rs
CHANGED
|
@@ -3,8 +3,6 @@ pub mod file_utils;
|
|
|
3
3
|
pub mod git_utils;
|
|
4
4
|
pub mod merkle_tree;
|
|
5
5
|
|
|
6
|
-
use std::vec;
|
|
7
|
-
|
|
8
6
|
use merkle_tree::{LocalConstruction, MerkleTree};
|
|
9
7
|
|
|
10
8
|
#[macro_use]
|
|
@@ -33,7 +31,7 @@ impl MerkleClient {
|
|
|
33
31
|
// 3. sync with the remote
|
|
34
32
|
self.compute_merkle_tree().await?;
|
|
35
33
|
|
|
36
|
-
|
|
34
|
+
Ok(())
|
|
37
35
|
}
|
|
38
36
|
|
|
39
37
|
pub async unsafe fn interrupt(&mut self) -> Result<(), napi::Error> {
|
|
@@ -69,87 +67,57 @@ impl MerkleClient {
|
|
|
69
67
|
let _r = self.tree.delete_file(file_path);
|
|
70
68
|
}
|
|
71
69
|
|
|
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
|
-
// TODO(sualeh): we should assert that the path is ascending up to the path.
|
|
125
|
-
|
|
126
|
-
let ret = vec![file];
|
|
127
|
-
let ret = ret.into_iter().chain(path.into_iter()).collect::<Vec<_>>();
|
|
128
|
-
|
|
129
|
-
Ok(ret)
|
|
130
|
-
}
|
|
131
|
-
Err(e) => Err(napi::Error::new(
|
|
132
|
-
napi::Status::Unknown,
|
|
133
|
-
format!("Error in get_next_file_to_embed: {:?}", e),
|
|
134
|
-
)),
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
#[napi]
|
|
139
|
-
pub async fn get_hashes_for_files(
|
|
140
|
-
&self,
|
|
141
|
-
files: Vec<String>,
|
|
142
|
-
) -> Result<Vec<String>, napi::Error> {
|
|
143
|
-
let hashes = self.tree.get_hashes_for_files(files).await;
|
|
144
|
-
|
|
145
|
-
match hashes {
|
|
146
|
-
Ok(hashes) => Ok(hashes),
|
|
147
|
-
Err(e) => Err(napi::Error::new(
|
|
148
|
-
napi::Status::Unknown,
|
|
149
|
-
format!("Error in get_hashes_for_files: {:?}", e),
|
|
150
|
-
)),
|
|
151
|
-
}
|
|
152
|
-
}
|
|
70
|
+
#[napi]
|
|
71
|
+
pub async fn get_subtree_hash(&self, path: String) -> Result<String, napi::Error> {
|
|
72
|
+
let hash = self.tree.get_subtree_hash(path).await;
|
|
73
|
+
|
|
74
|
+
match hash {
|
|
75
|
+
Ok(hash) => Ok(hash),
|
|
76
|
+
Err(e) => Err(napi::Error::new(
|
|
77
|
+
napi::Status::Unknown,
|
|
78
|
+
format!("Error in get_subtree_hash: {:?}", e),
|
|
79
|
+
)),
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
#[napi]
|
|
84
|
+
pub async fn get_num_embeddable_files(&self) -> Result<i32, napi::Error> {
|
|
85
|
+
let num = self.tree.get_num_embeddable_files().await;
|
|
86
|
+
|
|
87
|
+
match num {
|
|
88
|
+
Ok(num) => Ok(num),
|
|
89
|
+
Err(e) => Err(napi::Error::new(
|
|
90
|
+
napi::Status::Unknown,
|
|
91
|
+
format!("Error in get_num_embeddable_files: {:?}", e),
|
|
92
|
+
)),
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
#[napi]
|
|
97
|
+
pub async fn get_all_files(&self) -> Result<Vec<String>, napi::Error> {
|
|
98
|
+
let files = self.tree.get_all_files().await;
|
|
99
|
+
|
|
100
|
+
match files {
|
|
101
|
+
Ok(files) => Ok(files),
|
|
102
|
+
Err(e) => Err(napi::Error::new(
|
|
103
|
+
napi::Status::Unknown,
|
|
104
|
+
format!("Error in get_all_files: {:?}", e),
|
|
105
|
+
)),
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#[napi]
|
|
110
|
+
pub async fn get_hashes_for_files(&self, files: Vec<String>) -> Result<Vec<String>, napi::Error> {
|
|
111
|
+
let hashes = self.tree.get_hashes_for_files(files).await;
|
|
112
|
+
|
|
113
|
+
match hashes {
|
|
114
|
+
Ok(hashes) => Ok(hashes),
|
|
115
|
+
Err(e) => Err(napi::Error::new(
|
|
116
|
+
napi::Status::Unknown,
|
|
117
|
+
format!("Error in get_hashes_for_files: {:?}", e),
|
|
118
|
+
)),
|
|
119
|
+
}
|
|
120
|
+
}
|
|
153
121
|
|
|
154
122
|
// #[napi]
|
|
155
123
|
pub fn update_root_directory(&mut self, root_directory: String) {
|
|
@@ -9,9 +9,7 @@ use tonic::async_trait;
|
|
|
9
9
|
|
|
10
10
|
#[async_trait]
|
|
11
11
|
impl LocalConstruction for MerkleTree {
|
|
12
|
-
async fn new(
|
|
13
|
-
root_directory: Option<String>,
|
|
14
|
-
) -> Result<MerkleTree, anyhow::Error> {
|
|
12
|
+
async fn new(root_directory: Option<String>) -> Result<MerkleTree, anyhow::Error> {
|
|
15
13
|
if let Some(root_directory) = root_directory {
|
|
16
14
|
let n = MerkleTree::construct_merkle_tree(root_directory).await;
|
|
17
15
|
return n;
|
|
@@ -27,9 +25,7 @@ impl LocalConstruction for MerkleTree {
|
|
|
27
25
|
/// 2. compute hash for each file
|
|
28
26
|
/// 3. construct merkle tree
|
|
29
27
|
/// 4. return merkle tree
|
|
30
|
-
async fn construct_merkle_tree(
|
|
31
|
-
root_directory: String,
|
|
32
|
-
) -> Result<MerkleTree, anyhow::Error> {
|
|
28
|
+
async fn construct_merkle_tree(root_directory: String) -> Result<MerkleTree, anyhow::Error> {
|
|
33
29
|
let path = PathBuf::from(root_directory.clone());
|
|
34
30
|
if !path.exists() {
|
|
35
31
|
// FIXME: we should report this via a good logger.
|
|
@@ -41,7 +37,6 @@ impl LocalConstruction for MerkleTree {
|
|
|
41
37
|
root: root_node,
|
|
42
38
|
files: HashMap::new(),
|
|
43
39
|
root_path: root_directory,
|
|
44
|
-
cursor: None,
|
|
45
40
|
};
|
|
46
41
|
|
|
47
42
|
// we now iterate over all the nodes and add them to the hashmap
|
|
@@ -54,8 +49,8 @@ impl LocalConstruction for MerkleTree {
|
|
|
54
49
|
let node_reader = node.read().await;
|
|
55
50
|
match &node_reader.node_type {
|
|
56
51
|
NodeType::Branch(n) => {
|
|
57
|
-
|
|
58
|
-
|
|
52
|
+
let children = &n.1;
|
|
53
|
+
files.insert(n.0.clone(), File { node: node.clone() });
|
|
59
54
|
for child in children {
|
|
60
55
|
add_nodes_to_hashmap(child, files).await;
|
|
61
56
|
}
|
|
@@ -107,9 +102,9 @@ impl LocalConstruction for MerkleTree {
|
|
|
107
102
|
// File does not exist in the tree, let's add it.
|
|
108
103
|
let e = self.attach_new_node_to_tree(file_path.clone()).await;
|
|
109
104
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
105
|
+
if e.is_err() {
|
|
106
|
+
return Err(anyhow::anyhow!("Could not attach new node to tree!"));
|
|
107
|
+
}
|
|
113
108
|
}
|
|
114
109
|
|
|
115
110
|
Ok(())
|
|
@@ -144,17 +139,18 @@ impl LocalConstruction for MerkleTree {
|
|
|
144
139
|
let parent_node = parent_node.unwrap();
|
|
145
140
|
let mut mut_parent = parent_node.write().await;
|
|
146
141
|
|
|
147
|
-
// BUG(sualeh): need to actually drop everything that is a child here.
|
|
148
|
-
// idea: enumerate all nodes that are children of this through a starts with query on the hashtable.
|
|
149
|
-
// then drop all of them.
|
|
150
|
-
// in opposite order of length.
|
|
151
142
|
|
|
152
|
-
|
|
143
|
+
// BUG(sualeh): need to actually drop everything that is a child here.
|
|
144
|
+
// idea: enumerate all nodes that are children of this through a starts with query on the hashtable.
|
|
145
|
+
// then drop all of them.
|
|
146
|
+
// in opposite order of length.
|
|
147
|
+
|
|
148
|
+
// then remove the node from the parent and you are done
|
|
153
149
|
|
|
154
150
|
// Remove the child from the parent node
|
|
155
151
|
match mut_parent.node_type {
|
|
156
152
|
NodeType::Branch(ref mut node) => {
|
|
157
|
-
|
|
153
|
+
let children = &mut node.1;
|
|
158
154
|
let mut found = false;
|
|
159
155
|
let mut index = 0;
|
|
160
156
|
|
package/src/merkle_tree/mod.rs
CHANGED
|
@@ -13,7 +13,6 @@ pub struct MerkleTree {
|
|
|
13
13
|
root_path: String,
|
|
14
14
|
root: MerkleNodePtr,
|
|
15
15
|
files: HashMap<String, File>,
|
|
16
|
-
cursor: Option<MerkleNodePtr>,
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
#[derive(Debug)]
|
|
@@ -51,19 +50,12 @@ fn get_id() -> i32 {
|
|
|
51
50
|
|
|
52
51
|
#[async_trait]
|
|
53
52
|
pub trait LocalConstruction {
|
|
54
|
-
async fn new(
|
|
55
|
-
|
|
56
|
-
) -> Result<MerkleTree, anyhow::Error>;
|
|
57
|
-
|
|
58
|
-
async fn construct_merkle_tree(
|
|
59
|
-
root_directory: String,
|
|
60
|
-
) -> Result<MerkleTree, anyhow::Error>;
|
|
61
|
-
|
|
53
|
+
async fn new(root_directory: Option<String>) -> Result<MerkleTree, anyhow::Error>;
|
|
54
|
+
async fn construct_merkle_tree(root_directory: String) -> Result<MerkleTree, anyhow::Error>;
|
|
62
55
|
async fn update_file(
|
|
63
56
|
&mut self,
|
|
64
57
|
file_path: String,
|
|
65
58
|
) -> Result<(), anyhow::Error>;
|
|
66
|
-
|
|
67
59
|
async fn delete_file(
|
|
68
60
|
&mut self,
|
|
69
61
|
file_path: String,
|
|
@@ -89,183 +81,94 @@ impl MerkleTree {
|
|
|
89
81
|
root: Arc::new(RwLock::new(MerkleNode::empty_node(None, None))),
|
|
90
82
|
files: HashMap::new(),
|
|
91
83
|
root_path: "".to_string(),
|
|
92
|
-
cursor: None,
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
pub async fn get_subtree_hash(
|
|
97
|
-
&self,
|
|
98
|
-
path: String,
|
|
99
|
-
) -> Result<String, anyhow::Error> {
|
|
100
|
-
let path = PathBuf::from(path);
|
|
101
|
-
let node = match self.files.get(path.to_str().unwrap()) {
|
|
102
|
-
Some(file) => file.node.clone(),
|
|
103
|
-
None => {
|
|
104
|
-
return Err(anyhow::anyhow!("Could not find file in tree!"));
|
|
105
|
-
}
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
let node_reader = node.read().await;
|
|
109
|
-
let node_hash = node_reader.hash.clone();
|
|
110
|
-
|
|
111
|
-
Ok(node_hash)
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
pub async fn get_num_embeddable_files(&self) -> Result<i32, anyhow::Error> {
|
|
115
|
-
let mut count = 0;
|
|
116
|
-
|
|
117
|
-
for (_, file) in &self.files {
|
|
118
|
-
let file_reader = file.node.read().await;
|
|
119
|
-
match &file_reader.node_type {
|
|
120
|
-
NodeType::File(_) => {
|
|
121
|
-
count += 1;
|
|
122
|
-
}
|
|
123
|
-
NodeType::Branch(_) => {
|
|
124
|
-
continue;
|
|
125
|
-
}
|
|
126
|
-
NodeType::ErrorNode(_) => {
|
|
127
|
-
continue;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
Ok(count)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
pub async fn get_all_files(&self) -> Result<Vec<String>, anyhow::Error> {
|
|
136
|
-
let mut files = Vec::new();
|
|
137
|
-
|
|
138
|
-
for (file_name, file) in &self.files {
|
|
139
|
-
let file_reader = file.node.read().await;
|
|
140
|
-
match &file_reader.node_type {
|
|
141
|
-
NodeType::File(_) => {
|
|
142
|
-
files.push(file_name.clone());
|
|
143
|
-
}
|
|
144
|
-
NodeType::Branch(_) => {
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
NodeType::ErrorNode(_) => {
|
|
148
|
-
continue;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
Ok(files)
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
pub async fn get_hashes_for_files(
|
|
157
|
-
&self,
|
|
158
|
-
files: Vec<String>,
|
|
159
|
-
) -> Result<Vec<String>, anyhow::Error> {
|
|
160
|
-
let mut hashes = Vec::new();
|
|
161
|
-
|
|
162
|
-
for file_name in files {
|
|
163
|
-
let file = match self.files.get(&file_name) {
|
|
164
|
-
Some(file) => file,
|
|
165
|
-
None => {
|
|
166
|
-
return Err(anyhow::anyhow!("Could not find file in tree!"));
|
|
167
|
-
}
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
let file_reader = file.node.read().await;
|
|
171
|
-
match &file_reader.node_type {
|
|
172
|
-
NodeType::File(_) => {
|
|
173
|
-
hashes.push(file_reader.hash.clone());
|
|
174
|
-
}
|
|
175
|
-
NodeType::Branch(_) => {
|
|
176
|
-
continue;
|
|
177
|
-
}
|
|
178
|
-
NodeType::ErrorNode(_) => {
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
84
|
}
|
|
183
|
-
|
|
184
|
-
Ok(hashes)
|
|
185
85
|
}
|
|
186
86
|
|
|
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
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
87
|
+
pub async fn get_subtree_hash(&self, path: String) -> Result<String, anyhow::Error> {
|
|
88
|
+
let path = PathBuf::from(path);
|
|
89
|
+
let node = match self.files.get(path.to_str().unwrap()) {
|
|
90
|
+
Some(file) => file.node.clone(),
|
|
91
|
+
None => {
|
|
92
|
+
return Err(anyhow::anyhow!("Could not find file in tree!"));
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
let node_reader = node.read().await;
|
|
97
|
+
let node_hash = node_reader.hash.clone();
|
|
98
|
+
|
|
99
|
+
Ok(node_hash)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
pub async fn get_num_embeddable_files(&self) -> Result<i32, anyhow::Error> {
|
|
103
|
+
let mut count = 0;
|
|
104
|
+
|
|
105
|
+
for (_, file) in &self.files {
|
|
106
|
+
let file_reader = file.node.read().await;
|
|
107
|
+
match &file_reader.node_type {
|
|
108
|
+
NodeType::File(_) => {
|
|
109
|
+
count += 1;
|
|
110
|
+
}
|
|
111
|
+
NodeType::Branch(_) => {
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
NodeType::ErrorNode(_) => {
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
Ok(count)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
pub async fn get_all_files(&self) -> Result<Vec<String>, anyhow::Error> {
|
|
124
|
+
let mut files = Vec::new();
|
|
125
|
+
|
|
126
|
+
for (file_name, file) in &self.files {
|
|
127
|
+
let file_reader = file.node.read().await;
|
|
128
|
+
match &file_reader.node_type {
|
|
129
|
+
NodeType::File(_) => {
|
|
130
|
+
files.push(file_name.clone());
|
|
131
|
+
}
|
|
132
|
+
NodeType::Branch(_) => {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
NodeType::ErrorNode(_) => {
|
|
136
|
+
continue;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
Ok(files)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
pub async fn get_hashes_for_files(&self, files: Vec<String>) -> Result<Vec<String>, anyhow::Error> {
|
|
145
|
+
let mut hashes = Vec::new();
|
|
146
|
+
|
|
147
|
+
for file_name in files {
|
|
148
|
+
let file = match self.files.get(&file_name) {
|
|
149
|
+
Some(file) => file,
|
|
150
|
+
None => {
|
|
151
|
+
return Err(anyhow::anyhow!("Could not find file in tree!"));
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
let file_reader = file.node.read().await;
|
|
156
|
+
match &file_reader.node_type {
|
|
157
|
+
NodeType::File(_) => {
|
|
158
|
+
hashes.push(file_reader.hash.clone());
|
|
159
|
+
}
|
|
160
|
+
NodeType::Branch(_) => {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
163
|
+
NodeType::ErrorNode(_) => {
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
Ok(hashes)
|
|
170
|
+
}
|
|
259
171
|
|
|
260
|
-
// invariant: you must be a file!!
|
|
261
|
-
|
|
262
|
-
// everytime we get to a child list, we will add all the children to a fifo, and then pull from it as long as we need it.
|
|
263
|
-
|
|
264
|
-
// algorithm:
|
|
265
|
-
// 1.
|
|
266
|
-
|
|
267
|
-
Err(anyhow::anyhow!("Could not find file to embed!"))
|
|
268
|
-
}
|
|
269
172
|
|
|
270
173
|
/// creates a new node and attaches it to the current tree.
|
|
271
174
|
/// SPEC:
|
|
@@ -331,7 +234,7 @@ impl MerkleTree {
|
|
|
331
234
|
}
|
|
332
235
|
};
|
|
333
236
|
|
|
334
|
-
|
|
237
|
+
Ok(new_node)
|
|
335
238
|
}
|
|
336
239
|
|
|
337
240
|
/// Spec:
|
|
@@ -395,21 +298,17 @@ impl MerkleTree {
|
|
|
395
298
|
/// - attaches to the ancestor.
|
|
396
299
|
/// - adds to the filemap
|
|
397
300
|
/// - updates hashes of ancestor path.
|
|
398
|
-
async fn attach_new_node_to_tree(
|
|
399
|
-
&mut self,
|
|
400
|
-
file_path: String,
|
|
401
|
-
) -> Result<(), anyhow::Error> {
|
|
301
|
+
async fn attach_new_node_to_tree(&mut self, file_path: String) -> Result<(), anyhow::Error> {
|
|
402
302
|
let path = PathBuf::from(file_path.clone());
|
|
403
303
|
match self.create_new_node_and_attach_to_ancestors(path).await {
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
}
|
|
304
|
+
Ok(node_ptr) => {
|
|
305
|
+
self.add_subtree_to_filemap(node_ptr).await;
|
|
306
|
+
Ok(())
|
|
307
|
+
}
|
|
308
|
+
Err(e) => {
|
|
309
|
+
Err(anyhow::anyhow!("Could not create new node and attach to ancestors! {}", e.to_string()))
|
|
310
|
+
}
|
|
311
|
+
}
|
|
413
312
|
}
|
|
414
313
|
|
|
415
314
|
/// MUTATES MUTATES MUTATES
|
|
@@ -490,22 +389,20 @@ impl MerkleTree {
|
|
|
490
389
|
.create_new_node_and_attach_to_ancestors(file_path)
|
|
491
390
|
.await;
|
|
492
391
|
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
}
|
|
508
|
-
}
|
|
392
|
+
match node_ptr {
|
|
393
|
+
Ok(node_ptr) => {
|
|
394
|
+
|
|
395
|
+
self.files.insert(
|
|
396
|
+
file_string,
|
|
397
|
+
File {
|
|
398
|
+
node: node_ptr.clone(),
|
|
399
|
+
},
|
|
400
|
+
);
|
|
401
|
+
}
|
|
402
|
+
Err(e) => {
|
|
403
|
+
return Err(anyhow::anyhow!("Could not create new node and attach to ancestors! {}", e.to_string()));
|
|
404
|
+
}
|
|
405
|
+
}
|
|
509
406
|
|
|
510
407
|
Ok(())
|
|
511
408
|
}
|
|
@@ -635,15 +532,15 @@ impl MerkleNode {
|
|
|
635
532
|
));
|
|
636
533
|
}
|
|
637
534
|
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
535
|
+
// check if the directory fails the bad dir test.
|
|
536
|
+
let is_bad_dir = file_utils::is_in_bad_dir(file_or_directory);
|
|
537
|
+
if is_bad_dir.is_err() || is_bad_dir.unwrap_or(false) {
|
|
538
|
+
// println!("skipping directory: {}", path_str);
|
|
539
|
+
return Arc::new(RwLock::new(MerkleNode::empty_node(
|
|
540
|
+
Some(file_or_directory),
|
|
541
|
+
Some("Directory is in bad dir!".to_string()),
|
|
542
|
+
)));
|
|
543
|
+
}
|
|
647
544
|
|
|
648
545
|
let entries = fs::read_dir(file_or_directory);
|
|
649
546
|
match entries {
|
|
@@ -758,7 +655,7 @@ impl MerkleNode {
|
|
|
758
655
|
Ok(node) => node,
|
|
759
656
|
Err(e) => {
|
|
760
657
|
// println!("constructing error node. error: {}", e);
|
|
761
|
-
|
|
658
|
+
// println!("file_path: {:?}", file_path);
|
|
762
659
|
MerkleNode::empty_node(Some(file_path), Some(e))
|
|
763
660
|
}
|
|
764
661
|
};
|