Files
termscp/src/ui/activities/filetransfer/fswatcher.rs
T
Christian Visintin f6d35f1eea refactor: FileTransferActivity pane-agnostic dispatch (#386)
Comprehensive design for incremental refactoring of the 13k-line
FileTransferActivity god-struct using a unified Pane abstraction.
Detailed step-by-step plan covering 6 phases: split monoliths,
error handling, Pane struct, action dedup, session split, view reorg.
Extract 26 popup components from the monolithic 1,868-line popups.rs
into 20 individual files under popups/. Each file contains one or two
related components with their own imports. The popups.rs module file
now contains only module declarations and re-exports.
Replace 8 panic!() calls with error!() logging and early returns/fallthrough.
These panics documented invariants (e.g. "this tab can't do X") but would crash
the app if somehow triggered. Error logging is safer and more resilient.
Replace raw FileExplorer fields in Browser with Pane structs that bundle
the explorer and connected state. Move host_bridge_connected and
remote_connected from FileTransferActivity into the panes. Add navigation
API (fs_pane, opposite_pane, is_find_tab) for future unification tasks.
Rename private get_selected_file to get_selected_file_by_id and add three
new unified methods (get_selected_entries, get_selected_file, is_selected_one)
that dispatch based on self.browser.tab(). Old per-tab methods are kept for
now until their callers are migrated in subsequent tasks.
Collapse _local_/_remote_ action method pairs (mkdir, delete, symlink,
chmod, rename, copy) into unified methods that branch internally on
is_local_tab(). This halves the number of action methods and simplifies
the update.rs dispatch logic. Also unifies ShowFileInfoPopup and
ShowChmodPopup dispatching to use get_selected_entries().
Move `host_bridge` and `client` filesystem fields from FileTransferActivity
into the Pane struct, enabling tab-agnostic dispatch via `fs_pane()`/
`fs_pane_mut()`. This eliminates most `is_local_tab()` branching across
15+ action files.
Key changes:
- Add `fs: Box<dyn HostBridge>` to Pane, remove from FileTransferActivity
- Replace per-side method pairs with unified pane-dispatched methods
- Unify navigation (changedir, reload, scan, file_exists, has_file_changed)
- Replace 147-line popup if/else chain with data-driven priority table
- Replace assert!/panic!/unreachable! with proper error handling
- Fix typo "filetransfer_activiy" across ~29 files
- Add unit tests for Pane

Net result: -473 lines, single code path for most file operations.
2026-04-19 01:54:46 +05:30

144 lines
4.6 KiB
Rust

use std::path::Path;
use super::{FileTransferActivity, LogLevel, TransferPayload};
use crate::system::watcher::FsChange;
impl FileTransferActivity {
/// poll file watcher
pub(super) fn poll_watcher(&mut self) {
if self.fswatcher.is_none() {
return;
}
let watcher = self.fswatcher.as_mut().unwrap();
match watcher.poll() {
Ok(None) => {}
Ok(Some(FsChange::Move(mov))) => {
debug!(
"fs watcher reported a `Move` from {} to {}",
mov.source().display(),
mov.destination().display()
);
self.move_watched_file(mov.source(), mov.destination());
}
Ok(Some(FsChange::Remove(remove))) => {
debug!(
"fs watcher reported a `Remove` of {}",
remove.path().display()
);
self.remove_watched_file(remove.path());
}
Ok(Some(FsChange::Update(update))) => {
debug!(
"fs watcher reported an `Update` from {} to {}",
update.host_bridge().display(),
update.remote().display()
);
self.upload_watched_file(update.host_bridge(), update.remote());
}
Err(err) => {
self.log(
LogLevel::Error,
format!("error while polling file watcher: {err}"),
);
}
}
}
fn move_watched_file(&mut self, source: &Path, destination: &Path) {
// stat remote file
trace!(
"renaming watched file {} to {}",
source.display(),
destination.display()
);
// stat fs entry
let origin = match self.browser.remote_pane_mut().fs.stat(source) {
Ok(f) => f,
Err(err) => {
self.log(
LogLevel::Error,
format!(
"failed to stat file to rename {}: {}",
source.display(),
err
),
);
return;
}
};
// rename using action
self.remote_rename_file(&origin, destination)
}
fn remove_watched_file(&mut self, file: &Path) {
// stat the file first to use HostBridge::remove
let entry = match self.browser.remote_pane_mut().fs.stat(file) {
Ok(e) => e,
Err(err) => {
self.log(
LogLevel::Error,
format!("failed to stat watched file {}: {}", file.display(), err),
);
return;
}
};
match self.browser.remote_pane_mut().fs.remove(&entry) {
Ok(()) => {
self.log(
LogLevel::Info,
format!("removed watched file at {}", file.display()),
);
}
Err(err) => {
self.log(
LogLevel::Error,
format!("failed to remove watched file {}: {}", file.display(), err),
);
}
}
}
fn upload_watched_file(&mut self, host: &Path, remote: &Path) {
// stat host file
let entry = match self.browser.local_pane_mut().fs.stat(host) {
Ok(e) => e,
Err(err) => {
self.log(
LogLevel::Error,
format!(
"failed to sync file {} with remote (stat failed): {}",
remote.display(),
err
),
);
return;
}
};
// send
trace!(
"syncing host file {} with remote {}",
host.display(),
remote.display()
);
let remote_path = remote.parent().unwrap_or_else(|| Path::new("/"));
match self.filetransfer_send(TransferPayload::Any(entry), remote_path, None) {
Ok(()) => {
self.log(
LogLevel::Info,
format!(
"synched watched file {} with {}",
host.display(),
remote.display()
),
);
}
Err(err) => {
self.log(
LogLevel::Error,
format!("failed to sync watched file {}: {}", remote.display(), err),
);
}
}
}
}