@pyrokine/mcp-ssh 3.1.0 → 3.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +395 -112
- package/README_zh.md +211 -45
- package/dist/command-risk.js +5 -2
- package/dist/directory-verification.d.ts +10 -5
- package/dist/directory-verification.js +161 -74
- package/dist/file-mode.js +25 -13
- package/dist/file-ops.d.ts +22 -11
- package/dist/file-ops.js +605 -230
- package/dist/forward-manager.js +6 -1
- package/dist/known-hosts.d.ts +22 -0
- package/dist/known-hosts.js +563 -0
- package/dist/openssh.d.ts +5 -5
- package/dist/openssh.js +138 -59
- package/dist/operation-manager.js +13 -4
- package/dist/pty-manager.d.ts +2 -1
- package/dist/pty-manager.js +81 -19
- package/dist/remote-platform.d.ts +1 -1
- package/dist/remote-platform.js +82 -50
- package/dist/security.d.ts +2 -0
- package/dist/security.js +39 -3
- package/dist/session-manager.d.ts +29 -3
- package/dist/session-manager.js +367 -67
- package/dist/sftp-timeout.d.ts +16 -1
- package/dist/sftp-timeout.js +114 -12
- package/dist/ssh-config.js +1 -1
- package/dist/tools/connection.js +31 -13
- package/dist/tools/exec.js +147 -71
- package/dist/tools/file.js +333 -146
- package/dist/tools/operation.js +7 -5
- package/dist/tools/pty.js +16 -13
- package/dist/tools/utils.d.ts +1 -1
- package/dist/tools/utils.js +2 -2
- package/dist/transfer-outcome.js +1 -1
- package/dist/types.d.ts +24 -4
- package/dist/types.js +1 -0
- package/dist/utf8.d.ts +1 -1
- package/dist/utf8.js +2 -2
- package/dist/windows-acl.d.ts +1 -0
- package/dist/windows-acl.js +167 -80
- package/package.json +6 -6
package/dist/file-ops.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SSH File Operations - 文件操作
|
|
3
3
|
*/
|
|
4
|
-
import { spawn } from 'child_process';
|
|
5
|
-
import { OpenSshError } from './openssh.js';
|
|
4
|
+
import { spawn, spawnSync } from 'child_process';
|
|
6
5
|
import { createHash, randomBytes } from 'crypto';
|
|
7
6
|
import * as fs from 'fs';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
9
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
8
10
|
import * as os from 'os';
|
|
9
11
|
import * as path from 'path';
|
|
10
|
-
import {
|
|
12
|
+
import { Transform } from 'stream';
|
|
11
13
|
import { matchesDirectoryExclude } from './directory-verification.js';
|
|
12
14
|
import { extractFileMode, FILE_CREATE_MODE_MASK, formatFileMode, formatFilePermissions } from './file-mode.js';
|
|
15
|
+
import { OpenSshError } from './openssh.js';
|
|
16
|
+
import { encodedPowershell, remoteDirname, remoteJoin, remoteNormalize, remotePathModule, remoteShellPath, windowsCaseSemanticsScript, } from './remote-platform.js';
|
|
13
17
|
import { sessionManager } from './session-manager.js';
|
|
14
|
-
import { SftpOperationTimeoutError, withSftpOperationTimeout } from './sftp-timeout.js';
|
|
18
|
+
import { sftpOperationOutcome, SftpOperationTimeoutError, SftpWriteState, withSftpOperationTimeout, } from './sftp-timeout.js';
|
|
15
19
|
import { escapeShellArg, expandTilde } from './tools/utils.js';
|
|
16
|
-
import { encodedPowershell, remoteDirname, remoteJoin, remoteNormalize, remotePathModule, remoteShellPath, windowsCaseSemanticsScript, } from './remote-platform.js';
|
|
17
20
|
import { ensureUtf8Boundary, isUtf8ContinuationByte } from './utf8.js';
|
|
21
|
+
import { checkWindowsAcl, windowsAclScript } from './windows-acl.js';
|
|
18
22
|
// 文件类型 mode 常量
|
|
19
23
|
const S_IFMT = 0o170000; // 文件类型位掩码
|
|
20
24
|
const S_IFDIR = 0o40000;
|
|
@@ -32,41 +36,65 @@ function appendLimitedOutput(current, chunk) {
|
|
|
32
36
|
}
|
|
33
37
|
return (current + chunk).slice(0, LOCAL_PROCESS_OUTPUT_LIMIT);
|
|
34
38
|
}
|
|
35
|
-
function runLocalProcess(command, args, timeout, env, connection) {
|
|
39
|
+
function runLocalProcess(command, args, timeout, env, connection, onStdout, signal) {
|
|
36
40
|
return new Promise((resolve) => {
|
|
37
41
|
let stdout = '';
|
|
38
42
|
let stderr = '';
|
|
43
|
+
const stdoutDecoder = new StringDecoder('utf8');
|
|
44
|
+
let stdoutTruncated = false;
|
|
39
45
|
let settled = false;
|
|
40
46
|
let timedOut = false;
|
|
47
|
+
let cancelled = false;
|
|
48
|
+
throwIfAborted(signal);
|
|
41
49
|
let killTimer;
|
|
42
50
|
connection?.assertCurrent();
|
|
43
51
|
const child = spawn(command, args, {
|
|
44
52
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
45
53
|
env: env ?? process.env,
|
|
46
54
|
windowsHide: true,
|
|
47
|
-
detached:
|
|
55
|
+
detached: process.platform !== 'win32',
|
|
48
56
|
});
|
|
49
|
-
connection?.trackProcess(child,
|
|
57
|
+
connection?.trackProcess(child, process.platform !== 'win32');
|
|
50
58
|
const kill = (signal) => {
|
|
51
|
-
if (
|
|
59
|
+
if (process.platform !== 'win32' && child.pid) {
|
|
52
60
|
try {
|
|
53
61
|
process.kill(-child.pid, signal);
|
|
54
62
|
}
|
|
55
63
|
catch (error) {
|
|
56
|
-
if (error.code !== 'ESRCH')
|
|
64
|
+
if (error.code !== 'ESRCH') {
|
|
57
65
|
console.error('Local process cleanup failed:', error);
|
|
66
|
+
}
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
|
-
else
|
|
69
|
+
else {
|
|
61
70
|
child.kill(signal);
|
|
71
|
+
}
|
|
62
72
|
};
|
|
63
|
-
const
|
|
64
|
-
|
|
73
|
+
const stop = () => {
|
|
74
|
+
if (settled || killTimer) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
65
77
|
kill('SIGTERM');
|
|
66
78
|
killTimer = setTimeout(() => kill('SIGKILL'), 5000);
|
|
79
|
+
};
|
|
80
|
+
const onAbort = () => {
|
|
81
|
+
cancelled = true;
|
|
82
|
+
timedOut ||= signal?.reason instanceof SftpOperationTimeoutError;
|
|
83
|
+
stop();
|
|
84
|
+
};
|
|
85
|
+
const timer = setTimeout(() => {
|
|
86
|
+
timedOut = true;
|
|
87
|
+
stop();
|
|
67
88
|
}, timeout);
|
|
89
|
+
signal?.addEventListener('abort', onAbort, { once: true });
|
|
90
|
+
if (signal?.aborted) {
|
|
91
|
+
onAbort();
|
|
92
|
+
}
|
|
68
93
|
child.stdout.on('data', (chunk) => {
|
|
69
|
-
|
|
94
|
+
const text = stdoutDecoder.write(chunk);
|
|
95
|
+
onStdout?.(text);
|
|
96
|
+
stdoutTruncated ||= stdout.length + text.length > LOCAL_PROCESS_OUTPUT_LIMIT;
|
|
97
|
+
stdout = appendLimitedOutput(stdout, text);
|
|
70
98
|
});
|
|
71
99
|
child.stderr.on('data', (chunk) => {
|
|
72
100
|
stderr = appendLimitedOutput(stderr, chunk.toString('utf-8'));
|
|
@@ -77,10 +105,12 @@ function runLocalProcess(command, args, timeout, env, connection) {
|
|
|
77
105
|
}
|
|
78
106
|
settled = true;
|
|
79
107
|
clearTimeout(timer);
|
|
108
|
+
signal?.removeEventListener('abort', onAbort);
|
|
80
109
|
if (killTimer) {
|
|
81
110
|
clearTimeout(killTimer);
|
|
82
111
|
}
|
|
83
|
-
|
|
112
|
+
onStdout?.(stdoutDecoder.end(), true);
|
|
113
|
+
resolve({ status: null, stdout, stderr, error, timedOut, cancelled, stdoutTruncated });
|
|
84
114
|
});
|
|
85
115
|
child.on('close', (status) => {
|
|
86
116
|
if (settled) {
|
|
@@ -88,10 +118,15 @@ function runLocalProcess(command, args, timeout, env, connection) {
|
|
|
88
118
|
}
|
|
89
119
|
settled = true;
|
|
90
120
|
clearTimeout(timer);
|
|
121
|
+
signal?.removeEventListener('abort', onAbort);
|
|
91
122
|
if (killTimer) {
|
|
92
123
|
clearTimeout(killTimer);
|
|
93
124
|
}
|
|
94
|
-
|
|
125
|
+
const tail = stdoutDecoder.end();
|
|
126
|
+
onStdout?.(tail, true);
|
|
127
|
+
stdoutTruncated ||= stdout.length + tail.length > LOCAL_PROCESS_OUTPUT_LIMIT;
|
|
128
|
+
stdout = appendLimitedOutput(stdout, tail);
|
|
129
|
+
resolve({ status, stdout, stderr, timedOut, cancelled, stdoutTruncated });
|
|
95
130
|
});
|
|
96
131
|
});
|
|
97
132
|
}
|
|
@@ -107,7 +142,7 @@ function isCurrentRsyncGeneration(alias, generation) {
|
|
|
107
142
|
}
|
|
108
143
|
function assertCurrentRsyncGeneration(alias, generation) {
|
|
109
144
|
if (!isCurrentRsyncGeneration(alias, generation)) {
|
|
110
|
-
throw new Error(
|
|
145
|
+
throw new Error("SSH session '" + `${alias}` + "' changed during rsync setup; retry the sync on the current session");
|
|
111
146
|
}
|
|
112
147
|
}
|
|
113
148
|
/** 由 SessionManager.disconnect 调用,防止 alias 重连后读到旧主机的判断 */
|
|
@@ -212,7 +247,7 @@ export function convertRsyncPath(value, profile) {
|
|
|
212
247
|
if (profile === 'native-win32' || profile === 'posix') {
|
|
213
248
|
return value;
|
|
214
249
|
}
|
|
215
|
-
const drive = /^([a-zA-Z]):[\\/](
|
|
250
|
+
const drive = /^(?<drive>[a-zA-Z]):[\\/](?<path>.*)$/.exec(value);
|
|
216
251
|
if (drive) {
|
|
217
252
|
const prefix = profile === 'cygwin' ? '/cygdrive/' : '/';
|
|
218
253
|
return `${prefix}${drive[1].toLowerCase()}/${drive[2].replace(/\\/g, '/')}`;
|
|
@@ -227,7 +262,7 @@ export function quoteRsyncSshArg(value, profile = 'posix') {
|
|
|
227
262
|
if (/[\r\n\0]/.test(text)) {
|
|
228
263
|
throw new Error('rsync SSH argument contains a newline or NUL');
|
|
229
264
|
}
|
|
230
|
-
return profile === 'posix' ? escapeShellArg(text) : `"${text.replace(/([\\"])/g, '\\$1')}"`;
|
|
265
|
+
return profile === 'posix' ? escapeShellArg(text) : `"${text.replace(/(?<character>[\\"])/g, '\\$1')}"`;
|
|
231
266
|
}
|
|
232
267
|
function configuredAllowDirectories() {
|
|
233
268
|
const allowEnv = process.env.SSH_MCP_FILE_OPS_ALLOW_DIRS;
|
|
@@ -299,8 +334,8 @@ function validateLocalUploadSource(localPath, followSymlinks, symlinkError) {
|
|
|
299
334
|
throw new Error(symlinkError);
|
|
300
335
|
}
|
|
301
336
|
if (followSymlinks && getAllowListStatus(expandedPath).configured) {
|
|
302
|
-
throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS
|
|
303
|
-
'symlink targets can change during transfer');
|
|
337
|
+
throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS ' +
|
|
338
|
+
'because symlink targets can change during transfer');
|
|
304
339
|
}
|
|
305
340
|
return {
|
|
306
341
|
expandedPath,
|
|
@@ -348,16 +383,32 @@ export function probeLocalPath(localPath, followSymlinks = false) {
|
|
|
348
383
|
/**
|
|
349
384
|
* sftp.stat 的 Promise 包装
|
|
350
385
|
*/
|
|
351
|
-
|
|
386
|
+
function remainingSftpTimeout(deadline, signal) {
|
|
387
|
+
throwIfAborted(signal);
|
|
388
|
+
if (deadline === undefined) {
|
|
389
|
+
return undefined;
|
|
390
|
+
}
|
|
391
|
+
const remaining = deadline - Date.now();
|
|
392
|
+
if (remaining <= 0) {
|
|
393
|
+
throw new SftpOperationTimeoutError(0);
|
|
394
|
+
}
|
|
395
|
+
return remaining;
|
|
396
|
+
}
|
|
397
|
+
async function getSftpWithinDeadline(alias, timeout, signal) {
|
|
398
|
+
throwIfAborted(signal);
|
|
399
|
+
if (timeout !== undefined && timeout <= 0) {
|
|
400
|
+
throw new SftpOperationTimeoutError(timeout);
|
|
401
|
+
}
|
|
352
402
|
const request = sessionManager.getSftp(alias);
|
|
353
403
|
return withSftpOperationTimeout(request, timeout, () => {
|
|
354
404
|
void request.then((sftp) => sftp.end(), () => undefined);
|
|
355
|
-
});
|
|
405
|
+
}, signal);
|
|
356
406
|
}
|
|
357
|
-
function withSftpDeadline(sftp, operation, timeout) {
|
|
358
|
-
return withSftpOperationTimeout(operation, timeout, () => sftp.end());
|
|
407
|
+
function withSftpDeadline(sftp, operation, timeout, signal) {
|
|
408
|
+
return withSftpOperationTimeout(operation, timeout, () => sftp.end(), signal);
|
|
359
409
|
}
|
|
360
|
-
function sftpStat(sftp, remotePath, timeout) {
|
|
410
|
+
function sftpStat(sftp, remotePath, timeout, signal) {
|
|
411
|
+
throwIfAborted(signal);
|
|
361
412
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
362
413
|
sftp.stat(remotePath, (err, stats) => {
|
|
363
414
|
if (err) {
|
|
@@ -367,7 +418,7 @@ function sftpStat(sftp, remotePath, timeout) {
|
|
|
367
418
|
resolve(stats);
|
|
368
419
|
}
|
|
369
420
|
});
|
|
370
|
-
}), timeout);
|
|
421
|
+
}), timeout, signal);
|
|
371
422
|
}
|
|
372
423
|
function isSftpMissingError(error) {
|
|
373
424
|
if (!error || typeof error !== 'object') {
|
|
@@ -383,9 +434,9 @@ function isSftpMissingError(error) {
|
|
|
383
434
|
function isLocalMissingError(error) {
|
|
384
435
|
return Boolean(error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT');
|
|
385
436
|
}
|
|
386
|
-
async function sftpExists(sftp, remotePath) {
|
|
437
|
+
async function sftpExists(sftp, remotePath, timeout, signal) {
|
|
387
438
|
try {
|
|
388
|
-
await sftpStat(sftp, remotePath);
|
|
439
|
+
await sftpStat(sftp, remotePath, timeout, signal);
|
|
389
440
|
return true;
|
|
390
441
|
}
|
|
391
442
|
catch (error) {
|
|
@@ -395,22 +446,34 @@ async function sftpExists(sftp, remotePath) {
|
|
|
395
446
|
throw error;
|
|
396
447
|
}
|
|
397
448
|
}
|
|
398
|
-
async function remoteExists(alias, remotePath) {
|
|
399
|
-
const sftp = await getSftpWithinDeadline(alias);
|
|
449
|
+
async function remoteExists(alias, remotePath, deadline, signal) {
|
|
450
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
400
451
|
try {
|
|
401
|
-
return await sftpExists(sftp, remotePath);
|
|
452
|
+
return await sftpExists(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
402
453
|
}
|
|
403
454
|
finally {
|
|
404
455
|
sftp.end();
|
|
405
456
|
}
|
|
406
457
|
}
|
|
407
|
-
export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout, limits) {
|
|
458
|
+
export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout, limits, signal) {
|
|
459
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
460
|
+
throwIfAborted(signal);
|
|
408
461
|
const connection = sessionManager.getOpenSshConnection(alias);
|
|
409
|
-
if (connection)
|
|
462
|
+
if (connection) {
|
|
410
463
|
return connection.hashFile(remotePath, algorithm, timeout ?? 600_000, limits?.maxBytes, limits?.expectedSize);
|
|
411
|
-
|
|
464
|
+
}
|
|
465
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
466
|
+
let transferTimeout;
|
|
467
|
+
try {
|
|
468
|
+
transferTimeout = remainingSftpTimeout(deadline, signal);
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
sftp.end();
|
|
472
|
+
throw error;
|
|
473
|
+
}
|
|
412
474
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
413
475
|
const hash = createHash(algorithm);
|
|
476
|
+
throwIfAborted(signal);
|
|
414
477
|
const stream = sftp.createReadStream(remotePath);
|
|
415
478
|
let settled = false;
|
|
416
479
|
let bytes = 0;
|
|
@@ -428,21 +491,26 @@ export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout
|
|
|
428
491
|
}
|
|
429
492
|
};
|
|
430
493
|
stream.on('data', (chunk) => {
|
|
431
|
-
if (settled)
|
|
494
|
+
if (settled) {
|
|
432
495
|
return;
|
|
496
|
+
}
|
|
433
497
|
bytes += chunk.length;
|
|
434
498
|
if (limits && (bytes > limits.maxBytes || bytes > limits.expectedSize)) {
|
|
435
499
|
stream.destroy();
|
|
436
500
|
finish(new Error('Remote file grew or exceeded the verification byte budget'));
|
|
437
501
|
}
|
|
438
|
-
else
|
|
502
|
+
else {
|
|
439
503
|
hash.update(chunk);
|
|
504
|
+
}
|
|
440
505
|
});
|
|
441
506
|
stream.once('error', finish);
|
|
442
507
|
stream.once('end', () => finish(limits && bytes !== limits.expectedSize
|
|
443
508
|
? new Error('Remote file changed during verification')
|
|
444
509
|
: undefined));
|
|
445
|
-
}),
|
|
510
|
+
}), transferTimeout, signal).catch((error) => {
|
|
511
|
+
sftp.end();
|
|
512
|
+
throw error;
|
|
513
|
+
});
|
|
446
514
|
}
|
|
447
515
|
export async function atomicReplaceCapability(alias, timeout) {
|
|
448
516
|
const sftp = await getSftpWithinDeadline(alias, timeout);
|
|
@@ -464,7 +532,14 @@ export class RemoteRenameNotStartedError extends Error {
|
|
|
464
532
|
}
|
|
465
533
|
}
|
|
466
534
|
export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExisting = false, timeout) {
|
|
467
|
-
const
|
|
535
|
+
const state = new SftpWriteState(sourcePath);
|
|
536
|
+
const sftp = await getSftpWithinDeadline(alias, timeout).catch((error) => {
|
|
537
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
538
|
+
cause: error,
|
|
539
|
+
details: state.outcome(error),
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
const tracked = state.track(sftp);
|
|
468
543
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
469
544
|
const callback = (error) => {
|
|
470
545
|
sftp.end();
|
|
@@ -477,17 +552,23 @@ export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExi
|
|
|
477
552
|
};
|
|
478
553
|
try {
|
|
479
554
|
if (replaceExisting) {
|
|
480
|
-
|
|
555
|
+
tracked.ext_openssh_rename(sourcePath, targetPath, callback);
|
|
481
556
|
}
|
|
482
557
|
else {
|
|
483
|
-
|
|
558
|
+
tracked.rename(sourcePath, targetPath, callback);
|
|
484
559
|
}
|
|
485
560
|
}
|
|
486
561
|
catch (error) {
|
|
487
562
|
sftp.end();
|
|
488
|
-
reject(new RemoteRenameNotStartedError(
|
|
563
|
+
reject(new RemoteRenameNotStartedError('SFTP rename request was not sent: ' +
|
|
564
|
+
`${error instanceof Error ? error.message : String(error)}`));
|
|
489
565
|
}
|
|
490
|
-
}), timeout)
|
|
566
|
+
}), timeout).catch((error) => {
|
|
567
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
568
|
+
cause: error,
|
|
569
|
+
details: { ...state.outcome(error), targetPath },
|
|
570
|
+
});
|
|
571
|
+
});
|
|
491
572
|
}
|
|
492
573
|
export async function removeRemoteFile(alias, remotePath, timeout) {
|
|
493
574
|
const sftp = await getSftpWithinDeadline(alias, timeout);
|
|
@@ -506,7 +587,17 @@ export async function removeRemoteFile(alias, remotePath, timeout) {
|
|
|
506
587
|
/**
|
|
507
588
|
* 带进度追踪的流传输
|
|
508
589
|
*/
|
|
509
|
-
function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, closeSftp = true, signal) {
|
|
590
|
+
function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, closeSftp = true, signal, throttle) {
|
|
591
|
+
const originalReadStream = readStream;
|
|
592
|
+
if (throttle) {
|
|
593
|
+
const limiter = new Transform({
|
|
594
|
+
transform(chunk, _encoding, callback) {
|
|
595
|
+
void throttle(chunk.length).then(() => callback(null, chunk), callback);
|
|
596
|
+
},
|
|
597
|
+
});
|
|
598
|
+
originalReadStream.on('error', (error) => limiter.destroy(error));
|
|
599
|
+
readStream = originalReadStream.pipe(limiter);
|
|
600
|
+
}
|
|
510
601
|
return new Promise((resolve, reject) => {
|
|
511
602
|
let settled = false;
|
|
512
603
|
const onAbort = () => cleanup(abortError(signal));
|
|
@@ -519,6 +610,7 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
|
|
|
519
610
|
detachAbort();
|
|
520
611
|
// 先解管道并强制销毁两端,避免错误后流仍持有 sftp 资源
|
|
521
612
|
try {
|
|
613
|
+
originalReadStream.unpipe();
|
|
522
614
|
readStream.unpipe(writeStream);
|
|
523
615
|
}
|
|
524
616
|
catch {
|
|
@@ -526,6 +618,7 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
|
|
|
526
618
|
}
|
|
527
619
|
try {
|
|
528
620
|
;
|
|
621
|
+
originalReadStream.destroy?.(err);
|
|
529
622
|
readStream.destroy?.(err);
|
|
530
623
|
}
|
|
531
624
|
catch {
|
|
@@ -560,6 +653,18 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
|
|
|
560
653
|
if (settled) {
|
|
561
654
|
return;
|
|
562
655
|
}
|
|
656
|
+
if (writeStream.errored) {
|
|
657
|
+
cleanup(writeStream.errored);
|
|
658
|
+
return;
|
|
659
|
+
}
|
|
660
|
+
// ssh2 在 _final 中先关闭流,按已确认字节数判断完成
|
|
661
|
+
if (!writeStream.writableFinished &&
|
|
662
|
+
(!writeStream.writableEnded ||
|
|
663
|
+
writeStream.writableLength !== 0 ||
|
|
664
|
+
writeStream.bytesWritten !== totalSize)) {
|
|
665
|
+
cleanup(new Error('Transfer write stream closed before finish'));
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
563
668
|
if (transferred !== totalSize) {
|
|
564
669
|
cleanup(new Error(`Transfer size mismatch: expected ${totalSize} bytes, received ${transferred}`));
|
|
565
670
|
return;
|
|
@@ -587,15 +692,17 @@ export class UploadFileError extends Error {
|
|
|
587
692
|
this.remoteCreated = remoteCreated;
|
|
588
693
|
this.name = 'UploadFileError';
|
|
589
694
|
const cause = options?.cause;
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
695
|
+
const causeDetails = cause && typeof cause === 'object' && 'details' in cause
|
|
696
|
+
? cause.details
|
|
697
|
+
: undefined;
|
|
698
|
+
this.details = options?.details ? { ...causeDetails, ...options.details } : causeDetails;
|
|
593
699
|
}
|
|
594
700
|
}
|
|
595
701
|
/**
|
|
596
702
|
* 上传文件
|
|
597
703
|
*/
|
|
598
|
-
export async function uploadFile(alias, localPath, remotePath, onProgress, sharedSftp, createMode, signal, followSymlinks = false, exclusive = false, timeout) {
|
|
704
|
+
export async function uploadFile(alias, localPath, remotePath, onProgress, sharedSftp, createMode, signal, followSymlinks = false, exclusive = false, timeout, throttle) {
|
|
705
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
599
706
|
throwIfAborted(signal);
|
|
600
707
|
const source = followSymlinks
|
|
601
708
|
? validateLocalUploadSource(localPath, true, 'Refusing to upload symlink at top level (use ssh_sync with followSymlinks=true to traverse)')
|
|
@@ -608,21 +715,47 @@ export async function uploadFile(alias, localPath, remotePath, onProgress, share
|
|
|
608
715
|
if (!stats.isFile()) {
|
|
609
716
|
throw new Error('UPLOAD_PATH_IS_NOT_FILE: localPath is not a regular file');
|
|
610
717
|
}
|
|
611
|
-
const
|
|
718
|
+
const state = new SftpWriteState(remotePath);
|
|
719
|
+
const sftp = sharedSftp ??
|
|
720
|
+
(await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal).catch((error) => {
|
|
721
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
722
|
+
cause: error,
|
|
723
|
+
details: state.outcome(error),
|
|
724
|
+
});
|
|
725
|
+
}));
|
|
726
|
+
try {
|
|
727
|
+
remainingSftpTimeout(deadline, signal);
|
|
728
|
+
}
|
|
729
|
+
catch (error) {
|
|
730
|
+
if (!sharedSftp) {
|
|
731
|
+
sftp.end();
|
|
732
|
+
}
|
|
733
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
734
|
+
cause: error,
|
|
735
|
+
details: state.outcome(error),
|
|
736
|
+
});
|
|
737
|
+
}
|
|
612
738
|
const totalSize = stats.size;
|
|
613
739
|
const safeCreateMode = (createMode ?? stats.mode) & FILE_CREATE_MODE_MASK;
|
|
614
|
-
const writeStream = sftp.createWriteStream(remotePath, { mode: safeCreateMode, flags: exclusive ? 'wx' : 'w' });
|
|
615
|
-
let remoteCreated = false;
|
|
616
|
-
writeStream.once('open', () => {
|
|
617
|
-
remoteCreated = true;
|
|
618
|
-
});
|
|
619
740
|
try {
|
|
620
|
-
|
|
741
|
+
remainingSftpTimeout(deadline, signal);
|
|
742
|
+
const writeStream = state.track(sftp).createWriteStream(remotePath, {
|
|
743
|
+
mode: safeCreateMode,
|
|
744
|
+
flags: exclusive ? 'wx' : 'w',
|
|
745
|
+
});
|
|
746
|
+
writeStream.once('open', () => {
|
|
747
|
+
state.remoteCreated = true;
|
|
748
|
+
});
|
|
749
|
+
const result = await withSftpDeadline(sftp, pipeWithProgress(fs.createReadStream(localPath), writeStream, sftp, totalSize, onProgress, !sharedSftp, signal, throttle), remainingSftpTimeout(deadline, signal), signal);
|
|
621
750
|
return { ...result, createMode: formatFileMode(safeCreateMode) };
|
|
622
751
|
}
|
|
623
752
|
catch (error) {
|
|
624
|
-
|
|
753
|
+
if (!sharedSftp) {
|
|
754
|
+
sftp.end();
|
|
755
|
+
}
|
|
756
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), state.remoteCreated, {
|
|
625
757
|
cause: error,
|
|
758
|
+
details: state.outcome(error),
|
|
626
759
|
});
|
|
627
760
|
}
|
|
628
761
|
}
|
|
@@ -642,10 +775,12 @@ async function existingDownloadTargetMode(localPath) {
|
|
|
642
775
|
}
|
|
643
776
|
return stats.mode & FILE_CREATE_MODE_MASK;
|
|
644
777
|
}
|
|
645
|
-
async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, closeSftpOnFailure,
|
|
778
|
+
async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, closeSftpOnFailure, deadline, signal) {
|
|
646
779
|
let readStream;
|
|
647
780
|
try {
|
|
648
|
-
const totalSize = (await sftpStat(sftp, remotePath,
|
|
781
|
+
const totalSize = (await sftpStat(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal)).size;
|
|
782
|
+
remainingSftpTimeout(deadline, signal);
|
|
783
|
+
validateLocalPathAgainstAllowList(localDir);
|
|
649
784
|
if (!fs.existsSync(localDir)) {
|
|
650
785
|
fs.mkdirSync(localDir, { recursive: true });
|
|
651
786
|
}
|
|
@@ -653,9 +788,11 @@ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath
|
|
|
653
788
|
const handle = await fs.promises.open(temporaryPath, 'wx', 0o600);
|
|
654
789
|
await handle.close();
|
|
655
790
|
if (checkWindowsAcl(temporaryPath, true) !== 'key_path_usable') {
|
|
656
|
-
|
|
791
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
792
|
+
throw new Error('privacy_not_enforced_on_windows: temporary download file ACL could not ' + 'be verified');
|
|
657
793
|
}
|
|
658
794
|
}
|
|
795
|
+
remainingSftpTimeout(deadline, signal);
|
|
659
796
|
readStream = sftp.createReadStream(remotePath);
|
|
660
797
|
return {
|
|
661
798
|
totalSize,
|
|
@@ -683,32 +820,61 @@ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath
|
|
|
683
820
|
/**
|
|
684
821
|
* 下载文件
|
|
685
822
|
*/
|
|
686
|
-
export async function downloadFile(alias, remotePath, localPath, onProgress, sharedSftp, signal, timeout) {
|
|
823
|
+
export async function downloadFile(alias, remotePath, localPath, onProgress, sharedSftp, signal, timeout, throttle) {
|
|
824
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
687
825
|
throwIfAborted(signal);
|
|
688
826
|
localPath = expandTilde(localPath);
|
|
689
827
|
validateLocalPathAgainstAllowList(localPath);
|
|
690
828
|
const preservedMode = await existingDownloadTargetMode(localPath);
|
|
691
|
-
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias,
|
|
829
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
|
|
692
830
|
const localDir = path.dirname(localPath);
|
|
693
831
|
const temporaryPath = path.join(localDir, `.${path.basename(localPath)}.mcp-tmp-${process.pid}-${randomBytes(8).toString('hex')}`);
|
|
832
|
+
let failureStage = 'download';
|
|
833
|
+
let localWriteStream;
|
|
694
834
|
try {
|
|
695
|
-
const { totalSize, readStream, writeStream } = await prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, !sharedSftp,
|
|
696
|
-
|
|
835
|
+
const { totalSize, readStream, writeStream } = await prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, !sharedSftp, deadline, signal);
|
|
836
|
+
// noinspection JSUnusedAssignment -- read by the failure cleanup path
|
|
837
|
+
localWriteStream = writeStream;
|
|
838
|
+
const result = await withSftpDeadline(sftp, pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, !sharedSftp, signal, throttle), remainingSftpTimeout(deadline, signal), signal);
|
|
697
839
|
if (process.platform !== 'win32') {
|
|
698
840
|
await fs.promises.chmod(temporaryPath, preservedMode ?? 0o600);
|
|
699
841
|
}
|
|
842
|
+
remainingSftpTimeout(deadline, signal);
|
|
843
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
844
|
+
// noinspection JSUnusedAssignment -- read by the failure cleanup path
|
|
845
|
+
failureStage = 'local_rename';
|
|
700
846
|
await fs.promises.rename(temporaryPath, localPath);
|
|
701
847
|
return { ...result, atomic: true };
|
|
702
848
|
}
|
|
703
|
-
catch (
|
|
849
|
+
catch (cause) {
|
|
850
|
+
const error = new UploadFileError(cause instanceof Error ? cause.message : String(cause), false, {
|
|
851
|
+
cause,
|
|
852
|
+
details: {
|
|
853
|
+
...sftpOperationOutcome(cause),
|
|
854
|
+
operationStatus: 'failed',
|
|
855
|
+
retryable: false,
|
|
856
|
+
failureStage,
|
|
857
|
+
localPath,
|
|
858
|
+
temporaryPath,
|
|
859
|
+
targetReplaced: false,
|
|
860
|
+
},
|
|
861
|
+
});
|
|
704
862
|
try {
|
|
863
|
+
const writeStream = localWriteStream;
|
|
864
|
+
if (writeStream && !writeStream.closed) {
|
|
865
|
+
// 尚未完成的 open 会在 unlink 返回 ENOENT 后创建文件
|
|
866
|
+
await new Promise((resolve) => {
|
|
867
|
+
writeStream.once('close', resolve);
|
|
868
|
+
writeStream.destroy();
|
|
869
|
+
});
|
|
870
|
+
}
|
|
705
871
|
await fs.promises.unlink(temporaryPath);
|
|
706
872
|
}
|
|
707
873
|
catch (cleanupError) {
|
|
708
874
|
if (!isLocalMissingError(cleanupError)) {
|
|
709
|
-
throw new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
|
|
710
|
-
cause
|
|
711
|
-
});
|
|
875
|
+
throw Object.assign(new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
|
|
876
|
+
cause,
|
|
877
|
+
}), { details: error.details });
|
|
712
878
|
}
|
|
713
879
|
}
|
|
714
880
|
throw error;
|
|
@@ -989,14 +1155,21 @@ export async function readFile(alias, remotePath, options) {
|
|
|
989
1155
|
const readOptions = normalizeReadFileOptions(options);
|
|
990
1156
|
validateReadFileOptions(readOptions);
|
|
991
1157
|
const sftp = await getSftpWithinDeadline(alias, readOptions.timeout);
|
|
992
|
-
let
|
|
1158
|
+
let stats;
|
|
993
1159
|
try {
|
|
994
|
-
|
|
1160
|
+
stats = await sftpStat(sftp, remotePath, readOptions.timeout);
|
|
995
1161
|
}
|
|
996
1162
|
catch (error) {
|
|
997
1163
|
sftp.end();
|
|
998
1164
|
throw error;
|
|
999
1165
|
}
|
|
1166
|
+
if ((stats.mode & S_IFMT) !== S_IFREG) {
|
|
1167
|
+
sftp.end();
|
|
1168
|
+
throw new Error((stats.mode & S_IFMT) === S_IFDIR
|
|
1169
|
+
? 'Target is a directory; use ssh_list_dir to read its entries'
|
|
1170
|
+
: 'Target is not a regular file');
|
|
1171
|
+
}
|
|
1172
|
+
const actualSize = stats.size;
|
|
1000
1173
|
if (readOptions.lineRange) {
|
|
1001
1174
|
return readLineRange(sftp, remotePath, actualSize, readOptions.maxBytes, readOptions.lineRange, readOptions.encoding ?? 'auto', readOptions.binary === true, readOptions.timeout);
|
|
1002
1175
|
}
|
|
@@ -1056,7 +1229,8 @@ export async function readFile(alias, remotePath, options) {
|
|
|
1056
1229
|
detected.startsWith('utf16') &&
|
|
1057
1230
|
!readOptions.tail &&
|
|
1058
1231
|
requestedOffset % 2) {
|
|
1059
|
-
|
|
1232
|
+
reject(new Error(`Invalid ${detected} offset is not a character boundary`));
|
|
1233
|
+
return;
|
|
1060
1234
|
}
|
|
1061
1235
|
let contentStart = 0;
|
|
1062
1236
|
if (!readOptions.binary && detected === 'utf8' && readOptions.tail && requestedOffset > 0) {
|
|
@@ -1073,13 +1247,14 @@ export async function readFile(alias, remotePath, options) {
|
|
|
1073
1247
|
const firstUnit = detected === 'utf16be'
|
|
1074
1248
|
? buffer.readUInt16BE(contentStart)
|
|
1075
1249
|
: buffer.readUInt16LE(contentStart);
|
|
1076
|
-
if (firstUnit >= 0xdc00 && firstUnit
|
|
1250
|
+
if (firstUnit >= 0xdc00 && firstUnit < 0xe000) {
|
|
1077
1251
|
contentStart += 2;
|
|
1078
1252
|
}
|
|
1079
1253
|
}
|
|
1080
1254
|
}
|
|
1081
1255
|
if (!readOptions.binary && buffer.length > 0 && contentStart >= buffer.length) {
|
|
1082
|
-
|
|
1256
|
+
reject(new Error(`maxBytes is too small to read a complete ${detected} tail character`));
|
|
1257
|
+
return;
|
|
1083
1258
|
}
|
|
1084
1259
|
const decoded = decodeReadBuffer(buffer.subarray(contentStart), detected, readOptions.binary === true, requestedOffset + buffer.length < actualSize, requestedOffset + contentStart === 0);
|
|
1085
1260
|
const contentBuffer = buffer.subarray(contentStart, contentStart + decoded.read_bytes);
|
|
@@ -1125,35 +1300,53 @@ export async function readFile(alias, remotePath, options) {
|
|
|
1125
1300
|
readStream.once('error', (error) => finish(error));
|
|
1126
1301
|
}), readOptions.timeout);
|
|
1127
1302
|
}
|
|
1128
|
-
export async function createEmptyRemoteFile(alias, remotePath, timeout) {
|
|
1129
|
-
const
|
|
1303
|
+
export async function createEmptyRemoteFile(alias, remotePath, timeout, signal) {
|
|
1304
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1305
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
1306
|
+
const state = new SftpWriteState(remotePath);
|
|
1130
1307
|
try {
|
|
1131
1308
|
await withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
1132
|
-
|
|
1309
|
+
throwIfAborted(signal);
|
|
1310
|
+
const stream = state.track(sftp, signal).createWriteStream(remotePath, { flags: 'wx', mode: 0o600 });
|
|
1133
1311
|
stream.once('error', reject);
|
|
1134
1312
|
stream.once('close', resolve);
|
|
1135
1313
|
stream.end();
|
|
1136
|
-
}),
|
|
1314
|
+
}), remainingSftpTimeout(deadline, signal), signal);
|
|
1315
|
+
}
|
|
1316
|
+
catch (error) {
|
|
1317
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), state.remoteCreated, {
|
|
1318
|
+
cause: error,
|
|
1319
|
+
details: state.outcome(error),
|
|
1320
|
+
});
|
|
1137
1321
|
}
|
|
1138
1322
|
finally {
|
|
1139
1323
|
sftp.end();
|
|
1140
1324
|
}
|
|
1141
1325
|
}
|
|
1142
|
-
export async function prepareWindowsRemotePrivateFile(alias, remotePath, timeout, shellPath = remotePath) {
|
|
1143
|
-
|
|
1326
|
+
export async function prepareWindowsRemotePrivateFile(alias, remotePath, timeout, shellPath = remotePath, signal) {
|
|
1327
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1328
|
+
let created = false;
|
|
1144
1329
|
try {
|
|
1330
|
+
await createEmptyRemoteFile(alias, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
1331
|
+
created = true;
|
|
1332
|
+
throwIfAborted(signal);
|
|
1145
1333
|
const acl = await sessionManager.exec(alias, encodedPowershell(windowsAclScript(shellPath, true)), {
|
|
1146
|
-
timeout,
|
|
1334
|
+
timeout: remainingSftpTimeout(deadline, signal),
|
|
1335
|
+
signal,
|
|
1147
1336
|
useLoginUser: true,
|
|
1148
1337
|
maxOutputSize: 4096,
|
|
1149
1338
|
});
|
|
1150
1339
|
if (!acl.success || acl.stdout.trim() !== 'usable') {
|
|
1340
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
1151
1341
|
throw new Error('privacy_not_enforced_on_windows: remote temporary file ACL was not verified');
|
|
1152
1342
|
}
|
|
1153
1343
|
}
|
|
1154
1344
|
catch (error) {
|
|
1345
|
+
if (!created && !(error instanceof UploadFileError && error.remoteCreated)) {
|
|
1346
|
+
throw error;
|
|
1347
|
+
}
|
|
1155
1348
|
try {
|
|
1156
|
-
await removeRemoteFile(alias, remotePath, timeout);
|
|
1349
|
+
await removeRemoteFile(alias, remotePath, Math.min(timeout ?? 30_000, 30_000));
|
|
1157
1350
|
}
|
|
1158
1351
|
catch (cleanupError) {
|
|
1159
1352
|
throw new AggregateError([error, cleanupError], 'privacy_not_enforced_on_windows: remote ACL check and cleanup failed', { cause: cleanupError });
|
|
@@ -1164,23 +1357,30 @@ export async function prepareWindowsRemotePrivateFile(alias, remotePath, timeout
|
|
|
1164
1357
|
/**
|
|
1165
1358
|
* 写入远程文件
|
|
1166
1359
|
*/
|
|
1167
|
-
export async function writeFile(alias, remotePath, content, append = false, timeout) {
|
|
1168
|
-
const
|
|
1169
|
-
const
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
writeStream.on('close', () => {
|
|
1174
|
-
sftp.end();
|
|
1175
|
-
resolve({ success: true, size: contentBuffer.length });
|
|
1360
|
+
export async function writeFile(alias, remotePath, content, append = false, timeout, signal) {
|
|
1361
|
+
const state = new SftpWriteState(remotePath);
|
|
1362
|
+
const sftp = await getSftpWithinDeadline(alias, timeout, signal).catch((error) => {
|
|
1363
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
1364
|
+
cause: error,
|
|
1365
|
+
details: state.outcome(error),
|
|
1176
1366
|
});
|
|
1177
|
-
|
|
1367
|
+
});
|
|
1368
|
+
const contentBuffer = Buffer.from(content, 'utf-8');
|
|
1369
|
+
let writeStream;
|
|
1370
|
+
try {
|
|
1371
|
+
throwIfAborted(signal);
|
|
1372
|
+
writeStream = state.track(sftp, signal).createWriteStream(remotePath, { flags: append ? 'a' : 'w' });
|
|
1373
|
+
return await withSftpDeadline(sftp, pipeWithProgress(Readable.from([contentBuffer]), writeStream, sftp, contentBuffer.length, undefined, true, signal), timeout, signal);
|
|
1374
|
+
}
|
|
1375
|
+
catch (error) {
|
|
1376
|
+
if (!writeStream) {
|
|
1178
1377
|
sftp.end();
|
|
1179
|
-
|
|
1378
|
+
}
|
|
1379
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), state.remoteCreated, {
|
|
1380
|
+
cause: error,
|
|
1381
|
+
details: state.outcome(error),
|
|
1180
1382
|
});
|
|
1181
|
-
|
|
1182
|
-
writeStream.end();
|
|
1183
|
-
}), timeout);
|
|
1383
|
+
}
|
|
1184
1384
|
}
|
|
1185
1385
|
function validateSftpEntryName(filename) {
|
|
1186
1386
|
if (filename.length === 0 ||
|
|
@@ -1195,10 +1395,12 @@ function validateSftpEntryName(filename) {
|
|
|
1195
1395
|
/**
|
|
1196
1396
|
* 列出目录内容
|
|
1197
1397
|
*/
|
|
1198
|
-
export async function listDir(alias, remotePath, showHidden = false, sharedSftp, timeout, style = 'posix') {
|
|
1199
|
-
|
|
1398
|
+
export async function listDir(alias, remotePath, showHidden = false, sharedSftp, timeout, style = 'posix', signal) {
|
|
1399
|
+
throwIfAborted(signal);
|
|
1400
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout, signal));
|
|
1200
1401
|
const closeWhenDone = !sharedSftp;
|
|
1201
1402
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
1403
|
+
throwIfAborted(signal);
|
|
1202
1404
|
sftp.readdir(remotePath, (err, list) => {
|
|
1203
1405
|
if (err) {
|
|
1204
1406
|
if (closeWhenDone) {
|
|
@@ -1247,21 +1449,22 @@ export async function listDir(alias, remotePath, showHidden = false, sharedSftp,
|
|
|
1247
1449
|
}
|
|
1248
1450
|
resolve(files);
|
|
1249
1451
|
});
|
|
1250
|
-
}), timeout);
|
|
1452
|
+
}), timeout, signal);
|
|
1251
1453
|
}
|
|
1252
1454
|
/**
|
|
1253
1455
|
* 获取文件信息
|
|
1254
1456
|
*/
|
|
1255
|
-
export async function getFileInfo(alias, remotePath, timeout, style = 'posix') {
|
|
1457
|
+
export async function getFileInfo(alias, remotePath, timeout, style = 'posix', signal) {
|
|
1458
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1256
1459
|
const connection = sessionManager.getOpenSshConnection(alias);
|
|
1257
1460
|
let stats;
|
|
1258
1461
|
if (connection) {
|
|
1259
|
-
stats = await connection.stat(remotePath,
|
|
1462
|
+
stats = await connection.stat(remotePath, remainingSftpTimeout(deadline, signal) ?? 600_000, true, signal);
|
|
1260
1463
|
}
|
|
1261
1464
|
else {
|
|
1262
|
-
const sftp = await getSftpWithinDeadline(alias,
|
|
1465
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
1263
1466
|
try {
|
|
1264
|
-
stats = await sftpStat(sftp, remotePath,
|
|
1467
|
+
stats = await sftpStat(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
1265
1468
|
}
|
|
1266
1469
|
finally {
|
|
1267
1470
|
sftp.end();
|
|
@@ -1282,7 +1485,8 @@ export async function getFileInfo(alias, remotePath, timeout, style = 'posix') {
|
|
|
1282
1485
|
atime: new Date(stats.atime * 1000),
|
|
1283
1486
|
};
|
|
1284
1487
|
}
|
|
1285
|
-
function sftpMkdir(sftp, remotePath, timeout) {
|
|
1488
|
+
function sftpMkdir(sftp, remotePath, timeout, signal) {
|
|
1489
|
+
throwIfAborted(signal);
|
|
1286
1490
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
1287
1491
|
sftp.mkdir(remotePath, (error) => {
|
|
1288
1492
|
if (error) {
|
|
@@ -1292,16 +1496,17 @@ function sftpMkdir(sftp, remotePath, timeout) {
|
|
|
1292
1496
|
resolve();
|
|
1293
1497
|
}
|
|
1294
1498
|
});
|
|
1295
|
-
}), timeout);
|
|
1499
|
+
}), timeout, signal);
|
|
1296
1500
|
}
|
|
1297
|
-
async function ensureSftpDirectory(sftp, remotePath,
|
|
1501
|
+
async function ensureSftpDirectory(sftp, remotePath, deadline, style = 'posix', signal) {
|
|
1502
|
+
remainingSftpTimeout(deadline, signal);
|
|
1298
1503
|
const normalizedPath = remoteNormalize(style, remotePath);
|
|
1299
1504
|
if (normalizedPath === '/' || normalizedPath === '.') {
|
|
1300
1505
|
return;
|
|
1301
1506
|
}
|
|
1302
1507
|
let existingMode;
|
|
1303
1508
|
try {
|
|
1304
|
-
existingMode = (await sftpStat(sftp, normalizedPath,
|
|
1509
|
+
existingMode = (await sftpStat(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal)).mode;
|
|
1305
1510
|
}
|
|
1306
1511
|
catch (error) {
|
|
1307
1512
|
if (!isSftpMissingError(error)) {
|
|
@@ -1319,14 +1524,14 @@ async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
|
|
|
1319
1524
|
throw new Error('SFTP UNC share root does not exist; refusing to create a network host path');
|
|
1320
1525
|
}
|
|
1321
1526
|
if (parent !== normalizedPath) {
|
|
1322
|
-
await ensureSftpDirectory(sftp, parent,
|
|
1527
|
+
await ensureSftpDirectory(sftp, parent, deadline, style, signal);
|
|
1323
1528
|
}
|
|
1324
1529
|
try {
|
|
1325
|
-
await sftpMkdir(sftp, normalizedPath,
|
|
1530
|
+
await sftpMkdir(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal);
|
|
1326
1531
|
}
|
|
1327
1532
|
catch (error) {
|
|
1328
1533
|
try {
|
|
1329
|
-
const stats = await sftpStat(sftp, normalizedPath,
|
|
1534
|
+
const stats = await sftpStat(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal);
|
|
1330
1535
|
if ((stats.mode & S_IFMT) === S_IFDIR) {
|
|
1331
1536
|
return;
|
|
1332
1537
|
}
|
|
@@ -1340,15 +1545,16 @@ async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
|
|
|
1340
1545
|
/**
|
|
1341
1546
|
* 创建目录
|
|
1342
1547
|
*/
|
|
1343
|
-
export async function mkdir(alias, remotePath, recursive = false, sharedSftp, timeout, style = 'posix') {
|
|
1344
|
-
const
|
|
1548
|
+
export async function mkdir(alias, remotePath, recursive = false, sharedSftp, timeout, style = 'posix', signal) {
|
|
1549
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1550
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
|
|
1345
1551
|
return withSftpDeadline(sftp, (async () => {
|
|
1346
1552
|
try {
|
|
1347
1553
|
if (recursive) {
|
|
1348
|
-
await ensureSftpDirectory(sftp, remotePath,
|
|
1554
|
+
await ensureSftpDirectory(sftp, remotePath, deadline, style, signal);
|
|
1349
1555
|
}
|
|
1350
1556
|
else {
|
|
1351
|
-
await sftpMkdir(sftp, remotePath,
|
|
1557
|
+
await sftpMkdir(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
1352
1558
|
}
|
|
1353
1559
|
return true;
|
|
1354
1560
|
}
|
|
@@ -1357,7 +1563,7 @@ export async function mkdir(alias, remotePath, recursive = false, sharedSftp, ti
|
|
|
1357
1563
|
sftp.end();
|
|
1358
1564
|
}
|
|
1359
1565
|
}
|
|
1360
|
-
})(),
|
|
1566
|
+
})(), remainingSftpTimeout(deadline, signal), signal);
|
|
1361
1567
|
}
|
|
1362
1568
|
/**
|
|
1363
1569
|
* 检查远程是否安装 rsync
|
|
@@ -1372,6 +1578,7 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
|
|
|
1372
1578
|
const startedAt = Date.now();
|
|
1373
1579
|
try {
|
|
1374
1580
|
if (/[\r\n\0]/.test(executable)) {
|
|
1581
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
1375
1582
|
throw new Error('Invalid rsync executable path');
|
|
1376
1583
|
}
|
|
1377
1584
|
const probeCommand = shell === 'posix'
|
|
@@ -1447,7 +1654,9 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
|
|
|
1447
1654
|
retryable: result.failureKind !== 'remote_command',
|
|
1448
1655
|
exitCode: result.exitCode,
|
|
1449
1656
|
failureKind: result.failureKind,
|
|
1450
|
-
error: result.stderr.trim() ||
|
|
1657
|
+
error: result.stderr.trim() ||
|
|
1658
|
+
result.stdout.trim() ||
|
|
1659
|
+
'rsync probe failed with exit code ' + `${result.exitCode}`,
|
|
1451
1660
|
};
|
|
1452
1661
|
}
|
|
1453
1662
|
catch (error) {
|
|
@@ -1460,6 +1669,53 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
|
|
|
1460
1669
|
};
|
|
1461
1670
|
}
|
|
1462
1671
|
}
|
|
1672
|
+
function parseWindowsCaseSemantics(output, names) {
|
|
1673
|
+
const semantics = JSON.parse(output);
|
|
1674
|
+
if (typeof semantics?.caseSensitive !== 'boolean' ||
|
|
1675
|
+
!Object.hasOwn(semantics, 'collision') ||
|
|
1676
|
+
(semantics.collision !== null &&
|
|
1677
|
+
(!Array.isArray(semantics.collision) ||
|
|
1678
|
+
semantics.collision.length !== 2 ||
|
|
1679
|
+
semantics.collision[0] === semantics.collision[1] ||
|
|
1680
|
+
!semantics.collision.every((name) => typeof name === 'string' && names.includes(name)))) ||
|
|
1681
|
+
(semantics.caseSensitive && semantics.collision !== null)) {
|
|
1682
|
+
throw new Error('Windows case semantics query returned incomplete fields');
|
|
1683
|
+
}
|
|
1684
|
+
return semantics.collision;
|
|
1685
|
+
}
|
|
1686
|
+
function verifyWindowsDownloadNames(localPath, remoteNames, timeout) {
|
|
1687
|
+
const existing = fs.existsSync(localPath) ? fs.readdirSync(localPath) : [];
|
|
1688
|
+
const names = [...new Set([...existing, ...remoteNames])];
|
|
1689
|
+
if (names.length < 2) {
|
|
1690
|
+
return;
|
|
1691
|
+
}
|
|
1692
|
+
let parent = localPath;
|
|
1693
|
+
while (!fs.existsSync(parent)) {
|
|
1694
|
+
parent = path.dirname(parent);
|
|
1695
|
+
}
|
|
1696
|
+
try {
|
|
1697
|
+
const script = encodedPowershell(windowsCaseSemanticsScript(parent)).split(' ').at(-1);
|
|
1698
|
+
const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', script], {
|
|
1699
|
+
input: Buffer.from(JSON.stringify(names)).toString('base64'),
|
|
1700
|
+
encoding: 'utf8',
|
|
1701
|
+
windowsHide: true,
|
|
1702
|
+
timeout: Math.min(timeout ?? 10_000, 10_000),
|
|
1703
|
+
maxBuffer: 16_384,
|
|
1704
|
+
});
|
|
1705
|
+
if (result.error || result.status !== 0) {
|
|
1706
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
1707
|
+
throw new Error('Windows directory case query failed');
|
|
1708
|
+
}
|
|
1709
|
+
const collision = parseWindowsCaseSemantics(result.stdout, names);
|
|
1710
|
+
if (collision) {
|
|
1711
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
1712
|
+
throw new Error(`Windows local filename collision at ${localPath}: ${collision.join(' / ')}`);
|
|
1713
|
+
}
|
|
1714
|
+
}
|
|
1715
|
+
catch (error) {
|
|
1716
|
+
throw new Error(`case_semantics_unverified: ${localPath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1463
1719
|
async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
1464
1720
|
const candidates = [];
|
|
1465
1721
|
const style = options.remotePathStyle ?? sessionManager.getRemotePathStyle(alias);
|
|
@@ -1474,12 +1730,13 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
|
1474
1730
|
candidates.push({ parent, names });
|
|
1475
1731
|
}
|
|
1476
1732
|
});
|
|
1477
|
-
const deadline = Date.now() + (options.preflightTimeout ?? 10_000);
|
|
1733
|
+
const deadline = Math.min(Date.now() + (options.preflightTimeout ?? 10_000), options.deadline ?? Infinity);
|
|
1478
1734
|
for (const { parent, names } of candidates) {
|
|
1479
1735
|
let collision;
|
|
1480
1736
|
try {
|
|
1481
1737
|
const timeout = deadline - Date.now();
|
|
1482
1738
|
if (timeout <= 0) {
|
|
1739
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
1483
1740
|
throw new Error('name preflight timeout was exhausted');
|
|
1484
1741
|
}
|
|
1485
1742
|
const result = await sessionManager.exec(alias, encodedPowershell(windowsCaseSemanticsScript(remoteShellPath(parent, style), names)), { timeout, maxOutputSize: 65_536, useLoginUser: true, loadProfile: false });
|
|
@@ -1488,20 +1745,10 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
|
1488
1745
|
result.timedOut ||
|
|
1489
1746
|
result.stdoutTruncated ||
|
|
1490
1747
|
result.stderrTruncated) {
|
|
1748
|
+
// noinspection ExceptionCaughtLocallyJS — 保留传输失败阶段及所属资源清理
|
|
1491
1749
|
throw new Error(result.stderr || 'Windows case semantics query failed or returned truncated output');
|
|
1492
1750
|
}
|
|
1493
|
-
|
|
1494
|
-
if (typeof semantics?.caseSensitive !== 'boolean' ||
|
|
1495
|
-
!Object.hasOwn(semantics, 'collision') ||
|
|
1496
|
-
(semantics.collision !== null &&
|
|
1497
|
-
(!Array.isArray(semantics.collision) ||
|
|
1498
|
-
semantics.collision.length !== 2 ||
|
|
1499
|
-
semantics.collision[0] === semantics.collision[1] ||
|
|
1500
|
-
!semantics.collision.every((name) => typeof name === 'string' && names.includes(name)))) ||
|
|
1501
|
-
(semantics.caseSensitive && semantics.collision !== null)) {
|
|
1502
|
-
throw new Error('Windows case semantics query returned incomplete fields');
|
|
1503
|
-
}
|
|
1504
|
-
collision = semantics.collision;
|
|
1751
|
+
collision = parseWindowsCaseSemantics(result.stdout, names);
|
|
1505
1752
|
}
|
|
1506
1753
|
catch (error) {
|
|
1507
1754
|
throw new Error(`case_semantics_unverified: ${parent}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
@@ -1522,10 +1769,16 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
|
1522
1769
|
*/
|
|
1523
1770
|
export async function syncFiles(alias, localPath, remotePath, direction, options = {}) {
|
|
1524
1771
|
const startedAt = Date.now();
|
|
1772
|
+
if (options.bandwidthLimitKiBps !== undefined &&
|
|
1773
|
+
(!Number.isSafeInteger(options.bandwidthLimitKiBps) || options.bandwidthLimitKiBps <= 0)) {
|
|
1774
|
+
throw new Error('bandwidthLimitKiBps must be a positive safe integer in KiB/s');
|
|
1775
|
+
}
|
|
1776
|
+
options = { ...options, deadline: options.deadline ?? startedAt + (options.operationTimeout ?? 600_000) };
|
|
1777
|
+
remainingSftpTimeout(options.deadline, options.signal);
|
|
1525
1778
|
localPath = expandTilde(localPath);
|
|
1526
1779
|
validateLocalPathAgainstAllowList(localPath);
|
|
1527
1780
|
if (remotePath === '~' || remotePath.startsWith('~/')) {
|
|
1528
|
-
throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ is not supported');
|
|
1781
|
+
throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ ' + 'is not supported');
|
|
1529
1782
|
}
|
|
1530
1783
|
let sourceIsDirectory;
|
|
1531
1784
|
if (direction === 'upload') {
|
|
@@ -1534,10 +1787,10 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
1534
1787
|
sourceIsDirectory = source.stats.isDirectory();
|
|
1535
1788
|
}
|
|
1536
1789
|
else {
|
|
1537
|
-
sourceIsDirectory = (await getFileInfo(alias, remotePath,
|
|
1790
|
+
sourceIsDirectory = (await getFileInfo(alias, remotePath, remainingSftpTimeout(options.deadline, options.signal), options.remotePathStyle, options.signal)).isDirectory;
|
|
1538
1791
|
}
|
|
1539
1792
|
if (sourceIsDirectory && options.recursive === false) {
|
|
1540
|
-
throw new Error('recursive=false is not supported for directory sources; use a file source or enable recursion');
|
|
1793
|
+
throw new Error('recursive=false is not supported for directory sources; use a file ' + 'source or enable recursion');
|
|
1541
1794
|
}
|
|
1542
1795
|
const capabilityStartedAt = Date.now();
|
|
1543
1796
|
const rsyncGeneration = getRsyncGeneration(alias);
|
|
@@ -1546,10 +1799,12 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
1546
1799
|
assertCurrentRsyncGeneration(alias, rsyncGeneration);
|
|
1547
1800
|
}
|
|
1548
1801
|
const capability = sessionManager.getExternalTransferCapability(alias);
|
|
1549
|
-
const preflightTimeout = options.preflightTimeout ?? 10_000;
|
|
1802
|
+
const preflightTimeout = Math.min(options.preflightTimeout ?? 10_000, remainingSftpTimeout(options.deadline, options.signal));
|
|
1550
1803
|
const connection = sessionManager.getOpenSshConnection(alias);
|
|
1551
1804
|
if (connection) {
|
|
1552
|
-
await connection.capture('command -v find stat head realpath >/dev/null && find --version
|
|
1805
|
+
await connection.capture('command -v find stat head realpath >/dev/null && find --version ' +
|
|
1806
|
+
'>/dev/null && stat --version >/dev/null && head --version >/dev/null && ' +
|
|
1807
|
+
'realpath --version >/dev/null', preflightTimeout, 4096);
|
|
1553
1808
|
}
|
|
1554
1809
|
const rsyncProbe = capability.rsyncEligible
|
|
1555
1810
|
? await checkRsync(alias, preflightTimeout, capability.rsyncExecutable, capability.commandShell)
|
|
@@ -1624,17 +1879,18 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1624
1879
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1625
1880
|
const connection = sessionManager.getOpenSshConnection(alias);
|
|
1626
1881
|
const fallbackToSftp = (reason) => {
|
|
1627
|
-
if (connection)
|
|
1882
|
+
if (connection) {
|
|
1628
1883
|
throw new OpenSshError('OpenSSH master sync preflight failed; the selected backend was preserved', {
|
|
1629
1884
|
code: 'openssh_sync_preflight',
|
|
1630
1885
|
backend: 'openssh',
|
|
1631
1886
|
reason,
|
|
1632
1887
|
retryable: false,
|
|
1633
1888
|
});
|
|
1889
|
+
}
|
|
1634
1890
|
return syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
|
|
1635
1891
|
};
|
|
1636
1892
|
const checkCommand = os.platform() === 'win32' ? 'where' : 'which';
|
|
1637
|
-
const checkResult = await runLocalProcess(checkCommand, ['rsync'], options.preflightTimeout ?? 10_000);
|
|
1893
|
+
const checkResult = await runLocalProcess(checkCommand, ['rsync'], Math.min(options.preflightTimeout ?? 10_000, remainingSftpTimeout(options.deadline, options.signal)));
|
|
1638
1894
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1639
1895
|
if (checkResult.status !== 0) {
|
|
1640
1896
|
const fallback = await fallbackToSftp(checkResult.timedOut ? 'local_rsync_preflight_timeout' : 'local_rsync_unavailable');
|
|
@@ -1646,6 +1902,12 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1646
1902
|
}
|
|
1647
1903
|
// 构建 rsync 参数
|
|
1648
1904
|
const args = ['-avz', '--itemize-changes', '--stats'];
|
|
1905
|
+
if (options.onProgress || options.signal) {
|
|
1906
|
+
args.push('--progress');
|
|
1907
|
+
}
|
|
1908
|
+
if (options.bandwidthLimitKiBps !== undefined) {
|
|
1909
|
+
args.push(`--bwlimit=${options.bandwidthLimitKiBps}K`);
|
|
1910
|
+
}
|
|
1649
1911
|
if (options.delete) {
|
|
1650
1912
|
args.push('--delete');
|
|
1651
1913
|
}
|
|
@@ -1736,7 +1998,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1736
1998
|
: capability.commandShell === 'cmd'
|
|
1737
1999
|
? 'cmd.exe /D /C exit 0'
|
|
1738
2000
|
: 'true',
|
|
1739
|
-
], connectTimeout, childEnv, connection);
|
|
2001
|
+
], Math.min(connectTimeout, remainingSftpTimeout(options.deadline, options.signal)), childEnv, connection);
|
|
1740
2002
|
const connectDuration = Date.now() - connectStartedAt;
|
|
1741
2003
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1742
2004
|
if (connectResult.error || connectResult.status !== 0) {
|
|
@@ -1754,11 +2016,11 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1754
2016
|
};
|
|
1755
2017
|
}
|
|
1756
2018
|
if (capability.commandShell && capability.commandShell !== 'posix') {
|
|
1757
|
-
const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], connectTimeout, childEnv);
|
|
2019
|
+
const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], Math.min(connectTimeout, remainingSftpTimeout(options.deadline, options.signal)), childEnv);
|
|
1758
2020
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1759
2021
|
if (remoteProbe.error || remoteProbe.status !== 0) {
|
|
1760
2022
|
const fallback = await fallbackToSftp(remoteProbe.stderr.trim() || remoteProbe.error?.message || 'remote_rsync_unavailable');
|
|
1761
|
-
const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message ||
|
|
2023
|
+
const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message || 'exit code ' + `${remoteProbe.status}`;
|
|
1762
2024
|
return {
|
|
1763
2025
|
...fallback,
|
|
1764
2026
|
connectDuration,
|
|
@@ -1789,10 +2051,62 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1789
2051
|
const rsyncArgs = direction === 'upload'
|
|
1790
2052
|
? [...args, '-e', sshCmd, '--', rsyncLocalPath, remoteSpec]
|
|
1791
2053
|
: [...args, '-e', sshCmd, '--', remoteSpec, localRsyncPath];
|
|
2054
|
+
if (direction === 'download' && !options.dryRun) {
|
|
2055
|
+
try {
|
|
2056
|
+
remainingSftpTimeout(options.deadline, options.signal);
|
|
2057
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
2058
|
+
fs.mkdirSync(path.dirname(path.resolve(localPath)), { recursive: true });
|
|
2059
|
+
}
|
|
2060
|
+
catch (error) {
|
|
2061
|
+
return {
|
|
2062
|
+
success: false,
|
|
2063
|
+
method: 'rsync',
|
|
2064
|
+
connectDuration,
|
|
2065
|
+
connectStatus: 'completed',
|
|
2066
|
+
operationStatus: 'failed',
|
|
2067
|
+
failureStage: 'local_prepare',
|
|
2068
|
+
retryable: false,
|
|
2069
|
+
output: 'Local download parent preparation failed: ' +
|
|
2070
|
+
(error instanceof Error ? error.message : String(error)),
|
|
2071
|
+
};
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
1792
2074
|
try {
|
|
1793
2075
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1794
|
-
const operationTimeout = options.operationTimeout ?? 600_000;
|
|
1795
|
-
const
|
|
2076
|
+
const operationTimeout = remainingSftpTimeout(options.deadline, options.signal) ?? options.operationTimeout ?? 600_000;
|
|
2077
|
+
const stats = { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 0 };
|
|
2078
|
+
let partial = '';
|
|
2079
|
+
let lastProgress;
|
|
2080
|
+
let discardTail = false;
|
|
2081
|
+
const collectStats = (chunk, final = false) => {
|
|
2082
|
+
for (const [index, segment] of chunk.split(/[\r\n]/).entries()) {
|
|
2083
|
+
if (index > 0) {
|
|
2084
|
+
const progress = /^\s*(?<bytes>[\d,]+)\s+(?<percent>\d+)%\s+[\d.]+[KMGTPE]?B\/s/i.exec(partial);
|
|
2085
|
+
if (progress) {
|
|
2086
|
+
lastProgress = {
|
|
2087
|
+
transferred: Number(progress.groups.bytes.replace(/,/g, '')),
|
|
2088
|
+
total: 0,
|
|
2089
|
+
percent: Number(progress.groups.percent),
|
|
2090
|
+
};
|
|
2091
|
+
options.onProgress?.(lastProgress);
|
|
2092
|
+
}
|
|
2093
|
+
parseRsyncStats(partial, stats);
|
|
2094
|
+
partial = '';
|
|
2095
|
+
discardTail = false;
|
|
2096
|
+
}
|
|
2097
|
+
if (!discardTail) {
|
|
2098
|
+
// Only the itemization prefix and summaries are needed, never an unbounded filename.
|
|
2099
|
+
const available = 65_536 - partial.length;
|
|
2100
|
+
partial += segment.slice(0, available);
|
|
2101
|
+
discardTail = segment.length > available;
|
|
2102
|
+
}
|
|
2103
|
+
}
|
|
2104
|
+
if (final && partial) {
|
|
2105
|
+
parseRsyncStats(partial, stats);
|
|
2106
|
+
partial = '';
|
|
2107
|
+
}
|
|
2108
|
+
};
|
|
2109
|
+
const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout, childEnv, connection, collectStats, options.signal);
|
|
1796
2110
|
if (!isCurrentRsyncGeneration(alias, generation)) {
|
|
1797
2111
|
return {
|
|
1798
2112
|
success: false,
|
|
@@ -1805,8 +2119,26 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1805
2119
|
output: `SSH session '${alias}' changed during rsync; remote transfer state is unknown`,
|
|
1806
2120
|
};
|
|
1807
2121
|
}
|
|
1808
|
-
if (result.error || result.status !== 0) {
|
|
2122
|
+
if (result.cancelled || result.error || result.status !== 0) {
|
|
1809
2123
|
const stderr = result.stderr.trim();
|
|
2124
|
+
if (result.cancelled) {
|
|
2125
|
+
return {
|
|
2126
|
+
success: false,
|
|
2127
|
+
method: 'rsync',
|
|
2128
|
+
connectDuration,
|
|
2129
|
+
connectStatus: 'completed',
|
|
2130
|
+
operationStatus: 'unknown',
|
|
2131
|
+
cancelled: true,
|
|
2132
|
+
timedOut: result.timedOut || undefined,
|
|
2133
|
+
retryable: false,
|
|
2134
|
+
fallbackReason: result.timedOut ? 'rsync_operation_timeout' : 'rsync_operation_cancelled',
|
|
2135
|
+
lastProgress,
|
|
2136
|
+
stats,
|
|
2137
|
+
output: result.stdout +
|
|
2138
|
+
'\nrsync stopped after request cancellation; remote transfer and deletion state is unknown',
|
|
2139
|
+
outputTruncated: result.stdoutTruncated || undefined,
|
|
2140
|
+
};
|
|
2141
|
+
}
|
|
1810
2142
|
const reason = result.timedOut
|
|
1811
2143
|
? `timed out after ${operationTimeout}ms`
|
|
1812
2144
|
: (result.error?.message ?? `exit code ${result.status}`);
|
|
@@ -1820,7 +2152,10 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1820
2152
|
timedOut: true,
|
|
1821
2153
|
retryable: true,
|
|
1822
2154
|
fallbackReason: 'rsync_operation_timeout',
|
|
1823
|
-
output:
|
|
2155
|
+
output: 'rsync ' +
|
|
2156
|
+
`${reason}` +
|
|
2157
|
+
`${stderr ? ` | stderr: ${stderr}` : ''}` +
|
|
2158
|
+
'; remote transfer state is unknown',
|
|
1824
2159
|
};
|
|
1825
2160
|
}
|
|
1826
2161
|
return {
|
|
@@ -1831,11 +2166,13 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1831
2166
|
operationStatus: 'unknown',
|
|
1832
2167
|
retryable: true,
|
|
1833
2168
|
fallbackReason: 'rsync_operation_failed',
|
|
1834
|
-
output:
|
|
2169
|
+
output: 'rsync failed: ' +
|
|
2170
|
+
`${reason}` +
|
|
2171
|
+
`${stderr ? ` | stderr: ${stderr}` : ''}` +
|
|
2172
|
+
'; remote transfer state is unknown',
|
|
1835
2173
|
};
|
|
1836
2174
|
}
|
|
1837
2175
|
const output = result.stdout;
|
|
1838
|
-
const stats = parseRsyncStats(output);
|
|
1839
2176
|
const filesTransferred = stats.added + stats.updated;
|
|
1840
2177
|
return {
|
|
1841
2178
|
success: true,
|
|
@@ -1855,6 +2192,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1855
2192
|
usedKeyPath: Boolean(capability.keyPath),
|
|
1856
2193
|
},
|
|
1857
2194
|
output,
|
|
2195
|
+
outputTruncated: result.stdoutTruncated || undefined,
|
|
1858
2196
|
};
|
|
1859
2197
|
}
|
|
1860
2198
|
catch (e) {
|
|
@@ -1869,7 +2207,9 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1869
2207
|
operationStatus: 'unknown',
|
|
1870
2208
|
retryable: true,
|
|
1871
2209
|
fallbackReason: 'rsync_execution_error',
|
|
1872
|
-
output:
|
|
2210
|
+
output: 'rsync execution failed: ' +
|
|
2211
|
+
`${e instanceof Error ? e.message : String(e)}` +
|
|
2212
|
+
'; remote transfer state is unknown',
|
|
1873
2213
|
};
|
|
1874
2214
|
}
|
|
1875
2215
|
}
|
|
@@ -1943,22 +2283,21 @@ function buildSyncResult(fileCount, totalSize, warnings, skippedSymlinks = 0, st
|
|
|
1943
2283
|
output: warnings.length ? `Warning: ${warnings.join('; ')}` : undefined,
|
|
1944
2284
|
};
|
|
1945
2285
|
}
|
|
1946
|
-
function parseRsyncStats(output) {
|
|
1947
|
-
const stats = { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 0 };
|
|
2286
|
+
function parseRsyncStats(output, stats) {
|
|
1948
2287
|
for (const line of output.split('\n')) {
|
|
1949
2288
|
if (line.startsWith('*deleting')) {
|
|
1950
2289
|
stats.deleted++;
|
|
1951
2290
|
}
|
|
1952
|
-
else if (
|
|
2291
|
+
else if (/^[<>]f\+{9}\s/.test(line)) {
|
|
1953
2292
|
stats.added++;
|
|
1954
2293
|
}
|
|
1955
|
-
else if (
|
|
2294
|
+
else if (/^[<>]f\S{9}\s/.test(line)) {
|
|
1956
2295
|
stats.updated++;
|
|
1957
2296
|
}
|
|
1958
2297
|
}
|
|
1959
|
-
const transferredMatch = output.match(/Number of regular files transferred:\s*(?<count
|
|
2298
|
+
const transferredMatch = output.match(/Number of regular files transferred:\s*(?<count>[\d,]+)/);
|
|
1960
2299
|
if (transferredMatch?.groups && stats.added + stats.updated === 0) {
|
|
1961
|
-
stats.updated = Number(transferredMatch.groups.count);
|
|
2300
|
+
stats.updated = Number(transferredMatch.groups.count.replace(/,/g, ''));
|
|
1962
2301
|
}
|
|
1963
2302
|
return stats;
|
|
1964
2303
|
}
|
|
@@ -1974,27 +2313,32 @@ export function hashLocalFile(filePath, algorithm = 'sha256', timeout, limits) {
|
|
|
1974
2313
|
let bytes = 0;
|
|
1975
2314
|
let settled = false;
|
|
1976
2315
|
const finish = (error) => {
|
|
1977
|
-
if (settled)
|
|
2316
|
+
if (settled) {
|
|
1978
2317
|
return;
|
|
2318
|
+
}
|
|
1979
2319
|
settled = true;
|
|
1980
|
-
if (timer)
|
|
2320
|
+
if (timer) {
|
|
1981
2321
|
clearTimeout(timer);
|
|
2322
|
+
}
|
|
1982
2323
|
if (error) {
|
|
1983
2324
|
stream.destroy();
|
|
1984
2325
|
reject(error);
|
|
1985
2326
|
}
|
|
1986
|
-
else
|
|
2327
|
+
else {
|
|
1987
2328
|
resolve(hash.digest('hex'));
|
|
2329
|
+
}
|
|
1988
2330
|
};
|
|
1989
2331
|
const timer = timeout === undefined
|
|
1990
2332
|
? undefined
|
|
1991
2333
|
: setTimeout(() => finish(new Error(`Local file hashing timed out after ${timeout}ms`)), timeout);
|
|
1992
2334
|
stream.on('data', (chunk) => {
|
|
1993
2335
|
bytes += Buffer.byteLength(chunk);
|
|
1994
|
-
if (bytes > before.size || (limits && bytes > limits.maxBytes))
|
|
2336
|
+
if (bytes > before.size || (limits && bytes > limits.maxBytes)) {
|
|
1995
2337
|
finish(new Error('Local file grew during verification'));
|
|
1996
|
-
|
|
2338
|
+
}
|
|
2339
|
+
else {
|
|
1997
2340
|
hash.update(chunk);
|
|
2341
|
+
}
|
|
1998
2342
|
});
|
|
1999
2343
|
stream.on('error', finish);
|
|
2000
2344
|
stream.on('end', () => {
|
|
@@ -2014,9 +2358,9 @@ export function hashLocalFile(filePath, algorithm = 'sha256', timeout, limits) {
|
|
|
2014
2358
|
});
|
|
2015
2359
|
});
|
|
2016
2360
|
}
|
|
2017
|
-
async function hashRemoteFile(alias, remotePath) {
|
|
2361
|
+
async function hashRemoteFile(alias, remotePath, timeout, signal) {
|
|
2018
2362
|
try {
|
|
2019
|
-
const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256');
|
|
2363
|
+
const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256', timeout, undefined, signal);
|
|
2020
2364
|
return { status: 'available', digest, retryable: false };
|
|
2021
2365
|
}
|
|
2022
2366
|
catch (error) {
|
|
@@ -2027,23 +2371,31 @@ async function hashRemoteFile(alias, remotePath) {
|
|
|
2027
2371
|
};
|
|
2028
2372
|
}
|
|
2029
2373
|
}
|
|
2030
|
-
async function buildSingleFileVerification(alias, localPath, remotePath, direction) {
|
|
2374
|
+
async function buildSingleFileVerification(alias, localPath, remotePath, direction, verifySha256, deadline, signal) {
|
|
2031
2375
|
try {
|
|
2032
2376
|
const localStats = fs.statSync(localPath);
|
|
2033
2377
|
const [remoteInfo, localSha256, remoteHash] = await Promise.all([
|
|
2034
|
-
getFileInfo(alias, remotePath),
|
|
2035
|
-
hashLocalFile(localPath),
|
|
2036
|
-
|
|
2378
|
+
getFileInfo(alias, remotePath, remainingSftpTimeout(deadline, signal), 'posix', signal),
|
|
2379
|
+
verifySha256 ? hashLocalFile(localPath, 'sha256', remainingSftpTimeout(deadline, signal)) : undefined,
|
|
2380
|
+
verifySha256
|
|
2381
|
+
? hashRemoteFile(alias, remotePath, remainingSftpTimeout(deadline, signal), signal)
|
|
2382
|
+
: undefined,
|
|
2037
2383
|
]);
|
|
2384
|
+
const timedOut = signal?.reason instanceof SftpOperationTimeoutError || (deadline !== undefined && Date.now() >= deadline);
|
|
2038
2385
|
return {
|
|
2039
2386
|
kind: 'single_file',
|
|
2040
2387
|
direction,
|
|
2041
|
-
status: remoteHash
|
|
2042
|
-
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2388
|
+
status: timedOut ? 'unknown' : remoteHash?.status === 'error' ? 'error' : 'completed',
|
|
2389
|
+
timedOut: timedOut || undefined,
|
|
2390
|
+
failureStage: timedOut
|
|
2391
|
+
? 'verification_timeout'
|
|
2392
|
+
: remoteHash?.status === 'error'
|
|
2393
|
+
? 'remote_hash'
|
|
2394
|
+
: undefined,
|
|
2395
|
+
retryable: remoteHash?.retryable ?? false,
|
|
2396
|
+
error: remoteHash?.error,
|
|
2397
|
+
hashAlgorithm: verifySha256 ? 'sha256' : undefined,
|
|
2398
|
+
hashMatch: remoteHash?.digest ? localSha256 === remoteHash.digest : undefined,
|
|
2047
2399
|
local: {
|
|
2048
2400
|
size: localStats.size,
|
|
2049
2401
|
mode: (localStats.mode & 0o777).toString(8),
|
|
@@ -2056,27 +2408,31 @@ async function buildSingleFileVerification(alias, localPath, remotePath, directi
|
|
|
2056
2408
|
owner: remoteInfo.owner,
|
|
2057
2409
|
group: remoteInfo.group,
|
|
2058
2410
|
mtime: remoteInfo.mtime,
|
|
2059
|
-
sha256: remoteHash
|
|
2411
|
+
sha256: remoteHash?.digest,
|
|
2060
2412
|
},
|
|
2061
2413
|
};
|
|
2062
2414
|
}
|
|
2063
2415
|
catch (error) {
|
|
2416
|
+
const timedOut = error instanceof SftpOperationTimeoutError ||
|
|
2417
|
+
signal?.reason instanceof SftpOperationTimeoutError ||
|
|
2418
|
+
(deadline !== undefined && Date.now() >= deadline);
|
|
2064
2419
|
return {
|
|
2065
2420
|
kind: 'single_file',
|
|
2066
2421
|
direction,
|
|
2067
|
-
status: 'error',
|
|
2068
|
-
|
|
2422
|
+
status: timedOut ? 'unknown' : 'error',
|
|
2423
|
+
timedOut: timedOut || undefined,
|
|
2424
|
+
failureStage: timedOut ? 'verification_timeout' : 'remote_metadata_or_hash',
|
|
2069
2425
|
retryable: true,
|
|
2070
2426
|
error: error instanceof Error ? error.message : String(error),
|
|
2071
2427
|
};
|
|
2072
2428
|
}
|
|
2073
2429
|
}
|
|
2074
|
-
async function resolveSftpUploadFileTarget(alias, localPath, remotePath, style = 'posix') {
|
|
2430
|
+
async function resolveSftpUploadFileTarget(alias, localPath, remotePath, style = 'posix', deadline, signal) {
|
|
2075
2431
|
if (remotePath.endsWith('/') || remotePath.endsWith('\\')) {
|
|
2076
2432
|
return remoteJoin(style, remotePath, path.basename(localPath));
|
|
2077
2433
|
}
|
|
2078
2434
|
try {
|
|
2079
|
-
const info = await getFileInfo(alias, remotePath);
|
|
2435
|
+
const info = await getFileInfo(alias, remotePath, remainingSftpTimeout(deadline, signal), 'posix', signal);
|
|
2080
2436
|
if (info.isDirectory) {
|
|
2081
2437
|
return remoteJoin(style, remotePath, path.basename(localPath));
|
|
2082
2438
|
}
|
|
@@ -2100,34 +2456,45 @@ function resolveSftpDownloadFileTarget(remotePath, localPath, style = 'posix') {
|
|
|
2100
2456
|
return localPath;
|
|
2101
2457
|
}
|
|
2102
2458
|
async function syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options) {
|
|
2103
|
-
const timeout = options.operationTimeout ?? 600_000;
|
|
2459
|
+
const timeout = remainingSftpTimeout(options.deadline) ?? options.operationTimeout ?? 600_000;
|
|
2460
|
+
const deadline = options.deadline ?? Date.now() + timeout;
|
|
2104
2461
|
const controller = new AbortController();
|
|
2105
2462
|
const accumulator = new DirectoryTransferAccumulator();
|
|
2106
2463
|
const warnings = [];
|
|
2464
|
+
const abort = () => controller.abort(options.signal?.reason);
|
|
2465
|
+
options.signal?.addEventListener('abort', abort, { once: true });
|
|
2466
|
+
if (options.signal?.aborted) {
|
|
2467
|
+
abort();
|
|
2468
|
+
}
|
|
2469
|
+
let nextByteAt = performance.now();
|
|
2470
|
+
const throttle = options.bandwidthLimitKiBps === undefined
|
|
2471
|
+
? undefined
|
|
2472
|
+
: async (bytes) => {
|
|
2473
|
+
nextByteAt =
|
|
2474
|
+
Math.max(nextByteAt, performance.now()) + (bytes * 1000) / (options.bandwidthLimitKiBps * 1024);
|
|
2475
|
+
await delay(Math.ceil(Math.max(0, nextByteAt - performance.now())), undefined, {
|
|
2476
|
+
signal: controller.signal,
|
|
2477
|
+
});
|
|
2478
|
+
};
|
|
2107
2479
|
const operation = syncWithSftp(alias, localPath, remotePath, direction, {
|
|
2108
2480
|
...options,
|
|
2109
2481
|
signal: controller.signal,
|
|
2482
|
+
throttle,
|
|
2483
|
+
deadline,
|
|
2110
2484
|
}, accumulator, warnings);
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2122
|
-
|
|
2123
|
-
});
|
|
2124
|
-
}, timeout);
|
|
2125
|
-
});
|
|
2126
|
-
const result = await Promise.race([operation, timedOut]);
|
|
2127
|
-
if (timer) {
|
|
2128
|
-
clearTimeout(timer);
|
|
2485
|
+
const timer = setTimeout(() => controller.abort(new SftpOperationTimeoutError(timeout)), timeout);
|
|
2486
|
+
try {
|
|
2487
|
+
const result = await operation;
|
|
2488
|
+
return result.verification?.status === 'unknown'
|
|
2489
|
+
? { ...result, operationStatus: 'unknown', timedOut: true }
|
|
2490
|
+
: result;
|
|
2491
|
+
}
|
|
2492
|
+
finally {
|
|
2493
|
+
if (timer) {
|
|
2494
|
+
clearTimeout(timer);
|
|
2495
|
+
}
|
|
2496
|
+
options.signal?.removeEventListener('abort', abort);
|
|
2129
2497
|
}
|
|
2130
|
-
return result;
|
|
2131
2498
|
}
|
|
2132
2499
|
async function syncWithSftp(alias, localPath, remotePath, direction, options, accumulator, warnings) {
|
|
2133
2500
|
throwIfAborted(options.signal);
|
|
@@ -2137,7 +2504,7 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
|
|
|
2137
2504
|
if (options.dryRun) {
|
|
2138
2505
|
const localExists = fs.existsSync(localPath);
|
|
2139
2506
|
const localStats = localExists ? fs.lstatSync(localPath) : undefined;
|
|
2140
|
-
const remoteTargetExists = await remoteExists(alias, remotePath);
|
|
2507
|
+
const remoteTargetExists = await remoteExists(alias, remotePath, options.deadline, options.signal);
|
|
2141
2508
|
return {
|
|
2142
2509
|
success: true,
|
|
2143
2510
|
method: 'sftp',
|
|
@@ -2188,26 +2555,28 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
|
|
|
2188
2555
|
};
|
|
2189
2556
|
}
|
|
2190
2557
|
if (stats.isDirectory()) {
|
|
2191
|
-
const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal, '', options.remotePathStyle, undefined, accumulator);
|
|
2558
|
+
const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal, '', options.remotePathStyle, undefined, accumulator, options.deadline, options.onProgress, options.throttle);
|
|
2192
2559
|
return buildDirectorySyncResult(result, warnings);
|
|
2193
2560
|
}
|
|
2194
|
-
const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle);
|
|
2195
|
-
const existed = await remoteExists(alias, targetRemotePath);
|
|
2196
|
-
const { size } = await uploadFile(alias, localPath, targetRemotePath,
|
|
2561
|
+
const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle, options.deadline, options.signal);
|
|
2562
|
+
const existed = await remoteExists(alias, targetRemotePath, options.deadline, options.signal);
|
|
2563
|
+
const { size } = await uploadFile(alias, localPath, targetRemotePath, options.onProgress, undefined, undefined, options.signal, options.followSymlinks === true, false, remainingSftpTimeout(options.deadline, options.signal), options.throttle);
|
|
2564
|
+
accumulator.addFile(size, existed);
|
|
2197
2565
|
return {
|
|
2198
2566
|
...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
|
|
2199
|
-
verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload'),
|
|
2567
|
+
verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload', options.verifySha256 !== false, options.deadline, options.signal),
|
|
2200
2568
|
};
|
|
2201
2569
|
}
|
|
2202
2570
|
// download
|
|
2203
|
-
const info = await getFileInfo(alias, remotePath,
|
|
2571
|
+
const info = await getFileInfo(alias, remotePath, remainingSftpTimeout(options.deadline, options.signal), options.remotePathStyle, options.signal);
|
|
2204
2572
|
if (info.isDirectory && options.recursive === false) {
|
|
2205
2573
|
return {
|
|
2206
2574
|
success: false,
|
|
2207
2575
|
method: 'sftp',
|
|
2208
2576
|
operationStatus: 'failed',
|
|
2209
2577
|
stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
|
|
2210
|
-
output: 'recursive=false is not supported for directory sources; use a file
|
|
2578
|
+
output: 'recursive=false is not supported for directory sources; use a file ' +
|
|
2579
|
+
'source or enable recursion',
|
|
2211
2580
|
};
|
|
2212
2581
|
}
|
|
2213
2582
|
if (info.isDirectory) {
|
|
@@ -2215,26 +2584,29 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
|
|
|
2215
2584
|
files: 0,
|
|
2216
2585
|
directories: 0,
|
|
2217
2586
|
bytes: 0,
|
|
2218
|
-
}, 0, options.signal, '', options.remotePathStyle, accumulator);
|
|
2587
|
+
}, 0, options.signal, '', options.remotePathStyle, accumulator, options.deadline, options.onProgress, options.throttle);
|
|
2219
2588
|
return buildDirectorySyncResult(result, warnings);
|
|
2220
2589
|
}
|
|
2590
|
+
remainingSftpTimeout(options.deadline, options.signal);
|
|
2221
2591
|
const targetLocalPath = resolveSftpDownloadFileTarget(remotePath, localPath, options.remotePathStyle);
|
|
2222
2592
|
const existed = fs.existsSync(targetLocalPath);
|
|
2223
|
-
const { size } = await downloadFile(alias, remotePath, targetLocalPath,
|
|
2593
|
+
const { size } = await downloadFile(alias, remotePath, targetLocalPath, options.onProgress, undefined, options.signal, remainingSftpTimeout(options.deadline, options.signal), options.throttle);
|
|
2594
|
+
accumulator.addFile(size, existed);
|
|
2224
2595
|
return {
|
|
2225
2596
|
...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
|
|
2226
|
-
verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download'),
|
|
2597
|
+
verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download', options.verifySha256 !== false, options.deadline, options.signal),
|
|
2227
2598
|
};
|
|
2228
2599
|
}
|
|
2229
2600
|
catch (err) {
|
|
2230
2601
|
const result = buildDirectorySyncResult(accumulator.toResult(), warnings);
|
|
2231
|
-
const
|
|
2602
|
+
const outcome = sftpOperationOutcome(err);
|
|
2603
|
+
const timedOut = err instanceof SftpOperationTimeoutError || outcome.timeout !== undefined;
|
|
2232
2604
|
return {
|
|
2233
2605
|
...result,
|
|
2234
2606
|
success: false,
|
|
2235
|
-
operationStatus:
|
|
2607
|
+
operationStatus: 'failed',
|
|
2608
|
+
...outcome,
|
|
2236
2609
|
timedOut: timedOut || undefined,
|
|
2237
|
-
retryable: timedOut || undefined,
|
|
2238
2610
|
stats: { ...result.stats, failed: Math.max(1, result.stats.failed) },
|
|
2239
2611
|
output: [result.output, err instanceof Error ? err.message : String(err)].filter(Boolean).join('; '),
|
|
2240
2612
|
};
|
|
@@ -2243,13 +2615,15 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options, ac
|
|
|
2243
2615
|
/**
|
|
2244
2616
|
* 递归上传目录
|
|
2245
2617
|
*/
|
|
2246
|
-
async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', inspectNames, accumulator = new DirectoryTransferAccumulator()) {
|
|
2618
|
+
async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', inspectNames, accumulator = new DirectoryTransferAccumulator(), deadline, onProgress, throttle) {
|
|
2247
2619
|
throwIfAborted(signal);
|
|
2248
2620
|
recordSftpDirectory(traversal, depth, localPath);
|
|
2621
|
+
const realPath = fs.realpathSync(localPath);
|
|
2249
2622
|
const ownSftp = !sharedSftp && !inspectNames;
|
|
2250
|
-
const sftp = inspectNames
|
|
2623
|
+
const sftp = inspectNames
|
|
2624
|
+
? undefined
|
|
2625
|
+
: (sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal)));
|
|
2251
2626
|
const activeDirectories = (traversal.activeDirectories ??= new Set());
|
|
2252
|
-
const realPath = fs.realpathSync(localPath);
|
|
2253
2627
|
if (activeDirectories.has(realPath)) {
|
|
2254
2628
|
accumulator.addSkippedSymlink();
|
|
2255
2629
|
if (ownSftp) {
|
|
@@ -2260,8 +2634,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2260
2634
|
activeDirectories.add(realPath);
|
|
2261
2635
|
try {
|
|
2262
2636
|
validateLocalPathAgainstAllowList(localPath);
|
|
2637
|
+
remainingSftpTimeout(deadline, signal);
|
|
2263
2638
|
if (!inspectNames) {
|
|
2264
|
-
await mkdir(alias, remotePath, true, sftp,
|
|
2639
|
+
await mkdir(alias, remotePath, true, sftp, remainingSftpTimeout(deadline, signal), style, signal);
|
|
2265
2640
|
}
|
|
2266
2641
|
throwIfAborted(signal);
|
|
2267
2642
|
const items = fs.readdirSync(localPath);
|
|
@@ -2275,6 +2650,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2275
2650
|
}
|
|
2276
2651
|
const itemLocalPath = path.join(localPath, item);
|
|
2277
2652
|
const stats = followSymlinks ? fs.statSync(itemLocalPath) : fs.lstatSync(itemLocalPath);
|
|
2653
|
+
if (matchesDirectoryExclude(itemRelativePath, exclude, stats.isDirectory())) {
|
|
2654
|
+
continue;
|
|
2655
|
+
}
|
|
2278
2656
|
if (!followSymlinks && stats.isSymbolicLink()) {
|
|
2279
2657
|
if (!inspectNames) {
|
|
2280
2658
|
console.warn(`[ssh_sync] skipping symlink: ${itemLocalPath}`);
|
|
@@ -2305,9 +2683,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2305
2683
|
await runWithConcurrency(fileItems, SFTP_PARALLEL_LIMIT, async ({ name, stats }) => {
|
|
2306
2684
|
const itemLocalPath = path.join(localPath, name);
|
|
2307
2685
|
const itemRemotePath = remoteJoin(style, remotePath, name);
|
|
2308
|
-
const existed = await sftpExists(sftp, itemRemotePath);
|
|
2686
|
+
const existed = await sftpExists(sftp, itemRemotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
2309
2687
|
try {
|
|
2310
|
-
await uploadFile(alias, itemLocalPath, itemRemotePath,
|
|
2688
|
+
await uploadFile(alias, itemLocalPath, itemRemotePath, onProgress, sftp, undefined, signal, followSymlinks, false, remainingSftpTimeout(deadline, signal), throttle);
|
|
2311
2689
|
accumulator.addFile(stats.size, existed);
|
|
2312
2690
|
}
|
|
2313
2691
|
catch (error) {
|
|
@@ -2320,7 +2698,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2320
2698
|
for (const item of subDirItems) {
|
|
2321
2699
|
const itemLocalPath = path.join(localPath, item);
|
|
2322
2700
|
const itemRemotePath = remoteJoin(style, remotePath, item);
|
|
2323
|
-
await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames, accumulator);
|
|
2701
|
+
await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames, accumulator, deadline, onProgress, throttle);
|
|
2324
2702
|
}
|
|
2325
2703
|
}
|
|
2326
2704
|
finally {
|
|
@@ -2334,23 +2712,21 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2334
2712
|
/**
|
|
2335
2713
|
* 递归下载目录
|
|
2336
2714
|
*/
|
|
2337
|
-
async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', accumulator = new DirectoryTransferAccumulator()) {
|
|
2715
|
+
async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', accumulator = new DirectoryTransferAccumulator(), deadline, onProgress, throttle) {
|
|
2338
2716
|
throwIfAborted(signal);
|
|
2339
2717
|
recordSftpDirectory(traversal, depth, remotePath);
|
|
2340
2718
|
const ownSftp = !sharedSftp;
|
|
2341
|
-
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias));
|
|
2719
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
|
|
2342
2720
|
try {
|
|
2343
|
-
|
|
2721
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
2722
|
+
const items = await listDir(alias, remotePath, true, sftp, remainingSftpTimeout(deadline, signal), style, signal);
|
|
2344
2723
|
throwIfAborted(signal);
|
|
2345
2724
|
if (process.platform === 'win32') {
|
|
2346
|
-
const
|
|
2347
|
-
name.normalize('NFC').toLowerCase(),
|
|
2348
|
-
name,
|
|
2349
|
-
]));
|
|
2350
|
-
const remoteNames = new Set();
|
|
2725
|
+
const remoteNames = [];
|
|
2351
2726
|
for (const item of items) {
|
|
2352
2727
|
const relativeName = relativeBase ? `${relativeBase}/${item.name}` : item.name;
|
|
2353
|
-
if (matchesDirectoryExclude(relativeName, exclude
|
|
2728
|
+
if (matchesDirectoryExclude(relativeName, exclude, item.isDirectory) ||
|
|
2729
|
+
(!item.isDirectory && !item.isFile)) {
|
|
2354
2730
|
continue;
|
|
2355
2731
|
}
|
|
2356
2732
|
if (/[<>:"/\\|?*]/.test(item.name) ||
|
|
@@ -2359,13 +2735,12 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2359
2735
|
/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i.test(item.name)) {
|
|
2360
2736
|
throw new Error(`Remote entry cannot be represented as a Windows filename: ${relativeName}`);
|
|
2361
2737
|
}
|
|
2362
|
-
|
|
2363
|
-
if (remoteNames.has(key) || (localNames.has(key) && localNames.get(key) !== item.name)) {
|
|
2364
|
-
throw new Error(`Windows local filename collision at ${relativeName}`);
|
|
2365
|
-
}
|
|
2366
|
-
remoteNames.add(key);
|
|
2738
|
+
remoteNames.push(item.name);
|
|
2367
2739
|
}
|
|
2740
|
+
verifyWindowsDownloadNames(localPath, remoteNames, remainingSftpTimeout(deadline, signal));
|
|
2368
2741
|
}
|
|
2742
|
+
remainingSftpTimeout(deadline, signal);
|
|
2743
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
2369
2744
|
if (!fs.existsSync(localPath)) {
|
|
2370
2745
|
fs.mkdirSync(localPath, { recursive: true });
|
|
2371
2746
|
}
|
|
@@ -2375,7 +2750,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2375
2750
|
const symlinkItems = [];
|
|
2376
2751
|
for (const item of items) {
|
|
2377
2752
|
const itemRelativePath = relativeBase ? `${relativeBase}/${item.name}` : item.name;
|
|
2378
|
-
if (matchesDirectoryExclude(itemRelativePath, exclude)) {
|
|
2753
|
+
if (matchesDirectoryExclude(itemRelativePath, exclude, item.isDirectory)) {
|
|
2379
2754
|
continue;
|
|
2380
2755
|
}
|
|
2381
2756
|
if (item.isDirectory) {
|
|
@@ -2396,7 +2771,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2396
2771
|
const itemLocalPath = path.join(localPath, item.name);
|
|
2397
2772
|
const existed = fs.existsSync(itemLocalPath);
|
|
2398
2773
|
try {
|
|
2399
|
-
await downloadFile(alias, item.path, itemLocalPath,
|
|
2774
|
+
await downloadFile(alias, item.path, itemLocalPath, onProgress, sftp, signal, remainingSftpTimeout(deadline, signal), throttle);
|
|
2400
2775
|
accumulator.addFile(item.size, existed);
|
|
2401
2776
|
}
|
|
2402
2777
|
catch (error) {
|
|
@@ -2406,7 +2781,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2406
2781
|
});
|
|
2407
2782
|
for (const item of dirItems) {
|
|
2408
2783
|
const itemLocalPath = path.join(localPath, item.name);
|
|
2409
|
-
await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style, accumulator);
|
|
2784
|
+
await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style, accumulator, deadline, onProgress, throttle);
|
|
2410
2785
|
}
|
|
2411
2786
|
for (const item of symlinkItems) {
|
|
2412
2787
|
console.warn(`[ssh_sync] skipping remote symlink: ${item.path}`);
|