@pyrokine/mcp-ssh 3.0.0 → 3.2.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 +423 -102
- package/README_zh.md +300 -74
- package/dist/command-risk.js +5 -2
- package/dist/directory-verification.d.ts +10 -3
- package/dist/directory-verification.js +271 -55
- package/dist/file-mode.js +25 -13
- package/dist/file-ops.d.ts +20 -6
- package/dist/file-ops.js +669 -282
- package/dist/forward-manager.js +6 -1
- package/dist/known-hosts.d.ts +22 -0
- package/dist/known-hosts.js +553 -0
- package/dist/openssh.d.ts +51 -0
- package/dist/openssh.js +553 -0
- package/dist/operation-manager.d.ts +2 -3
- package/dist/operation-manager.js +33 -5
- 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 +40 -4
- package/dist/session-manager.js +377 -61
- package/dist/sftp-timeout.d.ts +16 -1
- package/dist/sftp-timeout.js +104 -12
- package/dist/ssh-config.js +1 -1
- package/dist/tools/connection.js +89 -5
- package/dist/tools/exec.js +90 -37
- package/dist/tools/file.js +457 -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/types.d.ts +46 -5
- 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 -5
package/dist/file-ops.js
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* SSH File Operations - 文件操作
|
|
3
3
|
*/
|
|
4
|
-
import { spawn } from 'child_process';
|
|
4
|
+
import { spawn, spawnSync } from 'child_process';
|
|
5
5
|
import { createHash, randomBytes } from 'crypto';
|
|
6
6
|
import * as fs from 'fs';
|
|
7
|
+
import { Readable } from 'node:stream';
|
|
8
|
+
import { StringDecoder } from 'node:string_decoder';
|
|
7
9
|
import * as os from 'os';
|
|
8
10
|
import * as path from 'path';
|
|
9
|
-
import { checkWindowsAcl, windowsAclScript } from './windows-acl.js';
|
|
10
11
|
import { matchesDirectoryExclude } from './directory-verification.js';
|
|
11
12
|
import { extractFileMode, FILE_CREATE_MODE_MASK, formatFileMode, formatFilePermissions } from './file-mode.js';
|
|
13
|
+
import { OpenSshError } from './openssh.js';
|
|
14
|
+
import { encodedPowershell, remoteDirname, remoteJoin, remoteNormalize, remotePathModule, remoteShellPath, windowsCaseSemanticsScript, } from './remote-platform.js';
|
|
12
15
|
import { sessionManager } from './session-manager.js';
|
|
13
|
-
import { SftpOperationTimeoutError, withSftpOperationTimeout } from './sftp-timeout.js';
|
|
16
|
+
import { SftpOperationTimeoutError, SftpWriteState, sftpOperationOutcome, withSftpOperationTimeout, } from './sftp-timeout.js';
|
|
14
17
|
import { escapeShellArg, expandTilde } from './tools/utils.js';
|
|
15
|
-
import { encodedPowershell, remoteDirname, remoteJoin, remoteNormalize, remotePathModule, remoteShellPath, windowsCaseSemanticsScript, } from './remote-platform.js';
|
|
16
18
|
import { ensureUtf8Boundary, isUtf8ContinuationByte } from './utf8.js';
|
|
19
|
+
import { checkWindowsAcl, windowsAclScript } from './windows-acl.js';
|
|
17
20
|
// 文件类型 mode 常量
|
|
18
21
|
const S_IFMT = 0o170000; // 文件类型位掩码
|
|
19
22
|
const S_IFDIR = 0o40000;
|
|
@@ -31,25 +34,48 @@ function appendLimitedOutput(current, chunk) {
|
|
|
31
34
|
}
|
|
32
35
|
return (current + chunk).slice(0, LOCAL_PROCESS_OUTPUT_LIMIT);
|
|
33
36
|
}
|
|
34
|
-
function runLocalProcess(command, args, timeout, env) {
|
|
37
|
+
function runLocalProcess(command, args, timeout, env, connection, onStdout) {
|
|
35
38
|
return new Promise((resolve) => {
|
|
36
39
|
let stdout = '';
|
|
37
40
|
let stderr = '';
|
|
41
|
+
const stdoutDecoder = new StringDecoder('utf8');
|
|
42
|
+
let stdoutTruncated = false;
|
|
38
43
|
let settled = false;
|
|
39
44
|
let timedOut = false;
|
|
40
45
|
let killTimer;
|
|
46
|
+
connection?.assertCurrent();
|
|
41
47
|
const child = spawn(command, args, {
|
|
42
48
|
stdio: ['ignore', 'pipe', 'pipe'],
|
|
43
49
|
env: env ?? process.env,
|
|
44
50
|
windowsHide: true,
|
|
51
|
+
detached: connection !== undefined,
|
|
45
52
|
});
|
|
53
|
+
connection?.trackProcess(child, true);
|
|
54
|
+
const kill = (signal) => {
|
|
55
|
+
if (connection && child.pid) {
|
|
56
|
+
try {
|
|
57
|
+
process.kill(-child.pid, signal);
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
if (error.code !== 'ESRCH') {
|
|
61
|
+
console.error('Local process cleanup failed:', error);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
child.kill(signal);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
46
69
|
const timer = setTimeout(() => {
|
|
47
70
|
timedOut = true;
|
|
48
|
-
|
|
49
|
-
killTimer = setTimeout(() =>
|
|
71
|
+
kill('SIGTERM');
|
|
72
|
+
killTimer = setTimeout(() => kill('SIGKILL'), 5000);
|
|
50
73
|
}, timeout);
|
|
51
74
|
child.stdout.on('data', (chunk) => {
|
|
52
|
-
|
|
75
|
+
const text = stdoutDecoder.write(chunk);
|
|
76
|
+
onStdout?.(text);
|
|
77
|
+
stdoutTruncated ||= stdout.length + text.length > LOCAL_PROCESS_OUTPUT_LIMIT;
|
|
78
|
+
stdout = appendLimitedOutput(stdout, text);
|
|
53
79
|
});
|
|
54
80
|
child.stderr.on('data', (chunk) => {
|
|
55
81
|
stderr = appendLimitedOutput(stderr, chunk.toString('utf-8'));
|
|
@@ -63,7 +89,8 @@ function runLocalProcess(command, args, timeout, env) {
|
|
|
63
89
|
if (killTimer) {
|
|
64
90
|
clearTimeout(killTimer);
|
|
65
91
|
}
|
|
66
|
-
|
|
92
|
+
onStdout?.(stdoutDecoder.end(), true);
|
|
93
|
+
resolve({ status: null, stdout, stderr, error, timedOut, stdoutTruncated });
|
|
67
94
|
});
|
|
68
95
|
child.on('close', (status) => {
|
|
69
96
|
if (settled) {
|
|
@@ -74,7 +101,11 @@ function runLocalProcess(command, args, timeout, env) {
|
|
|
74
101
|
if (killTimer) {
|
|
75
102
|
clearTimeout(killTimer);
|
|
76
103
|
}
|
|
77
|
-
|
|
104
|
+
const tail = stdoutDecoder.end();
|
|
105
|
+
onStdout?.(tail, true);
|
|
106
|
+
stdoutTruncated ||= stdout.length + tail.length > LOCAL_PROCESS_OUTPUT_LIMIT;
|
|
107
|
+
stdout = appendLimitedOutput(stdout, tail);
|
|
108
|
+
resolve({ status, stdout, stderr, timedOut, stdoutTruncated });
|
|
78
109
|
});
|
|
79
110
|
});
|
|
80
111
|
}
|
|
@@ -90,7 +121,7 @@ function isCurrentRsyncGeneration(alias, generation) {
|
|
|
90
121
|
}
|
|
91
122
|
function assertCurrentRsyncGeneration(alias, generation) {
|
|
92
123
|
if (!isCurrentRsyncGeneration(alias, generation)) {
|
|
93
|
-
throw new Error(
|
|
124
|
+
throw new Error("SSH session '" + `${alias}` + "' changed during rsync setup; retry the sync on the current session");
|
|
94
125
|
}
|
|
95
126
|
}
|
|
96
127
|
/** 由 SessionManager.disconnect 调用,防止 alias 重连后读到旧主机的判断 */
|
|
@@ -158,18 +189,29 @@ async function runWithConcurrency(items, limit, worker) {
|
|
|
158
189
|
}
|
|
159
190
|
const it = items[Symbol.iterator]();
|
|
160
191
|
const runners = [];
|
|
192
|
+
let failed = false;
|
|
161
193
|
for (let i = 0; i < Math.min(limit, items.length); i++) {
|
|
162
194
|
runners.push((async () => {
|
|
163
|
-
while (
|
|
195
|
+
while (!failed) {
|
|
164
196
|
const next = it.next();
|
|
165
197
|
if (next.done) {
|
|
166
198
|
return;
|
|
167
199
|
}
|
|
168
|
-
|
|
200
|
+
try {
|
|
201
|
+
await worker(next.value);
|
|
202
|
+
}
|
|
203
|
+
catch (error) {
|
|
204
|
+
failed = true;
|
|
205
|
+
throw error;
|
|
206
|
+
}
|
|
169
207
|
}
|
|
170
208
|
})());
|
|
171
209
|
}
|
|
172
|
-
await Promise.
|
|
210
|
+
const settled = await Promise.allSettled(runners);
|
|
211
|
+
const failure = settled.find((result) => result.status === 'rejected');
|
|
212
|
+
if (failure?.status === 'rejected') {
|
|
213
|
+
throw failure.reason;
|
|
214
|
+
}
|
|
173
215
|
}
|
|
174
216
|
/**
|
|
175
217
|
* 校验本地路径是否在 SSH_MCP_FILE_OPS_ALLOW_DIRS 白名单内
|
|
@@ -184,7 +226,7 @@ export function convertRsyncPath(value, profile) {
|
|
|
184
226
|
if (profile === 'native-win32' || profile === 'posix') {
|
|
185
227
|
return value;
|
|
186
228
|
}
|
|
187
|
-
const drive = /^([a-zA-Z]):[\\/](
|
|
229
|
+
const drive = /^(?<drive>[a-zA-Z]):[\\/](?<path>.*)$/.exec(value);
|
|
188
230
|
if (drive) {
|
|
189
231
|
const prefix = profile === 'cygwin' ? '/cygdrive/' : '/';
|
|
190
232
|
return `${prefix}${drive[1].toLowerCase()}/${drive[2].replace(/\\/g, '/')}`;
|
|
@@ -199,7 +241,7 @@ export function quoteRsyncSshArg(value, profile = 'posix') {
|
|
|
199
241
|
if (/[\r\n\0]/.test(text)) {
|
|
200
242
|
throw new Error('rsync SSH argument contains a newline or NUL');
|
|
201
243
|
}
|
|
202
|
-
return profile === 'posix' ? escapeShellArg(text) : `"${text.replace(/([\\"])/g, '\\$1')}"`;
|
|
244
|
+
return profile === 'posix' ? escapeShellArg(text) : `"${text.replace(/(?<character>[\\"])/g, '\\$1')}"`;
|
|
203
245
|
}
|
|
204
246
|
function configuredAllowDirectories() {
|
|
205
247
|
const allowEnv = process.env.SSH_MCP_FILE_OPS_ALLOW_DIRS;
|
|
@@ -271,8 +313,8 @@ function validateLocalUploadSource(localPath, followSymlinks, symlinkError) {
|
|
|
271
313
|
throw new Error(symlinkError);
|
|
272
314
|
}
|
|
273
315
|
if (followSymlinks && getAllowListStatus(expandedPath).configured) {
|
|
274
|
-
throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS
|
|
275
|
-
'symlink targets can change during transfer');
|
|
316
|
+
throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS ' +
|
|
317
|
+
'because symlink targets can change during transfer');
|
|
276
318
|
}
|
|
277
319
|
return {
|
|
278
320
|
expandedPath,
|
|
@@ -320,16 +362,32 @@ export function probeLocalPath(localPath, followSymlinks = false) {
|
|
|
320
362
|
/**
|
|
321
363
|
* sftp.stat 的 Promise 包装
|
|
322
364
|
*/
|
|
323
|
-
|
|
365
|
+
function remainingSftpTimeout(deadline, signal) {
|
|
366
|
+
throwIfAborted(signal);
|
|
367
|
+
if (deadline === undefined) {
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
const remaining = deadline - Date.now();
|
|
371
|
+
if (remaining <= 0) {
|
|
372
|
+
throw new SftpOperationTimeoutError(0);
|
|
373
|
+
}
|
|
374
|
+
return remaining;
|
|
375
|
+
}
|
|
376
|
+
async function getSftpWithinDeadline(alias, timeout, signal) {
|
|
377
|
+
throwIfAborted(signal);
|
|
378
|
+
if (timeout !== undefined && timeout <= 0) {
|
|
379
|
+
throw new SftpOperationTimeoutError(timeout);
|
|
380
|
+
}
|
|
324
381
|
const request = sessionManager.getSftp(alias);
|
|
325
382
|
return withSftpOperationTimeout(request, timeout, () => {
|
|
326
383
|
void request.then((sftp) => sftp.end(), () => undefined);
|
|
327
|
-
});
|
|
384
|
+
}, signal);
|
|
328
385
|
}
|
|
329
|
-
function withSftpDeadline(sftp, operation, timeout) {
|
|
330
|
-
return withSftpOperationTimeout(operation, timeout, () => sftp.end());
|
|
386
|
+
function withSftpDeadline(sftp, operation, timeout, signal) {
|
|
387
|
+
return withSftpOperationTimeout(operation, timeout, () => sftp.end(), signal);
|
|
331
388
|
}
|
|
332
|
-
function sftpStat(sftp, remotePath, timeout) {
|
|
389
|
+
function sftpStat(sftp, remotePath, timeout, signal) {
|
|
390
|
+
throwIfAborted(signal);
|
|
333
391
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
334
392
|
sftp.stat(remotePath, (err, stats) => {
|
|
335
393
|
if (err) {
|
|
@@ -339,7 +397,7 @@ function sftpStat(sftp, remotePath, timeout) {
|
|
|
339
397
|
resolve(stats);
|
|
340
398
|
}
|
|
341
399
|
});
|
|
342
|
-
}), timeout);
|
|
400
|
+
}), timeout, signal);
|
|
343
401
|
}
|
|
344
402
|
function isSftpMissingError(error) {
|
|
345
403
|
if (!error || typeof error !== 'object') {
|
|
@@ -355,9 +413,9 @@ function isSftpMissingError(error) {
|
|
|
355
413
|
function isLocalMissingError(error) {
|
|
356
414
|
return Boolean(error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT');
|
|
357
415
|
}
|
|
358
|
-
async function sftpExists(sftp, remotePath) {
|
|
416
|
+
async function sftpExists(sftp, remotePath, timeout, signal) {
|
|
359
417
|
try {
|
|
360
|
-
await sftpStat(sftp, remotePath);
|
|
418
|
+
await sftpStat(sftp, remotePath, timeout, signal);
|
|
361
419
|
return true;
|
|
362
420
|
}
|
|
363
421
|
catch (error) {
|
|
@@ -367,21 +425,37 @@ async function sftpExists(sftp, remotePath) {
|
|
|
367
425
|
throw error;
|
|
368
426
|
}
|
|
369
427
|
}
|
|
370
|
-
async function remoteExists(alias, remotePath) {
|
|
371
|
-
const sftp = await getSftpWithinDeadline(alias);
|
|
428
|
+
async function remoteExists(alias, remotePath, deadline, signal) {
|
|
429
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
372
430
|
try {
|
|
373
|
-
return await sftpExists(sftp, remotePath);
|
|
431
|
+
return await sftpExists(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
374
432
|
}
|
|
375
433
|
finally {
|
|
376
434
|
sftp.end();
|
|
377
435
|
}
|
|
378
436
|
}
|
|
379
|
-
export async function hashRemoteFileDigest(alias, remotePath, algorithm) {
|
|
380
|
-
const
|
|
437
|
+
export async function hashRemoteFileDigest(alias, remotePath, algorithm, timeout, limits, signal) {
|
|
438
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
439
|
+
throwIfAborted(signal);
|
|
440
|
+
const connection = sessionManager.getOpenSshConnection(alias);
|
|
441
|
+
if (connection) {
|
|
442
|
+
return connection.hashFile(remotePath, algorithm, timeout ?? 600_000, limits?.maxBytes, limits?.expectedSize);
|
|
443
|
+
}
|
|
444
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
445
|
+
let transferTimeout;
|
|
446
|
+
try {
|
|
447
|
+
transferTimeout = remainingSftpTimeout(deadline, signal);
|
|
448
|
+
}
|
|
449
|
+
catch (error) {
|
|
450
|
+
sftp.end();
|
|
451
|
+
throw error;
|
|
452
|
+
}
|
|
381
453
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
382
454
|
const hash = createHash(algorithm);
|
|
455
|
+
throwIfAborted(signal);
|
|
383
456
|
const stream = sftp.createReadStream(remotePath);
|
|
384
457
|
let settled = false;
|
|
458
|
+
let bytes = 0;
|
|
385
459
|
const finish = (error) => {
|
|
386
460
|
if (settled) {
|
|
387
461
|
return;
|
|
@@ -395,10 +469,27 @@ export async function hashRemoteFileDigest(alias, remotePath, algorithm) {
|
|
|
395
469
|
resolve(hash.digest('hex'));
|
|
396
470
|
}
|
|
397
471
|
};
|
|
398
|
-
stream.on('data', (chunk) =>
|
|
472
|
+
stream.on('data', (chunk) => {
|
|
473
|
+
if (settled) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
bytes += chunk.length;
|
|
477
|
+
if (limits && (bytes > limits.maxBytes || bytes > limits.expectedSize)) {
|
|
478
|
+
stream.destroy();
|
|
479
|
+
finish(new Error('Remote file grew or exceeded the verification byte budget'));
|
|
480
|
+
}
|
|
481
|
+
else {
|
|
482
|
+
hash.update(chunk);
|
|
483
|
+
}
|
|
484
|
+
});
|
|
399
485
|
stream.once('error', finish);
|
|
400
|
-
stream.once('end', () => finish(
|
|
401
|
-
|
|
486
|
+
stream.once('end', () => finish(limits && bytes !== limits.expectedSize
|
|
487
|
+
? new Error('Remote file changed during verification')
|
|
488
|
+
: undefined));
|
|
489
|
+
}), transferTimeout, signal).catch((error) => {
|
|
490
|
+
sftp.end();
|
|
491
|
+
throw error;
|
|
492
|
+
});
|
|
402
493
|
}
|
|
403
494
|
export async function atomicReplaceCapability(alias, timeout) {
|
|
404
495
|
const sftp = await getSftpWithinDeadline(alias, timeout);
|
|
@@ -420,7 +511,14 @@ export class RemoteRenameNotStartedError extends Error {
|
|
|
420
511
|
}
|
|
421
512
|
}
|
|
422
513
|
export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExisting = false, timeout) {
|
|
423
|
-
const
|
|
514
|
+
const state = new SftpWriteState(sourcePath);
|
|
515
|
+
const sftp = await getSftpWithinDeadline(alias, timeout).catch((error) => {
|
|
516
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
517
|
+
cause: error,
|
|
518
|
+
details: state.outcome(error),
|
|
519
|
+
});
|
|
520
|
+
});
|
|
521
|
+
const tracked = state.track(sftp);
|
|
424
522
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
425
523
|
const callback = (error) => {
|
|
426
524
|
sftp.end();
|
|
@@ -433,17 +531,23 @@ export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExi
|
|
|
433
531
|
};
|
|
434
532
|
try {
|
|
435
533
|
if (replaceExisting) {
|
|
436
|
-
|
|
534
|
+
tracked.ext_openssh_rename(sourcePath, targetPath, callback);
|
|
437
535
|
}
|
|
438
536
|
else {
|
|
439
|
-
|
|
537
|
+
tracked.rename(sourcePath, targetPath, callback);
|
|
440
538
|
}
|
|
441
539
|
}
|
|
442
540
|
catch (error) {
|
|
443
541
|
sftp.end();
|
|
444
|
-
reject(new RemoteRenameNotStartedError(
|
|
542
|
+
reject(new RemoteRenameNotStartedError('SFTP rename request was not sent: ' +
|
|
543
|
+
`${error instanceof Error ? error.message : String(error)}`));
|
|
445
544
|
}
|
|
446
|
-
}), timeout)
|
|
545
|
+
}), timeout).catch((error) => {
|
|
546
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
547
|
+
cause: error,
|
|
548
|
+
details: { ...state.outcome(error), targetPath },
|
|
549
|
+
});
|
|
550
|
+
});
|
|
447
551
|
}
|
|
448
552
|
export async function removeRemoteFile(alias, remotePath, timeout) {
|
|
449
553
|
const sftp = await getSftpWithinDeadline(alias, timeout);
|
|
@@ -516,6 +620,18 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
|
|
|
516
620
|
if (settled) {
|
|
517
621
|
return;
|
|
518
622
|
}
|
|
623
|
+
if (writeStream.errored) {
|
|
624
|
+
cleanup(writeStream.errored);
|
|
625
|
+
return;
|
|
626
|
+
}
|
|
627
|
+
// ssh2 在 _final 中先关闭流,按已确认字节数判断完成
|
|
628
|
+
if (!writeStream.writableFinished &&
|
|
629
|
+
(!writeStream.writableEnded ||
|
|
630
|
+
writeStream.writableLength !== 0 ||
|
|
631
|
+
writeStream.bytesWritten !== totalSize)) {
|
|
632
|
+
cleanup(new Error('Transfer write stream closed before finish'));
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
519
635
|
if (transferred !== totalSize) {
|
|
520
636
|
cleanup(new Error(`Transfer size mismatch: expected ${totalSize} bytes, received ${transferred}`));
|
|
521
637
|
return;
|
|
@@ -543,15 +659,17 @@ export class UploadFileError extends Error {
|
|
|
543
659
|
this.remoteCreated = remoteCreated;
|
|
544
660
|
this.name = 'UploadFileError';
|
|
545
661
|
const cause = options?.cause;
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
662
|
+
const causeDetails = cause && typeof cause === 'object' && 'details' in cause
|
|
663
|
+
? cause.details
|
|
664
|
+
: undefined;
|
|
665
|
+
this.details = options?.details ? { ...causeDetails, ...options.details } : causeDetails;
|
|
549
666
|
}
|
|
550
667
|
}
|
|
551
668
|
/**
|
|
552
669
|
* 上传文件
|
|
553
670
|
*/
|
|
554
671
|
export async function uploadFile(alias, localPath, remotePath, onProgress, sharedSftp, createMode, signal, followSymlinks = false, exclusive = false, timeout) {
|
|
672
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
555
673
|
throwIfAborted(signal);
|
|
556
674
|
const source = followSymlinks
|
|
557
675
|
? validateLocalUploadSource(localPath, true, 'Refusing to upload symlink at top level (use ssh_sync with followSymlinks=true to traverse)')
|
|
@@ -564,21 +682,47 @@ export async function uploadFile(alias, localPath, remotePath, onProgress, share
|
|
|
564
682
|
if (!stats.isFile()) {
|
|
565
683
|
throw new Error('UPLOAD_PATH_IS_NOT_FILE: localPath is not a regular file');
|
|
566
684
|
}
|
|
567
|
-
const
|
|
685
|
+
const state = new SftpWriteState(remotePath);
|
|
686
|
+
const sftp = sharedSftp ??
|
|
687
|
+
(await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal).catch((error) => {
|
|
688
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
689
|
+
cause: error,
|
|
690
|
+
details: state.outcome(error),
|
|
691
|
+
});
|
|
692
|
+
}));
|
|
693
|
+
try {
|
|
694
|
+
remainingSftpTimeout(deadline, signal);
|
|
695
|
+
}
|
|
696
|
+
catch (error) {
|
|
697
|
+
if (!sharedSftp) {
|
|
698
|
+
sftp.end();
|
|
699
|
+
}
|
|
700
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
701
|
+
cause: error,
|
|
702
|
+
details: state.outcome(error),
|
|
703
|
+
});
|
|
704
|
+
}
|
|
568
705
|
const totalSize = stats.size;
|
|
569
706
|
const safeCreateMode = (createMode ?? stats.mode) & FILE_CREATE_MODE_MASK;
|
|
570
|
-
const writeStream = sftp.createWriteStream(remotePath, { mode: safeCreateMode, flags: exclusive ? 'wx' : 'w' });
|
|
571
|
-
let remoteCreated = false;
|
|
572
|
-
writeStream.once('open', () => {
|
|
573
|
-
remoteCreated = true;
|
|
574
|
-
});
|
|
575
707
|
try {
|
|
576
|
-
|
|
708
|
+
remainingSftpTimeout(deadline, signal);
|
|
709
|
+
const writeStream = state.track(sftp).createWriteStream(remotePath, {
|
|
710
|
+
mode: safeCreateMode,
|
|
711
|
+
flags: exclusive ? 'wx' : 'w',
|
|
712
|
+
});
|
|
713
|
+
writeStream.once('open', () => {
|
|
714
|
+
state.remoteCreated = true;
|
|
715
|
+
});
|
|
716
|
+
const result = await withSftpDeadline(sftp, pipeWithProgress(fs.createReadStream(localPath), writeStream, sftp, totalSize, onProgress, !sharedSftp, signal), remainingSftpTimeout(deadline, signal), signal);
|
|
577
717
|
return { ...result, createMode: formatFileMode(safeCreateMode) };
|
|
578
718
|
}
|
|
579
719
|
catch (error) {
|
|
580
|
-
|
|
720
|
+
if (!sharedSftp) {
|
|
721
|
+
sftp.end();
|
|
722
|
+
}
|
|
723
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), state.remoteCreated, {
|
|
581
724
|
cause: error,
|
|
725
|
+
details: state.outcome(error),
|
|
582
726
|
});
|
|
583
727
|
}
|
|
584
728
|
}
|
|
@@ -598,10 +742,12 @@ async function existingDownloadTargetMode(localPath) {
|
|
|
598
742
|
}
|
|
599
743
|
return stats.mode & FILE_CREATE_MODE_MASK;
|
|
600
744
|
}
|
|
601
|
-
async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, closeSftpOnFailure,
|
|
745
|
+
async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, closeSftpOnFailure, deadline, signal) {
|
|
602
746
|
let readStream;
|
|
603
747
|
try {
|
|
604
|
-
const totalSize = (await sftpStat(sftp, remotePath,
|
|
748
|
+
const totalSize = (await sftpStat(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal)).size;
|
|
749
|
+
remainingSftpTimeout(deadline, signal);
|
|
750
|
+
validateLocalPathAgainstAllowList(localDir);
|
|
605
751
|
if (!fs.existsSync(localDir)) {
|
|
606
752
|
fs.mkdirSync(localDir, { recursive: true });
|
|
607
753
|
}
|
|
@@ -609,9 +755,10 @@ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath
|
|
|
609
755
|
const handle = await fs.promises.open(temporaryPath, 'wx', 0o600);
|
|
610
756
|
await handle.close();
|
|
611
757
|
if (checkWindowsAcl(temporaryPath, true) !== 'key_path_usable') {
|
|
612
|
-
throw new Error('privacy_not_enforced_on_windows: temporary download file ACL could not be verified');
|
|
758
|
+
throw new Error('privacy_not_enforced_on_windows: temporary download file ACL could not ' + 'be verified');
|
|
613
759
|
}
|
|
614
760
|
}
|
|
761
|
+
remainingSftpTimeout(deadline, signal);
|
|
615
762
|
readStream = sftp.createReadStream(remotePath);
|
|
616
763
|
return {
|
|
617
764
|
totalSize,
|
|
@@ -640,31 +787,58 @@ async function prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath
|
|
|
640
787
|
* 下载文件
|
|
641
788
|
*/
|
|
642
789
|
export async function downloadFile(alias, remotePath, localPath, onProgress, sharedSftp, signal, timeout) {
|
|
790
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
643
791
|
throwIfAborted(signal);
|
|
644
792
|
localPath = expandTilde(localPath);
|
|
645
793
|
validateLocalPathAgainstAllowList(localPath);
|
|
646
794
|
const preservedMode = await existingDownloadTargetMode(localPath);
|
|
647
|
-
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias,
|
|
795
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
|
|
648
796
|
const localDir = path.dirname(localPath);
|
|
649
797
|
const temporaryPath = path.join(localDir, `.${path.basename(localPath)}.mcp-tmp-${process.pid}-${randomBytes(8).toString('hex')}`);
|
|
798
|
+
let failureStage = 'download';
|
|
799
|
+
let localWriteStream;
|
|
650
800
|
try {
|
|
651
|
-
const { totalSize, readStream, writeStream } = await prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, !sharedSftp,
|
|
652
|
-
|
|
801
|
+
const { totalSize, readStream, writeStream } = await prepareDownloadTransfer(sftp, remotePath, localDir, temporaryPath, !sharedSftp, deadline, signal);
|
|
802
|
+
localWriteStream = writeStream;
|
|
803
|
+
const result = await withSftpDeadline(sftp, pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, !sharedSftp, signal), remainingSftpTimeout(deadline, signal), signal);
|
|
653
804
|
if (process.platform !== 'win32') {
|
|
654
805
|
await fs.promises.chmod(temporaryPath, preservedMode ?? 0o600);
|
|
655
806
|
}
|
|
807
|
+
remainingSftpTimeout(deadline, signal);
|
|
808
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
809
|
+
failureStage = 'local_rename';
|
|
656
810
|
await fs.promises.rename(temporaryPath, localPath);
|
|
657
811
|
return { ...result, atomic: true };
|
|
658
812
|
}
|
|
659
|
-
catch (
|
|
813
|
+
catch (cause) {
|
|
814
|
+
const error = new UploadFileError(cause instanceof Error ? cause.message : String(cause), false, {
|
|
815
|
+
cause,
|
|
816
|
+
details: {
|
|
817
|
+
...sftpOperationOutcome(cause),
|
|
818
|
+
operationStatus: 'failed',
|
|
819
|
+
retryable: false,
|
|
820
|
+
failureStage,
|
|
821
|
+
localPath,
|
|
822
|
+
temporaryPath,
|
|
823
|
+
targetReplaced: false,
|
|
824
|
+
},
|
|
825
|
+
});
|
|
660
826
|
try {
|
|
827
|
+
const writeStream = localWriteStream;
|
|
828
|
+
if (writeStream && !writeStream.closed) {
|
|
829
|
+
// 尚未完成的 open 会在 unlink 返回 ENOENT 后创建文件
|
|
830
|
+
await new Promise((resolve) => {
|
|
831
|
+
writeStream.once('close', resolve);
|
|
832
|
+
writeStream.destroy();
|
|
833
|
+
});
|
|
834
|
+
}
|
|
661
835
|
await fs.promises.unlink(temporaryPath);
|
|
662
836
|
}
|
|
663
837
|
catch (cleanupError) {
|
|
664
838
|
if (!isLocalMissingError(cleanupError)) {
|
|
665
|
-
throw new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
|
|
666
|
-
cause
|
|
667
|
-
});
|
|
839
|
+
throw Object.assign(new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
|
|
840
|
+
cause,
|
|
841
|
+
}), { details: error.details });
|
|
668
842
|
}
|
|
669
843
|
}
|
|
670
844
|
throw error;
|
|
@@ -743,13 +917,14 @@ function detectReadEncoding(firstBytes, requested) {
|
|
|
743
917
|
}
|
|
744
918
|
return 'utf8';
|
|
745
919
|
}
|
|
746
|
-
function decodeReadBuffer(bytes, encoding, binary, budgetTruncated) {
|
|
920
|
+
function decodeReadBuffer(bytes, encoding, binary, budgetTruncated, atFileStart) {
|
|
747
921
|
if (binary) {
|
|
748
922
|
return { content: bytes.toString('base64'), content_base64: bytes.toString('base64'), read_bytes: bytes.length };
|
|
749
923
|
}
|
|
750
924
|
const label = encoding === 'utf8' ? 'utf-8' : encoding === 'gbk' ? 'gbk' : encoding === 'utf16le' ? 'utf-16le' : 'utf-16be';
|
|
751
925
|
const invalid = () => new Error(`Invalid ${encoding} sequence or offset is not a character boundary`);
|
|
752
|
-
const
|
|
926
|
+
const decoderOptions = { fatal: true, ignoreBOM: !atFileStart };
|
|
927
|
+
const decoder = new TextDecoder(label, decoderOptions);
|
|
753
928
|
let content;
|
|
754
929
|
try {
|
|
755
930
|
// Streaming validation rejects malformed complete characters before considering an incomplete suffix.
|
|
@@ -770,7 +945,7 @@ function decodeReadBuffer(bytes, encoding, binary, budgetTruncated) {
|
|
|
770
945
|
// Only a byte-budget boundary may leave up to three bytes of an incomplete character.
|
|
771
946
|
for (let end = bytes.length - 1; end >= Math.max(0, bytes.length - 3); --end) {
|
|
772
947
|
try {
|
|
773
|
-
content = new TextDecoder(label,
|
|
948
|
+
content = new TextDecoder(label, decoderOptions).decode(bytes.subarray(0, end));
|
|
774
949
|
}
|
|
775
950
|
catch {
|
|
776
951
|
continue;
|
|
@@ -893,7 +1068,7 @@ async function readLineRange(sftp, remotePath, totalSize, maxBytes, lineRange, e
|
|
|
893
1068
|
sftp.end();
|
|
894
1069
|
}
|
|
895
1070
|
const raw = Buffer.concat(chunks);
|
|
896
|
-
const decoded = decodeReadBuffer(raw, detected, binary, byteLimitReached);
|
|
1071
|
+
const decoded = decodeReadBuffer(raw, detected, binary, byteLimitReached, lineStart === 1);
|
|
897
1072
|
if (decoded.read_bytes < raw.length) {
|
|
898
1073
|
byteLimitReached = true;
|
|
899
1074
|
}
|
|
@@ -946,7 +1121,13 @@ export async function readFile(alias, remotePath, options) {
|
|
|
946
1121
|
const sftp = await getSftpWithinDeadline(alias, readOptions.timeout);
|
|
947
1122
|
let actualSize;
|
|
948
1123
|
try {
|
|
949
|
-
|
|
1124
|
+
const stats = await sftpStat(sftp, remotePath, readOptions.timeout);
|
|
1125
|
+
if ((stats.mode & S_IFMT) !== S_IFREG) {
|
|
1126
|
+
throw new Error((stats.mode & S_IFMT) === S_IFDIR
|
|
1127
|
+
? 'Target is a directory; use ssh_list_dir to read its entries'
|
|
1128
|
+
: 'Target is not a regular file');
|
|
1129
|
+
}
|
|
1130
|
+
actualSize = stats.size;
|
|
950
1131
|
}
|
|
951
1132
|
catch (error) {
|
|
952
1133
|
sftp.end();
|
|
@@ -974,6 +1155,23 @@ export async function readFile(alias, remotePath, options) {
|
|
|
974
1155
|
sftp.end();
|
|
975
1156
|
return emptyReadResult(actualSize, sampleKind, requestedOffset, readOptions.binary === true);
|
|
976
1157
|
}
|
|
1158
|
+
let encoding = readOptions.encoding ?? 'auto';
|
|
1159
|
+
if (!readOptions.binary && encoding === 'auto' && requestedOffset > 0) {
|
|
1160
|
+
try {
|
|
1161
|
+
const first = await withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
1162
|
+
const stream = sftp.createReadStream(remotePath, { start: 0, end: 1 });
|
|
1163
|
+
const chunks = [];
|
|
1164
|
+
stream.on('data', (chunk) => chunks.push(chunk));
|
|
1165
|
+
stream.once('error', reject);
|
|
1166
|
+
stream.once('end', () => resolve(Buffer.concat(chunks)));
|
|
1167
|
+
}), readOptions.timeout);
|
|
1168
|
+
encoding = detectReadEncoding(first, 'auto');
|
|
1169
|
+
}
|
|
1170
|
+
catch (error) {
|
|
1171
|
+
sftp.end();
|
|
1172
|
+
throw error;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
977
1175
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
978
1176
|
const chunks = [];
|
|
979
1177
|
let settled = false;
|
|
@@ -989,7 +1187,13 @@ export async function readFile(alias, remotePath, options) {
|
|
|
989
1187
|
}
|
|
990
1188
|
try {
|
|
991
1189
|
const buffer = Buffer.concat(chunks);
|
|
992
|
-
const detected = detectReadEncoding(buffer,
|
|
1190
|
+
const detected = detectReadEncoding(buffer, encoding);
|
|
1191
|
+
if (!readOptions.binary &&
|
|
1192
|
+
detected.startsWith('utf16') &&
|
|
1193
|
+
!readOptions.tail &&
|
|
1194
|
+
requestedOffset % 2) {
|
|
1195
|
+
throw new Error(`Invalid ${detected} offset is not a character boundary`);
|
|
1196
|
+
}
|
|
993
1197
|
let contentStart = 0;
|
|
994
1198
|
if (!readOptions.binary && detected === 'utf8' && readOptions.tail && requestedOffset > 0) {
|
|
995
1199
|
while (contentStart < buffer.length && isUtf8ContinuationByte(buffer[contentStart])) {
|
|
@@ -1013,7 +1217,7 @@ export async function readFile(alias, remotePath, options) {
|
|
|
1013
1217
|
if (!readOptions.binary && buffer.length > 0 && contentStart >= buffer.length) {
|
|
1014
1218
|
throw new Error(`maxBytes is too small to read a complete ${detected} tail character`);
|
|
1015
1219
|
}
|
|
1016
|
-
const decoded = decodeReadBuffer(buffer.subarray(contentStart), detected, readOptions.binary === true, requestedOffset + buffer.length < actualSize);
|
|
1220
|
+
const decoded = decodeReadBuffer(buffer.subarray(contentStart), detected, readOptions.binary === true, requestedOffset + buffer.length < actualSize, requestedOffset + contentStart === 0);
|
|
1017
1221
|
const contentBuffer = buffer.subarray(contentStart, contentStart + decoded.read_bytes);
|
|
1018
1222
|
const actualReadOffset = requestedOffset + contentStart;
|
|
1019
1223
|
const nextOffset = actualReadOffset + contentBuffer.length;
|
|
@@ -1097,22 +1301,24 @@ export async function prepareWindowsRemotePrivateFile(alias, remotePath, timeout
|
|
|
1097
1301
|
* 写入远程文件
|
|
1098
1302
|
*/
|
|
1099
1303
|
export async function writeFile(alias, remotePath, content, append = false, timeout) {
|
|
1100
|
-
const
|
|
1101
|
-
const
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
writeStream.on('close', () => {
|
|
1106
|
-
sftp.end();
|
|
1107
|
-
resolve({ success: true, size: contentBuffer.length });
|
|
1304
|
+
const state = new SftpWriteState(remotePath);
|
|
1305
|
+
const sftp = await getSftpWithinDeadline(alias, timeout).catch((error) => {
|
|
1306
|
+
throw new UploadFileError(error instanceof Error ? error.message : String(error), false, {
|
|
1307
|
+
cause: error,
|
|
1308
|
+
details: state.outcome(error),
|
|
1108
1309
|
});
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1310
|
+
});
|
|
1311
|
+
const contentBuffer = Buffer.from(content, 'utf-8');
|
|
1312
|
+
try {
|
|
1313
|
+
const writeStream = state.track(sftp).createWriteStream(remotePath, { flags: append ? 'a' : 'w' });
|
|
1314
|
+
return await withSftpDeadline(sftp, pipeWithProgress(Readable.from([contentBuffer]), writeStream, sftp, contentBuffer.length), timeout);
|
|
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),
|
|
1112
1320
|
});
|
|
1113
|
-
|
|
1114
|
-
writeStream.end();
|
|
1115
|
-
}), timeout);
|
|
1321
|
+
}
|
|
1116
1322
|
}
|
|
1117
1323
|
function validateSftpEntryName(filename) {
|
|
1118
1324
|
if (filename.length === 0 ||
|
|
@@ -1127,10 +1333,12 @@ function validateSftpEntryName(filename) {
|
|
|
1127
1333
|
/**
|
|
1128
1334
|
* 列出目录内容
|
|
1129
1335
|
*/
|
|
1130
|
-
export async function listDir(alias, remotePath, showHidden = false, sharedSftp, timeout, style = 'posix') {
|
|
1131
|
-
|
|
1336
|
+
export async function listDir(alias, remotePath, showHidden = false, sharedSftp, timeout, style = 'posix', signal) {
|
|
1337
|
+
throwIfAborted(signal);
|
|
1338
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, timeout, signal));
|
|
1132
1339
|
const closeWhenDone = !sharedSftp;
|
|
1133
1340
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
1341
|
+
throwIfAborted(signal);
|
|
1134
1342
|
sftp.readdir(remotePath, (err, list) => {
|
|
1135
1343
|
if (err) {
|
|
1136
1344
|
if (closeWhenDone) {
|
|
@@ -1179,38 +1387,44 @@ export async function listDir(alias, remotePath, showHidden = false, sharedSftp,
|
|
|
1179
1387
|
}
|
|
1180
1388
|
resolve(files);
|
|
1181
1389
|
});
|
|
1182
|
-
}), timeout);
|
|
1390
|
+
}), timeout, signal);
|
|
1183
1391
|
}
|
|
1184
1392
|
/**
|
|
1185
1393
|
* 获取文件信息
|
|
1186
1394
|
*/
|
|
1187
|
-
export async function getFileInfo(alias, remotePath, timeout, style = 'posix') {
|
|
1188
|
-
const
|
|
1189
|
-
|
|
1190
|
-
|
|
1395
|
+
export async function getFileInfo(alias, remotePath, timeout, style = 'posix', signal) {
|
|
1396
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1397
|
+
const connection = sessionManager.getOpenSshConnection(alias);
|
|
1398
|
+
let stats;
|
|
1399
|
+
if (connection) {
|
|
1400
|
+
stats = await connection.stat(remotePath, timeout ?? 600_000);
|
|
1401
|
+
}
|
|
1402
|
+
else {
|
|
1403
|
+
const sftp = await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal);
|
|
1404
|
+
try {
|
|
1405
|
+
stats = await sftpStat(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
1406
|
+
}
|
|
1407
|
+
finally {
|
|
1191
1408
|
sftp.end();
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
atime: new Date(stats.atime * 1000),
|
|
1209
|
-
});
|
|
1210
|
-
});
|
|
1211
|
-
}), timeout);
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
return {
|
|
1412
|
+
name: remotePathModule(style).basename(remotePath),
|
|
1413
|
+
path: remotePath,
|
|
1414
|
+
size: stats.size,
|
|
1415
|
+
isDirectory: (stats.mode & S_IFMT) === S_IFDIR,
|
|
1416
|
+
isFile: (stats.mode & S_IFMT) === S_IFREG,
|
|
1417
|
+
isSymlink: (stats.mode & S_IFMT) === S_IFLNK,
|
|
1418
|
+
mode: extractFileMode(stats.mode),
|
|
1419
|
+
permissions: formatFilePermissions(stats.mode),
|
|
1420
|
+
owner: stats.uid,
|
|
1421
|
+
group: stats.gid,
|
|
1422
|
+
mtime: new Date(stats.mtime * 1000),
|
|
1423
|
+
atime: new Date(stats.atime * 1000),
|
|
1424
|
+
};
|
|
1212
1425
|
}
|
|
1213
|
-
function sftpMkdir(sftp, remotePath, timeout) {
|
|
1426
|
+
function sftpMkdir(sftp, remotePath, timeout, signal) {
|
|
1427
|
+
throwIfAborted(signal);
|
|
1214
1428
|
return withSftpDeadline(sftp, new Promise((resolve, reject) => {
|
|
1215
1429
|
sftp.mkdir(remotePath, (error) => {
|
|
1216
1430
|
if (error) {
|
|
@@ -1220,16 +1434,17 @@ function sftpMkdir(sftp, remotePath, timeout) {
|
|
|
1220
1434
|
resolve();
|
|
1221
1435
|
}
|
|
1222
1436
|
});
|
|
1223
|
-
}), timeout);
|
|
1437
|
+
}), timeout, signal);
|
|
1224
1438
|
}
|
|
1225
|
-
async function ensureSftpDirectory(sftp, remotePath,
|
|
1439
|
+
async function ensureSftpDirectory(sftp, remotePath, deadline, style = 'posix', signal) {
|
|
1440
|
+
remainingSftpTimeout(deadline, signal);
|
|
1226
1441
|
const normalizedPath = remoteNormalize(style, remotePath);
|
|
1227
1442
|
if (normalizedPath === '/' || normalizedPath === '.') {
|
|
1228
1443
|
return;
|
|
1229
1444
|
}
|
|
1230
1445
|
let existingMode;
|
|
1231
1446
|
try {
|
|
1232
|
-
existingMode = (await sftpStat(sftp, normalizedPath,
|
|
1447
|
+
existingMode = (await sftpStat(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal)).mode;
|
|
1233
1448
|
}
|
|
1234
1449
|
catch (error) {
|
|
1235
1450
|
if (!isSftpMissingError(error)) {
|
|
@@ -1247,14 +1462,14 @@ async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
|
|
|
1247
1462
|
throw new Error('SFTP UNC share root does not exist; refusing to create a network host path');
|
|
1248
1463
|
}
|
|
1249
1464
|
if (parent !== normalizedPath) {
|
|
1250
|
-
await ensureSftpDirectory(sftp, parent,
|
|
1465
|
+
await ensureSftpDirectory(sftp, parent, deadline, style, signal);
|
|
1251
1466
|
}
|
|
1252
1467
|
try {
|
|
1253
|
-
await sftpMkdir(sftp, normalizedPath,
|
|
1468
|
+
await sftpMkdir(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal);
|
|
1254
1469
|
}
|
|
1255
1470
|
catch (error) {
|
|
1256
1471
|
try {
|
|
1257
|
-
const stats = await sftpStat(sftp, normalizedPath,
|
|
1472
|
+
const stats = await sftpStat(sftp, normalizedPath, remainingSftpTimeout(deadline, signal), signal);
|
|
1258
1473
|
if ((stats.mode & S_IFMT) === S_IFDIR) {
|
|
1259
1474
|
return;
|
|
1260
1475
|
}
|
|
@@ -1268,15 +1483,16 @@ async function ensureSftpDirectory(sftp, remotePath, timeout, style = 'posix') {
|
|
|
1268
1483
|
/**
|
|
1269
1484
|
* 创建目录
|
|
1270
1485
|
*/
|
|
1271
|
-
export async function mkdir(alias, remotePath, recursive = false, sharedSftp, timeout, style = 'posix') {
|
|
1272
|
-
const
|
|
1486
|
+
export async function mkdir(alias, remotePath, recursive = false, sharedSftp, timeout, style = 'posix', signal) {
|
|
1487
|
+
const deadline = timeout === undefined ? undefined : Date.now() + timeout;
|
|
1488
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
|
|
1273
1489
|
return withSftpDeadline(sftp, (async () => {
|
|
1274
1490
|
try {
|
|
1275
1491
|
if (recursive) {
|
|
1276
|
-
await ensureSftpDirectory(sftp, remotePath,
|
|
1492
|
+
await ensureSftpDirectory(sftp, remotePath, deadline, style, signal);
|
|
1277
1493
|
}
|
|
1278
1494
|
else {
|
|
1279
|
-
await sftpMkdir(sftp, remotePath,
|
|
1495
|
+
await sftpMkdir(sftp, remotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
1280
1496
|
}
|
|
1281
1497
|
return true;
|
|
1282
1498
|
}
|
|
@@ -1285,7 +1501,7 @@ export async function mkdir(alias, remotePath, recursive = false, sharedSftp, ti
|
|
|
1285
1501
|
sftp.end();
|
|
1286
1502
|
}
|
|
1287
1503
|
}
|
|
1288
|
-
})(),
|
|
1504
|
+
})(), remainingSftpTimeout(deadline, signal), signal);
|
|
1289
1505
|
}
|
|
1290
1506
|
/**
|
|
1291
1507
|
* 检查远程是否安装 rsync
|
|
@@ -1375,7 +1591,9 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
|
|
|
1375
1591
|
retryable: result.failureKind !== 'remote_command',
|
|
1376
1592
|
exitCode: result.exitCode,
|
|
1377
1593
|
failureKind: result.failureKind,
|
|
1378
|
-
error: result.stderr.trim() ||
|
|
1594
|
+
error: result.stderr.trim() ||
|
|
1595
|
+
result.stdout.trim() ||
|
|
1596
|
+
'rsync probe failed with exit code ' + `${result.exitCode}`,
|
|
1379
1597
|
};
|
|
1380
1598
|
}
|
|
1381
1599
|
catch (error) {
|
|
@@ -1388,6 +1606,51 @@ export async function checkRsync(alias, timeout = 10_000, executable = 'rsync',
|
|
|
1388
1606
|
};
|
|
1389
1607
|
}
|
|
1390
1608
|
}
|
|
1609
|
+
function parseWindowsCaseSemantics(output, names) {
|
|
1610
|
+
const semantics = JSON.parse(output);
|
|
1611
|
+
if (typeof semantics?.caseSensitive !== 'boolean' ||
|
|
1612
|
+
!Object.hasOwn(semantics, 'collision') ||
|
|
1613
|
+
(semantics.collision !== null &&
|
|
1614
|
+
(!Array.isArray(semantics.collision) ||
|
|
1615
|
+
semantics.collision.length !== 2 ||
|
|
1616
|
+
semantics.collision[0] === semantics.collision[1] ||
|
|
1617
|
+
!semantics.collision.every((name) => typeof name === 'string' && names.includes(name)))) ||
|
|
1618
|
+
(semantics.caseSensitive && semantics.collision !== null)) {
|
|
1619
|
+
throw new Error('Windows case semantics query returned incomplete fields');
|
|
1620
|
+
}
|
|
1621
|
+
return semantics.collision;
|
|
1622
|
+
}
|
|
1623
|
+
function verifyWindowsDownloadNames(localPath, remoteNames, timeout) {
|
|
1624
|
+
const existing = fs.existsSync(localPath) ? fs.readdirSync(localPath) : [];
|
|
1625
|
+
const names = [...new Set([...existing, ...remoteNames])];
|
|
1626
|
+
if (names.length < 2) {
|
|
1627
|
+
return;
|
|
1628
|
+
}
|
|
1629
|
+
let parent = localPath;
|
|
1630
|
+
while (!fs.existsSync(parent)) {
|
|
1631
|
+
parent = path.dirname(parent);
|
|
1632
|
+
}
|
|
1633
|
+
try {
|
|
1634
|
+
const script = encodedPowershell(windowsCaseSemanticsScript(parent)).split(' ').at(-1);
|
|
1635
|
+
const result = spawnSync('powershell.exe', ['-NoProfile', '-NonInteractive', '-EncodedCommand', script], {
|
|
1636
|
+
input: Buffer.from(JSON.stringify(names)).toString('base64'),
|
|
1637
|
+
encoding: 'utf8',
|
|
1638
|
+
windowsHide: true,
|
|
1639
|
+
timeout: Math.min(timeout ?? 10_000, 10_000),
|
|
1640
|
+
maxBuffer: 16_384,
|
|
1641
|
+
});
|
|
1642
|
+
if (result.error || result.status !== 0) {
|
|
1643
|
+
throw new Error('Windows directory case query failed');
|
|
1644
|
+
}
|
|
1645
|
+
const collision = parseWindowsCaseSemantics(result.stdout, names);
|
|
1646
|
+
if (collision) {
|
|
1647
|
+
throw new Error(`Windows local filename collision at ${localPath}: ${collision.join(' / ')}`);
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
catch (error) {
|
|
1651
|
+
throw new Error(`case_semantics_unverified: ${localPath}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
1652
|
+
}
|
|
1653
|
+
}
|
|
1391
1654
|
async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
1392
1655
|
const candidates = [];
|
|
1393
1656
|
const style = options.remotePathStyle ?? sessionManager.getRemotePathStyle(alias);
|
|
@@ -1402,7 +1665,7 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
|
1402
1665
|
candidates.push({ parent, names });
|
|
1403
1666
|
}
|
|
1404
1667
|
});
|
|
1405
|
-
const deadline = Date.now() + (options.preflightTimeout ?? 10_000);
|
|
1668
|
+
const deadline = Math.min(Date.now() + (options.preflightTimeout ?? 10_000), options.deadline ?? Infinity);
|
|
1406
1669
|
for (const { parent, names } of candidates) {
|
|
1407
1670
|
let collision;
|
|
1408
1671
|
try {
|
|
@@ -1418,18 +1681,7 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
|
1418
1681
|
result.stderrTruncated) {
|
|
1419
1682
|
throw new Error(result.stderr || 'Windows case semantics query failed or returned truncated output');
|
|
1420
1683
|
}
|
|
1421
|
-
|
|
1422
|
-
if (typeof semantics?.caseSensitive !== 'boolean' ||
|
|
1423
|
-
!Object.hasOwn(semantics, 'collision') ||
|
|
1424
|
-
(semantics.collision !== null &&
|
|
1425
|
-
(!Array.isArray(semantics.collision) ||
|
|
1426
|
-
semantics.collision.length !== 2 ||
|
|
1427
|
-
semantics.collision[0] === semantics.collision[1] ||
|
|
1428
|
-
!semantics.collision.every((name) => typeof name === 'string' && names.includes(name)))) ||
|
|
1429
|
-
(semantics.caseSensitive && semantics.collision !== null)) {
|
|
1430
|
-
throw new Error('Windows case semantics query returned incomplete fields');
|
|
1431
|
-
}
|
|
1432
|
-
collision = semantics.collision;
|
|
1684
|
+
collision = parseWindowsCaseSemantics(result.stdout, names);
|
|
1433
1685
|
}
|
|
1434
1686
|
catch (error) {
|
|
1435
1687
|
throw new Error(`case_semantics_unverified: ${parent}: ${error instanceof Error ? error.message : String(error)}`, { cause: error });
|
|
@@ -1450,10 +1702,12 @@ async function verifyWindowsUploadNames(alias, localPath, remotePath, options) {
|
|
|
1450
1702
|
*/
|
|
1451
1703
|
export async function syncFiles(alias, localPath, remotePath, direction, options = {}) {
|
|
1452
1704
|
const startedAt = Date.now();
|
|
1705
|
+
options = { ...options, deadline: options.deadline ?? startedAt + (options.operationTimeout ?? 600_000) };
|
|
1706
|
+
remainingSftpTimeout(options.deadline, options.signal);
|
|
1453
1707
|
localPath = expandTilde(localPath);
|
|
1454
1708
|
validateLocalPathAgainstAllowList(localPath);
|
|
1455
1709
|
if (remotePath === '~' || remotePath.startsWith('~/')) {
|
|
1456
|
-
throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ is not supported');
|
|
1710
|
+
throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ ' + 'is not supported');
|
|
1457
1711
|
}
|
|
1458
1712
|
let sourceIsDirectory;
|
|
1459
1713
|
if (direction === 'upload') {
|
|
@@ -1462,10 +1716,10 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
1462
1716
|
sourceIsDirectory = source.stats.isDirectory();
|
|
1463
1717
|
}
|
|
1464
1718
|
else {
|
|
1465
|
-
sourceIsDirectory = (await getFileInfo(alias, remotePath,
|
|
1719
|
+
sourceIsDirectory = (await getFileInfo(alias, remotePath, remainingSftpTimeout(options.deadline, options.signal), options.remotePathStyle, options.signal)).isDirectory;
|
|
1466
1720
|
}
|
|
1467
1721
|
if (sourceIsDirectory && options.recursive === false) {
|
|
1468
|
-
throw new Error('recursive=false is not supported for directory sources; use a file source or enable recursion');
|
|
1722
|
+
throw new Error('recursive=false is not supported for directory sources; use a file ' + 'source or enable recursion');
|
|
1469
1723
|
}
|
|
1470
1724
|
const capabilityStartedAt = Date.now();
|
|
1471
1725
|
const rsyncGeneration = getRsyncGeneration(alias);
|
|
@@ -1474,7 +1728,13 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
1474
1728
|
assertCurrentRsyncGeneration(alias, rsyncGeneration);
|
|
1475
1729
|
}
|
|
1476
1730
|
const capability = sessionManager.getExternalTransferCapability(alias);
|
|
1477
|
-
const preflightTimeout = options.preflightTimeout ?? 10_000;
|
|
1731
|
+
const preflightTimeout = Math.min(options.preflightTimeout ?? 10_000, remainingSftpTimeout(options.deadline, options.signal));
|
|
1732
|
+
const connection = sessionManager.getOpenSshConnection(alias);
|
|
1733
|
+
if (connection) {
|
|
1734
|
+
await connection.capture('command -v find stat head realpath >/dev/null && find --version ' +
|
|
1735
|
+
'>/dev/null && stat --version >/dev/null && head --version >/dev/null && ' +
|
|
1736
|
+
'realpath --version >/dev/null', preflightTimeout, 4096);
|
|
1737
|
+
}
|
|
1478
1738
|
const rsyncProbe = capability.rsyncEligible
|
|
1479
1739
|
? await checkRsync(alias, preflightTimeout, capability.rsyncExecutable, capability.commandShell)
|
|
1480
1740
|
: {
|
|
@@ -1491,6 +1751,15 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
1491
1751
|
remotePathStyle: options.remotePathStyle ?? capability.remotePathStyle ?? 'posix',
|
|
1492
1752
|
};
|
|
1493
1753
|
const capabilityDuration = Date.now() - capabilityStartedAt;
|
|
1754
|
+
if (capability.backend === 'openssh' && !rsyncProbe.available) {
|
|
1755
|
+
throw new OpenSshError('The OpenSSH master backend requires remote rsync; no SFTP fallback is available', {
|
|
1756
|
+
code: 'unsupported_backend',
|
|
1757
|
+
backend: 'openssh',
|
|
1758
|
+
capability: 'rsync',
|
|
1759
|
+
reason: rsyncProbe.reason ?? rsyncProbe.error,
|
|
1760
|
+
retryable: false,
|
|
1761
|
+
});
|
|
1762
|
+
}
|
|
1494
1763
|
const selectedTransport = capability.rsyncEligible && rsyncProbe?.available ? 'rsync' : 'sftp';
|
|
1495
1764
|
const decisionReason = !capability.rsyncEligible
|
|
1496
1765
|
? capability.decisionReason
|
|
@@ -1537,11 +1806,23 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
|
|
|
1537
1806
|
*/
|
|
1538
1807
|
async function syncWithRsync(alias, localPath, remotePath, direction, options, capability, generation) {
|
|
1539
1808
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1809
|
+
const connection = sessionManager.getOpenSshConnection(alias);
|
|
1810
|
+
const fallbackToSftp = (reason) => {
|
|
1811
|
+
if (connection) {
|
|
1812
|
+
throw new OpenSshError('OpenSSH master sync preflight failed; the selected backend was preserved', {
|
|
1813
|
+
code: 'openssh_sync_preflight',
|
|
1814
|
+
backend: 'openssh',
|
|
1815
|
+
reason,
|
|
1816
|
+
retryable: false,
|
|
1817
|
+
});
|
|
1818
|
+
}
|
|
1819
|
+
return syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
|
|
1820
|
+
};
|
|
1540
1821
|
const checkCommand = os.platform() === 'win32' ? 'where' : 'which';
|
|
1541
|
-
const checkResult = await runLocalProcess(checkCommand, ['rsync'], options.preflightTimeout ?? 10_000);
|
|
1822
|
+
const checkResult = await runLocalProcess(checkCommand, ['rsync'], Math.min(options.preflightTimeout ?? 10_000, remainingSftpTimeout(options.deadline, options.signal)));
|
|
1542
1823
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1543
1824
|
if (checkResult.status !== 0) {
|
|
1544
|
-
const fallback = await
|
|
1825
|
+
const fallback = await fallbackToSftp(checkResult.timedOut ? 'local_rsync_preflight_timeout' : 'local_rsync_unavailable');
|
|
1545
1826
|
return {
|
|
1546
1827
|
...fallback,
|
|
1547
1828
|
connectStatus: 'not_applicable',
|
|
@@ -1550,6 +1831,9 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1550
1831
|
}
|
|
1551
1832
|
// 构建 rsync 参数
|
|
1552
1833
|
const args = ['-avz', '--itemize-changes', '--stats'];
|
|
1834
|
+
if (options.onProgress) {
|
|
1835
|
+
args.push('--progress');
|
|
1836
|
+
}
|
|
1553
1837
|
if (options.delete) {
|
|
1554
1838
|
args.push('--delete');
|
|
1555
1839
|
}
|
|
@@ -1597,18 +1881,20 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1597
1881
|
else if (capability.agent) {
|
|
1598
1882
|
childEnv.SSH_AUTH_SOCK = capability.agent;
|
|
1599
1883
|
}
|
|
1600
|
-
const sshOptions =
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1884
|
+
const sshOptions = connection
|
|
1885
|
+
? connection.arguments().slice(0, -1)
|
|
1886
|
+
: [
|
|
1887
|
+
'-p',
|
|
1888
|
+
String(capability.port),
|
|
1889
|
+
'-o',
|
|
1890
|
+
'StrictHostKeyChecking=accept-new',
|
|
1891
|
+
'-o',
|
|
1892
|
+
'BatchMode=yes',
|
|
1893
|
+
'-o',
|
|
1894
|
+
`ConnectTimeout=${Math.max(1, Math.ceil(connectTimeout / 1000))}`,
|
|
1895
|
+
'-o',
|
|
1896
|
+
'ProxyCommand=none',
|
|
1897
|
+
];
|
|
1612
1898
|
if (capability.agent === 'none') {
|
|
1613
1899
|
sshOptions.push('-o', 'IdentityAgent=none');
|
|
1614
1900
|
}
|
|
@@ -1638,11 +1924,11 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1638
1924
|
: capability.commandShell === 'cmd'
|
|
1639
1925
|
? 'cmd.exe /D /C exit 0'
|
|
1640
1926
|
: 'true',
|
|
1641
|
-
], connectTimeout, childEnv);
|
|
1927
|
+
], Math.min(connectTimeout, remainingSftpTimeout(options.deadline, options.signal)), childEnv, connection);
|
|
1642
1928
|
const connectDuration = Date.now() - connectStartedAt;
|
|
1643
1929
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1644
1930
|
if (connectResult.error || connectResult.status !== 0) {
|
|
1645
|
-
const fallback = await
|
|
1931
|
+
const fallback = await fallbackToSftp(connectResult.stderr.trim() || connectResult.error?.message || 'rsync_connect_failed');
|
|
1646
1932
|
const fallbackReason = connectResult.timedOut ? 'rsync_connect_timeout' : 'rsync_connect_failed';
|
|
1647
1933
|
const detail = connectResult.stderr.trim() || connectResult.error?.message;
|
|
1648
1934
|
return {
|
|
@@ -1656,11 +1942,11 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1656
1942
|
};
|
|
1657
1943
|
}
|
|
1658
1944
|
if (capability.commandShell && capability.commandShell !== 'posix') {
|
|
1659
|
-
const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], connectTimeout, childEnv);
|
|
1945
|
+
const remoteProbe = await runLocalProcess('ssh', [...sshOptions, destination, `${remoteCommand} --version`], Math.min(connectTimeout, remainingSftpTimeout(options.deadline, options.signal)), childEnv);
|
|
1660
1946
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1661
1947
|
if (remoteProbe.error || remoteProbe.status !== 0) {
|
|
1662
|
-
const fallback = await
|
|
1663
|
-
const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message ||
|
|
1948
|
+
const fallback = await fallbackToSftp(remoteProbe.stderr.trim() || remoteProbe.error?.message || 'remote_rsync_unavailable');
|
|
1949
|
+
const reason = remoteProbe.stderr.trim() || remoteProbe.error?.message || 'exit code ' + `${remoteProbe.status}`;
|
|
1664
1950
|
return {
|
|
1665
1951
|
...fallback,
|
|
1666
1952
|
connectDuration,
|
|
@@ -1684,17 +1970,66 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1684
1970
|
if (executable) {
|
|
1685
1971
|
args.push(`--rsync-path=${remoteCommand}`);
|
|
1686
1972
|
}
|
|
1687
|
-
if (capability.commandShell !== 'posix' && capability.commandShell !== undefined) {
|
|
1973
|
+
if (connection || (capability.commandShell !== 'posix' && capability.commandShell !== undefined)) {
|
|
1688
1974
|
args.push('--protect-args');
|
|
1689
1975
|
}
|
|
1690
1976
|
// 在 path 参数前插 `--`,明确剩余为路径而非选项(双重保险)
|
|
1691
1977
|
const rsyncArgs = direction === 'upload'
|
|
1692
1978
|
? [...args, '-e', sshCmd, '--', rsyncLocalPath, remoteSpec]
|
|
1693
1979
|
: [...args, '-e', sshCmd, '--', remoteSpec, localRsyncPath];
|
|
1980
|
+
if (direction === 'download' && !options.dryRun) {
|
|
1981
|
+
try {
|
|
1982
|
+
remainingSftpTimeout(options.deadline, options.signal);
|
|
1983
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
1984
|
+
fs.mkdirSync(path.dirname(path.resolve(localPath)), { recursive: true });
|
|
1985
|
+
}
|
|
1986
|
+
catch (error) {
|
|
1987
|
+
return {
|
|
1988
|
+
success: false,
|
|
1989
|
+
method: 'rsync',
|
|
1990
|
+
connectDuration,
|
|
1991
|
+
connectStatus: 'completed',
|
|
1992
|
+
operationStatus: 'failed',
|
|
1993
|
+
failureStage: 'local_prepare',
|
|
1994
|
+
retryable: false,
|
|
1995
|
+
output: `Local download parent preparation failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
1996
|
+
};
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1694
1999
|
try {
|
|
1695
2000
|
assertCurrentRsyncGeneration(alias, generation);
|
|
1696
|
-
const operationTimeout = options.operationTimeout ?? 600_000;
|
|
1697
|
-
const
|
|
2001
|
+
const operationTimeout = remainingSftpTimeout(options.deadline, options.signal) ?? options.operationTimeout ?? 600_000;
|
|
2002
|
+
const stats = { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 0 };
|
|
2003
|
+
let partial = '';
|
|
2004
|
+
let discardTail = false;
|
|
2005
|
+
const collectStats = (chunk, final = false) => {
|
|
2006
|
+
for (const [index, segment] of chunk.split(/[\r\n]/).entries()) {
|
|
2007
|
+
if (index > 0) {
|
|
2008
|
+
const progress = /^\s*([\d,]+)\s+(\d+)%\s+[\d.]+[kMGTPE]?B\/s/i.exec(partial);
|
|
2009
|
+
if (progress) {
|
|
2010
|
+
options.onProgress?.({
|
|
2011
|
+
transferred: Number(progress[1].replace(/,/g, '')),
|
|
2012
|
+
total: 0,
|
|
2013
|
+
percent: Number(progress[2]),
|
|
2014
|
+
});
|
|
2015
|
+
}
|
|
2016
|
+
parseRsyncStats(partial, stats);
|
|
2017
|
+
partial = '';
|
|
2018
|
+
discardTail = false;
|
|
2019
|
+
}
|
|
2020
|
+
if (!discardTail) {
|
|
2021
|
+
// Only the itemization prefix and summaries are needed, never an unbounded filename.
|
|
2022
|
+
const available = 65_536 - partial.length;
|
|
2023
|
+
partial += segment.slice(0, available);
|
|
2024
|
+
discardTail = segment.length > available;
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
if (final && partial) {
|
|
2028
|
+
parseRsyncStats(partial, stats);
|
|
2029
|
+
partial = '';
|
|
2030
|
+
}
|
|
2031
|
+
};
|
|
2032
|
+
const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout, childEnv, connection, collectStats);
|
|
1698
2033
|
if (!isCurrentRsyncGeneration(alias, generation)) {
|
|
1699
2034
|
return {
|
|
1700
2035
|
success: false,
|
|
@@ -1722,7 +2057,10 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1722
2057
|
timedOut: true,
|
|
1723
2058
|
retryable: true,
|
|
1724
2059
|
fallbackReason: 'rsync_operation_timeout',
|
|
1725
|
-
output:
|
|
2060
|
+
output: 'rsync ' +
|
|
2061
|
+
`${reason}` +
|
|
2062
|
+
`${stderr ? ` | stderr: ${stderr}` : ''}` +
|
|
2063
|
+
'; remote transfer state is unknown',
|
|
1726
2064
|
};
|
|
1727
2065
|
}
|
|
1728
2066
|
return {
|
|
@@ -1733,11 +2071,13 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1733
2071
|
operationStatus: 'unknown',
|
|
1734
2072
|
retryable: true,
|
|
1735
2073
|
fallbackReason: 'rsync_operation_failed',
|
|
1736
|
-
output:
|
|
2074
|
+
output: 'rsync failed: ' +
|
|
2075
|
+
`${reason}` +
|
|
2076
|
+
`${stderr ? ` | stderr: ${stderr}` : ''}` +
|
|
2077
|
+
'; remote transfer state is unknown',
|
|
1737
2078
|
};
|
|
1738
2079
|
}
|
|
1739
2080
|
const output = result.stdout;
|
|
1740
|
-
const stats = parseRsyncStats(output);
|
|
1741
2081
|
const filesTransferred = stats.added + stats.updated;
|
|
1742
2082
|
return {
|
|
1743
2083
|
success: true,
|
|
@@ -1757,6 +2097,7 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1757
2097
|
usedKeyPath: Boolean(capability.keyPath),
|
|
1758
2098
|
},
|
|
1759
2099
|
output,
|
|
2100
|
+
outputTruncated: result.stdoutTruncated || undefined,
|
|
1760
2101
|
};
|
|
1761
2102
|
}
|
|
1762
2103
|
catch (e) {
|
|
@@ -1771,7 +2112,9 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options, c
|
|
|
1771
2112
|
operationStatus: 'unknown',
|
|
1772
2113
|
retryable: true,
|
|
1773
2114
|
fallbackReason: 'rsync_execution_error',
|
|
1774
|
-
output:
|
|
2115
|
+
output: 'rsync execution failed: ' +
|
|
2116
|
+
`${e instanceof Error ? e.message : String(e)}` +
|
|
2117
|
+
'; remote transfer state is unknown',
|
|
1775
2118
|
};
|
|
1776
2119
|
}
|
|
1777
2120
|
}
|
|
@@ -1808,20 +2151,6 @@ class DirectoryTransferAccumulator {
|
|
|
1808
2151
|
addFailed() {
|
|
1809
2152
|
this.result.failed++;
|
|
1810
2153
|
}
|
|
1811
|
-
merge(result) {
|
|
1812
|
-
this.result.fileCount += result.fileCount;
|
|
1813
|
-
this.result.totalSize += result.totalSize;
|
|
1814
|
-
this.result.skippedSymlinks += result.skippedSymlinks;
|
|
1815
|
-
this.result.skippedUnsupported += result.skippedUnsupported;
|
|
1816
|
-
for (const itemPath of result.unsupportedSamples) {
|
|
1817
|
-
if (this.result.unsupportedSamples.length < 10) {
|
|
1818
|
-
this.result.unsupportedSamples.push(itemPath);
|
|
1819
|
-
}
|
|
1820
|
-
}
|
|
1821
|
-
this.result.added += result.added;
|
|
1822
|
-
this.result.updated += result.updated;
|
|
1823
|
-
this.result.failed += result.failed;
|
|
1824
|
-
}
|
|
1825
2154
|
toResult() {
|
|
1826
2155
|
return { ...this.result };
|
|
1827
2156
|
}
|
|
@@ -1841,7 +2170,7 @@ function buildDirectorySyncResult(result, warnings) {
|
|
|
1841
2170
|
}
|
|
1842
2171
|
function buildSyncResult(fileCount, totalSize, warnings, skippedSymlinks = 0, stats) {
|
|
1843
2172
|
if (skippedSymlinks > 0) {
|
|
1844
|
-
warnings.push(`skipped ${skippedSymlinks} symlink(s)
|
|
2173
|
+
warnings.push(`skipped ${skippedSymlinks} symlink(s); link targets were not transferred`);
|
|
1845
2174
|
}
|
|
1846
2175
|
return {
|
|
1847
2176
|
success: true,
|
|
@@ -1859,37 +2188,84 @@ function buildSyncResult(fileCount, totalSize, warnings, skippedSymlinks = 0, st
|
|
|
1859
2188
|
output: warnings.length ? `Warning: ${warnings.join('; ')}` : undefined,
|
|
1860
2189
|
};
|
|
1861
2190
|
}
|
|
1862
|
-
function parseRsyncStats(output) {
|
|
1863
|
-
const stats = { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 0 };
|
|
2191
|
+
function parseRsyncStats(output, stats) {
|
|
1864
2192
|
for (const line of output.split('\n')) {
|
|
1865
2193
|
if (line.startsWith('*deleting')) {
|
|
1866
2194
|
stats.deleted++;
|
|
1867
2195
|
}
|
|
1868
|
-
else if (
|
|
2196
|
+
else if (/^[<>]f\+{9}\s/.test(line)) {
|
|
1869
2197
|
stats.added++;
|
|
1870
2198
|
}
|
|
1871
|
-
else if (
|
|
2199
|
+
else if (/^[<>]f\S{9}\s/.test(line)) {
|
|
1872
2200
|
stats.updated++;
|
|
1873
2201
|
}
|
|
1874
2202
|
}
|
|
1875
|
-
const transferredMatch = output.match(/Number of regular files transferred:\s*(?<count
|
|
2203
|
+
const transferredMatch = output.match(/Number of regular files transferred:\s*(?<count>[\d,]+)/);
|
|
1876
2204
|
if (transferredMatch?.groups && stats.added + stats.updated === 0) {
|
|
1877
|
-
stats.updated = Number(transferredMatch.groups.count);
|
|
2205
|
+
stats.updated = Number(transferredMatch.groups.count.replace(/,/g, ''));
|
|
1878
2206
|
}
|
|
1879
2207
|
return stats;
|
|
1880
2208
|
}
|
|
1881
|
-
function hashLocalFile(filePath) {
|
|
2209
|
+
export function hashLocalFile(filePath, algorithm = 'sha256', timeout, limits) {
|
|
1882
2210
|
return new Promise((resolve, reject) => {
|
|
1883
|
-
const
|
|
2211
|
+
const before = fs.statSync(filePath);
|
|
2212
|
+
if (!before.isFile() || (limits && (before.size > limits.maxBytes || before.size !== limits.expectedSize))) {
|
|
2213
|
+
reject(new Error('Local file size changed or exceeds the verification byte budget'));
|
|
2214
|
+
return;
|
|
2215
|
+
}
|
|
2216
|
+
const hash = createHash(algorithm);
|
|
1884
2217
|
const stream = fs.createReadStream(filePath);
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
2218
|
+
let bytes = 0;
|
|
2219
|
+
let settled = false;
|
|
2220
|
+
const finish = (error) => {
|
|
2221
|
+
if (settled) {
|
|
2222
|
+
return;
|
|
2223
|
+
}
|
|
2224
|
+
settled = true;
|
|
2225
|
+
if (timer) {
|
|
2226
|
+
clearTimeout(timer);
|
|
2227
|
+
}
|
|
2228
|
+
if (error) {
|
|
2229
|
+
stream.destroy();
|
|
2230
|
+
reject(error);
|
|
2231
|
+
}
|
|
2232
|
+
else {
|
|
2233
|
+
resolve(hash.digest('hex'));
|
|
2234
|
+
}
|
|
2235
|
+
};
|
|
2236
|
+
const timer = timeout === undefined
|
|
2237
|
+
? undefined
|
|
2238
|
+
: setTimeout(() => finish(new Error(`Local file hashing timed out after ${timeout}ms`)), timeout);
|
|
2239
|
+
stream.on('data', (chunk) => {
|
|
2240
|
+
bytes += Buffer.byteLength(chunk);
|
|
2241
|
+
if (bytes > before.size || (limits && bytes > limits.maxBytes)) {
|
|
2242
|
+
finish(new Error('Local file grew during verification'));
|
|
2243
|
+
}
|
|
2244
|
+
else {
|
|
2245
|
+
hash.update(chunk);
|
|
2246
|
+
}
|
|
2247
|
+
});
|
|
2248
|
+
stream.on('error', finish);
|
|
2249
|
+
stream.on('end', () => {
|
|
2250
|
+
try {
|
|
2251
|
+
const after = fs.statSync(filePath);
|
|
2252
|
+
const changed = bytes !== before.size ||
|
|
2253
|
+
after.size !== before.size ||
|
|
2254
|
+
after.ino !== before.ino ||
|
|
2255
|
+
after.dev !== before.dev ||
|
|
2256
|
+
after.mtimeMs !== before.mtimeMs ||
|
|
2257
|
+
after.ctimeMs !== before.ctimeMs;
|
|
2258
|
+
finish(changed ? new Error('Local file changed during verification') : undefined);
|
|
2259
|
+
}
|
|
2260
|
+
catch (error) {
|
|
2261
|
+
finish(error instanceof Error ? error : new Error(String(error)));
|
|
2262
|
+
}
|
|
2263
|
+
});
|
|
1888
2264
|
});
|
|
1889
2265
|
}
|
|
1890
|
-
async function hashRemoteFile(alias, remotePath) {
|
|
2266
|
+
async function hashRemoteFile(alias, remotePath, timeout, signal) {
|
|
1891
2267
|
try {
|
|
1892
|
-
const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256');
|
|
2268
|
+
const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256', timeout, undefined, signal);
|
|
1893
2269
|
return { status: 'available', digest, retryable: false };
|
|
1894
2270
|
}
|
|
1895
2271
|
catch (error) {
|
|
@@ -1900,23 +2276,31 @@ async function hashRemoteFile(alias, remotePath) {
|
|
|
1900
2276
|
};
|
|
1901
2277
|
}
|
|
1902
2278
|
}
|
|
1903
|
-
async function buildSingleFileVerification(alias, localPath, remotePath, direction) {
|
|
2279
|
+
async function buildSingleFileVerification(alias, localPath, remotePath, direction, verifySha256, deadline, signal) {
|
|
1904
2280
|
try {
|
|
1905
2281
|
const localStats = fs.statSync(localPath);
|
|
1906
2282
|
const [remoteInfo, localSha256, remoteHash] = await Promise.all([
|
|
1907
|
-
getFileInfo(alias, remotePath),
|
|
1908
|
-
hashLocalFile(localPath),
|
|
1909
|
-
|
|
2283
|
+
getFileInfo(alias, remotePath, remainingSftpTimeout(deadline, signal), 'posix', signal),
|
|
2284
|
+
verifySha256 ? hashLocalFile(localPath, 'sha256', remainingSftpTimeout(deadline, signal)) : undefined,
|
|
2285
|
+
verifySha256
|
|
2286
|
+
? hashRemoteFile(alias, remotePath, remainingSftpTimeout(deadline, signal), signal)
|
|
2287
|
+
: undefined,
|
|
1910
2288
|
]);
|
|
2289
|
+
const timedOut = signal?.reason instanceof SftpOperationTimeoutError || (deadline !== undefined && Date.now() >= deadline);
|
|
1911
2290
|
return {
|
|
1912
2291
|
kind: 'single_file',
|
|
1913
2292
|
direction,
|
|
1914
|
-
status: remoteHash
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
2293
|
+
status: timedOut ? 'unknown' : remoteHash?.status === 'error' ? 'error' : 'completed',
|
|
2294
|
+
timedOut: timedOut || undefined,
|
|
2295
|
+
failureStage: timedOut
|
|
2296
|
+
? 'verification_timeout'
|
|
2297
|
+
: remoteHash?.status === 'error'
|
|
2298
|
+
? 'remote_hash'
|
|
2299
|
+
: undefined,
|
|
2300
|
+
retryable: remoteHash?.retryable ?? false,
|
|
2301
|
+
error: remoteHash?.error,
|
|
2302
|
+
hashAlgorithm: verifySha256 ? 'sha256' : undefined,
|
|
2303
|
+
hashMatch: remoteHash?.digest ? localSha256 === remoteHash.digest : undefined,
|
|
1920
2304
|
local: {
|
|
1921
2305
|
size: localStats.size,
|
|
1922
2306
|
mode: (localStats.mode & 0o777).toString(8),
|
|
@@ -1929,27 +2313,31 @@ async function buildSingleFileVerification(alias, localPath, remotePath, directi
|
|
|
1929
2313
|
owner: remoteInfo.owner,
|
|
1930
2314
|
group: remoteInfo.group,
|
|
1931
2315
|
mtime: remoteInfo.mtime,
|
|
1932
|
-
sha256: remoteHash
|
|
2316
|
+
sha256: remoteHash?.digest,
|
|
1933
2317
|
},
|
|
1934
2318
|
};
|
|
1935
2319
|
}
|
|
1936
2320
|
catch (error) {
|
|
2321
|
+
const timedOut = error instanceof SftpOperationTimeoutError ||
|
|
2322
|
+
signal?.reason instanceof SftpOperationTimeoutError ||
|
|
2323
|
+
(deadline !== undefined && Date.now() >= deadline);
|
|
1937
2324
|
return {
|
|
1938
2325
|
kind: 'single_file',
|
|
1939
2326
|
direction,
|
|
1940
|
-
status: 'error',
|
|
1941
|
-
|
|
2327
|
+
status: timedOut ? 'unknown' : 'error',
|
|
2328
|
+
timedOut: timedOut || undefined,
|
|
2329
|
+
failureStage: timedOut ? 'verification_timeout' : 'remote_metadata_or_hash',
|
|
1942
2330
|
retryable: true,
|
|
1943
2331
|
error: error instanceof Error ? error.message : String(error),
|
|
1944
2332
|
};
|
|
1945
2333
|
}
|
|
1946
2334
|
}
|
|
1947
|
-
async function resolveSftpUploadFileTarget(alias, localPath, remotePath, style = 'posix') {
|
|
2335
|
+
async function resolveSftpUploadFileTarget(alias, localPath, remotePath, style = 'posix', deadline, signal) {
|
|
1948
2336
|
if (remotePath.endsWith('/') || remotePath.endsWith('\\')) {
|
|
1949
2337
|
return remoteJoin(style, remotePath, path.basename(localPath));
|
|
1950
2338
|
}
|
|
1951
2339
|
try {
|
|
1952
|
-
const info = await getFileInfo(alias, remotePath);
|
|
2340
|
+
const info = await getFileInfo(alias, remotePath, remainingSftpTimeout(deadline, signal), 'posix', signal);
|
|
1953
2341
|
if (info.isDirectory) {
|
|
1954
2342
|
return remoteJoin(style, remotePath, path.basename(localPath));
|
|
1955
2343
|
}
|
|
@@ -1973,42 +2361,44 @@ function resolveSftpDownloadFileTarget(remotePath, localPath, style = 'posix') {
|
|
|
1973
2361
|
return localPath;
|
|
1974
2362
|
}
|
|
1975
2363
|
async function syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options) {
|
|
1976
|
-
const timeout = options.operationTimeout ?? 600_000;
|
|
2364
|
+
const timeout = remainingSftpTimeout(options.deadline) ?? options.operationTimeout ?? 600_000;
|
|
2365
|
+
const deadline = options.deadline ?? Date.now() + timeout;
|
|
1977
2366
|
const controller = new AbortController();
|
|
2367
|
+
const accumulator = new DirectoryTransferAccumulator();
|
|
2368
|
+
const warnings = [];
|
|
2369
|
+
const abort = () => controller.abort(options.signal?.reason);
|
|
2370
|
+
options.signal?.addEventListener('abort', abort, { once: true });
|
|
2371
|
+
if (options.signal?.aborted) {
|
|
2372
|
+
abort();
|
|
2373
|
+
}
|
|
1978
2374
|
const operation = syncWithSftp(alias, localPath, remotePath, direction, {
|
|
1979
2375
|
...options,
|
|
1980
2376
|
signal: controller.signal,
|
|
1981
|
-
|
|
1982
|
-
|
|
1983
|
-
const
|
|
1984
|
-
|
|
1985
|
-
|
|
1986
|
-
|
|
1987
|
-
|
|
1988
|
-
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
});
|
|
1996
|
-
const result = await Promise.race([operation, timedOut]);
|
|
1997
|
-
if (timer) {
|
|
1998
|
-
clearTimeout(timer);
|
|
2377
|
+
deadline,
|
|
2378
|
+
}, accumulator, warnings);
|
|
2379
|
+
const timer = setTimeout(() => controller.abort(new SftpOperationTimeoutError(timeout)), timeout);
|
|
2380
|
+
try {
|
|
2381
|
+
const result = await operation;
|
|
2382
|
+
return result.verification?.status === 'unknown'
|
|
2383
|
+
? { ...result, operationStatus: 'unknown', timedOut: true }
|
|
2384
|
+
: result;
|
|
2385
|
+
}
|
|
2386
|
+
finally {
|
|
2387
|
+
if (timer) {
|
|
2388
|
+
clearTimeout(timer);
|
|
2389
|
+
}
|
|
2390
|
+
options.signal?.removeEventListener('abort', abort);
|
|
1999
2391
|
}
|
|
2000
|
-
return result;
|
|
2001
2392
|
}
|
|
2002
|
-
async function syncWithSftp(alias, localPath, remotePath, direction, options) {
|
|
2393
|
+
async function syncWithSftp(alias, localPath, remotePath, direction, options, accumulator, warnings) {
|
|
2003
2394
|
throwIfAborted(options.signal);
|
|
2004
|
-
const warnings = [];
|
|
2005
2395
|
if (options.delete) {
|
|
2006
2396
|
warnings.push('delete option is not supported in SFTP mode (requires rsync)');
|
|
2007
2397
|
}
|
|
2008
2398
|
if (options.dryRun) {
|
|
2009
2399
|
const localExists = fs.existsSync(localPath);
|
|
2010
2400
|
const localStats = localExists ? fs.lstatSync(localPath) : undefined;
|
|
2011
|
-
const remoteTargetExists = await remoteExists(alias, remotePath);
|
|
2401
|
+
const remoteTargetExists = await remoteExists(alias, remotePath, options.deadline, options.signal);
|
|
2012
2402
|
return {
|
|
2013
2403
|
success: true,
|
|
2014
2404
|
method: 'sftp',
|
|
@@ -2059,26 +2449,28 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
|
|
|
2059
2449
|
};
|
|
2060
2450
|
}
|
|
2061
2451
|
if (stats.isDirectory()) {
|
|
2062
|
-
const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal, '', options.remotePathStyle);
|
|
2452
|
+
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);
|
|
2063
2453
|
return buildDirectorySyncResult(result, warnings);
|
|
2064
2454
|
}
|
|
2065
|
-
const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle);
|
|
2066
|
-
const existed = await remoteExists(alias, targetRemotePath);
|
|
2067
|
-
const { size } = await uploadFile(alias, localPath, targetRemotePath,
|
|
2455
|
+
const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath, options.remotePathStyle, options.deadline, options.signal);
|
|
2456
|
+
const existed = await remoteExists(alias, targetRemotePath, options.deadline, options.signal);
|
|
2457
|
+
const { size } = await uploadFile(alias, localPath, targetRemotePath, options.onProgress, undefined, undefined, options.signal, options.followSymlinks === true, false, remainingSftpTimeout(options.deadline, options.signal));
|
|
2458
|
+
accumulator.addFile(size, existed);
|
|
2068
2459
|
return {
|
|
2069
2460
|
...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
|
|
2070
|
-
verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload'),
|
|
2461
|
+
verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload', options.verifySha256 !== false, options.deadline, options.signal),
|
|
2071
2462
|
};
|
|
2072
2463
|
}
|
|
2073
2464
|
// download
|
|
2074
|
-
const info = await getFileInfo(alias, remotePath,
|
|
2465
|
+
const info = await getFileInfo(alias, remotePath, remainingSftpTimeout(options.deadline, options.signal), options.remotePathStyle, options.signal);
|
|
2075
2466
|
if (info.isDirectory && options.recursive === false) {
|
|
2076
2467
|
return {
|
|
2077
2468
|
success: false,
|
|
2078
2469
|
method: 'sftp',
|
|
2079
2470
|
operationStatus: 'failed',
|
|
2080
2471
|
stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
|
|
2081
|
-
output: 'recursive=false is not supported for directory sources; use a file
|
|
2472
|
+
output: 'recursive=false is not supported for directory sources; use a file ' +
|
|
2473
|
+
'source or enable recursion',
|
|
2082
2474
|
};
|
|
2083
2475
|
}
|
|
2084
2476
|
if (info.isDirectory) {
|
|
@@ -2086,49 +2478,46 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
|
|
|
2086
2478
|
files: 0,
|
|
2087
2479
|
directories: 0,
|
|
2088
2480
|
bytes: 0,
|
|
2089
|
-
}, 0, options.signal, '', options.remotePathStyle);
|
|
2481
|
+
}, 0, options.signal, '', options.remotePathStyle, accumulator, options.deadline, options.onProgress);
|
|
2090
2482
|
return buildDirectorySyncResult(result, warnings);
|
|
2091
2483
|
}
|
|
2484
|
+
remainingSftpTimeout(options.deadline, options.signal);
|
|
2092
2485
|
const targetLocalPath = resolveSftpDownloadFileTarget(remotePath, localPath, options.remotePathStyle);
|
|
2093
2486
|
const existed = fs.existsSync(targetLocalPath);
|
|
2094
|
-
const { size } = await downloadFile(alias, remotePath, targetLocalPath,
|
|
2487
|
+
const { size } = await downloadFile(alias, remotePath, targetLocalPath, options.onProgress, undefined, options.signal, remainingSftpTimeout(options.deadline, options.signal));
|
|
2488
|
+
accumulator.addFile(size, existed);
|
|
2095
2489
|
return {
|
|
2096
2490
|
...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
|
|
2097
|
-
verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download'),
|
|
2491
|
+
verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download', options.verifySha256 !== false, options.deadline, options.signal),
|
|
2098
2492
|
};
|
|
2099
2493
|
}
|
|
2100
2494
|
catch (err) {
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
method: 'sftp',
|
|
2105
|
-
operationStatus: 'unknown',
|
|
2106
|
-
timedOut: true,
|
|
2107
|
-
retryable: true,
|
|
2108
|
-
stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
|
|
2109
|
-
output: err.message,
|
|
2110
|
-
};
|
|
2111
|
-
}
|
|
2495
|
+
const result = buildDirectorySyncResult(accumulator.toResult(), warnings);
|
|
2496
|
+
const outcome = sftpOperationOutcome(err);
|
|
2497
|
+
const timedOut = err instanceof SftpOperationTimeoutError || outcome.timeout !== undefined;
|
|
2112
2498
|
return {
|
|
2499
|
+
...result,
|
|
2113
2500
|
success: false,
|
|
2114
|
-
method: 'sftp',
|
|
2115
2501
|
operationStatus: 'failed',
|
|
2116
|
-
|
|
2117
|
-
|
|
2502
|
+
...outcome,
|
|
2503
|
+
timedOut: timedOut || undefined,
|
|
2504
|
+
stats: { ...result.stats, failed: Math.max(1, result.stats.failed) },
|
|
2505
|
+
output: [result.output, err instanceof Error ? err.message : String(err)].filter(Boolean).join('; '),
|
|
2118
2506
|
};
|
|
2119
2507
|
}
|
|
2120
2508
|
}
|
|
2121
2509
|
/**
|
|
2122
2510
|
* 递归上传目录
|
|
2123
2511
|
*/
|
|
2124
|
-
async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix', inspectNames) {
|
|
2512
|
+
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) {
|
|
2125
2513
|
throwIfAborted(signal);
|
|
2126
|
-
const accumulator = new DirectoryTransferAccumulator();
|
|
2127
2514
|
recordSftpDirectory(traversal, depth, localPath);
|
|
2515
|
+
const realPath = fs.realpathSync(localPath);
|
|
2128
2516
|
const ownSftp = !sharedSftp && !inspectNames;
|
|
2129
|
-
const sftp = inspectNames
|
|
2517
|
+
const sftp = inspectNames
|
|
2518
|
+
? undefined
|
|
2519
|
+
: (sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal)));
|
|
2130
2520
|
const activeDirectories = (traversal.activeDirectories ??= new Set());
|
|
2131
|
-
const realPath = fs.realpathSync(localPath);
|
|
2132
2521
|
if (activeDirectories.has(realPath)) {
|
|
2133
2522
|
accumulator.addSkippedSymlink();
|
|
2134
2523
|
if (ownSftp) {
|
|
@@ -2139,8 +2528,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2139
2528
|
activeDirectories.add(realPath);
|
|
2140
2529
|
try {
|
|
2141
2530
|
validateLocalPathAgainstAllowList(localPath);
|
|
2531
|
+
remainingSftpTimeout(deadline, signal);
|
|
2142
2532
|
if (!inspectNames) {
|
|
2143
|
-
await mkdir(alias, remotePath, true, sftp,
|
|
2533
|
+
await mkdir(alias, remotePath, true, sftp, remainingSftpTimeout(deadline, signal), style, signal);
|
|
2144
2534
|
}
|
|
2145
2535
|
throwIfAborted(signal);
|
|
2146
2536
|
const items = fs.readdirSync(localPath);
|
|
@@ -2154,6 +2544,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2154
2544
|
}
|
|
2155
2545
|
const itemLocalPath = path.join(localPath, item);
|
|
2156
2546
|
const stats = followSymlinks ? fs.statSync(itemLocalPath) : fs.lstatSync(itemLocalPath);
|
|
2547
|
+
if (matchesDirectoryExclude(itemRelativePath, exclude, stats.isDirectory())) {
|
|
2548
|
+
continue;
|
|
2549
|
+
}
|
|
2157
2550
|
if (!followSymlinks && stats.isSymbolicLink()) {
|
|
2158
2551
|
if (!inspectNames) {
|
|
2159
2552
|
console.warn(`[ssh_sync] skipping symlink: ${itemLocalPath}`);
|
|
@@ -2184,9 +2577,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2184
2577
|
await runWithConcurrency(fileItems, SFTP_PARALLEL_LIMIT, async ({ name, stats }) => {
|
|
2185
2578
|
const itemLocalPath = path.join(localPath, name);
|
|
2186
2579
|
const itemRemotePath = remoteJoin(style, remotePath, name);
|
|
2187
|
-
const existed = await sftpExists(sftp, itemRemotePath);
|
|
2580
|
+
const existed = await sftpExists(sftp, itemRemotePath, remainingSftpTimeout(deadline, signal), signal);
|
|
2188
2581
|
try {
|
|
2189
|
-
await uploadFile(alias, itemLocalPath, itemRemotePath,
|
|
2582
|
+
await uploadFile(alias, itemLocalPath, itemRemotePath, onProgress, sftp, undefined, signal, followSymlinks, false, remainingSftpTimeout(deadline, signal));
|
|
2190
2583
|
accumulator.addFile(stats.size, existed);
|
|
2191
2584
|
}
|
|
2192
2585
|
catch (error) {
|
|
@@ -2199,8 +2592,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2199
2592
|
for (const item of subDirItems) {
|
|
2200
2593
|
const itemLocalPath = path.join(localPath, item);
|
|
2201
2594
|
const itemRemotePath = remoteJoin(style, remotePath, item);
|
|
2202
|
-
|
|
2203
|
-
accumulator.merge(result);
|
|
2595
|
+
await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item, style, inspectNames, accumulator, deadline, onProgress);
|
|
2204
2596
|
}
|
|
2205
2597
|
}
|
|
2206
2598
|
finally {
|
|
@@ -2214,24 +2606,21 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
|
|
|
2214
2606
|
/**
|
|
2215
2607
|
* 递归下载目录
|
|
2216
2608
|
*/
|
|
2217
|
-
async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '', style = 'posix') {
|
|
2609
|
+
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) {
|
|
2218
2610
|
throwIfAborted(signal);
|
|
2219
|
-
const accumulator = new DirectoryTransferAccumulator();
|
|
2220
2611
|
recordSftpDirectory(traversal, depth, remotePath);
|
|
2221
2612
|
const ownSftp = !sharedSftp;
|
|
2222
|
-
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias));
|
|
2613
|
+
const sftp = sharedSftp ?? (await getSftpWithinDeadline(alias, remainingSftpTimeout(deadline, signal), signal));
|
|
2223
2614
|
try {
|
|
2224
|
-
|
|
2615
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
2616
|
+
const items = await listDir(alias, remotePath, true, sftp, remainingSftpTimeout(deadline, signal), style, signal);
|
|
2225
2617
|
throwIfAborted(signal);
|
|
2226
2618
|
if (process.platform === 'win32') {
|
|
2227
|
-
const
|
|
2228
|
-
name.normalize('NFC').toLowerCase(),
|
|
2229
|
-
name,
|
|
2230
|
-
]));
|
|
2231
|
-
const remoteNames = new Set();
|
|
2619
|
+
const remoteNames = [];
|
|
2232
2620
|
for (const item of items) {
|
|
2233
2621
|
const relativeName = relativeBase ? `${relativeBase}/${item.name}` : item.name;
|
|
2234
|
-
if (matchesDirectoryExclude(relativeName, exclude
|
|
2622
|
+
if (matchesDirectoryExclude(relativeName, exclude, item.isDirectory) ||
|
|
2623
|
+
(!item.isDirectory && !item.isFile)) {
|
|
2235
2624
|
continue;
|
|
2236
2625
|
}
|
|
2237
2626
|
if (/[<>:"/\\|?*]/.test(item.name) ||
|
|
@@ -2240,13 +2629,12 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2240
2629
|
/^(?:con|prn|aux|nul|com[1-9]|lpt[1-9])(?:\..*)?$/i.test(item.name)) {
|
|
2241
2630
|
throw new Error(`Remote entry cannot be represented as a Windows filename: ${relativeName}`);
|
|
2242
2631
|
}
|
|
2243
|
-
|
|
2244
|
-
if (remoteNames.has(key) || (localNames.has(key) && localNames.get(key) !== item.name)) {
|
|
2245
|
-
throw new Error(`Windows local filename collision at ${relativeName}`);
|
|
2246
|
-
}
|
|
2247
|
-
remoteNames.add(key);
|
|
2632
|
+
remoteNames.push(item.name);
|
|
2248
2633
|
}
|
|
2634
|
+
verifyWindowsDownloadNames(localPath, remoteNames, remainingSftpTimeout(deadline, signal));
|
|
2249
2635
|
}
|
|
2636
|
+
remainingSftpTimeout(deadline, signal);
|
|
2637
|
+
validateLocalPathAgainstAllowList(localPath);
|
|
2250
2638
|
if (!fs.existsSync(localPath)) {
|
|
2251
2639
|
fs.mkdirSync(localPath, { recursive: true });
|
|
2252
2640
|
}
|
|
@@ -2256,7 +2644,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2256
2644
|
const symlinkItems = [];
|
|
2257
2645
|
for (const item of items) {
|
|
2258
2646
|
const itemRelativePath = relativeBase ? `${relativeBase}/${item.name}` : item.name;
|
|
2259
|
-
if (matchesDirectoryExclude(itemRelativePath, exclude)) {
|
|
2647
|
+
if (matchesDirectoryExclude(itemRelativePath, exclude, item.isDirectory)) {
|
|
2260
2648
|
continue;
|
|
2261
2649
|
}
|
|
2262
2650
|
if (item.isDirectory) {
|
|
@@ -2277,7 +2665,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2277
2665
|
const itemLocalPath = path.join(localPath, item.name);
|
|
2278
2666
|
const existed = fs.existsSync(itemLocalPath);
|
|
2279
2667
|
try {
|
|
2280
|
-
await downloadFile(alias, item.path, itemLocalPath,
|
|
2668
|
+
await downloadFile(alias, item.path, itemLocalPath, onProgress, sftp, signal, remainingSftpTimeout(deadline, signal));
|
|
2281
2669
|
accumulator.addFile(item.size, existed);
|
|
2282
2670
|
}
|
|
2283
2671
|
catch (error) {
|
|
@@ -2287,8 +2675,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
|
|
|
2287
2675
|
});
|
|
2288
2676
|
for (const item of dirItems) {
|
|
2289
2677
|
const itemLocalPath = path.join(localPath, item.name);
|
|
2290
|
-
|
|
2291
|
-
accumulator.merge(result);
|
|
2678
|
+
await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name, style, accumulator, deadline, onProgress);
|
|
2292
2679
|
}
|
|
2293
2680
|
for (const item of symlinkItems) {
|
|
2294
2681
|
console.warn(`[ssh_sync] skipping remote symlink: ${item.path}`);
|