Open a file for writing, creating it if necessary.
(&self, path: &str, inode: Inode)
| 311 | |
| 312 | /// Open a file for writing, creating it if necessary. |
| 313 | fn write_file(&self, path: &str, inode: Inode) -> Result<Self::Writer, Self::Error> { |
| 314 | let abs_path = self.resolve_path(path)?; |
| 315 | |
| 316 | // Create parent directories |
| 317 | if let Some(parent) = abs_path.parent() { |
| 318 | if !parent.exists() { |
| 319 | fs::create_dir_all(parent)?; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // A kind transition from symlink to regular must unlink first; |
| 324 | // opening with truncate would otherwise overwrite the link target. |
| 325 | if fs::symlink_metadata(&abs_path) |
| 326 | .map(|metadata| metadata.file_type().is_symlink()) |
| 327 | .unwrap_or(false) |
| 328 | { |
| 329 | fs::remove_file(&abs_path)?; |
| 330 | } |
| 331 | |
| 332 | let file = OpenOptions::new() |
| 333 | .write(true) |
| 334 | .create(true) |
| 335 | .truncate(true) |
| 336 | .open(&abs_path)?; |
| 337 | |
| 338 | Ok(FileWriter::new(file, abs_path, inode)) |
| 339 | } |
| 340 | } |