(
builder: &mut tar::Builder<BufWriter<File>>,
source: &Path,
archive_prefix: &Path,
)
| 280 | |
| 281 | #[cfg(test)] |
| 282 | fn append_rootfs_tree_to_archive( |
| 283 | builder: &mut tar::Builder<BufWriter<File>>, |
| 284 | source: &Path, |
| 285 | archive_prefix: &Path, |
| 286 | ) -> Result<(), String> { |
| 287 | let mut entries = fs::read_dir(source) |
| 288 | .map_err(|e| format!("read {}: {e}", source.display()))? |
| 289 | .collect::<Result<Vec<_>, _>>() |
| 290 | .map_err(|e| format!("read {}: {e}", source.display()))?; |
| 291 | entries.sort_by_key(fs::DirEntry::file_name); |
| 292 | |
| 293 | for entry in entries { |
| 294 | let entry_name = entry.file_name(); |
| 295 | let source_path = entry.path(); |
| 296 | let archive_path = if archive_prefix.as_os_str().is_empty() { |
| 297 | entry_name.into() |
| 298 | } else { |
| 299 | archive_prefix.join(entry_name) |
| 300 | }; |
| 301 | let metadata = fs::symlink_metadata(&source_path) |
| 302 | .map_err(|e| format!("stat {}: {e}", source_path.display()))?; |
| 303 | let file_type = metadata.file_type(); |
| 304 | |
| 305 | if file_type.is_dir() { |
| 306 | builder |
| 307 | .append_dir(&archive_path, &source_path) |
| 308 | .map_err(|e| format!("append dir {}: {e}", source_path.display()))?; |
| 309 | append_rootfs_tree_to_archive(builder, &source_path, &archive_path)?; |
| 310 | continue; |
| 311 | } |
| 312 | |
| 313 | if file_type.is_file() { |
| 314 | let mut file = File::open(&source_path) |
| 315 | .map_err(|e| format!("open {}: {e}", source_path.display()))?; |
| 316 | builder |
| 317 | .append_file(&archive_path, &mut file) |
| 318 | .map_err(|e| format!("append file {}: {e}", source_path.display()))?; |
| 319 | continue; |
| 320 | } |
| 321 | |
| 322 | if file_type.is_symlink() { |
| 323 | append_symlink_to_archive(builder, &source_path, &archive_path, &metadata)?; |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | return Err(format!( |
| 328 | "unsupported rootfs entry type at {}", |
| 329 | source_path.display() |
| 330 | )); |
| 331 | } |
| 332 | |
| 333 | Ok(()) |
| 334 | } |
| 335 | |
| 336 | #[cfg(test)] |
| 337 | fn append_symlink_to_archive( |
no test coverage detected