| 594 | } |
| 595 | |
| 596 | fn remove_path(&self, path: &str, recursive: bool) -> Result<(), Self::Error> { |
| 597 | let mut files = self.files.borrow_mut(); |
| 598 | |
| 599 | // Check if path exists |
| 600 | if !files.contains_key(path) { |
| 601 | return Err(MemoryError::NotFound { |
| 602 | path: path.to_string(), |
| 603 | }); |
| 604 | } |
| 605 | |
| 606 | // Check if it's a directory with contents |
| 607 | let is_dir = files.get(path).map(|f| f.metadata.is_dir).unwrap_or(false); |
| 608 | if is_dir && !recursive { |
| 609 | // Check for children |
| 610 | let prefix = format!("{}/", path); |
| 611 | if files.keys().any(|p| p.starts_with(&prefix)) { |
| 612 | return Err(MemoryError::DirectoryNotEmpty { |
| 613 | path: path.to_string(), |
| 614 | }); |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | if recursive { |
| 619 | // Remove all paths that start with this path |
| 620 | let prefix = format!("{}/", path); |
| 621 | files.retain(|p, _| !p.starts_with(&prefix) && p != path); |
| 622 | } else { |
| 623 | files.remove(path); |
| 624 | } |
| 625 | |
| 626 | Ok(()) |
| 627 | } |
| 628 | |
| 629 | fn rename(&self, from: &str, to: &str) -> Result<(), Self::Error> { |
| 630 | let mut files = self.files.borrow_mut(); |