You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/chunks/tree.rs
+43-15Lines changed: 43 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,9 @@
1
1
use anyhow::{Context,Result};
2
-
use std::{fs, os::unix::fs::PermissionsExt, path::Path};
2
+
use std::{
3
+
fs,
4
+
os::unix::fs::PermissionsExt,
5
+
path::{Path,PathBuf},
6
+
};
3
7
use walkdir::WalkDir;
4
8
5
9
usecrate::chunks::{Chunk,HashKind, hash::hash};
@@ -9,32 +13,30 @@ use crate::chunks::{Chunk, HashKind, hash::hash};
9
13
/// # Errors
10
14
///
11
15
/// - Filesystem out of space (Very likely)
16
+
///
17
+
/// # Panics
18
+
///
19
+
/// - If `tree_path` points to a file, but the file somehow has no parent (eg: is root), then this will panic because there is no way that can be handled.
12
20
pubfnsave_tree(
13
21
tree_path:&Path,
14
22
chunk_store_path:&Path,
15
23
hash_kind:HashKind,
16
24
) -> Result<Vec<Chunk>>{
17
25
letmut chunks = Vec::new();
18
26
19
-
for entry inWalkDir::new(tree_path){
20
-
let file = entry?;
21
-
22
-
if !file.file_type().is_file(){
23
-
continue;
24
-
}
27
+
if !chunk_store_path.exists(){
28
+
fs::create_dir_all(chunk_store_path)?;
29
+
}
25
30
26
-
let path = file.path().strip_prefix(tree_path)?.to_path_buf();
27
-
let contents = fs::read(file.path())?;
31
+
if tree_path.is_file(){
32
+
let path:PathBuf = tree_path.file_name().unwrap().into();
33
+
let contents = fs::read(tree_path)?;
28
34
let size = (contents.len()asu64) / 1024;
29
35
let hash = hash(hash_kind,&contents);
30
-
let mode = file.metadata()?.permissions().mode()&0o777;
31
-
32
-
if !chunk_store_path.exists(){
33
-
fs::create_dir_all(chunk_store_path)?;
34
-
}
36
+
let mode = fs::metadata(tree_path)?.permissions().mode()&0o777;
35
37
36
38
let chunk_path = &chunk_store_path.join(get_chunk_filename(&hash, mode));
37
-
if fs::hard_link(file.path(), chunk_path).is_err(){
39
+
if fs::hard_link(tree_path, chunk_path).is_err(){
38
40
fs::write(chunk_path, contents)?;
39
41
}
40
42
@@ -44,6 +46,32 @@ pub fn save_tree(
44
46
size,
45
47
permissions: mode,
46
48
});
49
+
}else{
50
+
for entry inWalkDir::new(tree_path){
51
+
let file = entry?;
52
+
53
+
if !file.file_type().is_file(){
54
+
continue;
55
+
}
56
+
57
+
let path = file.path().strip_prefix(tree_path)?.to_path_buf();
58
+
let contents = fs::read(file.path())?;
59
+
let size = (contents.len()asu64) / 1024;
60
+
let hash = hash(hash_kind,&contents);
61
+
let mode = file.metadata()?.permissions().mode()&0o777;
62
+
63
+
let chunk_path = &chunk_store_path.join(get_chunk_filename(&hash, mode));
64
+
if fs::hard_link(file.path(), chunk_path).is_err(){
0 commit comments