|
3 | 3 | use crate::core_types::{Action, CreateStatus}; |
4 | 4 | use crate::errors::ProcessError; |
5 | 5 | use std::fs; |
| 6 | +use std::io; // Import io for ErrorKind |
6 | 7 | use std::path::Path; |
7 | 8 |
|
8 | 9 | /// Creates or overwrites a file with the provided content. |
@@ -63,21 +64,51 @@ pub(crate) fn process_create( |
63 | 64 | /// Also checks if the parent path itself is unexpectedly a file. |
64 | 65 | fn ensure_parent_directory(target_path: &Path, resolved_base: &Path) -> Result<(), ProcessError> { |
65 | 66 | if let Some(parent_dir) = target_path.parent() { |
66 | | - if parent_dir == resolved_base || parent_dir.exists() { |
67 | | - // If parent exists, ensure it's a directory |
68 | | - if !parent_dir.is_dir() { |
69 | | - return Err(ProcessError::ParentIsNotDirectory { |
70 | | - path: target_path.to_path_buf(), |
71 | | - parent_path: parent_dir.to_path_buf(), |
72 | | - }); |
| 67 | + // Avoid checking the base directory itself if it's the parent |
| 68 | + if parent_dir == resolved_base || parent_dir.as_os_str().is_empty() { |
| 69 | + return Ok(()); // Base directory is guaranteed to exist and be a dir, or path is in root |
| 70 | + } |
| 71 | + |
| 72 | + match fs::metadata(parent_dir) { |
| 73 | + Ok(metadata) => { |
| 74 | + // Parent exists, check if it's a directory |
| 75 | + if !metadata.is_dir() { |
| 76 | + return Err(ProcessError::ParentIsNotDirectory { |
| 77 | + path: target_path.to_path_buf(), |
| 78 | + parent_path: parent_dir.to_path_buf(), |
| 79 | + }); |
| 80 | + } |
| 81 | + // Parent exists and is a directory, all good. |
| 82 | + } |
| 83 | + Err(ref e) if e.kind() == io::ErrorKind::NotFound => { |
| 84 | + // Parent does not exist, try to create it |
| 85 | + let relative_parent_dir = |
| 86 | + parent_dir.strip_prefix(resolved_base).unwrap_or(parent_dir); |
| 87 | + println!(" Creating directory: {}", relative_parent_dir.display()); |
| 88 | + |
| 89 | + if let Err(create_err) = fs::create_dir_all(parent_dir) { |
| 90 | + // Check if the error is specifically "Not a directory" |
| 91 | + // This often indicates an intermediate path component was a file. |
| 92 | + if create_err.kind() == io::ErrorKind::NotADirectory { |
| 93 | + // Map this specific IO error to our more descriptive error |
| 94 | + return Err(ProcessError::ParentIsNotDirectory { |
| 95 | + path: target_path.to_path_buf(), |
| 96 | + // Report the parent directory we *failed* to create |
| 97 | + parent_path: parent_dir.to_path_buf(), |
| 98 | + }); |
| 99 | + } else { |
| 100 | + // Other I/O error during creation |
| 101 | + return Err(ProcessError::Io { source: create_err }); |
| 102 | + } |
| 103 | + } |
| 104 | + // Creation successful |
| 105 | + } |
| 106 | + Err(e) => { |
| 107 | + // Other error getting metadata (permissions?) |
| 108 | + return Err(ProcessError::Io { source: e }); |
73 | 109 | } |
74 | | - } else { |
75 | | - // Parent does not exist, create it |
76 | | - let relative_parent_dir = parent_dir.strip_prefix(resolved_base).unwrap_or(parent_dir); |
77 | | - println!(" Creating directory: {}", relative_parent_dir.display()); |
78 | | - fs::create_dir_all(parent_dir).map_err(|e| ProcessError::Io { source: e })?; |
79 | 110 | } |
80 | 111 | } |
81 | | - // If no parent (e.g., root directory), assume it's okay. |
| 112 | + // If no parent (e.g., file directly in base), assume it's okay. |
82 | 113 | Ok(()) |
83 | 114 | } |
0 commit comments