| 101 | } |
| 102 | |
| 103 | fn stash_entry_path(root: &std::path::Path, path: &str) -> Result<std::path::PathBuf, String> { |
| 104 | use std::path::Component; |
| 105 | let relative = std::path::Path::new(path); |
| 106 | if path.is_empty() |
| 107 | || relative |
| 108 | .components() |
| 109 | .any(|component| !matches!(component, Component::Normal(_))) |
| 110 | || relative.starts_with(".atomic") |
| 111 | || relative.starts_with(".atomic-sandbox") |
| 112 | { |
| 113 | return Err(format!("invalid stashed file path: {path}")); |
| 114 | } |
| 115 | // The leaf may be a symlink; only parents must be real directories. |
| 116 | let mut destination = root.to_path_buf(); |
| 117 | let mut components = relative.components().peekable(); |
| 118 | while let Some(component) = components.next() { |
| 119 | destination.push(component); |
| 120 | if components.peek().is_some() { |
| 121 | match std::fs::symlink_metadata(&destination) { |
| 122 | Ok(metadata) if metadata.file_type().is_symlink() => { |
| 123 | return Err(format!("refusing stashed file through symlink: {path}")); |
| 124 | } |
| 125 | Ok(_) => {} |
| 126 | Err(error) if error.kind() == std::io::ErrorKind::NotFound => {} |
| 127 | Err(error) => return Err(format!("cannot inspect stashed file {path}: {error}")), |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | Ok(destination) |
| 132 | } |
| 133 | |
| 134 | // A captured addition has no recorded version to materialize. Before removing |
| 135 | // it from the working copy, verify that the saved entry still matches it. |