| use std::path::PathBuf; |
|
|
| |
| #[tauri::command] |
| pub fn fs_create_file(path: String) -> Result<(), String> { |
| let p = PathBuf::from(&path); |
| if p.exists() { |
| return Err(format!("already exists: {}", p.display())); |
| } |
| std::fs::write(&p, "").map_err(|e| { |
| log::debug!("fs_create_file({}) failed: {e}", p.display()); |
| e.to_string() |
| }) |
| } |
|
|
| |
| |
| |
| #[tauri::command] |
| pub fn fs_create_dir(path: String) -> Result<(), String> { |
| let p = PathBuf::from(&path); |
| if p.exists() { |
| return Err(format!("already exists: {}", p.display())); |
| } |
| std::fs::create_dir_all(&p).map_err(|e| { |
| log::debug!("fs_create_dir({}) failed: {e}", p.display()); |
| e.to_string() |
| }) |
| } |
|
|
| |
| #[tauri::command] |
| pub fn fs_rename(from: String, to: String) -> Result<(), String> { |
| let from_p = PathBuf::from(&from); |
| let to_p = PathBuf::from(&to); |
| if !from_p.exists() { |
| return Err(format!("not found: {}", from_p.display())); |
| } |
| if to_p.exists() { |
| return Err(format!("already exists: {}", to_p.display())); |
| } |
| std::fs::rename(&from_p, &to_p).map_err(|e| { |
| log::debug!( |
| "fs_rename({} -> {}) failed: {e}", |
| from_p.display(), |
| to_p.display() |
| ); |
| e.to_string() |
| }) |
| } |
|
|
| |
| |
| #[tauri::command] |
| pub fn fs_delete(path: String) -> Result<(), String> { |
| let p = PathBuf::from(&path); |
| let meta = std::fs::symlink_metadata(&p).map_err(|e| { |
| log::debug!("fs_delete stat({}) failed: {e}", p.display()); |
| e.to_string() |
| })?; |
|
|
| let result = if meta.is_dir() { |
| std::fs::remove_dir_all(&p) |
| } else { |
| std::fs::remove_file(&p) |
| }; |
|
|
| result.map_err(|e| { |
| log::warn!("fs_delete({}) failed: {e}", p.display()); |
| e.to_string() |
| }) |
| } |
|
|