@anysphere/file-service 0.0.0-e1f2f04d → 0.0.0-e36a46ab

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/src/lib.rs CHANGED
@@ -1,11 +1,16 @@
1
+ #![windows_subsystem = "windows"]
1
2
  #![deny(clippy::all)]
3
+ #![deny(unsafe_op_in_unsafe_fn)]
2
4
  pub mod file_utils;
3
- pub mod git_utils;
4
5
  pub mod merkle_tree;
5
6
 
6
- use std::vec;
7
+ use std::{vec, collections::HashSet};
7
8
 
9
+ use anyhow::Context;
8
10
  use merkle_tree::{LocalConstruction, MerkleTree};
11
+ use tracing::{info, Level};
12
+ use tracing_appender::rolling::{RollingFileAppender, Rotation};
13
+ use tracing_subscriber::fmt;
9
14
 
10
15
  #[macro_use]
11
16
  extern crate napi_derive;
@@ -13,25 +18,58 @@ extern crate napi_derive;
13
18
  #[napi]
14
19
  pub struct MerkleClient {
15
20
  tree: MerkleTree,
16
- root_directory: String,
21
+ absolute_root_directory: String,
22
+ _guard: tracing_appender::non_blocking::WorkerGuard,
23
+ }
24
+
25
+ pub fn init_logger() -> tracing_appender::non_blocking::WorkerGuard {
26
+ let file_appender =
27
+ RollingFileAppender::new(Rotation::NEVER, "./", "rust_log.txt");
28
+ let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
29
+ let subscriber = fmt::Subscriber::builder()
30
+ .with_max_level(Level::TRACE)
31
+ .with_writer(non_blocking)
32
+ .with_ansi(false)
33
+ .with_line_number(true)
34
+ .finish();
35
+
36
+ let _ = tracing::subscriber::set_global_default(subscriber);
37
+
38
+ _guard
17
39
  }
18
40
 
19
41
  #[napi]
20
42
  impl MerkleClient {
21
43
  #[napi(constructor)]
22
- pub fn new(root_directory: String) -> MerkleClient {
44
+ pub fn new(absolute_root_directory: String) -> MerkleClient {
45
+ let _guard = init_logger();
46
+
47
+ // let canonical_root_directory = std::path::Path::new(&absolute_root_directory);
48
+ // use dunce::canonicalize;
49
+ // let canonical_root_directory = match dunce::canonicalize(&canonical_root_directory) {
50
+ // Ok(path) => path.to_str().unwrap_or(&absolute_root_directory).to_string().to_lowercase(),
51
+ // Err(e) => {
52
+ // info!("Error in canonicalizing path: path: {:?}, error {:?}", canonical_root_directory, e);
53
+ // absolute_root_directory
54
+ // }
55
+ // };
56
+
23
57
  MerkleClient {
24
58
  tree: MerkleTree::empty_tree(),
25
- root_directory,
59
+ absolute_root_directory,
60
+ _guard,
26
61
  }
27
62
  }
28
63
 
29
64
  #[napi]
30
- pub async unsafe fn init(&mut self) -> Result<(), napi::Error> {
65
+ pub async unsafe fn init(&mut self, git_ignored_files: Vec<String>, is_git_repo: bool) -> Result<(), napi::Error> {
31
66
  // 1. compute the merkle tree
32
67
  // 2. update the backend
33
68
  // 3. sync with the remote
34
- self.compute_merkle_tree().await?;
69
+ info!("Merkle tree compute started!");
70
+ unsafe {
71
+ self.compute_merkle_tree(git_ignored_files, is_git_repo).await?;
72
+ }
35
73
 
36
74
  Ok(())
37
75
  }
@@ -40,12 +78,18 @@ impl MerkleClient {
40
78
  unimplemented!("Interrupt is not implemented yet");
41
79
  }
42
80
 
43
- // #[napi]
81
+ #[napi]
44
82
  pub async unsafe fn compute_merkle_tree(
45
83
  &mut self,
84
+ git_ignored_files: Vec<String>,
85
+ is_git_repo: bool
46
86
  ) -> Result<(), napi::Error> {
87
+ // make the git ignored files into a hash set
88
+ let git_ignored_set = HashSet::from_iter(git_ignored_files.into_iter());
89
+
47
90
  let t =
48
- MerkleTree::construct_merkle_tree(self.root_directory.clone()).await;
91
+ MerkleTree::construct_merkle_tree(self.absolute_root_directory.clone(), git_ignored_set, is_git_repo)
92
+ .await;
49
93
 
50
94
  match t {
51
95
  Ok(tree) => {
@@ -72,15 +116,42 @@ impl MerkleClient {
72
116
  #[napi]
73
117
  pub async fn get_subtree_hash(
74
118
  &self,
75
- path: String,
119
+ relative_path: String,
76
120
  ) -> Result<String, napi::Error> {
77
- let hash = self.tree.get_subtree_hash(path).await;
121
+ let relative_path_without_leading_slash = match relative_path
122
+ .strip_prefix('.')
123
+ {
124
+ Some(path) => path.strip_prefix(std::path::MAIN_SEPARATOR).unwrap_or(""),
125
+ None => relative_path.as_str(),
126
+ };
127
+
128
+ let absolute_path = if !relative_path_without_leading_slash.is_empty() {
129
+ std::path::Path::new(&self.absolute_root_directory)
130
+ .join(relative_path_without_leading_slash)
131
+ } else {
132
+ std::path::Path::new(&self.absolute_root_directory).to_path_buf()
133
+ };
134
+
135
+ let absolute_path_string = match absolute_path.to_str() {
136
+ Some(path) => path.to_string(),
137
+ None => {
138
+ return Err(napi::Error::new(
139
+ napi::Status::Unknown,
140
+ format!("some string error"),
141
+ ))
142
+ }
143
+ };
144
+
145
+ let hash = self
146
+ .tree
147
+ .get_subtree_hash(absolute_path_string.as_str())
148
+ .await;
78
149
 
79
150
  match hash {
80
151
  Ok(hash) => Ok(hash),
81
152
  Err(e) => Err(napi::Error::new(
82
153
  napi::Status::Unknown,
83
- format!("Error in get_subtree_hash: {:?}", e),
154
+ format!("Error in get_subtree_hash. \nRelative path: {:?}, \nAbsolute path: {:?}, \nRoot directory: {:?}\nError: {:?}", &relative_path, absolute_path, self.absolute_root_directory, e)
84
155
  )),
85
156
  }
86
157
  }
@@ -98,6 +169,28 @@ impl MerkleClient {
98
169
  }
99
170
  }
100
171
 
172
+ pub async fn get_num_embeddable_files_in_subtree(
173
+ &self,
174
+ relative_path: String,
175
+ ) -> Result<i32, napi::Error> {
176
+ let absolute_path = std::path::Path::new(&self.absolute_root_directory)
177
+ .join(relative_path)
178
+ .canonicalize()?;
179
+
180
+ let num = self
181
+ .tree
182
+ .get_num_embeddable_files_in_subtree(absolute_path)
183
+ .await;
184
+
185
+ match num {
186
+ Ok(num) => Ok(num),
187
+ Err(e) => Err(napi::Error::new(
188
+ napi::Status::Unknown,
189
+ format!("Error in get_num_embeddable_files_in_subtree: {:?}", e),
190
+ )),
191
+ }
192
+ }
193
+
101
194
  #[napi]
102
195
  pub async fn get_all_files(&self) -> Result<Vec<String>, napi::Error> {
103
196
  let files = self.tree.get_all_files().await;
@@ -111,6 +204,28 @@ impl MerkleClient {
111
204
  }
112
205
  }
113
206
 
207
+ #[napi]
208
+ pub async fn get_all_dir_files_to_embed(
209
+ &self,
210
+ absolute_file_path: String,
211
+ ) -> Result<Vec<String>, napi::Error> {
212
+ // let absolute_path = absolute_file_path.to_lowercase();
213
+ // let absolute_path_str = absolute_path.as_str();
214
+
215
+ let files = self
216
+ .tree
217
+ .get_all_dir_files_to_embed(absolute_file_path.as_str())
218
+ .await;
219
+
220
+ match files {
221
+ Ok(files) => Ok(files),
222
+ Err(e) => Err(napi::Error::new(
223
+ napi::Status::Unknown,
224
+ format!("Error in get_all_dir_files_to_embed: {:?}", e),
225
+ )),
226
+ }
227
+ }
228
+
114
229
  #[napi]
115
230
  pub async unsafe fn get_next_file_to_embed(
116
231
  &mut self,
@@ -125,7 +240,6 @@ impl MerkleClient {
125
240
 
126
241
  let ret = vec![file];
127
242
  let ret = ret.into_iter().chain(path.into_iter()).collect::<Vec<_>>();
128
-
129
243
  Ok(ret)
130
244
  }
131
245
  Err(e) => Err(napi::Error::new(
@@ -135,6 +249,25 @@ impl MerkleClient {
135
249
  }
136
250
  }
137
251
 
252
+ // FIXME(sualeh): get_spline
253
+ #[napi]
254
+ pub async fn get_spline(
255
+ &self,
256
+ absolute_file_path: String,
257
+ ) -> Result<Vec<String>, napi::Error> {
258
+ // let absolute_path = absolute_file_path.to_lowercase();
259
+ // let absolute_path_str = absolute_path.as_str();
260
+ let spline = self.tree.get_spline(absolute_file_path.as_str()).await;
261
+
262
+ match spline {
263
+ Ok(spline) => Ok(spline),
264
+ Err(e) => Err(napi::Error::new(
265
+ napi::Status::Unknown,
266
+ format!("Error in get_spline: {:?}", e),
267
+ )),
268
+ }
269
+ }
270
+
138
271
  #[napi]
139
272
  pub async fn get_hashes_for_files(
140
273
  &self,
@@ -151,8 +284,8 @@ impl MerkleClient {
151
284
  }
152
285
  }
153
286
 
154
- // #[napi]
287
+ #[napi]
155
288
  pub fn update_root_directory(&mut self, root_directory: String) {
156
- self.root_directory = root_directory;
289
+ self.absolute_root_directory = root_directory;
157
290
  }
158
291
  }
@@ -3,8 +3,8 @@ use crate::merkle_tree::{
3
3
  };
4
4
 
5
5
  use super::{LocalConstruction, MerkleTree};
6
- use std::path::PathBuf;
7
- use std::{collections::HashMap, path::Path, sync::Arc};
6
+ use std::collections::{BTreeMap, HashSet};
7
+ use std::path::{Path, PathBuf};
8
8
  use tonic::async_trait;
9
9
 
10
10
  #[async_trait]
@@ -12,8 +12,13 @@ impl LocalConstruction for MerkleTree {
12
12
  async fn new(
13
13
  root_directory: Option<String>,
14
14
  ) -> Result<MerkleTree, anyhow::Error> {
15
+ let git_ignored_files = HashSet::<String>::new();
15
16
  if let Some(root_directory) = root_directory {
16
- let n = MerkleTree::construct_merkle_tree(root_directory).await;
17
+ let n = MerkleTree::construct_merkle_tree(
18
+ root_directory,
19
+ git_ignored_files,
20
+ false
21
+ ).await;
17
22
  return n;
18
23
  }
19
24
 
@@ -28,32 +33,54 @@ impl LocalConstruction for MerkleTree {
28
33
  /// 3. construct merkle tree
29
34
  /// 4. return merkle tree
30
35
  async fn construct_merkle_tree(
31
- root_directory: String,
36
+ absolute_path_to_root_directory: String,
37
+ git_ignored_files_and_dirs: HashSet<String>,
38
+ is_git_repo: bool
32
39
  ) -> Result<MerkleTree, anyhow::Error> {
33
- let path = PathBuf::from(root_directory.clone());
40
+ let path = PathBuf::from(absolute_path_to_root_directory.clone());
34
41
  if !path.exists() {
35
42
  // FIXME: we should report this via a good logger.
36
43
  panic!("Root directory does not exist!");
37
44
  }
38
45
 
39
- let root_node = MerkleNode::new(path, None).await;
46
+ // 1. get all the gitignored files
47
+ // let git_ignored_files_and_dirs =
48
+ // match git_utils::list_ignored_files_and_directories(
49
+ // absolute_path_to_root_directory.as_str(),
50
+ // true,
51
+ // ) {
52
+ // Ok(git_ignored) => git_ignored,
53
+ // Err(_e) => HashSet::new(),
54
+ // };
55
+
56
+ let root_node = MerkleNode::new(
57
+ path,
58
+ None,
59
+ &git_ignored_files_and_dirs,
60
+ absolute_path_to_root_directory.as_str(),
61
+ is_git_repo
62
+ )
63
+ .await;
40
64
  let mut mt = MerkleTree {
41
65
  root: root_node,
42
- files: HashMap::new(),
43
- root_path: root_directory,
66
+ files: BTreeMap::new(),
67
+ root_path: absolute_path_to_root_directory,
44
68
  cursor: None,
69
+ git_ignored_files_and_dirs: git_ignored_files_and_dirs,
70
+ is_git_repo
45
71
  };
46
72
 
47
73
  // we now iterate over all the nodes and add them to the hashmap
48
74
  // TODO(later): i can make this parallel.
49
75
  fn add_nodes_to_hashmap<'a>(
50
76
  node: &'a MerkleNodePtr,
51
- files: &'a mut HashMap<String, File>,
77
+ files: &'a mut BTreeMap<String, File>,
52
78
  ) -> PinnedFuture<'a, ()> {
53
79
  Box::pin(async move {
54
80
  let node_reader = node.read().await;
55
81
  match &node_reader.node_type {
56
82
  NodeType::Branch(n) => {
83
+ tracing::info!("Branch: {:?}", n.0);
57
84
  let children = &n.1;
58
85
  files.insert(n.0.clone(), File { node: node.clone() });
59
86
  for child in children {
@@ -62,6 +89,13 @@ impl LocalConstruction for MerkleTree {
62
89
  }
63
90
  NodeType::File(file_name) => {
64
91
  let f = File { node: node.clone() };
92
+
93
+ // i dont reallly like this :(((
94
+ // let canonical_file_name = match dunce::canonicalize(file_name) {
95
+ // Ok(path) => path.to_str().unwrap_or(file_name).to_string(),
96
+ // Err(_) => file_name.clone(),
97
+ // };
98
+
65
99
  files.insert(file_name.clone(), f);
66
100
  }
67
101
  NodeType::ErrorNode(_) => {
@@ -73,6 +107,9 @@ impl LocalConstruction for MerkleTree {
73
107
 
74
108
  add_nodes_to_hashmap(&mt.root, &mut mt.files).await;
75
109
 
110
+ tracing::info!("Merkle tree compute finished!");
111
+ tracing::info!("Merkle tree: {}", mt);
112
+
76
113
  Ok(mt)
77
114
  }
78
115