Guest User

grusty copy

a guest
Aug 8th, 2025
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 3.73 KB | None | 0 0
  1. fn copy_file(source_path: &Path, target_dir: &Path, _source_panel: &Rc<FilePanel>, target_panel: &Rc<FilePanel>) {
  2.         // Создаем окно для отображения ошибок - получаем его из target_panel
  3.         let parent_window = target_panel.widget()
  4.             .root()
  5.             .and_then(|root| root.downcast::<gtk::Window>().ok())
  6.             .or_else(|| {
  7.                 // Резервный способ получения родительского окна
  8.                 target_panel.widget()
  9.                     .ancestor(gtk::Window::static_type())
  10.                     .and_then(|w| w.downcast::<gtk::Window>().ok())
  11.             });
  12.  
  13.         if !source_path.exists() {
  14.             let message = format!("Файл или директория не существует:\n{}", source_path.display());
  15.             if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
  16.                 Self::show_error_dialog(&window, "Ошибка копирования", &message);
  17.             }
  18.             return;
  19.         }
  20.  
  21.         let file_name = source_path.file_name().unwrap();
  22.         let target_path = target_dir.join(file_name);
  23.  
  24.         if target_path.exists() {
  25.             let message = format!(
  26.                 "Файл или директория с таким именем уже существует в целевой директории:\n{}",
  27.                 target_path.display()
  28.             );
  29.             if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
  30.                 Self::show_error_dialog(&window, "Ошибка копирования", &message);
  31.             }
  32.             return;
  33.         }
  34.  
  35.         if source_path.is_dir() {
  36.             // Копирование директории рекурсивно
  37.             match Self::copy_directory_recursive(source_path, &target_path) {
  38.                 Ok(()) => {
  39.                     println!("Директория скопирована: {:?} -> {:?}", source_path, target_path);
  40.                     // Обновляем целевую панель
  41.                     target_panel.refresh();
  42.                 }
  43.                 Err(err) => {
  44.                     let message = format!(
  45.                         "Не удалось скопировать директорию:\n{}\n\nОшибка: {}",
  46.                         source_path.display(),
  47.                         err
  48.                     );
  49.                     if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
  50.                         Self::show_error_dialog(&window, "Ошибка копирования", &message);
  51.                     }
  52.                 }
  53.             }
  54.         } else {
  55.             // Копирование файла
  56.             match std::fs::copy(source_path, &target_path) {
  57.                 Ok(bytes_copied) => {
  58.                     println!("Скопировано {} байт: {:?} -> {:?}", bytes_copied, source_path, target_path);
  59.                     // Обновляем целевую панель
  60.                     target_panel.refresh();
  61.                 }
  62.                 Err(err) => {
  63.                     let message = format!(
  64.                         "Не удалось скопировать файл:\n{}\n\nОшибка: {}",
  65.                         source_path.display(),
  66.                         err
  67.                     );
  68.                     if let Some(window) = parent_window.and_then(|w| w.downcast::<ApplicationWindow>().ok()) {
  69.                         Self::show_error_dialog(&window, "Ошибка копирования", &message);
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.     }
Advertisement
Add Comment
Please, Sign In to add comment