Skip to content

Commit 29c3469

Browse files
committed
fix: don't copy celestia.
1 parent 19d43a4 commit 29c3469

File tree

5 files changed

+119
-20
lines changed

5 files changed

+119
-20
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ libp2p = { version = "0.55.0", features = ["tcp", "quic"] }
8484
chrono = "0.4.31"
8585
rand = "0.7.3"
8686
uuid = "1.10.0"
87+
glob = "0.3.2"
8788

8889
poem = { version = "=3.1.3", features = ["anyhow", "compression", "rustls"] }
8990
poem-openapi = { version = "=5.1.2", features = ["swagger-ui", "url"] }

checks/migrator/checks/sketchpad/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ mtma-node-test-types = { workspace = true }
2929
[dev-dependencies]
3030
tracing-test = { workspace = true }
3131
tracing = { workspace = true }
32+
tempfile = { workspace = true }
3233

3334
[lints]
3435
workspace = true

migration/util/node-types/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ uuid = { workspace = true }
2828
walkdir = { workspace = true }
2929
mtma-types = { workspace = true }
3030

31+
[dev-dependencies]
32+
tempfile = { workspace = true }
33+
tracing-test = { workspace = true }
34+
3135
[lints]
3236
workspace = true

migration/util/node-types/src/executor/movement_executor.rs

Lines changed: 110 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,70 @@ pub struct MovementNode {
3434
opt_executor: MovementOptExecutor,
3535
}
3636

37-
/// Copies a directory recursively.
38-
fn copy_dir_recursive(src: &Path, dst: &Path) -> Result<(), anyhow::Error> {
39-
40-
// make sure the dst directory exists
41-
fs::create_dir_all(dst).context(format!("failed to create debug directory: {}", dst.display()))?;
42-
43-
for entry in WalkDir::new(src) {
44-
let entry = entry?;
45-
let rel_path = entry.path().strip_prefix(src).unwrap();
46-
let dest_path = dst.join(rel_path);
47-
48-
if entry.file_type().is_dir() {
49-
fs::create_dir_all(&dest_path).context(format!("failed to create directory: {}", dest_path.display()))?;
50-
} else {
51-
fs::copy(entry.path(), &dest_path).context(format!("failed to copy file: {}", entry.path().display()))?;
52-
}
53-
}
54-
Ok(())
37+
/// Copies a directory recursively while ignoring specified paths
38+
fn copy_dir_recursive_with_ignore(src: &Path, ignore_paths: impl IntoIterator<Item = impl AsRef<Path>>, dst: &Path) -> Result<(), anyhow::Error> {
39+
let ignore_paths: Vec<_> = ignore_paths.into_iter().collect();
40+
let mut errors: Vec<Result<(), anyhow::Error>> = Vec::new();
41+
42+
for entry in WalkDir::new(src) {
43+
let entry = match entry {
44+
Ok(e) => e,
45+
Err(e) => {
46+
errors.push(Err(e.into()));
47+
continue;
48+
}
49+
};
50+
51+
debug!("Processing entry: {}", entry.path().display());
52+
53+
let path = entry.path();
54+
55+
// Skip if path matches any ignore pattern
56+
if ignore_paths.iter().any(|ignore| path.to_string_lossy().contains(ignore.as_ref().to_string_lossy().as_ref())) {
57+
debug!("skipping ignored path: {}", path.display());
58+
continue;
59+
}
60+
61+
// Make the path relative to src
62+
let rel_path = match path.strip_prefix(src) {
63+
Ok(p) => p,
64+
Err(e) => {
65+
errors.push(Err(e.into()));
66+
continue;
67+
}
68+
};
69+
70+
let dest_path = dst.join(rel_path);
71+
72+
if entry.file_type().is_dir() {
73+
match fs::create_dir_all(&dest_path) {
74+
Ok(_) => (),
75+
Err(e) => errors.push(Err(e.into())),
76+
}
77+
} else {
78+
if let Some(parent) = dest_path.parent() {
79+
match fs::create_dir_all(parent) {
80+
Ok(_) => (),
81+
Err(e) => errors.push(Err(e.into())),
82+
}
83+
}
84+
match fs::copy(path, &dest_path) {
85+
Ok(_) => (),
86+
Err(e) => errors.push(Err(e.into())),
87+
}
88+
}
89+
}
90+
91+
// Combine all results into one
92+
if !errors.is_empty() {
93+
let mut total_error_message = String::from("failed to copy directory with the following errors: ");
94+
for error in errors {
95+
total_error_message = total_error_message + &format!("{:?}\n", error);
96+
}
97+
return Err(anyhow::anyhow!(total_error_message));
98+
}
99+
100+
Ok(())
55101
}
56102

57103
/// Sets all permission in a directory recursively.
@@ -78,13 +124,20 @@ impl MovementNode {
78124

79125
// Copy the entire .movement directory recursively
80126
let movement_dir = dir.join(".movement");
81-
copy_dir_recursive(&movement_dir, &debug_dir).context("failed to copy movement dir")?;
127+
128+
// set the permissions on the movement dir to 755
129+
// Note: this would mess up celestia node permissions, but we don't care about that here.
130+
// We really only care about maptos db permissions.
131+
fs::set_permissions(&movement_dir, Permissions::from_mode(0o755)).context(format!("failed to set permissions on the movement directory {}", movement_dir.display()))?;
132+
133+
// don't copy anything from the celestia directory
134+
copy_dir_recursive_with_ignore(&movement_dir, [PathBuf::from("celestia")], &debug_dir).context("failed to copy movement dir")?;
82135

83136
// Set all permissions in the debug directory recursively
84137
// Note: this would mess up celestia node permissions, but we don't care about that here.
85138
// We really only care about maptos db permissions.
86139
// TODO: tighten the copying accordingly.
87-
set_permissions_recursive(&debug_dir, Permissions::from_mode(0o755)).context("failed to set permissions")?;
140+
set_permissions_recursive(&debug_dir, Permissions::from_mode(0o755)).context(format!("failed to set permissions on the debug directory {}", debug_dir.display()))?;
88141

89142
let movement_args = MovementArgs { movement_path: Some(debug_dir.display().to_string()) };
90143

@@ -419,3 +472,40 @@ impl<'a> Iterator for AccountAddressIterator<'a> {
419472
self.get_next_address()
420473
}
421474
}
475+
476+
#[cfg(test)]
477+
mod test {
478+
use super::*;
479+
use tempfile::TempDir;
480+
481+
#[test]
482+
#[tracing_test::traced_test]
483+
fn test_copy_dir_recursive_with_ignore() -> Result<(), anyhow::Error> {
484+
// put some file in a temp dir
485+
let temp_dir = TempDir::new()?;
486+
487+
// write a file that should be copied
488+
let file_path = temp_dir.path().join("maptos").join("test_file.txt");
489+
fs::create_dir_all(file_path.parent().context("failed to get parent directory for file that should be copied")?).context("failed to create directory")?;
490+
fs::write(file_path, "test").context("failed to write file that should be copied")?;
491+
492+
// write a file that should not be copied
493+
let file_path = temp_dir.path().join("celestia").join("test_file2.txt");
494+
fs::create_dir_all(file_path.parent().context("failed to get parent directory for file that should not be copied")?).context("failed to create directory")?;
495+
fs::write(file_path, "test").context("failed to write file that should not be copied")?;
496+
497+
// create the target temp dir
498+
let dst = TempDir::new()?;
499+
500+
// copy the file to a new dir, ignoring celestia directory
501+
copy_dir_recursive_with_ignore(&temp_dir.path(), [PathBuf::from("celestia")], &dst.path()).context("failed to copy directory")?;
502+
503+
// check that the file was copied
504+
assert!(dst.path().join("maptos").join("test_file.txt").exists());
505+
506+
// check that the celestia directory was not copied
507+
assert!(!dst.path().join("celestia").join("test_file2.txt").exists());
508+
509+
Ok(())
510+
}
511+
}

0 commit comments

Comments
 (0)