| 129 | } |
| 130 | |
| 131 | pub fn clone_or_copy_sparse_file(source: &Path, dest: &Path) -> Result<(), String> { |
| 132 | if let Some(parent) = dest.parent() { |
| 133 | fs::create_dir_all(parent).map_err(|e| format!("create {}: {e}", parent.display()))?; |
| 134 | } |
| 135 | if dest.exists() { |
| 136 | fs::remove_file(dest).map_err(|e| format!("remove old file {}: {e}", dest.display()))?; |
| 137 | } |
| 138 | |
| 139 | let clone_error = match try_clone_file(source, dest) { |
| 140 | Ok(()) => return Ok(()), |
| 141 | Err(err) => { |
| 142 | let _ = fs::remove_file(dest); |
| 143 | err |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | copy_sparse_file(source, dest).map_err(|copy_error| { |
| 148 | format!( |
| 149 | "clone {} to {} failed ({clone_error}); sparse copy failed: {copy_error}", |
| 150 | source.display(), |
| 151 | dest.display() |
| 152 | ) |
| 153 | }) |
| 154 | } |
| 155 | |
| 156 | pub fn write_rootfs_image_file( |
| 157 | image_path: &Path, |