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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::{fs, path::Path};
pub struct RepositoryFilter;
impl RepositoryFilter {
/// Filter repository content based on the configuration filters.
/// This function copies only the specified paths from source to destination.
pub fn filter_repository_content(
&self,
source_path: &str,
destination_path: &str,
filters: &[String],
) -> Result<(), Box<dyn std::error::Error>> {
// Create destination directory if it doesn't exist.
fs::create_dir_all(destination_path)?;
// Process each filter path.
for filter_path in filters {
self.copy_filtered_content(source_path, destination_path, filter_path)?;
}
Ok(())
}
/// Copy specific content from source to destination based on a filter path.
fn copy_filtered_content(
&self,
source_path: &str,
destination_path: &str,
filter_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let source_dir = Path::new(source_path);
let dest_dir = Path::new(destination_path);
// Sanitize the filter path to prevent directory traversal.
let normalized_filter = Self::normalize_path(filter_path);
let source_filtered_path = source_dir.join(&normalized_filter);
if source_filtered_path.exists() {
if source_filtered_path.is_file() {
// Copy a single file, preserving only the filename.
let file_name = source_filtered_path
.file_name()
.ok_or("Invalid file path")?;
let dest_file = dest_dir.join(file_name);
fs::copy(&source_filtered_path, &dest_file)?;
} else if source_filtered_path.is_dir() {
// Copy the entire directory recursively, preserving the path structure.
let dest_subdir = dest_dir.join(&normalized_filter);
self.copy_dir_all(&source_filtered_path, &dest_subdir)?;
}
} else {
// The filter path doesn't exist in the source; skip it.
eprintln!("Warning: Filter path '{filter_path}' does not exist in source repository");
}
Ok(())
}
/// Recursively copy a directory and its contents.
/// Skips symlinks and other non-regular file types.
#[allow(clippy::only_used_in_recursion)]
fn copy_dir_all(&self, src: &Path, dst: &Path) -> Result<(), Box<dyn std::error::Error>> {
// Ensure the destination directory exists.
fs::create_dir_all(dst)?;
// Read and process each entry in the source directory.
for entry in fs::read_dir(src)? {
let entry = entry?;
let metadata = entry.metadata()?;
let file_type = metadata.file_type();
let entry_path = entry.path();
let dest_entry_path = dst.join(entry.file_name());
if file_type.is_dir() {
// Recurse into subdirectory.
self.copy_dir_all(&entry_path, &dest_entry_path)?;
} else if file_type.is_file() {
// Copy the file.
fs::copy(&entry_path, &dest_entry_path)?;
}
// Intentionally skip symlinks, sockets, etc.
}
Ok(())
}
/// Normalize a path to prevent directory traversal attacks (e.g., removes "../" sequences).
/// This is a basic implementation; production code may require more robust validation.
fn normalize_path(path: &str) -> String {
// Split the path into components.
let parts: Vec<&str> = path.split('/').collect();
// Build a stack of valid path components.
let mut stack: Vec<&str> = Vec::new();
for &part in &parts {
match part {
"" | "." => {
// Skip empty parts and current directory markers.
}
".." => {
// Go up one level if possible.
stack.pop();
}
_ => {
// Add normal directory or file name.
stack.push(part);
}
}
}
// Join the stack into a path string.
let mut normalized = stack.join("/");
// Remove leading slash to avoid absolute paths.
if normalized.starts_with('/') {
normalized = normalized[1..].to_string();
}
normalized
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::TempDir;
#[test]
fn test_normalize_path() {
// Test basic path normalization.
assert_eq!(
RepositoryFilter::normalize_path("src/main.rs"),
"src/main.rs"
);
assert_eq!(
RepositoryFilter::normalize_path("./src/main.rs"),
"src/main.rs"
);
assert_eq!(
RepositoryFilter::normalize_path("src/./main.rs"),
"src/main.rs"
);
// Test path traversal removal.
assert_eq!(
RepositoryFilter::normalize_path("../src/main.rs"),
"src/main.rs"
);
assert_eq!(
RepositoryFilter::normalize_path("src/../lib/utils.rs"),
"lib/utils.rs"
);
}
#[test]
fn test_filter_repository_content() {
// Create a temporary source directory structure.
let src_dir = TempDir::new().expect("Failed to create temporary source directory");
let src_path = src_dir.path();
// Create test files and directories.
fs::create_dir_all(src_path.join("src")).expect("Failed to create src directory");
fs::create_dir_all(src_path.join("docs")).expect("Failed to create docs directory");
fs::write(src_path.join("src").join("main.rs"), "fn main() {}")
.expect("Failed to write main.rs");
fs::write(src_path.join("docs").join("README.md"), "# Docs")
.expect("Failed to write README.md");
fs::write(src_path.join("LICENSE"), "MIT License").expect("Failed to write LICENSE");
// Create a temporary destination directory.
let dest_dir = TempDir::new().expect("Failed to create temporary destination directory");
let dest_path = dest_dir.path();
let filter = RepositoryFilter;
let filters = vec!["src/".to_string()];
// Apply the filter.
filter
.filter_repository_content(
src_path.to_str().expect("Source path is not valid UTF-8"),
dest_path
.to_str()
.expect("Destination path is not valid UTF-8"),
&filters,
)
.expect("Failed to filter repository content");
// Verify that only the filtered content was copied.
assert!(dest_path.join("src").exists());
assert!(dest_path.join("src").join("main.rs").exists());
assert!(!dest_path.join("docs").exists());
assert!(!dest_path.join("LICENSE").exists());
}
}