Files
Netcatty/application/state/sftp/useSftpTransfers.ts
T
Mateus A.M Ferreiraandbincxz a62cc1bd3f feat(sftp): add a one-click Duplicate action for files and folders (#4133)
* feat(sftp): add a one-click Duplicate action for files and folders

Duplicate copies the selected entries next to their originals in the same
folder, covering files, folders, and multi-selection. It reuses the existing
transfer queue and the transfer pipeline's "(copy)" naming helper, so progress,
cancel, and error reporting behave like Copy to other pane.

The target name is resolved before the transfer starts. Resolving through the
batch conflict defaults instead would re-enter processTransfer for a task that
is still marked in flight and stall the copy.

Adds the action to both the list and tree context menus, plus translations for
en, es, ru, zh-CN, and zh-TW.

Closes #4132

* fix(sftp): keep Duplicate safe around navigation rows and reserved names

Hide and ignore the tree's synthetic ".." row: duplicating it resolved the
source to an ancestor directory and could walk the copy into its own output.
The transfer hook also drops "." / ".." inputs for duplicate batches, and the
same-pane handler filters them defensively.

Pick duplicate names around destinations already reserved by a queued or paused
transfer. Those rows have no file on disk yet, so stat-only naming returned the
same "(copy)" path and the follow-up active-path check turned the second
one-click duplicate into a conflict dialog. The naming helper now accepts an
occupied-path predicate fed from the active transfer set.

* fix(sftp): block stale entry menus on parent navigation

---------

Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
2026-11-07 10:07:08 +08:00

2509 lines
106 KiB
TypeScript

import { useCallback, useEffect, useMemo, useRef, useState, type SetStateAction } from "react";
import {
FileConflict,
FileConflictAction,
SftpFilenameEncoding,
TransferDirection,
TransferStatus,
TransferTask,
} from "../../../domain/models";
import {
canReplaceSftpConflict,
describeSftpExistingKind,
describeSftpIncomingKind,
getSftpConflictTypeKey,
shouldUnlinkSftpConflictBeforeReplace,
} from "../../../domain/sftpConflict";
import {
findActivePathConflict,
pathConflictMessage,
} from "../../../domain/sftpTransferConflicts";
import { useI18n } from "../../i18n/I18nProvider";
import { notify } from "../../notification";
import { netcattyBridge } from "../../../infrastructure/services/netcattyBridge";
import { logger } from "../../../lib/logger";
import { sftpTransferCenterStore } from "../sftpTransferCenterStore";
import { SftpPane } from "./types";
import { isMissingStatError, unwrapSftpIpcError } from "./errors";
import { useSftpDirectoryTransferOps } from "./transferDirectoryOps";
import { useSftpTransferConflictOps } from "./transferConflictOps";
import { useSftpTransferTaskOps } from "./transferTaskOps";
import { globalSftpTransferScheduler } from "./globalTransferScheduler";
import {
createDirectDownloadTransferTask,
resolveDirectDirectoryDownloadFinalStatus,
} from "./downloadTransferTask";
import {
isTransferOrRootPauseLatched,
releaseTransferPauseTree,
waitUntilTransferPauseReleased,
} from "./transferPauseLatch";
import { bumpTransferControlEpoch } from "./transferControlEpoch";
import { transferRuntime } from "./transferRuntime";
import {
clearTransferCancelled,
clearTransferCancelledTree,
isTransferCancelledFlag,
markTransferCancelled,
markTransferCancelledTree,
} from "./transferCancelLatch";
import type { TransferResult, UseSftpTransfersParams, UseSftpTransfersResult } from "./useSftpTransfers.types";
import type { TransferConnectionLease } from "./transferConnectionPool";
import {
captureDeferredTransferAttempt,
createDeferredTransferAttemptQueue,
isDeferredTransferAttemptCurrent,
pruneTransferConflictDefaults,
type DeferredTransferAttemptQueue,
type TransferConflictDefaults,
} from "./transferConflictLifecycle";
import { getParentPath, joinPath } from "./utils";
import { promoteDirectoryReplaceStage as promoteDirectoryReplacePaths } from "./directoryReplacePromotion";
import {
isExternalDragDropFileUpload,
retryExternalDragDropFileUpload,
} from "./externalDragDropRetry";
// The bridge's statSftp awaits an unbounded SFTP callback, so a half-open
// session can leave a stat pending forever. Every stat issued outside the
// bridge is bounded with this timeout; on timeout the caller falls back to
// stale metadata / a 0 plan instead of one hung stat blocking a batch.
const STAT_SFTP_TIMEOUT_MS = 10_000;
// Preflight re-stats share the browse SFTP session and the renderer IPC
// queue. Launching one stat per file for a huge multi-file selection would
// flood both before any transfer task reaches the UI, so cap the number of
// in-flight preflight stats and skip the re-stat entirely once the selection
// grows past a bound (files beyond the cap stay on the safe 0 plan, which
// makes the bridge measure the live size anyway).
export const PREFLIGHT_STAT_CONCURRENCY = 8;
export const PREFLIGHT_STAT_MAX_FILES = 128;
/** Keep the MutableRefObject mirror in sync with the process-global latch set. */
function syncPausedTasksRef(ref: { current: Set<string> }, taskId: string, latched: boolean) {
if (latched) ref.current.add(taskId);
else ref.current.delete(taskId);
}
/** Set whose has() also observes process-global cancel flags (tab-close path). */
class PanelCancelSet extends Set<string> {
override add(id: string): this {
super.add(id);
markTransferCancelled(id);
return this;
}
override delete(id: string): boolean {
const had = super.delete(id);
clearTransferCancelled(id);
return had;
}
override has(id: string): boolean {
return super.has(id) || isTransferCancelledFlag(id);
}
}
function createPanelCancelSet(): Set<string> {
return new PanelCancelSet();
}
export async function runTrackedTransferAttempt<T>(
inFlightTransferIds: Set<string>,
taskId: string,
runner: () => Promise<T>,
): Promise<T> {
inFlightTransferIds.add(taskId);
try {
return await runner();
} finally {
// Completion callbacks are caller-owned and may throw. The process-global
// runtime already releases its walk in finally; keep the panel mirror just
// as strict so one callback failure cannot permanently block a retry.
inFlightTransferIds.delete(taskId);
}
}
export function finishTransferTask(
task: TransferTask,
outcome: {
partialFailure: boolean;
cancelled: boolean;
endTime?: number;
},
flushPendingProgress: () => void,
mirrorPanelTask: (task: TransferTask) => void,
): TransferStatus {
flushPendingProgress();
const latestTask = transferRuntime.getTask(task.id) ?? task;
const endTime = outcome.endTime ?? Date.now();
const cancelled = outcome.cancelled || latestTask.status === "cancelled";
const status: TransferStatus = cancelled
? "cancelled"
: outcome.partialFailure ? "failed" : "completed";
const updates: Partial<TransferTask> = cancelled
? {
status: "cancelled",
error: undefined,
endTime,
speed: 0,
}
: {
status,
error: outcome.partialFailure ? "Some files failed to transfer" : undefined,
retryable: outcome.partialFailure ? false : latestTask.retryable,
endTime,
transferredBytes: outcome.partialFailure ? latestTask.transferredBytes : latestTask.totalBytes,
speed: 0,
};
transferRuntime.patchTask(latestTask.id, updates);
mirrorPanelTask(transferRuntime.getTask(latestTask.id) ?? { ...latestTask, ...updates });
return status;
}
const STICKY_CONTROL_STATUSES = new Set<TransferStatus>([
"paused",
"pausing",
"cancelled",
"completed",
"failed",
"interrupted",
"attention",
]);
const LIVE_QUEUE_STATUSES = new Set<TransferStatus>([
"transferring",
"pending",
"queued",
]);
/**
* Pause, resume, and cancel paint on the same turn. Per-file complete/queue
* churn stays on the animation frame so a folder upload does not commit once
* per child.
*/
export function shouldPaintTransferControlNow(
previous: readonly TransferTask[],
next: readonly TransferTask[],
): boolean {
const prevById = new Map(previous.map((row) => [row.id, row]));
return next.some((row) => {
const prevRow = prevById.get(row.id);
if (prevRow?.status === row.status) return false;
if (row.status === "paused" || row.status === "pausing" || row.status === "cancelled") return true;
if (prevRow && (prevRow.status === "paused" || prevRow.status === "pausing" || prevRow.status === "cancelled")) {
return true;
}
if (
!row.parentTaskId
&& prevRow
&& (STICKY_CONTROL_STATUSES.has(row.status) || STICKY_CONTROL_STATUSES.has(prevRow.status))
) {
return true;
}
return false;
});
}
/**
* Child lifecycle updates clone the ref they saw. If that clone still says the
* folder is transferring after the store already paused it, publishing the
* clone reopens the parent. An explicit status edit (resume, retry) is kept.
*/
export function reconcilePanelRowsWithStore(
previous: readonly TransferTask[],
next: readonly TransferTask[],
storeRows: readonly TransferTask[],
): TransferTask[] {
const prevById = new Map(previous.map((row) => [row.id, row]));
const storeById = new Map(storeRows.map((row) => [row.id, row]));
let changed = false;
const reconciled = next.map((row) => {
const storeRow = storeById.get(row.id);
const prevRow = prevById.get(row.id);
if (!storeRow || !STICKY_CONTROL_STATUSES.has(storeRow.status) || !LIVE_QUEUE_STATUSES.has(row.status)) {
return row;
}
// A full-list write can disagree with the ref only because the ref already
// moved to the store's pause. That is a stale snapshot, not Resume.
// Resume releases the latch before it republishes transferring.
const rootId = row.parentTaskId ?? row.id;
const latchedReopen = isTransferOrRootPauseLatched(rootId, row.id);
if (prevRow && prevRow.status !== row.status && !latchedReopen) return row;
if (row.status === storeRow.status) return row;
changed = true;
return {
...row,
status: storeRow.status,
speed: 0,
error: storeRow.error,
endTime: storeRow.endTime,
pauseUnavailableReason: storeRow.pauseUnavailableReason,
};
});
return changed ? reconciled : next as TransferTask[];
}
/** User-facing path exclusivity notice (toast + logs). */
function notifyPathConflict(
existing: Pick<TransferTask, "fileName" | "status">,
t: (key: string, values?: Record<string, string | number>) => string,
): void {
const name = existing.fileName || "file";
const message = existing.status === "paused"
? t("sftp.transfers.pathConflict.paused", { name })
: t("sftp.transfers.pathConflict.inProgress", { name });
logger.warn("[SFTP] path conflict", pathConflictMessage(existing));
notify.warning(message, t("sftp.transfers.pathConflict.title"));
}
export const useSftpTransfers = ({
ownerId,
canPrepareAdoption,
surfaceVisible = true,
getActivePane,
getPaneByConnectionId,
getTabByConnectionId,
updateTab,
refresh,
clearCacheForConnection,
sftpSessionsRef,
connectionCacheKeyMapRef,
listLocalFiles,
listRemoteFiles,
handleSessionError,
acquireTransferSession,
}: UseSftpTransfersParams): UseSftpTransfersResult => {
const { t } = useI18n();
const tRef = useRef(t);
tRef.current = t;
// startTransfer keeps the processTransfer closure from its first render.
// Read the latest acquire fn so a host key trusted after that render is
// included in the transfer open, instead of the empty list from mount.
const acquireTransferSessionRef = useRef(acquireTransferSession);
acquireTransferSessionRef.current = acquireTransferSession;
const acquireTransferSessionLive = useCallback<NonNullable<UseSftpTransfersParams["acquireTransferSession"]>>(
(hostId, transferId, connectHost) => {
const acquire = acquireTransferSessionRef.current;
if (!acquire) {
return Promise.reject(new Error("SFTP transfer session acquisition is unavailable"));
}
return acquire(hostId, transferId, connectHost);
},
[],
);
// Ref so progress flushes always see the latest visibility without rebinding.
const surfaceVisibleRef = useRef(surfaceVisible);
surfaceVisibleRef.current = surfaceVisible;
const [transfers, setTransfersState] = useState<TransferTask[]>(() => sftpTransferCenterStore.getOwnerTasks(ownerId));
// Only re-surface live attention conflicts. Terminal rows may still carry a
// stale conflict payload after older Skip paths — those must not reopen the dialog.
const [conflicts, setConflicts] = useState<FileConflict[]>(() => sftpTransferCenterStore
.getOwnerTasks(ownerId)
.filter((task) => task.status === "attention" && !!task.conflict)
.map((task) => task.conflict as FileConflict));
// Track cancelled task IDs. has() also sees process-global cancel flags set by
// the transfer center after the React owner unmounts.
const cancelledTasksRef = useRef<Set<string>>(createPanelCancelSet());
// Process-global pause latches (survive panel unmount / tab close). Local ref
// mirrors the global set so transferDirectoryOps can keep using a MutableRefObject.
const pausedTasksRef = useRef<Set<string>>(new Set());
const pauseWaitersRef = useRef<Map<string, Set<() => void>>>(new Map());
// Track active child transfer IDs per parent (outside React state for immediate visibility)
const activeChildIdsRef = useRef<Map<string, Set<string>>>(new Map());
// Live processTransfer walks (esp. directory parents). Resume must unlatch
// these instead of starting a second full re-walk of the tree.
const inFlightTransferIdsRef = useRef<Set<string>>(new Set());
/** While folder is latched, keep hammering pauseTransfer on live children. */
const folderPauseWatchdogsRef = useRef<Map<string, ReturnType<typeof setInterval>>>(new Map());
// Sole writer after mount is setTransfers (ref-first). Do not clobber with
// React state on every render — a parent re-render between progress IPC ticks
// would roll the ref back and freeze the global center bar.
const transfersRef = useRef(transfers);
const conflictsRef = useRef(conflicts);
conflictsRef.current = conflicts;
// Keep owner lifecycle changes in the global transfer store. Byte progress
// arrives independently through the unified process-wide event stream.
//
// Do not pre-assign transfersRef before publishing the same array reference —
// identity equality skips the store merge (avoids store↔listener loops).
// Lifecycle writers (child add/start/complete) fire several times per file.
// Repainting the whole SFTP view synchronously on each one saturates the
// renderer on folders with many small files, which then throttles the
// transfer itself. transfersRef/store stay authoritative; React state
// catches up once per frame, except pause/resume/cancel and folder terminal
// rows, which paint immediately.
//
// A frame queued for child churn must not commit a pre-pause ref after the
// store has already paused the parent. The flush re-reads the store, and a
// synchronous control paint bumps paintEpoch so that queued frame no-ops.
const stateFlushHandleRef = useRef<{ cancel: () => void } | null>(null);
const paintEpochRef = useRef(0);
const cancelScheduledTransferPaint = useCallback(() => {
stateFlushHandleRef.current?.cancel();
stateFlushHandleRef.current = null;
}, []);
const paintTransfersNow = useCallback((rows: TransferTask[]) => {
cancelScheduledTransferPaint();
paintEpochRef.current += 1;
transfersRef.current = rows;
setTransfersState(rows);
}, [cancelScheduledTransferPaint]);
const scheduleTransfersStateFlush = useCallback(() => {
if (stateFlushHandleRef.current) return;
const epochAtSchedule = paintEpochRef.current;
const flush = () => {
stateFlushHandleRef.current = null;
// A pause/resume/cancel/terminal paint landed after this frame was queued.
if (paintEpochRef.current !== epochAtSchedule) return;
const storeRows = sftpTransferCenterStore.getOwnerTasks(ownerId);
const painted = reconcilePanelRowsWithStore(transfersRef.current, transfersRef.current, storeRows);
paintEpochRef.current += 1;
transfersRef.current = painted;
setTransfersState(painted);
};
if (typeof window !== "undefined" && typeof window.requestAnimationFrame === "function") {
const id = window.requestAnimationFrame(flush);
stateFlushHandleRef.current = { cancel: () => window.cancelAnimationFrame(id) };
} else {
const id = setTimeout(flush, 16);
stateFlushHandleRef.current = { cancel: () => clearTimeout(id) };
}
}, [ownerId]);
useEffect(() => () => {
cancelScheduledTransferPaint();
}, [cancelScheduledTransferPaint]);
// When the retained panel is re-opened, catch up React state from the ref that
// kept receiving progress while the surface was hidden. Cancel any frame that
// was queued against the hidden snapshot so it cannot paint over this catch-up.
useEffect(() => {
if (!surfaceVisible) return;
paintTransfersNow(transfersRef.current);
}, [paintTransfersNow, surfaceVisible]);
const setTransfers = useCallback((update: SetStateAction<TransferTask[]>) => {
const prev = transfersRef.current;
const next = typeof update === "function"
? (update as (value: TransferTask[]) => TransferTask[])(prev)
: update;
if (next === prev) return;
const reconciled = reconcilePanelRowsWithStore(
prev,
next,
sftpTransferCenterStore.getOwnerTasks(ownerId),
);
if (reconciled === prev) return;
sftpTransferCenterStore.publishOwner(ownerId, reconciled);
const canonical = sftpTransferCenterStore.getOwnerTasks(ownerId);
if (shouldPaintTransferControlNow(prev, canonical)) {
paintTransfersNow(canonical);
return;
}
transfersRef.current = canonical;
scheduleTransfersStateFlush();
}, [ownerId, paintTransfersNow, scheduleTransfersStateFlush]);
// Coalesce progress-only patches to one paint per animation frame. Without
// this, each stream tick maps the full transfer list + publishOwner + store
// emit, and the main-window renderer pegs ~100%+ CPU during large copies.
//
// Progress flushes intentionally skip publishOwner: the store is updated via
// transferRuntime.patchTask (cheap single-row merge). Re-publishing the full
// owner snapshot on every tick was the freeze path for directory copies.
const pendingProgressByIdRef = useRef<Map<string, Partial<TransferTask>>>(new Map());
const progressFlushRafRef = useRef<number | null>(null);
// Progress coalesce: store is the live authority (SftpTransferItem uses
// useSftpTransferTask). Batch patchTask to one rAF — do not add another 500ms
// timer on top of main-process IPC (stacked lag made the bar jump).
// Do not setTransfersState here — that re-reconciled the retained SFTP tree.
const flushPendingProgress = useCallback(() => {
progressFlushRafRef.current = null;
const pending = pendingProgressByIdRef.current;
if (pending.size === 0) return;
pendingProgressByIdRef.current = new Map();
for (const [taskId, updates] of pending) {
transferRuntime.patchTask(taskId, updates);
}
}, []);
const scheduleProgressFlush = useCallback(() => {
if (progressFlushRafRef.current != null) return;
progressFlushRafRef.current = window.requestAnimationFrame(flushPendingProgress);
}, [flushPendingProgress]);
const applyProgress = useCallback((taskId: string, updates: Partial<TransferTask>) => {
const prevPending = pendingProgressByIdRef.current.get(taskId) || {};
pendingProgressByIdRef.current.set(taskId, { ...prevPending, ...updates });
// Keep ref hot for cancel/pause checks without waiting for paint.
// A byte tick that still says "transferring" must not reopen a latched pause.
transfersRef.current = transfersRef.current.map((row) => {
if (row.id !== taskId) return row;
const merged = { ...row, ...updates };
const latched = isTransferOrRootPauseLatched(row.parentTaskId ?? row.id, row.id);
if (
(latched || row.status === "paused" || row.status === "pausing")
&& (updates.status === "transferring" || updates.status === "pending" || updates.status === "queued")
) {
return { ...merged, status: row.status, speed: 0 };
}
return merged;
});
scheduleProgressFlush();
}, [scheduleProgressFlush]);
useEffect(() => () => {
if (progressFlushRafRef.current != null) {
window.cancelAnimationFrame(progressFlushRafRef.current);
progressFlushRafRef.current = null;
}
}, []);
const completionHandlersRef = useRef<Map<string, (result: TransferResult) => void | Promise<void>>>(new Map());
const conflictDefaultsRef = useRef<TransferConflictDefaults>(new Map());
const deferredConflictAttemptsRef = useRef<DeferredTransferAttemptQueue | null>(null);
useEffect(() => {
const conflictDefaults = conflictDefaultsRef.current;
const queue = createDeferredTransferAttemptQueue({
onError: (error) => logger.warn("[SFTP] Deferred conflict transfer failed", error),
});
deferredConflictAttemptsRef.current = queue;
return () => {
queue.dispose();
if (deferredConflictAttemptsRef.current === queue) {
deferredConflictAttemptsRef.current = null;
}
conflictDefaults.clear();
};
}, [ownerId]);
useEffect(() => {
pruneTransferConflictDefaults(conflictDefaultsRef.current, transfers);
}, [transfers]);
const clearCancelledTask = useCallback((taskId: string) => {
cancelledTasksRef.current.delete(taskId);
clearTransferCancelled(taskId);
}, []);
const waitUntilTransferResumed = useCallback(async (taskId: string) => {
// Process-global latch: survives panel unmount so the transfer center can
// release a walk that outlived the React owner.
await waitUntilTransferPauseReleased(taskId);
syncPausedTasksRef(pausedTasksRef, taskId, false);
}, []);
const stopFolderPauseWatchdog = useCallback((taskId: string) => {
const timer = folderPauseWatchdogsRef.current.get(taskId);
if (timer) {
clearInterval(timer);
folderPauseWatchdogsRef.current.delete(taskId);
}
}, []);
/** Every unfinished child id under a folder parent (state + live workers). */
const collectPauseTreeChildIds = useCallback((rootTaskId: string): string[] => {
const childIdSet = new Set(activeChildIdsRef.current.get(rootTaskId) ?? []);
for (const candidate of transfersRef.current) {
if (
candidate.parentTaskId === rootTaskId
&& !["completed", "cancelled", "failed"].includes(candidate.status)
) {
childIdSet.add(candidate.id);
}
}
return [...childIdSet];
}, []);
/**
* Resume/cancel must clear the whole latch tree. Store pause latches parent
* + children; releasing only the parent left child latches stuck and the
* directory walk blocked forever (UI "transferring", 0 bytes moving).
*/
const releasePausedTransfer = useCallback((taskId: string, childIds?: readonly string[]) => {
// Invalidate any in-flight soft-drain / watchdog pause IPC for this tree.
bumpTransferControlEpoch(taskId);
const kids = childIds ?? collectPauseTreeChildIds(taskId);
releaseTransferPauseTree(taskId, kids);
syncPausedTasksRef(pausedTasksRef, taskId, false);
for (const id of kids) syncPausedTasksRef(pausedTasksRef, id, false);
stopFolderPauseWatchdog(taskId);
const wake = (id: string) => {
const waiters = pauseWaitersRef.current.get(id);
pauseWaitersRef.current.delete(id);
for (const resolve of waiters ?? []) resolve();
};
wake(taskId);
for (const id of kids) wake(id);
}, [collectPauseTreeChildIds, stopFolderPauseWatchdog]);
/** Latch the tree and return the pause control epoch for soft-drain gating. */
const resolveTaskEndpoints = useCallback((task: TransferTask) => {
const sourceTab = getTabByConnectionId(task.sourceConnectionId);
const targetTab = task.targetConnectionId === "local"
? null
: getTabByConnectionId(task.targetConnectionId);
const sourceByHost = !sourceTab && task.sourceHostId
? (["left", "right"] as const).map((side) => {
const pane = getActivePane(side);
return pane?.connection?.hostId === task.sourceHostId ? { side, pane } : null;
}).find(Boolean)
: null;
const targetByHost = !targetTab && task.targetHostId
? (["left", "right"] as const).map((side) => {
const pane = getActivePane(side);
return pane?.connection?.hostId === task.targetHostId ? { side, pane } : null;
}).find(Boolean)
: null;
const localTab = (["left", "right"] as const).map((side) => {
const pane = getActivePane(side);
return pane?.connection?.isLocal ? { side, pane } : null;
}).find(Boolean);
const source = sourceTab
? { side: sourceTab.side, pane: sourceTab.pane }
: sourceByHost
?? (!task.sourceHostId ? localTab : null);
const target = targetTab
? { side: targetTab.side, pane: targetTab.pane }
: targetByHost
?? (!task.targetHostId || task.targetConnectionId === "local" ? localTab : null);
if (!source?.pane.connection || !target?.pane.connection) {
return null;
}
return {
sourceSide: source.side,
targetSide: target.side,
sourcePane: source.pane,
targetPane: target.pane,
};
}, [getActivePane, getTabByConnectionId]);
const cleanupTaskArtifacts = useCallback(async (task: TransferTask) => {
const endpoints = resolveTaskEndpoints(task);
const targetPane = endpoints?.targetPane;
const targetSftpId = targetPane?.connection && !targetPane.connection.isLocal
? sftpSessionsRef.current.get(targetPane.connection.id)
: undefined;
await netcattyBridge.get()?.cleanupTransferArtifacts?.({
transferId: task.id,
sourcePath: task.sourcePath,
targetPath: task.targetPath,
targetSftpId,
targetEncoding: targetPane?.filenameEncoding,
stagedTargetPath: task.stagedTargetPath,
});
}, [resolveTaskEndpoints, sftpSessionsRef]);
const isTransferCancelledError = useCallback(
(error: unknown): boolean =>
error instanceof Error && error.message === "Transfer cancelled",
[],
);
const conflictDefaultKey = useCallback(
(batchId: string | undefined, isDirectory: boolean, existingType?: "file" | "directory" | "symlink") =>
`${batchId ?? "global"}:${getSftpConflictTypeKey(isDirectory, existingType)}`,
[],
);
const buildReplaceTypeMismatchError = useCallback(
(isDirectory: boolean, existingType: "file" | "directory" | "symlink" | undefined, targetPath: string) =>
`Cannot replace existing ${describeSftpExistingKind(existingType)} with ${describeSftpIncomingKind(isDirectory)}: ${targetPath}`,
[],
);
const { completeCancelledTask, cancelBackendTransfers, markBatchStopped } = useSftpTransferTaskOps({
cancelledTasksRef,
activeChildIdsRef,
transfersRef,
completionHandlersRef,
setConflicts,
setTransfers,
releasePausedTransfer,
cleanupTaskArtifacts,
});
const { statTargetPath, getDuplicateTarget, deleteTargetPath, isSameSourceEntry } = useSftpTransferConflictOps();
const { transferFile, transferDirectory } = useSftpDirectoryTransferOps({
ownerId,
cancelledTasksRef,
pausedTasksRef,
waitUntilTransferResumed,
activeChildIdsRef,
transfersRef,
setTransfers,
listLocalFiles,
listRemoteFiles,
acquireTransferSession: acquireTransferSession ? acquireTransferSessionLive : undefined,
});
const processTransfer = async (
task: TransferTask,
sourcePane: SftpPane,
targetPane: SftpPane,
targetSide: "left" | "right",
): Promise<TransferStatus> => {
if (cancelledTasksRef.current.has(task.id)) {
return "cancelled";
}
// Guard against concurrent processTransfer on the same id (resume used to
// re-enter while a paused directory walk was still alive). Process-global
// registry also covers walks after the SFTP panel / terminal tab unmounts.
if (inFlightTransferIdsRef.current.has(task.id) || transferRuntime.isWalkInFlight(task.id)) {
logger.warn("[SFTP] processTransfer already in flight; skipping re-entry", task.id);
return transfersRef.current.find((candidate) => candidate.id === task.id)?.status
?? transferRuntime.getTask(task.id)?.status
?? "transferring";
}
return runTrackedTransferAttempt(inFlightTransferIdsRef.current, task.id, async () => {
// Runtime is the authority writer for live lifecycle. Also mirror into the
// panel list when mounted (view only — soft control does not depend on it).
// Progress-only patches are rAF-coalesced so large-file streams do not
// force a full React + global-center re-render on every IPC tick.
const updateTask = (updates: Partial<TransferTask>) => {
const statusChange = updates.status !== undefined
&& updates.status !== "transferring"
&& updates.status !== "paused"
&& updates.status !== "pausing";
// Pause-latched progress still counts as progress-only (no publishOwner).
const progressOnly = !statusChange
&& updates.error === undefined
&& updates.conflict === undefined
&& updates.ownerId === undefined
&& (
updates.transferredBytes !== undefined
|| updates.speed !== undefined
|| updates.checkpointBytes !== undefined
|| updates.resumeStage !== undefined
|| updates.downloadCheckpointBytes !== undefined
|| updates.uploadCheckpointBytes !== undefined
|| updates.sourceFingerprint !== undefined
|| updates.totalBytes !== undefined
|| updates.phase !== undefined
|| updates.status === "transferring"
|| updates.status === "paused"
|| updates.status === "pausing"
);
if (progressOnly) {
applyProgress(task.id, updates);
return;
}
// Lifecycle transitions: flush any pending progress first, then apply.
if (pendingProgressByIdRef.current.size > 0) {
flushPendingProgress();
}
transferRuntime.patchTask(task.id, updates);
setTransfers((prev) =>
prev.map((t) => (t.id === task.id ? { ...t, ...updates } : t)),
);
};
let walkStatus: TransferStatus = "transferring";
await transferRuntime.runWalk(task.id, async () => {
walkStatus = await processTransferBody(task, sourcePane, targetPane, targetSide, updateTask);
});
return walkStatus;
});
};
const processTransferBody = async (
task: TransferTask,
sourcePane: SftpPane,
targetPane: SftpPane,
targetSide: "left" | "right",
updateTask: (updates: Partial<TransferTask>) => void,
): Promise<TransferStatus> => {
// Initialize encoding early to avoid temporal dead zone issues
const sourceEncoding: SftpFilenameEncoding = sourcePane.connection?.isLocal
? "auto"
: sourcePane.filenameEncoding || "auto";
const targetEncoding: SftpFilenameEncoding = targetPane.connection?.isLocal
? "auto"
: targetPane.filenameEncoding || "auto";
// Browse panel sessions may be soft-closed while transfers continue on the
// dedicated pool. Prefer live browse ids, else open pool sessions for work.
let sourceSftpId: string | null = sourcePane.connection?.isLocal
? null
: (sftpSessionsRef.current.get(sourcePane.connection!.id) ?? null);
let targetSftpId: string | null = targetPane.connection?.isLocal
? null
: (sftpSessionsRef.current.get(targetPane.connection!.id) ?? null);
let sourceWorkLease: TransferConnectionLease | null = null;
let targetWorkLease: TransferConnectionLease | null = null;
const releaseWorkLeases = (mode: "release" | "discard" = "release") => {
if (mode === "discard") {
sourceWorkLease?.discard();
targetWorkLease?.discard();
} else {
sourceWorkLease?.release();
targetWorkLease?.release();
}
sourceWorkLease = null;
targetWorkLease = null;
};
// Detect same-host: both sides connected to the same remote endpoint.
// Use per-connection cache keys (hostname+port+protocol+sudo+username) instead of
// just hostId, because the same hostId can have different session-time overrides.
const sourceCacheKey = sourcePane.connection?.id
? connectionCacheKeyMapRef.current.get(sourcePane.connection.id)
: undefined;
const targetCacheKey = targetPane.connection?.id
? connectionCacheKeyMapRef.current.get(targetPane.connection.id)
: undefined;
// sameHost remote-cp needs a live sftpId; pool-only transfers fall back to
// recursive file copy via transferDirectory.
const sameHostEndpoints = !!(
!sourcePane.connection?.isLocal && !targetPane.connection?.isLocal
&& sourceCacheKey && targetCacheKey
&& sourceCacheKey === targetCacheKey
);
try {
// Prefer dedicated work leases for remote ends so folder walks/listings
// survive browse park and tab hide (pool holders, not panel map ids).
if (!sourcePane.connection?.isLocal) {
const sourceHostId = task.sourceHostId || sourcePane.connection?.hostId;
const acquireSourceSession = acquireTransferSessionRef.current;
if (acquireSourceSession && sourceHostId) {
sourceWorkLease = await acquireSourceSession(sourceHostId, `${task.id}:work-source`);
sourceSftpId = sourceWorkLease.sftpId;
}
if (!sourceSftpId) {
const sourceSide = targetSide === "left" ? "right" : "left";
handleSessionError(sourceSide, new Error("Source SFTP session lost"));
throw new Error("Source SFTP session not found");
}
}
if (!targetPane.connection?.isLocal) {
const targetHostId = task.targetHostId || targetPane.connection?.hostId;
const acquireTargetSession = acquireTransferSessionRef.current;
if (acquireTargetSession && targetHostId) {
targetWorkLease = await acquireTargetSession(targetHostId, `${task.id}:work-target`);
targetSftpId = targetWorkLease.sftpId;
}
if (!targetSftpId) {
handleSessionError(targetSide, new Error("Target SFTP session lost"));
throw new Error("Target SFTP session not found");
}
}
const sameHost = sameHostEndpoints && !!sourceSftpId && !!targetSftpId;
// Returns the discovered stat so callers that need the size (e.g. the
// overwrite conflict dialog) can await this instead of reading stale
// metadata from the local task copy.
const discoverTransferSize = async (): Promise<{
size: number;
lastModified?: number;
/** False for pane-listing display data. That size must not become the plan. */
plan: boolean;
} | null> => {
// When the endpoint cannot stat the source (legacy SCP returns the
// sizeKnown: false placeholder) or the stat fails, the transfer plan
// stays unknown (0 bytes, so the bridge measures the live size). The
// overwrite dialog still needs display metadata, so fall back to the
// retained cached listing entry instead of reporting 0 bytes — but only
// for the dialog; the plan itself must never take the listing's
// possibly stale size back.
const cachedListingStatForDialog = (): { size: number; lastModified?: number; plan: false } | null => {
if (getParentPath(task.sourcePath) !== sourcePane.connection?.currentPath) return null;
const entry = sourcePane.files.find((candidate) => candidate.name === task.fileName);
if (!entry || !(entry.size > 0)) return null;
return { size: entry.size, lastModified: entry.lastModified, plan: false };
};
try {
if (task.totalBytes > 0 || !!task.sourceLastModified) return null;
if (sourcePane.connection?.isLocal) {
const stat = await netcattyBridge.get()?.statLocal?.(task.sourcePath);
if (stat) {
if (!task.sourceLastModified && stat.lastModified) {
task.sourceLastModified = stat.lastModified;
}
if (!cancelledTasksRef.current.has(task.id)) {
updateTask({
totalBytes: stat.size,
});
}
return { size: stat.size, lastModified: stat.lastModified, plan: true };
}
return null;
}
if (sourceSftpId) {
if (task.preflightStatSkipped) {
return cachedListingStatForDialog();
}
// This stat runs deferred (started before the conflict check) and
// can be awaited by the conflict path, so it must not hang on a
// half-open session. Bound it like the preflight stat and settle
// early once the task is cancelled, since a marked task cannot
// settle an in-flight SFTP callback itself.
let timeoutTimer: ReturnType<typeof setTimeout> | undefined;
let cancelTimer: ReturnType<typeof setInterval> | undefined;
const stat = await Promise.race([
Promise.resolve(netcattyBridge.get()?.statSftp?.(
sourceSftpId,
task.sourcePath,
sourceEncoding,
)),
new Promise<undefined>((resolve) => {
timeoutTimer = setTimeout(() => resolve(undefined), STAT_SFTP_TIMEOUT_MS);
}),
new Promise<undefined>((resolve) => {
cancelTimer = setInterval(() => {
if (cancelledTasksRef.current.has(task.id)) resolve(undefined);
}, 250);
}),
]).finally(() => {
if (timeoutTimer) clearTimeout(timeoutTimer);
if (cancelTimer) clearInterval(cancelTimer);
});
if (stat && stat.sizeKnown !== false) {
if (!task.sourceLastModified && stat.lastModified) {
task.sourceLastModified = stat.lastModified;
}
if (!cancelledTasksRef.current.has(task.id)) {
updateTask({
totalBytes: stat.size,
});
}
return { size: stat.size, lastModified: stat.lastModified, plan: true };
}
// Unknown size (e.g. legacy SCP stat): keep the transfer plan
// unknown so a 0-byte placeholder never replaces trusted metadata,
// but give the conflict dialog the retained cached listing entry.
return cachedListingStatForDialog();
}
} catch (err) {
if (!isTransferCancelledError(err)) {
logger.debug?.("[SFTP] Deferred transfer size discovery failed", err);
}
return cachedListingStatForDialog();
}
};
const t0 = performance.now();
logger.debug(`[SFTP:perf] processTransfer START — file=${task.fileName} isDir=${task.isDirectory}`);
// Directory parents use file counts in transferredBytes/totalBytes. Never
// seed from byte checkpointBytes (pause clears those, which would show 0/N
// while completed children are skipped without re-counting).
const directoryCompletedCount = task.isDirectory
? (task.directoryResumeCheckpoint?.completedEntries ?? 0) + transfersRef.current.filter(
(candidate) => candidate.parentTaskId === task.id && candidate.status === "completed",
).length
: 0;
updateTask({
status: "transferring",
totalBytes: task.isDirectory
? directoryCompletedCount
: Math.max(task.totalBytes, 0),
transferredBytes: task.isDirectory
? Math.max(task.transferredBytes, directoryCompletedCount)
: (task.checkpointBytes ?? task.transferredBytes ?? 0),
startTime: Date.now(),
phase: "transferring",
resumable: task.resumable !== false,
reconnectRequired: false,
error: undefined,
});
// Run size discovery and conflict check in parallel
const conflictCheckPromise = (async (): Promise<FileConflict | null> => {
if (task.skipConflictCheck || !targetPane.connection) return null;
const sourceStat: { size: number; mtime: number } | null =
(task.totalBytes > 0 || task.sourceLastModified)
? { size: task.totalBytes, mtime: task.sourceLastModified || Date.now() }
: null;
try {
const existingStat = await statTargetPath(targetPane, targetSftpId, task.targetPath, targetEncoding);
if (existingStat) {
const applyToAllCount = task.batchId
? await (async () => {
const candidates = transfersRef.current.filter((candidate) =>
candidate.batchId === task.batchId &&
candidate.isDirectory === task.isDirectory &&
!candidate.parentTaskId &&
candidate.status !== "completed" &&
candidate.status !== "cancelled",
);
const matches = await Promise.all(candidates.map(async (candidate) => {
if (candidate.id === task.id) return true;
try {
const candidateStat = await statTargetPath(
targetPane,
targetSftpId,
candidate.targetPath,
targetEncoding,
);
return candidateStat?.type === existingStat.type;
} catch {
return false;
}
}));
return Math.max(1, matches.filter(Boolean).length);
})()
: 1;
return {
transferId: task.id,
batchId: task.batchId,
fileName: task.fileName,
sourcePath: task.sourcePath,
targetPath: task.targetPath,
isDirectory: task.isDirectory,
existingType: existingStat.type,
applyToAllCount,
existingSize: existingStat.size,
newSize: sourceStat?.size || task.totalBytes || 0,
existingModified: existingStat.mtime,
newModified: sourceStat?.mtime || Date.now(),
};
}
} catch (error) {
// Missing path = no conflict. ENOTSUP / unknown type fail closed.
if (isMissingStatError(error)) return null;
throw error;
}
return null;
})();
// For single files: start size discovery without blocking the conflict
// check, but always settle the promise before any dialog or transfer
// launch (bounded per-task; the task is already visible/cancellable).
const sizeDiscoveryPromise = task.isDirectory ? null : discoverTransferSize();
// Only await conflict check (fast single stat call)
const conflict = await conflictCheckPromise;
// Cancel/Stop may have won while conflict stats were in flight.
if (cancelledTasksRef.current.has(task.id)) return "cancelled";
// null means the plan size is still unknown. A verified 0-byte stat is 0,
// not null, so an empty file does not pick up a stale listing size.
let verifiedPlanBytes: number | null = task.totalBytes > 0 ? task.totalBytes : null;
if (sizeDiscoveryPromise) {
// The task's totalBytes may still be 0 here because discovery ran
// deferred and updateTask never mutates the local task copy. Await
// discovery even without a conflict so its statLocal updateTask cannot
// resolve after the transfer finished and overwrite the bridge's
// authoritative total with a stale late size (the conflict dialog also
// needs it: #3559).
const discovered = await sizeDiscoveryPromise;
// Cancel during this stat must win before an apply-to-all Replace
// deletes an existing symlink.
if (cancelledTasksRef.current.has(task.id)) return "cancelled";
if (discovered && discovered.size >= 0) {
if (conflict) {
conflict.newSize = discovered.size;
if (discovered.lastModified) conflict.newModified = discovered.lastModified;
if (discovered.plan) verifiedPlanBytes = discovered.size;
}
}
}
if (conflict) {
const defaultAction = conflictDefaultsRef.current
.get(task.batchId ?? "global")
?.get(getSftpConflictTypeKey(task.isDirectory, conflict.existingType));
if (defaultAction) {
if (defaultAction === "stop") {
await markBatchStopped(task);
return "cancelled";
}
if (defaultAction === "skip") {
cancelledTasksRef.current.add(task.id);
updateTask({ status: "cancelled", endTime: Date.now() });
await completeCancelledTask(task);
return "cancelled";
}
if (defaultAction === "replace" && !canReplaceSftpConflict(task.isDirectory, conflict.existingType)) {
updateTask({
status: "failed",
endTime: Date.now(),
error: buildReplaceTypeMismatchError(task.isDirectory, conflict.existingType, task.targetPath),
retryable: false,
});
return "failed";
}
if (
defaultAction === "replace"
&& shouldUnlinkSftpConflictBeforeReplace(conflict.existingType)
) {
await deleteTargetPath(task, targetPane, targetSftpId, targetEncoding, "symlink");
}
const duplicateTarget = defaultAction === "duplicate"
? await getDuplicateTarget(task, targetPane, targetSftpId, targetEncoding)
: null;
if (cancelledTasksRef.current.has(task.id)) return "cancelled";
const updatedTask: TransferTask = {
...task,
...(duplicateTarget
? {
fileName: duplicateTarget.fileName,
targetPath: duplicateTarget.targetPath,
}
: null),
skipConflictCheck: true,
replaceExistingTarget: defaultAction === "replace",
};
setTransfers((prev) =>
prev.map((t) =>
t.id === task.id
? { ...updatedTask, status: "pending" as TransferStatus }
: t,
),
);
return processTransfer(updatedTask, sourcePane, targetPane, targetSide);
}
if (cancelledTasksRef.current.has(task.id)) return "cancelled";
setConflicts((prev) => [...prev, conflict]);
updateTask({
status: "attention",
// Dialog newSize may be the listing fallback (plan: false). Only a
// verified stat may become the plan; 0 stays omitted at startStreamTransfer.
totalBytes: verifiedPlanBytes ?? 0,
conflict,
});
return "attention";
}
logger.debug(`[SFTP:perf] starting actual transfer — file=${task.fileName} isDir=${task.isDirectory} — ${(performance.now() - t0).toFixed(0)}ms since start`);
await waitUntilTransferResumed(task.id);
if (cancelledTasksRef.current.has(task.id)) return "cancelled";
let dirPartialFailure = false;
// Same-host exec-based paths are only safe for UTF-8 compatible encodings.
// "auto" is allowed here — the backend resolves it to the actual encoding
// and skips exec if it resolved to non-UTF-8 (e.g. gb18030).
const encodingSafeForExec =
(!sourceEncoding || sourceEncoding === "utf-8" || sourceEncoding === "auto") &&
(!targetEncoding || targetEncoding === "utf-8" || targetEncoding === "auto");
// Try same-host directory optimization first; falls back to recursive transfer
// if remote cp is unavailable (e.g. Windows SSH servers).
// Merge/Replace onto the source directory is already satisfied. Walking
// it would rewrite its children (including replacing links with files).
// Duplicate has a different target and continues through normal copying.
let directoryHandled = task.isDirectory
&& await isSameSourceEntry(task, targetPane, targetSftpId, targetEncoding);
if (
task.isDirectory
&& !directoryHandled
&& !task.replaceExistingTarget
&& task.resumable === false
&& sameHost
&& encodingSafeForExec
&& sourceSftpId
) {
if (cancelledTasksRef.current.has(task.id)) {
throw new Error("Transfer cancelled");
}
const result = await netcattyBridge.require().sameHostCopyDirectory!(
sourceSftpId,
task.sourcePath,
task.targetPath,
sourceEncoding,
task.id,
);
if (cancelledTasksRef.current.has(task.id)) {
throw new Error("Transfer cancelled");
}
directoryHandled = result.success;
}
if (task.isDirectory && !directoryHandled) {
// For directory transfers, parent task uses:
// totalBytes = total file count (discovered async)
// transferredBytes = completed file count (incremented by child completions)
// Child file tasks are registered in transfers array with their own byte progress.
const stagedTargetPath = task.replaceExistingTarget
? `${task.targetPath}.netcatty-${task.id.replace(/[^A-Za-z0-9_-]/g, "_")}.part`
: undefined;
const directoryTask = stagedTargetPath ? { ...task, targetPath: stagedTargetPath } : task;
if (stagedTargetPath) updateTask({ stagedTargetPath });
const dirErrors = await transferDirectory(
directoryTask,
sourceSftpId,
targetSftpId,
sourcePane.connection!.isLocal,
targetPane.connection!.isLocal,
sourceEncoding,
targetEncoding,
task.id, // rootTaskId - this is the top-level task
sameHost,
);
if (dirErrors > 0) {
dirPartialFailure = true;
} else if (stagedTargetPath) {
const bridge = netcattyBridge.require();
const backupPath = `${task.targetPath}.netcatty-${task.id.replace(/[^A-Za-z0-9_-]/g, "_")}.backup`;
if (targetPane.connection!.isLocal) {
if (!bridge.statLocal || !bridge.renameLocalFile || !bridge.deleteLocalFile) {
throw new Error("Local directory replacement is unavailable");
}
await promoteDirectoryReplacePaths({
targetPath: task.targetPath,
stagedPath: stagedTargetPath,
backupPath,
statPath: bridge.statLocal,
renamePath: bridge.renameLocalFile,
deletePath: bridge.deleteLocalFile,
});
} else if (targetSftpId) {
if (!bridge.statSftp || !bridge.renameSftp || !bridge.deleteSftp) {
throw new Error("Remote directory replacement is unavailable");
}
await promoteDirectoryReplacePaths({
targetPath: task.targetPath,
stagedPath: stagedTargetPath,
backupPath,
statPath: (candidate) => bridge.statSftp!(targetSftpId, candidate, targetEncoding),
renamePath: (source, target) => bridge.renameSftp!(targetSftpId, source, target, targetEncoding),
deletePath: (candidate) => bridge.deleteSftp!(targetSftpId, candidate, targetEncoding),
});
} else {
throw new Error("Target SFTP session missing for directory promote");
}
updateTask({ stagedTargetPath: undefined });
}
} else if (!task.isDirectory) {
await transferFile(
task,
sourceSftpId,
targetSftpId,
sourcePane.connection!.isLocal,
targetPane.connection!.isLocal,
sourceEncoding,
targetEncoding,
task.id, // rootTaskId - this is the top-level task
sameHost,
);
}
if (cancelledTasksRef.current.has(task.id)) {
throw new Error("Transfer cancelled");
}
const fallbackTask = transfersRef.current.find((candidate) => candidate.id === task.id)
?? task;
// Publish terminal lifecycle through the runtime-aware writer while the
// directory walk is still registered. A panel-only setState could be
// rejected as stale by the global transfer center and remain active at 100%.
const finalStatus = finishTransferTask(
fallbackTask,
{
partialFailure: dirPartialFailure,
cancelled: cancelledTasksRef.current.has(task.id),
},
() => {
if (pendingProgressByIdRef.current.size > 0) {
flushPendingProgress();
}
},
(canonicalTask) => {
const next = transfersRef.current.map((candidate) => (
candidate.id === task.id ? canonicalTask : candidate
));
// Terminal rows used to setState directly and leave a child frame
// queued. That frame later painted the pre-terminal ref.
paintTransfersNow(next);
},
);
// Target contents may have been cached before this transfer started,
// especially when dropping into a subdirectory like "/tmp" from its parent.
// Clear the target connection cache so the next navigation reloads fresh data.
clearCacheForConnection(task.targetConnectionId);
const targetTab = getTabByConnectionId(task.targetConnectionId);
if (targetTab) {
updateTab(targetTab.side, targetTab.tabId, (prev) => ({
...prev,
transferMutationToken: prev.transferMutationToken + 1,
}));
}
// Refresh the specific target tab, not whichever tab happens to be
// active now — focus may have switched during the transfer.
if (getParentPath(task.targetPath) === targetPane.connection!.currentPath) {
await refresh(targetSide, { tabId: targetPane.id });
}
// Clean up tracked child IDs for this transfer
activeChildIdsRef.current.delete(task.id);
const completionHandler = completionHandlersRef.current.get(task.id);
if (completionHandler) {
try {
await completionHandler({
id: task.id,
fileName: task.fileName,
originalFileName: task.originalFileName ?? task.fileName,
status: finalStatus,
});
} finally {
completionHandlersRef.current.delete(task.id);
}
}
return finalStatus;
} catch (err) {
activeChildIdsRef.current.delete(task.id);
// Check if this was a cancellation
const isCancelled = cancelledTasksRef.current.has(task.id) ||
(err instanceof Error && err.message === "Transfer cancelled");
if (isCancelled) {
// Don't update status - cancelTransfer already set it to cancelled
const completionHandler = completionHandlersRef.current.get(task.id);
if (completionHandler) {
try {
await completionHandler({
id: task.id,
fileName: task.fileName,
originalFileName: task.originalFileName ?? task.fileName,
status: "cancelled",
});
} finally {
completionHandlersRef.current.delete(task.id);
}
}
clearCancelledTask(task.id);
return "cancelled";
}
updateTask({
status: "failed",
error: unwrapSftpIpcError(err) || "Transfer failed",
endTime: Date.now(),
speed: 0,
});
const completionHandler = completionHandlersRef.current.get(task.id);
if (completionHandler) {
try {
await completionHandler({
id: task.id,
fileName: task.fileName,
originalFileName: task.originalFileName ?? task.fileName,
status: "failed",
});
} finally {
completionHandlersRef.current.delete(task.id);
}
}
return "failed";
} finally {
// Walk registry cleanup is owned by transferRuntime.runWalk.
// Drop ephemeral pool holds opened when browse was soft-closed.
// Per-file transferDirectory leases are released inside transferDirectory.
releaseWorkLeases();
}
};
const startTransfer = useCallback(
async (
sourceFiles: { name: string; isDirectory: boolean }[],
sourceSide: "left" | "right",
targetSide: "left" | "right",
options?: {
sourcePane?: SftpPane;
sourcePath?: string;
sourceConnectionId?: string;
targetPath?: string;
/**
* Create copies next to the originals instead of prompting for the
* same-name conflict. Each source name is resolved to the next free
* `name (copy)`, `name (copy 2)`, ... before the transfer starts.
* Used by the SFTP context menu's Duplicate action.
*/
duplicate?: boolean;
onTransferComplete?: (result: TransferResult) => void | Promise<void>;
},
) => {
const sourcePane = options?.sourcePane
?? (options?.sourceConnectionId ? getPaneByConnectionId(options.sourceConnectionId) : null)
?? getActivePane(sourceSide);
const targetPane = getActivePane(targetSide);
if (!sourcePane?.connection || !targetPane?.connection) return [];
const sourcePath = options?.sourcePath ?? sourcePane.connection.currentPath;
const targetPath = options?.targetPath ?? targetPane.connection.currentPath;
const sourceConnectionId = options?.sourceConnectionId ?? sourcePane.connection.id;
const batchId = crypto.randomUUID();
const usesLegacyScp = sourcePane.connection.fileProtocol === "scp" || targetPane.connection.fileProtocol === "scp";
const newTasks: TransferTask[] = [];
const skippedResults: TransferResult[] = [];
const canReusePaneMetadata = sourcePath === sourcePane.connection.currentPath;
const fileEntryMap = canReusePaneMetadata
? new Map(sourcePane.files.map(f => [f.name, f]))
: null;
// Pane listings can be stale when the file changed in a terminal or
// another session after the pane was listed (#3559). A stale undersized
// size becomes the transfer plan and silently truncates the download, so
// remote single files are always re-statted before the plan is built.
// When no live stat is possible the plan stays 0, which makes the bridge
// measure the live size instead of trusting the stale listing.
const freshRemoteMetadata = new Map<string, { size: number; lastModified: number }>();
const sourceSftpIdForPlan = sourcePane.connection.isLocal
? null
: (sftpSessionsRef.current.get(sourceConnectionId) ?? null);
const remoteSingleFiles = sourcePane.connection.isLocal
? []
: sourceFiles.filter((file) => !file.isDirectory);
const preflightFiles = remoteSingleFiles.slice(0, PREFLIGHT_STAT_MAX_FILES);
// Remote files without a verified live stat are not statted again in
// discoverTransferSize. That covers the tail past the cap, a missing
// browse session, a timeout that stops the pool, and a stat that
// returns no usable size. The plan stays unknown so the bridge measures
// the live size; the overwrite dialog still uses the pane listing.
const preflightSkippedNames = new Set<string>();
if (remoteSingleFiles.length > 0 && sourceSftpIdForPlan) {
const sourceEncoding = sourcePane.filenameEncoding || "auto";
// The bridge's statSftp awaits an unbounded SFTP callback, so a
// half-open session can leave a stat pending forever. Bound each
// preflight stat: on timeout the file keeps its 0 plan and the bridge
// measures the live size during the transfer instead of one hung stat
// blocking the entire selected batch.
const PREFLIGHT_STAT_TIMEOUT_MS = STAT_SFTP_TIMEOUT_MS;
// These stats share the browse session. Keep a few in flight, and
// after the first timeout stop claiming more files so a half-open
// session cannot run the whole selection out to 10s each.
let preflightStopped = false;
const statOneRemoteFile = async (file: { name: string; isDirectory: boolean }) => {
let timeoutTimer: ReturnType<typeof setTimeout> | undefined;
let timedOut = false;
try {
const stat = await Promise.race([
Promise.resolve(netcattyBridge.get()?.statSftp?.(
sourceSftpIdForPlan,
joinPath(sourcePath, file.name),
sourceEncoding,
)),
new Promise<undefined>((resolve) => {
timeoutTimer = setTimeout(() => {
timedOut = true;
resolve(undefined);
}, PREFLIGHT_STAT_TIMEOUT_MS);
}),
]);
if (timedOut) {
preflightStopped = true;
return;
}
if (stat && stat.type !== "symlink" && stat.sizeKnown !== false && stat.size >= 0) {
freshRemoteMetadata.set(file.name, { size: stat.size, lastModified: stat.lastModified });
}
} catch (err) {
// Fall through to a 0 plan so the bridge measures the live size.
logger.debug?.("[SFTP] Transfer plan re-stat failed; falling back to live size", err);
} finally {
if (timeoutTimer) clearTimeout(timeoutTimer);
}
};
const workers = Math.min(PREFLIGHT_STAT_CONCURRENCY, preflightFiles.length);
let preflightCursor = 0;
await Promise.allSettled(
Array.from({ length: workers }, async () => {
while (!preflightStopped && preflightCursor < preflightFiles.length) {
const file = preflightFiles[preflightCursor++];
await statOneRemoteFile(file);
}
}),
);
}
for (const file of remoteSingleFiles) {
if (!freshRemoteMetadata.has(file.name)) preflightSkippedNames.add(file.name);
}
for (const file of sourceFiles) {
// "." / ".." are navigation rows, not real entries. Duplicating one
// would resolve to an ancestor (e.g. "/") and could walk the copy into
// its own output.
if (options?.duplicate && (file.name === "." || file.name === "..")) continue;
const direction: TransferDirection =
sourcePane.connection!.isLocal && !targetPane.connection!.isLocal
? "upload"
: !sourcePane.connection!.isLocal && targetPane.connection!.isLocal
? "download"
: "remote-to-remote";
// Prefer the fresh re-stat for remote files: a cached listing size can
// be stale and undersized, which would silently truncate downloads
// (#3559). Remote files without a live stat use plan 0 so the bridge
// measures the live size. Local files also start unknown so deferred
// discovery reads their current size after edits, not the cached listing.
const fileEntry = fileEntryMap?.get(file.name);
const freshMetadata = freshRemoteMetadata.get(file.name);
const fileSize = file.isDirectory
? 0
: freshMetadata
? freshMetadata.size
: 0;
// Only keep an mtime paired with a live re-stat or directory totals.
// A file without a live stat must stay fully unknown
// (size 0 AND mtime 0 in the plan):
// discoverTransferSize and the conflict check both treat a nonzero
// sourceLastModified as proof the size is already known, so a stale
// mtime would suppress the deferred source re-stat. The overwrite
// dialog still gets the pane listing's cached entry via
// discoverTransferSize's cachedListingStatForDialog fallback.
const sourceLastModified = !file.isDirectory && !freshMetadata
? 0
: (freshMetadata?.lastModified ?? fileEntry?.lastModified ?? 0);
const nextSourcePath = joinPath(sourcePath, file.name);
const targetIsLocal = targetPane.connection!.isLocal;
// A duplicate targets the folder the item already lives in, so the
// same-name conflict is guaranteed. Resolve the "(copy)" name up front
// instead of routing through the conflict dialog: the one-click action
// must not prompt, and resolving it inside processTransfer would
// re-enter the same task while it is still marked in flight.
const duplicateTarget = options?.duplicate
? await getDuplicateTarget(
{ fileName: file.name, isDirectory: file.isDirectory, targetPath: joinPath(targetPath, file.name) },
targetPane,
targetIsLocal ? null : (sftpSessionsRef.current.get(targetPane.connection!.id) ?? null),
targetIsLocal ? "auto" : (targetPane.filenameEncoding || "auto"),
// A queued or paused copy has not created its destination on disk
// yet, so the next duplicate would pick the same name and surface
// a conflict dialog. Reserve active destinations while naming.
(candidatePath) => Boolean(findActivePathConflict(
[...sftpTransferCenterStore.getSnapshot().tasks, ...transfersRef.current, ...newTasks],
{
id: "",
targetPath: candidatePath,
targetConnectionId: targetIsLocal ? "local" : targetPane.connection!.id,
targetHostId: targetIsLocal ? undefined : targetPane.connection!.hostId,
targetHostLabel: targetIsLocal ? "Local" : targetPane.connection!.hostLabel,
isDirectory: file.isDirectory,
},
)),
)
: null;
const nextFileName = duplicateTarget?.fileName ?? file.name;
const nextTargetPath = duplicateTarget?.targetPath ?? joinPath(targetPath, file.name);
const pathTaken = findActivePathConflict(
[...sftpTransferCenterStore.getSnapshot().tasks, ...transfersRef.current, ...newTasks],
{
id: "",
targetPath: nextTargetPath,
// Normalize local pane ids to the same sentinel downloadToLocal uses.
targetConnectionId: targetIsLocal ? "local" : targetPane.connection!.id,
targetHostId: targetIsLocal ? undefined : targetPane.connection!.hostId,
targetHostLabel: targetIsLocal ? "Local" : targetPane.connection!.hostLabel,
isDirectory: file.isDirectory,
},
);
if (pathTaken) {
notifyPathConflict(pathTaken, tRef.current);
const message = pathConflictMessage(pathTaken);
const attentionTask: TransferTask = {
id: crypto.randomUUID(),
batchId,
fileName: nextFileName,
originalFileName: file.name,
sourcePath: nextSourcePath,
targetPath: nextTargetPath,
sourceConnectionId,
targetConnectionId: targetPane.connection!.id,
sourceHostId: sourcePane.connection!.isLocal ? undefined : sourcePane.connection!.hostId,
sourceHostLabel: sourcePane.connection!.isLocal ? "Local" : sourcePane.connection!.hostLabel,
targetHostId: targetIsLocal ? undefined : targetPane.connection!.hostId,
targetHostLabel: targetIsLocal ? "Local" : targetPane.connection!.hostLabel,
direction,
status: "attention",
totalBytes: fileSize,
transferredBytes: 0,
speed: 0,
startTime: Date.now(),
endTime: Date.now(),
isDirectory: file.isDirectory,
progressMode: file.isDirectory ? "files" : "bytes",
sourceLastModified,
origin: "manual",
error: message,
resumable: false,
};
newTasks.push(attentionTask);
const skipped: TransferResult = {
id: attentionTask.id,
fileName: attentionTask.fileName,
originalFileName: attentionTask.originalFileName ?? attentionTask.fileName,
status: "attention",
};
skippedResults.push(skipped);
if (options?.onTransferComplete) {
await options.onTransferComplete(skipped);
}
continue;
}
const taskId = crypto.randomUUID();
newTasks.push({
id: taskId,
batchId,
fileName: nextFileName,
originalFileName: file.name,
sourcePath: nextSourcePath,
targetPath: nextTargetPath,
sourceConnectionId,
targetConnectionId: targetPane.connection!.id,
sourceHostId: sourcePane.connection!.isLocal ? undefined : sourcePane.connection!.hostId,
sourceHostLabel: sourcePane.connection!.isLocal ? "Local" : sourcePane.connection!.hostLabel,
targetHostId: targetPane.connection!.isLocal ? undefined : targetPane.connection!.hostId,
targetHostLabel: targetPane.connection!.isLocal ? "Local" : targetPane.connection!.hostLabel,
direction,
status: "pending" as TransferStatus,
totalBytes: fileSize,
transferredBytes: 0,
speed: 0,
startTime: Date.now(),
isDirectory: file.isDirectory,
progressMode: file.isDirectory ? "files" : "bytes",
sourceLastModified,
preflightStatSkipped: preflightSkippedNames.has(file.name),
origin: "manual",
resumable: !usesLegacyScp,
pauseUnavailableReason: usesLegacyScp ? "This server uses legacy SCP; cancel and retry from the beginning instead" : undefined,
phase: file.isDirectory ? "scanning" : "transferring",
});
}
setTransfers((prev) => [...prev, ...newTasks]);
// Only enqueue runnable tasks — attention path-conflict rows stay visible
// and must not re-enter processTransfer.
const runnableTasks = newTasks.filter((task) => task.status !== "attention");
if (options?.onTransferComplete) {
for (const task of runnableTasks) {
completionHandlersRef.current.set(task.id, options.onTransferComplete);
}
}
const results = await Promise.all(runnableTasks.map(async (task): Promise<TransferResult> => {
const status = await processTransfer(task, sourcePane, targetPane, targetSide);
return {
id: task.id,
fileName: task.fileName,
originalFileName: task.originalFileName ?? task.fileName,
status,
};
}));
return [...skippedResults, ...results];
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[getActivePane, getPaneByConnectionId, getTabByConnectionId, sftpSessionsRef, updateTab],
);
const cancelTransfer = useCallback(
async (transferId: string) => {
const taskToCancel = transfersRef.current.find((task) => task.id === transferId);
// Add to cancelled set so async operations can check (local + process-global
// so walks survive panel unmount and still honor center Cancel).
cancelledTasksRef.current.add(transferId);
markTransferCancelled(transferId);
releasePausedTransfer(transferId);
globalSftpTransferScheduler.cancel(transferId);
// Keep child rows and checkpoints until the old walk has stopped. A
// partial backend failure needs a fresh re-walk, including queued jobs
// the scheduler has already rejected and will never run again.
const children = transfersRef.current.filter((task) =>
task.parentTaskId === transferId
&& !["completed", "cancelled", "failed"].includes(task.status));
const childIds = new Set([
...children.map((task) => task.id),
...(activeChildIdsRef.current.get(transferId) ?? []),
]);
for (const id of childIds) {
cancelledTasksRef.current.add(id);
releasePausedTransfer(id);
globalSftpTransferScheduler.cancel(id);
}
markTransferCancelledTree(transferId, [...childIds]);
conflictsRef.current = conflictsRef.current.filter((conflict) =>
conflict.transferId !== transferId && !childIds.has(conflict.transferId));
setConflicts(conflictsRef.current);
const ids = new Set([transferId, ...childIds]);
const retainedTasks = (taskToCancel ? [taskToCancel, ...children] : children)
.map((task) => ({ task, ...sftpTransferCenterStore.observeTaskSettlement(task) }));
const failedIds = new Set(await cancelBackendTransfers([...ids]));
await sftpTransferCenterStore.settleCancellation(
transferId, ids, failedIds, retainedTasks,
async () => {
setTransfers(sftpTransferCenterStore.getOwnerTasks(ownerId));
for (const id of ids) clearCancelledTask(id);
clearTransferCancelledTree(transferId, [...childIds]);
if (failedIds.size > 0) {
// Preserve stages while any writer could not be cancelled. The
// fresh recovery walk reuses ids only after this settlement.
const recoveryIds = [...ids];
for (let offset = 0; offset < recoveryIds.length; offset += 32) {
await Promise.all(recoveryIds.slice(offset, offset + 32).map(async (id) => {
try { await netcattyBridge.get()?.clearPendingTransferCancel?.(id); } catch { /* best-effort */ }
}));
}
return;
}
if (taskToCancel && sftpTransferCenterStore.getTask(transferId)?.status === "cancelled") {
try { await cleanupTaskArtifacts(taskToCancel); } catch { /* best-effort */ }
}
for (const child of children) {
if (sftpTransferCenterStore.getTask(child.id)?.status === "completed") continue;
try { await cleanupTaskArtifacts(child); } catch { /* best-effort */ }
}
},
);
},
[cancelBackendTransfers, clearCancelledTask, cleanupTaskArtifacts, ownerId, releasePausedTransfer, setTransfers],
);
// All controls go through the process-global runtime, including cancellation
// settlement. A panel must never clear cancellation latches on Resume.
const resumeTransfer = useCallback(async (transferId: string) => {
await transferRuntime.resume(transferId);
}, []);
const prioritizeTransfer = useCallback((transferId: string) => {
globalSftpTransferScheduler.prioritize(transferId);
void netcattyBridge.get()?.prioritizeTransfer?.(transferId);
setTransfers((prev) => {
const nextPriority = prev.reduce((max, task) => Math.max(max, task.priority ?? 0), 0) + 1;
return prev.map((task) => task.id === transferId ? { ...task, priority: nextPriority } : task);
});
}, [setTransfers]);
const retryTransfer = useCallback(
async (transferId: string) => {
// Prefer the live owner list; fall back to the center store (drag-drop
// children are upserted there and mirrored via subscribe).
const task = transfersRef.current.find((t) => t.id === transferId)
?? sftpTransferCenterStore.getTask(transferId);
if (!task || task.retryable === false) return;
// Progressive / external drag-drop children never have dual-pane endpoints
// (sourceConnectionId is "external"). Re-run startStreamTransfer in place.
if (isExternalDragDropFileUpload(task)) {
if (inFlightTransferIdsRef.current.has(transferId)) return;
inFlightTransferIdsRef.current.add(transferId);
clearTransferCancelled(transferId);
cancelledTasksRef.current.delete(transferId);
try {
const result = await retryExternalDragDropFileUpload(task, {
getBrowseSftpId: (connectionId) => sftpSessionsRef.current.get(connectionId),
acquireTransferSession: acquireTransferSessionRef.current
? (hostId, id) => acquireTransferSessionLive(hostId, id)
: undefined,
startStreamTransfer: async (options) => {
const bridge = netcattyBridge.get();
if (!bridge?.startStreamTransfer) {
return { error: "Stream transfer is unavailable" };
}
return bridge.startStreamTransfer(options);
},
clearPendingCancel: (id) => netcattyBridge.get()?.clearPendingTransferCancel?.(id),
cleanupArtifacts: cleanupTaskArtifacts,
getTask: (id) => sftpTransferCenterStore.getTask(id)
?? transfersRef.current.find((row) => row.id === id),
getChildTasks: (parentId) => {
const fromStore = sftpTransferCenterStore.getOwnerTasks(ownerId)
.filter((row) => row.parentTaskId === parentId);
if (fromStore.length > 0) return fromStore;
return transfersRef.current.filter((row) => row.parentTaskId === parentId);
},
onPatch: (taskId, updates) => {
transferRuntime.patchTask(taskId, updates);
setTransfers((prev) => {
if (!prev.some((row) => row.id === taskId)) {
// Store-only child: mirror the patch by re-pulling owner rows.
return sftpTransferCenterStore.getOwnerTasks(ownerId);
}
return prev.map((row) => (row.id === taskId ? { ...row, ...updates } : row));
});
},
});
if (!result.success && result.error && !/cancelled/i.test(result.error)) {
notify.warning(result.error, "SFTP");
}
} finally {
inFlightTransferIdsRef.current.delete(transferId);
}
return;
}
await cleanupTaskArtifacts(task);
const retriedTask: TransferTask = {
...task,
id: crypto.randomUUID(),
status: "pending" as TransferStatus,
error: undefined,
transferredBytes: 0,
checkpointBytes: 0,
resumeStage: undefined,
downloadCheckpointBytes: undefined,
uploadCheckpointBytes: undefined,
directoryResumeCheckpoint: undefined,
speed: 0,
startTime: Date.now(),
endTime: undefined,
lifecycleEpoch: undefined,
};
const endpoints = resolveTaskEndpoints(task);
if (!endpoints) {
notify.warning(
"Could not resolve transfer endpoints for retry. Reconnect the target and try again.",
"SFTP",
);
return;
}
const { targetSide, sourcePane, targetPane } = endpoints;
const completionHandler = completionHandlersRef.current.get(transferId);
if (completionHandler) {
completionHandlersRef.current.set(retriedTask.id, completionHandler);
completionHandlersRef.current.delete(transferId);
}
setTransfers((prev) =>
prev.map((t) =>
t.id === transferId
? retriedTask
: t,
),
);
await processTransfer(retriedTask, sourcePane, targetPane, targetSide);
},
// eslint-disable-next-line react-hooks/exhaustive-deps -- processTransfer is defined inline
[acquireTransferSession, cleanupTaskArtifacts, ownerId, resolveTaskEndpoints, setTransfers, sftpSessionsRef],
);
const clearCompletedTransfers = useCallback(() => {
setTransfers((prev) => {
const unfinishedParents = new Set(
prev.filter((t) => !["completed", "cancelled", "failed"].includes(t.status)).map((t) => t.id),
);
// Keep non-compacted terminal exceptions of unfinished directory parents;
// compacted completions already live in the bounded parent checkpoint.
return prev.filter((t) => {
if (t.parentTaskId && unfinishedParents.has(t.parentTaskId)) return true;
return t.status !== "completed" && t.status !== "cancelled";
});
});
}, [setTransfers]);
const dismissTransfer = useCallback((transferId: string) => {
const task = transfersRef.current.find((candidate) => candidate.id === transferId);
if (task) void cleanupTaskArtifacts(task);
setTransfers((prev) => prev.filter((t) => t.id !== transferId && t.parentTaskId !== transferId));
}, [cleanupTaskArtifacts, setTransfers]);
const dismissTransfers = useCallback((prunedTasks: readonly TransferTask[]) => {
if (prunedTasks.length === 0) return;
const removing = new Set(prunedTasks.map((task) => task.id));
const artifactTasks = prunedTasks.filter((task) => task.status !== "completed");
// This callback is used only for automatic history eviction. Completed
// streams already cleaned their staging files; sending one cleanup IPC per
// pruned child recreates a main-process event storm at folder completion.
setTransfers((prev) => prev.filter((task) => (
!removing.has(task.id) && !removing.has(task.parentTaskId ?? "")
)));
// Failed/cancelled transfers can retain resumable staging files. Clean them
// sequentially in the background so a large eviction never floods IPC.
if (artifactTasks.length > 0) {
void (async () => {
for (const task of artifactTasks) {
try {
await cleanupTaskArtifacts(task);
} catch {
// best-effort history cleanup
}
}
})();
}
}, [cleanupTaskArtifacts, setTransfers]);
const isTransferCancelled = useCallback((transferId: string) => {
return cancelledTasksRef.current.has(transferId) || isTransferCancelledFlag(transferId);
}, []);
const resolveConflict = useCallback(
async (conflictId: string, action: FileConflictAction, applyToAll = false) => {
if (cancelledTasksRef.current.has(conflictId)) return;
const conflict = conflictsRef.current.find((c) => c.transferId === conflictId);
if (!conflict) return;
const task = transfersRef.current.find((t) => t.id === conflictId);
if (!task) {
// Dialog can show a store-owned conflict before the panel adopts the row.
// Clearing only React state leaves attention+conflict in localStorage and
// the prompt returns on the next SFTP open (issue #2636).
conflictsRef.current = conflictsRef.current.filter((c) => c.transferId !== conflictId);
setConflicts(conflictsRef.current);
if (action === "stop" || action === "skip") {
sftpTransferCenterStore.patchTask(conflictId, {
status: "cancelled",
conflict: undefined,
endTime: Date.now(),
speed: 0,
error: undefined,
});
}
return;
}
const selectedConflictKey = conflictDefaultKey(conflict.batchId, conflict.isDirectory, conflict.existingType);
const affectedConflicts = applyToAll
? conflictsRef.current.filter((candidate) =>
conflictDefaultKey(candidate.batchId, candidate.isDirectory, candidate.existingType) === selectedConflictKey,
)
: [conflict];
const affectedConflictIds = new Set(affectedConflicts.map((candidate) => candidate.transferId));
const affectedTasks = affectedConflicts
.map((candidate) => transfersRef.current.find((transfer) => transfer.id === candidate.transferId))
.filter((candidate): candidate is TransferTask => Boolean(candidate));
const affectedConflictById = new Map<string, FileConflict>(
affectedConflicts.map((candidate): [string, FileConflict] => [candidate.transferId, candidate]),
);
if (applyToAll) {
const batchId = conflict.batchId ?? "global";
const batchDefaults = conflictDefaultsRef.current.get(batchId) ?? new Map<string, FileConflictAction>();
batchDefaults.set(getSftpConflictTypeKey(conflict.isDirectory, conflict.existingType), action);
conflictDefaultsRef.current.set(batchId, batchDefaults);
}
// Eagerly clear refs so a double-click cannot schedule two processTransfer calls.
conflictsRef.current = conflictsRef.current.filter((c) => !affectedConflictIds.has(c.transferId));
setConflicts(conflictsRef.current);
if (affectedTasks.length === 0) {
return;
}
if (action === "stop") {
await markBatchStopped(task);
return;
}
if (action === "skip") {
for (const affectedTask of affectedTasks) {
cancelledTasksRef.current.add(affectedTask.id);
}
// Drop conflict so remount / restart cannot rehydrate the dialog from a
// cancelled row that still carries conflict metadata.
setTransfers((prev) =>
prev.map((t) => affectedConflictIds.has(t.id)
? {
...t,
status: "cancelled" as TransferStatus,
endTime: Date.now(),
conflict: undefined,
error: undefined,
speed: 0,
}
: t,
),
);
for (const affectedTask of affectedTasks) {
await completeCancelledTask(affectedTask);
}
return;
}
const updatedTasks: TransferTask[] = [];
const blockedReplaceTasks: Array<{ task: TransferTask; conflict: FileConflict }> = [];
const failedReplaceTasks: Array<{ task: TransferTask; error: string }> = [];
for (const affectedTask of affectedTasks) {
if (cancelledTasksRef.current.has(affectedTask.id)) continue;
let updatedTask = { ...affectedTask };
const affectedConflict = affectedConflictById.get(affectedTask.id);
if (action === "duplicate") {
const endpoints = resolveTaskEndpoints(affectedTask);
if (!endpoints) continue;
const targetSftpId = endpoints.targetPane.connection?.isLocal
? null
: sftpSessionsRef.current.get(endpoints.targetPane.connection!.id) ?? null;
const targetEncoding = endpoints.targetPane.connection?.isLocal
? "auto"
: endpoints.targetPane.filenameEncoding || "auto";
const duplicateTarget = await getDuplicateTarget(affectedTask, endpoints.targetPane, targetSftpId, targetEncoding);
if (cancelledTasksRef.current.has(affectedTask.id)) continue;
updatedTask = {
...affectedTask,
fileName: duplicateTarget.fileName,
targetPath: duplicateTarget.targetPath,
skipConflictCheck: true,
};
} else if (action === "replace") {
if (
affectedConflict &&
!canReplaceSftpConflict(affectedTask.isDirectory, affectedConflict.existingType)
) {
blockedReplaceTasks.push({ task: affectedTask, conflict: affectedConflict });
continue;
}
if (
affectedConflict
&& shouldUnlinkSftpConflictBeforeReplace(affectedConflict.existingType)
) {
const endpoints = resolveTaskEndpoints(affectedTask);
if (!endpoints?.targetPane.connection) continue;
const targetSftpId = endpoints.targetPane.connection.isLocal
? null
: sftpSessionsRef.current.get(endpoints.targetPane.connection.id) ?? null;
const targetEncoding = endpoints.targetPane.connection.isLocal
? "auto"
: endpoints.targetPane.filenameEncoding || "auto";
try {
await deleteTargetPath(
affectedTask,
endpoints.targetPane,
targetSftpId,
targetEncoding,
"symlink",
);
} catch (error) {
failedReplaceTasks.push({
task: affectedTask,
error: error instanceof Error ? error.message : String(error),
});
continue;
}
}
updatedTask = {
...affectedTask,
skipConflictCheck: true,
replaceExistingTarget: true,
};
} else if (action === "merge") {
updatedTask = {
...affectedTask,
skipConflictCheck: true,
replaceExistingTarget: false,
};
}
updatedTasks.push(updatedTask);
}
if (blockedReplaceTasks.length > 0) {
const blockedTaskIds = new Set(blockedReplaceTasks.map(({ task }) => task.id));
const blockedErrors = new Map(
blockedReplaceTasks.map(({ task, conflict }) => [
task.id,
buildReplaceTypeMismatchError(task.isDirectory, conflict.existingType, task.targetPath),
]),
);
setTransfers((prev) =>
prev.map((t) => blockedTaskIds.has(t.id)
? {
...t,
status: "failed" as TransferStatus,
endTime: Date.now(),
error: blockedErrors.get(t.id),
retryable: false,
conflict: undefined,
}
: t,
),
);
}
if (failedReplaceTasks.length > 0) {
const failedErrors = new Map(
failedReplaceTasks.map(({ task: failedTask, error }) => [failedTask.id, error]),
);
setTransfers((prev) =>
prev.map((candidate) => failedErrors.has(candidate.id)
? {
...candidate,
status: "failed" as TransferStatus,
endTime: Date.now(),
error: failedErrors.get(candidate.id),
conflict: undefined,
}
: candidate,
),
);
}
const liveUpdatedTasks = updatedTasks.filter((candidate) => !cancelledTasksRef.current.has(candidate.id));
const updatedTaskMap = new Map(liveUpdatedTasks.map((updatedTask) => [updatedTask.id, updatedTask]));
setTransfers((prev) =>
prev.map((t) => {
if (t.status === "cancelled" || cancelledTasksRef.current.has(t.id)) return t;
const updatedTask = updatedTaskMap.get(t.id);
return updatedTask
? { ...updatedTask, status: "pending" as TransferStatus, conflict: undefined }
: t;
}),
);
for (const updatedTask of liveUpdatedTasks) {
const identity = captureDeferredTransferAttempt(
updatedTask,
ownerId,
connectionCacheKeyMapRef.current,
);
const resolveCurrentAttempt = () => {
const current = transfersRef.current.find((candidate) => candidate.id === updatedTask.id);
if (
cancelledTasksRef.current.has(updatedTask.id)
|| !isDeferredTransferAttemptCurrent(current, identity, connectionCacheKeyMapRef.current)
) return null;
const endpoints = resolveTaskEndpoints(current!);
if (!endpoints?.sourcePane.connection || !endpoints.targetPane.connection) return null;
const sourceMatches = current!.sourceConnectionId === "local"
? endpoints.sourcePane.connection.isLocal
: endpoints.sourcePane.connection.id === current!.sourceConnectionId;
const targetMatches = current!.targetConnectionId === "local"
? endpoints.targetPane.connection.isLocal
: endpoints.targetPane.connection.id === current!.targetConnectionId;
if (!sourceMatches || !targetMatches) return null;
return { current: current!, endpoints };
};
deferredConflictAttemptsRef.current?.schedule(
updatedTask.id,
100,
() => resolveCurrentAttempt() !== null,
async () => {
const attempt = resolveCurrentAttempt();
if (!attempt) return;
await processTransfer(
attempt.current,
attempt.endpoints.sourcePane,
attempt.endpoints.targetPane,
attempt.endpoints.targetSide,
);
},
);
}
},
// eslint-disable-next-line react-hooks/exhaustive-deps -- processTransfer is defined inline; transfers/conflicts accessed via refs
[
completeCancelledTask,
connectionCacheKeyMapRef,
conflictDefaultKey,
getDuplicateTarget,
markBatchStopped,
ownerId,
resolveTaskEndpoints,
sftpSessionsRef,
],
);
// Only work that is still moving or queued — paused rows must not keep the
// header "N active" lit or pause feels like it did nothing.
const activeTransfersCount = useMemo(() => transfers.filter(
(t) => !t.parentTaskId
&& ["pending", "queued", "transferring", "pausing"].includes(t.status),
).length, [transfers]);
const downloadToLocal = useCallback(
async (params: {
fileName: string;
sourcePath: string;
targetPath: string;
sftpId: string;
connectionId: string;
sourceHostId: string;
sourceHostLabel: string;
sourceEncoding?: SftpFilenameEncoding;
isDirectory: boolean;
totalBytes?: number;
}): Promise<TransferStatus> => {
// Same destination concurrent writers corrupt .part / final files.
// Must pass the local endpoint identity — createDirectDownloadTransferTask
// stores targetConnectionId:"local"; a bare { targetPath } never matches
// isLocalTransferDestination and silently skips dedupe (double download).
const downloadDest = {
targetPath: params.targetPath,
targetConnectionId: "local" as const,
targetHostLabel: "Local",
isDirectory: params.isDirectory,
};
const conflict = findActivePathConflict(
[...sftpTransferCenterStore.getSnapshot().tasks, ...transfersRef.current],
{ id: "", ...downloadDest },
);
if (conflict) {
notifyPathConflict(conflict, tRef.current);
return conflict.status === "completed" ? "completed" : "attention";
}
const task = createDirectDownloadTransferTask({
id: crypto.randomUUID(),
fileName: params.fileName,
sourcePath: params.sourcePath,
targetPath: params.targetPath,
sourceConnectionId: params.connectionId,
sourceHostId: params.sourceHostId,
sourceHostLabel: params.sourceHostLabel,
totalBytes: params.totalBytes ?? 0,
isDirectory: params.isDirectory,
});
// Reserve the path in transfersRef before any await so concurrent
// downloadToLocal calls (double-click) see each other.
setTransfers((prev) => {
const raced = findActivePathConflict(
[...sftpTransferCenterStore.getSnapshot().tasks, ...prev],
{ id: task.id, ...downloadDest },
);
if (raced) return prev;
return [...prev, task];
});
if (!transfersRef.current.some((row) => row.id === task.id)) {
notifyPathConflict(
findActivePathConflict(
[...sftpTransferCenterStore.getSnapshot().tasks, ...transfersRef.current],
{ id: "", ...downloadDest },
) ?? task,
tRef.current,
);
return "attention";
}
const patchDownload = (updates: Partial<TransferTask>) => {
transferRuntime.patchTask(task.id, updates);
const canonical = transferRuntime.getTask(task.id);
if (canonical) setTransfers((prev) => prev.map((row) => row.id === task.id ? canonical : row));
};
const executeDownload = async (): Promise<TransferStatus> => {
const sourceEncoding = params.sourceEncoding ?? "auto";
// Mutable counter to track child failures outside React state,
// so the final status check doesn't depend on render timing.
let childFailureCount = 0;
// Dedicated pool only when host id is known — never fall back to browse
// (tab close would kill the download and freeze the global center).
let sourceWorkLease: TransferConnectionLease | null = null;
let workingSourceSftpId = params.sftpId;
try {
const acquireDownloadSession = acquireTransferSessionRef.current;
if (acquireDownloadSession && params.sourceHostId) {
sourceWorkLease = await acquireDownloadSession(
params.sourceHostId,
`${task.id}:work-source`,
);
workingSourceSftpId = sourceWorkLease.sftpId;
}
await waitUntilTransferResumed(task.id);
if (cancelledTasksRef.current.has(task.id)) throw new Error("Transfer cancelled");
if (params.isDirectory) patchDownload({ status: "transferring", error: undefined });
if (params.isDirectory) {
childFailureCount = await transferDirectory(
task,
workingSourceSftpId,
null, // targetSftpId = null (local)
false, // sourceIsLocal = false
true, // targetIsLocal = true
sourceEncoding,
"auto", // targetEncoding
task.id,
false, // sameHost
0, // symlinkDepth
true, // followSymlinks — download should expand symlink dirs
);
} else {
await transferFile(
task,
workingSourceSftpId,
null,
false,
true,
sourceEncoding,
"auto",
task.id,
);
}
// Runtime owns lifecycle while the walk is registered. Publish the
// final root there before mirroring it to the panel.
const rows = sftpTransferCenterStore.getSnapshot().tasks;
const liveParent = transferRuntime.getTask(task.id) ?? task;
const completedCount = (liveParent.directoryResumeCheckpoint?.completedEntries ?? 0)
+ rows.filter((row) => row.parentTaskId === task.id && row.status === "completed").length;
const resolved = resolveDirectDirectoryDownloadFinalStatus({
parentCancelled: liveParent.status === "cancelled" || cancelledTasksRef.current.has(task.id),
childFailureCount,
});
const finalTotal = liveParent.totalBytes > 0 ? liveParent.totalBytes : completedCount;
patchDownload({
status: resolved.status,
error: resolved.error,
endTime: Date.now(),
speed: 0,
...(resolved.status === "cancelled" ? {} : {
totalBytes: finalTotal,
transferredBytes: resolved.status === "failed" ? completedCount : finalTotal,
}),
});
const appliedStatus = transferRuntime.getTask(task.id)?.status ?? resolved.status;
activeChildIdsRef.current.delete(task.id);
return appliedStatus;
} catch (err) {
activeChildIdsRef.current.delete(task.id);
const isCancelled = cancelledTasksRef.current.has(task.id);
// Clean up cancelled task tracking to prevent memory leak
if (isCancelled) cancelledTasksRef.current.delete(task.id);
const errMsg = unwrapSftpIpcError(err);
patchDownload({
status: isCancelled ? "cancelled" : "failed",
error: isCancelled ? undefined : errMsg,
endTime: Date.now(),
speed: 0,
});
return isCancelled ? "cancelled" : "failed";
} finally {
sourceWorkLease?.release();
sourceWorkLease = null;
}
};
let result: TransferStatus = "failed";
await runTrackedTransferAttempt(inFlightTransferIdsRef.current, task.id, () =>
transferRuntime.runWalk(task.id, async () => {
result = await executeDownload();
}),
);
return result;
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[sftpSessionsRef, acquireTransferSession],
);
// Publish only on owner change / mount. Do NOT re-publish on every `transfers`
// React state change: progress flushes use setTransfersState without
// publishOwner, and a transfers→publishOwner effect reintroduced the full
// store merge + emit on every tick (hidden retained SFTP panels still
// remount useSftpTransfers after close).
// Lifecycle writers already call setTransfers → publishOwner.
useEffect(() => {
sftpTransferCenterStore.publishOwner(ownerId, transfersRef.current);
}, [ownerId]);
// The store is the only transfer-task authority. Lifecycle changes update the
// panel list; byte-only ticks update refs without repainting the whole panel.
useEffect(() => {
return sftpTransferCenterStore.subscribe(() => {
const previous = transfersRef.current;
const ownerRows = sftpTransferCenterStore.getSnapshot().tasks.filter((task) => task.ownerId === ownerId);
transfersRef.current = ownerRows;
if (shouldPaintTransferControlNow(previous, ownerRows)) {
paintTransfersNow(ownerRows);
return;
}
scheduleTransfersStateFlush();
});
}, [ownerId, paintTransfersNow, scheduleTransfersStateFlush]);
useEffect(() => sftpTransferCenterStore.subscribeProgress(() => {
const liveById = new Map(sftpTransferCenterStore.getSnapshot().tasks.map((task) => [task.id, task]));
transfersRef.current = transfersRef.current.map((task) => liveById.get(task.id) ?? task);
}), []);
const resolveAdoptionPanes = useCallback((task: TransferTask) => {
const panes = [getActivePane("left"), getActivePane("right")].filter((pane): pane is SftpPane => !!pane?.connection);
const sourcePane = panes.find((pane) => task.sourceHostId
? pane.connection?.hostId === task.sourceHostId
: pane.connection?.isLocal);
const targetPane = panes.find((pane) => task.targetHostId
? pane.connection?.hostId === task.targetHostId
: pane.connection?.isLocal);
// Downloads to local only need the remote source; local pane is optional for
// matching but preferred so processTransfer can run dual-endpoint checks.
if (sourcePane && targetPane) return { sourcePane, targetPane };
if (sourcePane && task.sourceHostId && !task.targetHostId) {
const localPane = panes.find((pane) => pane.connection?.isLocal);
if (localPane) return { sourcePane, targetPane: localPane };
}
if (targetPane && task.targetHostId && !task.sourceHostId) {
const localPane = panes.find((pane) => pane.connection?.isLocal);
if (localPane) return { sourcePane: localPane, targetPane };
}
return null;
}, [getActivePane]);
const adoptInterruptedTransfer = useCallback(async (task: TransferTask) => {
const panes = resolveAdoptionPanes(task);
if (!panes?.sourcePane.connection || !panes.targetPane.connection) return;
// Keep checkpoint / fingerprint fields so resume restarts from the saved byte offset.
const hasConflict = !!task.conflict;
const isDirectoryResume = !!task.isDirectory && !hasConflict;
const adoptedTask: TransferTask = {
...task,
ownerId,
sourceConnectionId: panes.sourcePane.connection.id,
targetConnectionId: panes.targetPane.connection.isLocal
? (task.targetConnectionId === "local" ? "local" : panes.targetPane.connection.id)
: panes.targetPane.connection.id,
// Conflict rows stay in attention until resolveConflict applies the action.
// Other reconnects stay "pending" + reconnectRequired for the spinner.
// Directory resume: skip re-prompting conflict when the target already
// exists from the first attempt (merge/continue semantics).
status: hasConflict ? "attention" : "pending",
reconnectRequired: !hasConflict,
error: hasConflict ? task.error : undefined,
speed: 0,
// Preserve replace/staging flags so resume keeps the original conflict
// choice; only force skipConflict when continuing a directory resume.
skipConflictCheck: isDirectoryResume ? true : task.skipConflictCheck,
replaceExistingTarget: isDirectoryResume
? !!task.replaceExistingTarget
: task.replaceExistingTarget,
stagedTargetPath: task.stagedTargetPath,
};
// Re-home orphaned children under this parent so directory resume can skip
// already-completed files using persisted checkpoints (same ownerId).
const storeChildren = sftpTransferCenterStore.getSnapshot().tasks.filter(
(candidate) => candidate.parentTaskId === task.id,
);
const rehomedChildren = storeChildren.map((child) => ({
...child,
ownerId,
status: (child.status === "completed" || child.status === "cancelled" || child.status === "failed")
? child.status
: "interrupted" as TransferStatus,
reconnectRequired: child.status !== "completed" && child.status !== "cancelled" && child.status !== "failed",
}));
const nextTransfers = [
...transfersRef.current.filter(
(candidate) => candidate.id !== task.id && candidate.parentTaskId !== task.id,
),
adoptedTask,
...rehomedChildren,
];
setTransfers(nextTransfers);
if (task.conflict) {
// Keep conflictsRef in sync immediately — store.resolveConflict may call
// controller.resolveConflict in the same turn before React re-renders.
const nextConflicts = [
...conflictsRef.current.filter((item) => item.transferId !== task.id),
task.conflict,
];
conflictsRef.current = nextConflicts;
setConflicts(nextConflicts);
// Do not auto-resume — caller will apply the chosen conflict action.
return;
}
// Cancel may have won while we were rehoming ownership.
if (cancelledTasksRef.current.has(task.id)) return;
// Force resumeTransfer to accept pending (reconnect path) statuses.
await resumeTransfer(task.id);
}, [ownerId, resolveAdoptionPanes, resumeTransfer, setTransfers]);
const syncOwnedTasksFromStore = useCallback(() => {
const snap = sftpTransferCenterStore.getSnapshot().tasks;
setTransfers((prev) => {
let changed = false;
const next = prev.map((row) => {
const storeRow = snap.find((candidate) => candidate.id === row.id);
if (!storeRow) return row;
if (
storeRow.status === row.status
&& storeRow.speed === row.speed
&& (storeRow.checkpointBytes ?? 0) === (row.checkpointBytes ?? 0)
&& (storeRow.transferredBytes ?? 0) === (row.transferredBytes ?? 0)
&& storeRow.pauseUnavailableReason === row.pauseUnavailableReason
&& storeRow.reconnectRequired === row.reconnectRequired
&& storeRow.error === row.error
&& storeRow.lifecycleEpoch === row.lifecycleEpoch
) {
return row;
}
changed = true;
return {
...row,
status: storeRow.status,
speed: storeRow.speed,
checkpointBytes: storeRow.checkpointBytes,
transferredBytes: storeRow.transferredBytes,
pauseUnavailableReason: storeRow.pauseUnavailableReason,
reconnectRequired: storeRow.reconnectRequired,
error: storeRow.error,
phase: storeRow.phase,
resumeStage: storeRow.resumeStage,
downloadCheckpointBytes: storeRow.downloadCheckpointBytes,
uploadCheckpointBytes: storeRow.uploadCheckpointBytes,
sourceFingerprint: storeRow.sourceFingerprint,
lifecycleEpoch: storeRow.lifecycleEpoch,
};
});
return changed ? next : prev;
});
}, [setTransfers]);
useEffect(() => sftpTransferCenterStore.registerOwner(ownerId, {
// Soft pause/resume: TransferRuntime (process-global). Owner registration is
// view/sync + cancel/retry/adopt only — not control authority for live walks.
pause: async (taskId: string) => {
await transferRuntime.pause(taskId);
},
resume: async (taskId: string) => {
await resumeTransfer(taskId);
},
cancel: cancelTransfer,
retry: retryTransfer,
prioritize: prioritizeTransfer,
dismiss: dismissTransfer,
dismissMany: dismissTransfers,
resolveConflict,
// Origin/local listing only — soft pause/resume must not key on this.
ownsTask: (taskId: string) => transfersRef.current.some(
(row) => row.id === taskId || row.parentTaskId === taskId,
),
syncOwnedTasks: syncOwnedTasksFromStore,
canAdopt: (task) => resolveAdoptionPanes(task) !== null,
canPrepareAdoption,
adopt: adoptInterruptedTransfer,
}), [adoptInterruptedTransfer, canPrepareAdoption, cancelTransfer, dismissTransfer, dismissTransfers, ownerId, prioritizeTransfer, resolveAdoptionPanes, resolveConflict, resumeTransfer, retryTransfer, syncOwnedTasksFromStore]);
return {
transfers,
conflicts,
activeTransfersCount,
startTransfer,
downloadToLocal,
cancelTransfer: transferRuntime.cancel,
// Single process-level control surface (TransferRuntime).
pauseTransfer: async (transferId: string) => {
await transferRuntime.pause(transferId);
},
resumeTransfer,
prioritizeTransfer,
isTransferCancelled,
retryTransfer,
clearCompletedTransfers,
dismissTransfer,
resolveConflict,
};
};