@pyrokine/mcp-ssh 2.3.2 → 2.4.1

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/dist/file-ops.js CHANGED
@@ -2,10 +2,12 @@
2
2
  * SSH File Operations - 文件操作
3
3
  */
4
4
  import { spawn } from 'child_process';
5
- import { createHash } from 'crypto';
5
+ import { createHash, randomBytes } from 'crypto';
6
6
  import * as fs from 'fs';
7
7
  import * as os from 'os';
8
8
  import * as path from 'path';
9
+ import { StringDecoder } from 'string_decoder';
10
+ import { matchesDirectoryExclude } from './directory-verification.js';
9
11
  import { sessionManager } from './session-manager.js';
10
12
  import { escapeShellArg, expandTilde } from './tools/utils.js';
11
13
  // 文件类型 mode 常量
@@ -17,6 +19,8 @@ const S_IFLNK = 0o120000;
17
19
  const RSYNC_CACHE_TTL_MS = 5 * 60_000;
18
20
  const LOCAL_PROCESS_OUTPUT_LIMIT = 200_000;
19
21
  const rsyncCache = new Map();
22
+ const rsyncAliasGenerations = new Map();
23
+ let rsyncGlobalGeneration = 0;
20
24
  function appendLimitedOutput(current, chunk) {
21
25
  if (current.length >= LOCAL_PROCESS_OUTPUT_LIMIT) {
22
26
  return current;
@@ -66,13 +70,30 @@ function runLocalProcess(command, args, timeout) {
66
70
  });
67
71
  });
68
72
  }
73
+ function getRsyncGeneration(alias) {
74
+ return {
75
+ global: rsyncGlobalGeneration,
76
+ alias: rsyncAliasGenerations.get(alias) ?? 0,
77
+ };
78
+ }
79
+ function isCurrentRsyncGeneration(alias, generation) {
80
+ const current = getRsyncGeneration(alias);
81
+ return current.global === generation.global && current.alias === generation.alias;
82
+ }
83
+ function assertCurrentRsyncGeneration(alias, generation) {
84
+ if (!isCurrentRsyncGeneration(alias, generation)) {
85
+ throw new Error(`SSH session '${alias}' changed during rsync setup; retry the sync on the current session`);
86
+ }
87
+ }
69
88
  /** 由 SessionManager.disconnect 调用,防止 alias 重连后读到旧主机的判断 */
70
89
  export function clearRsyncCache(alias) {
71
90
  if (alias === undefined) {
72
91
  rsyncCache.clear();
92
+ ++rsyncGlobalGeneration;
73
93
  }
74
94
  else {
75
95
  rsyncCache.delete(alias);
96
+ rsyncAliasGenerations.set(alias, (rsyncAliasGenerations.get(alias) ?? 0) + 1);
76
97
  }
77
98
  }
78
99
  function positiveIntegerEnv(name, defaultValue) {
@@ -85,6 +106,22 @@ const SFTP_SYNC_MAX_FILES = positiveIntegerEnv('SSH_MCP_SFTP_MAX_FILES', 10_000)
85
106
  const SFTP_SYNC_MAX_DIRECTORIES = positiveIntegerEnv('SSH_MCP_SFTP_MAX_DIRECTORIES', 2_000);
86
107
  const SFTP_SYNC_MAX_DEPTH = positiveIntegerEnv('SSH_MCP_SFTP_MAX_DEPTH', 64);
87
108
  const SFTP_SYNC_MAX_BYTES = positiveIntegerEnv('SSH_MCP_SFTP_MAX_BYTES', 10 * 1024 * 1024 * 1024);
109
+ class SftpOperationTimeoutError extends Error {
110
+ timeout;
111
+ constructor(timeout) {
112
+ super(`SFTP operation timed out after ${timeout}ms; remote transfer state is unknown`);
113
+ this.timeout = timeout;
114
+ this.name = 'SftpOperationTimeoutError';
115
+ }
116
+ }
117
+ function abortError(signal) {
118
+ return signal.reason instanceof Error ? signal.reason : new Error('SFTP operation aborted');
119
+ }
120
+ function throwIfAborted(signal) {
121
+ if (signal?.aborted) {
122
+ throw abortError(signal);
123
+ }
124
+ }
88
125
  function assertSftpTraversalBudget(state, depth, currentPath) {
89
126
  if (depth > SFTP_SYNC_MAX_DEPTH) {
90
127
  throw new Error(`SFTP sync traversal depth exceeded at ${currentPath} (max ${SFTP_SYNC_MAX_DEPTH})`);
@@ -142,24 +179,35 @@ async function runWithConcurrency(items, limit, worker) {
142
179
  function quoteRsyncSshArg(value) {
143
180
  return escapeShellArg(String(value));
144
181
  }
145
- function getAllowListStatus(localPath) {
182
+ function configuredAllowDirectories() {
146
183
  const allowEnv = process.env.SSH_MCP_FILE_OPS_ALLOW_DIRS;
147
- const allowDirs = allowEnv?.split(path.delimiter).filter((item) => item.trim().length > 0) ?? [];
148
- if (allowDirs.length === 0) {
184
+ const configuredPaths = allowEnv?.split(path.delimiter).filter((item) => item.trim().length > 0) ?? [];
185
+ return {
186
+ configured: configuredPaths.length > 0,
187
+ directories: configuredPaths.flatMap((directory) => {
188
+ try {
189
+ return [fs.realpathSync(directory)];
190
+ }
191
+ catch {
192
+ return [];
193
+ }
194
+ }),
195
+ };
196
+ }
197
+ function isPathInsideDirectory(directory, targetPath) {
198
+ const relative = path.relative(directory, targetPath);
199
+ return relative !== '..' && !relative.startsWith(`..${path.sep}`) && !path.isAbsolute(relative);
200
+ }
201
+ function getAllowListStatus(localPath) {
202
+ const allowList = configuredAllowDirectories();
203
+ if (!allowList.configured) {
149
204
  return { configured: false };
150
205
  }
151
206
  const resolvedLocal = resolvePathOrAncestor(localPath);
152
- const matched = allowDirs.some((dir) => {
153
- try {
154
- const resolvedDir = fs.realpathSync(dir);
155
- const rel = path.relative(resolvedDir, resolvedLocal);
156
- return !rel.startsWith('..') && !path.isAbsolute(rel);
157
- }
158
- catch {
159
- return false;
160
- }
161
- });
162
- return { configured: true, matched };
207
+ return {
208
+ configured: true,
209
+ matched: allowList.directories.some((directory) => isPathInsideDirectory(directory, resolvedLocal)),
210
+ };
163
211
  }
164
212
  function validateLocalPathAgainstAllowList(localPath) {
165
213
  const allowList = getAllowListStatus(localPath);
@@ -167,6 +215,9 @@ function validateLocalPathAgainstAllowList(localPath) {
167
215
  throw new Error('localPath 不在 SSH_MCP_FILE_OPS_ALLOW_DIRS 允许目录内');
168
216
  }
169
217
  }
218
+ export function validateLocalPathPolicy(localPath) {
219
+ validateLocalPathAgainstAllowList(expandTilde(localPath));
220
+ }
170
221
  function resolvePathOrAncestor(p) {
171
222
  const absolute = path.isAbsolute(p) ? p : path.resolve(p);
172
223
  try {
@@ -187,14 +238,47 @@ function resolvePathOrAncestor(p) {
187
238
  return absolute;
188
239
  }
189
240
  }
190
- export function probeLocalPath(localPath) {
241
+ function validateLocalUploadSource(localPath, followSymlinks, symlinkError) {
242
+ const expandedPath = expandTilde(localPath);
243
+ validateLocalPathAgainstAllowList(expandedPath);
244
+ if (!fs.existsSync(expandedPath)) {
245
+ throw new Error(`Local source does not exist: ${expandedPath}`);
246
+ }
247
+ const linkStats = fs.lstatSync(expandedPath);
248
+ if (linkStats.isSymbolicLink() && !followSymlinks) {
249
+ throw new Error(symlinkError);
250
+ }
251
+ if (followSymlinks && getAllowListStatus(expandedPath).configured) {
252
+ throw new Error('followSymlinks=true cannot be combined with SSH_MCP_FILE_OPS_ALLOW_DIRS because symlink targets can change during transfer');
253
+ }
254
+ return {
255
+ expandedPath,
256
+ linkStats,
257
+ stats: linkStats.isSymbolicLink() ? fs.statSync(expandedPath) : linkStats,
258
+ };
259
+ }
260
+ export function validateUploadFileSource(localPath) {
261
+ const source = validateLocalUploadSource(localPath, false, 'Refusing to upload symlink at top level (use ssh_sync with followSymlinks=true to traverse)');
262
+ if (source.stats.isDirectory()) {
263
+ throw new Error('UPLOAD_PATH_IS_DIRECTORY: localPath is a directory, use ssh_sync for directory upload');
264
+ }
265
+ if (!source.stats.isFile()) {
266
+ throw new Error('UPLOAD_PATH_IS_NOT_FILE: localPath is not a regular file');
267
+ }
268
+ return source;
269
+ }
270
+ export function validateSyncUploadSource(localPath, followSymlinks = false, _exclude) {
271
+ return validateLocalUploadSource(localPath, followSymlinks, 'Refusing to sync a top-level symlink; set followSymlinks=true to traverse its target');
272
+ }
273
+ export function probeLocalPath(localPath, followSymlinks = false) {
191
274
  const expanded = expandTilde(localPath);
192
275
  const resolvedPath = resolvePathOrAncestor(expanded);
193
276
  const allowList = getAllowListStatus(expanded);
194
277
  if (!fs.existsSync(expanded)) {
195
278
  return { exists: false, inputPath: localPath, expandedPath: expanded, resolvedPath, allowList };
196
279
  }
197
- const stats = fs.lstatSync(expanded);
280
+ const linkStats = fs.lstatSync(expanded);
281
+ const stats = followSymlinks ? fs.statSync(expanded) : linkStats;
198
282
  return {
199
283
  exists: true,
200
284
  inputPath: localPath,
@@ -203,7 +287,7 @@ export function probeLocalPath(localPath) {
203
287
  size: stats.size,
204
288
  mode: (stats.mode & 0o777).toString(8).padStart(4, '0'),
205
289
  mtimeMs: Math.round(stats.mtimeMs),
206
- isSymlink: stats.isSymbolicLink(),
290
+ isSymlink: linkStats.isSymbolicLink(),
207
291
  isDirectory: stats.isDirectory(),
208
292
  isFile: stats.isFile(),
209
293
  allowList,
@@ -224,13 +308,30 @@ function sftpStat(sftp, remotePath) {
224
308
  });
225
309
  });
226
310
  }
311
+ function isSftpMissingError(error) {
312
+ if (!error || typeof error !== 'object') {
313
+ return false;
314
+ }
315
+ const code = 'code' in error ? error.code : undefined;
316
+ if (code === 2 || code === 'ENOENT') {
317
+ return true;
318
+ }
319
+ const message = error instanceof Error ? error.message : String(error);
320
+ return /no such file|not found/i.test(message);
321
+ }
322
+ function isLocalMissingError(error) {
323
+ return Boolean(error && typeof error === 'object' && 'code' in error && error.code === 'ENOENT');
324
+ }
227
325
  async function sftpExists(sftp, remotePath) {
228
326
  try {
229
327
  await sftpStat(sftp, remotePath);
230
328
  return true;
231
329
  }
232
- catch {
233
- return false;
330
+ catch (error) {
331
+ if (isSftpMissingError(error)) {
332
+ return false;
333
+ }
334
+ throw error;
234
335
  }
235
336
  }
236
337
  async function remoteExists(alias, remotePath) {
@@ -242,17 +343,90 @@ async function remoteExists(alias, remotePath) {
242
343
  sftp.end();
243
344
  }
244
345
  }
346
+ export async function hashRemoteFileDigest(alias, remotePath, algorithm) {
347
+ const sftp = await sessionManager.getSftp(alias);
348
+ return new Promise((resolve, reject) => {
349
+ const hash = createHash(algorithm);
350
+ const stream = sftp.createReadStream(remotePath);
351
+ let settled = false;
352
+ const finish = (error) => {
353
+ if (settled) {
354
+ return;
355
+ }
356
+ settled = true;
357
+ sftp.end();
358
+ if (error) {
359
+ reject(error);
360
+ }
361
+ else {
362
+ resolve(hash.digest('hex'));
363
+ }
364
+ };
365
+ stream.on('data', (chunk) => hash.update(chunk));
366
+ stream.once('error', finish);
367
+ stream.once('end', () => finish());
368
+ });
369
+ }
370
+ export class RemoteRenameNotStartedError extends Error {
371
+ constructor(message) {
372
+ super(message);
373
+ this.name = 'RemoteRenameNotStartedError';
374
+ }
375
+ }
376
+ export async function renameRemoteFile(alias, sourcePath, targetPath, replaceExisting = false) {
377
+ const sftp = await sessionManager.getSftp(alias);
378
+ return new Promise((resolve, reject) => {
379
+ const callback = (error) => {
380
+ sftp.end();
381
+ if (error) {
382
+ reject(error);
383
+ }
384
+ else {
385
+ resolve();
386
+ }
387
+ };
388
+ try {
389
+ if (replaceExisting) {
390
+ sftp.ext_openssh_rename(sourcePath, targetPath, callback);
391
+ }
392
+ else {
393
+ sftp.rename(sourcePath, targetPath, callback);
394
+ }
395
+ }
396
+ catch (error) {
397
+ sftp.end();
398
+ reject(new RemoteRenameNotStartedError(`SFTP rename request was not sent: ${error instanceof Error ? error.message : String(error)}`));
399
+ }
400
+ });
401
+ }
402
+ export async function removeRemoteFile(alias, remotePath) {
403
+ const sftp = await sessionManager.getSftp(alias);
404
+ return new Promise((resolve, reject) => {
405
+ sftp.unlink(remotePath, (error) => {
406
+ sftp.end();
407
+ if (error && !isSftpMissingError(error)) {
408
+ reject(error);
409
+ }
410
+ else {
411
+ resolve();
412
+ }
413
+ });
414
+ });
415
+ }
245
416
  /**
246
417
  * 带进度追踪的流传输
247
418
  */
248
- function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, closeSftp = true) {
419
+ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress, closeSftp = true, signal) {
249
420
  return new Promise((resolve, reject) => {
250
421
  let settled = false;
422
+ const onAbort = () => cleanup(abortError(signal));
423
+ const detachAbort = () => signal?.removeEventListener('abort', onAbort);
251
424
  const cleanup = (err) => {
252
425
  if (settled) {
253
426
  return;
254
427
  }
255
428
  settled = true;
429
+ detachAbort();
256
430
  // 先解管道并强制销毁两端,避免错误后流仍持有 sftp 资源
257
431
  try {
258
432
  readStream.unpipe(writeStream);
@@ -262,14 +436,14 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
262
436
  }
263
437
  try {
264
438
  ;
265
- readStream.destroy?.();
439
+ readStream.destroy?.(err);
266
440
  }
267
441
  catch {
268
442
  /* ignore */
269
443
  }
270
444
  try {
271
445
  ;
272
- writeStream.destroy?.();
446
+ writeStream.destroy?.(err);
273
447
  }
274
448
  catch {
275
449
  /* ignore */
@@ -293,62 +467,105 @@ function pipeWithProgress(readStream, writeStream, sftp, totalSize, onProgress,
293
467
  readStream.on('error', (err) => cleanup(err));
294
468
  writeStream.on('error', (err) => cleanup(err));
295
469
  writeStream.on('close', () => {
296
- if (!settled) {
297
- settled = true;
298
- if (closeSftp) {
299
- sftp.end();
300
- }
301
- resolve({ success: true, size: totalSize });
470
+ if (settled) {
471
+ return;
472
+ }
473
+ if (transferred !== totalSize) {
474
+ cleanup(new Error(`Transfer size mismatch: expected ${totalSize} bytes, received ${transferred}`));
475
+ return;
476
+ }
477
+ settled = true;
478
+ detachAbort();
479
+ if (closeSftp) {
480
+ sftp.end();
302
481
  }
482
+ resolve({ success: true, size: totalSize });
303
483
  });
484
+ if (signal?.aborted) {
485
+ cleanup(abortError(signal));
486
+ return;
487
+ }
488
+ signal?.addEventListener('abort', onAbort, { once: true });
304
489
  readStream.pipe(writeStream);
305
490
  });
306
491
  }
492
+ export class UploadFileError extends Error {
493
+ remoteCreated;
494
+ constructor(message, remoteCreated, options) {
495
+ super(message, options);
496
+ this.remoteCreated = remoteCreated;
497
+ this.name = 'UploadFileError';
498
+ }
499
+ }
307
500
  /**
308
501
  * 上传文件
309
502
  */
310
- export async function uploadFile(alias, localPath, remotePath, onProgress, sharedSftp) {
311
- localPath = expandTilde(localPath);
312
- validateLocalPathAgainstAllowList(localPath);
313
- if (!fs.existsSync(localPath)) {
314
- throw new Error('Local file not found');
315
- }
316
- // 用 lstat 拒绝顶层 symlink,与 ssh_sync 顶层 followSymlinks=false 行为一致;
317
- // 单文件接口不引入 followSymlinks 选项,需走 symlink 请用 ssh_sync
318
- const lstats = fs.lstatSync(localPath);
319
- if (lstats.isSymbolicLink()) {
320
- throw new Error('Refusing to upload symlink at top level (use ssh_sync with followSymlinks=true to traverse)');
321
- }
322
- if (lstats.isDirectory()) {
503
+ export async function uploadFile(alias, localPath, remotePath, onProgress, sharedSftp, createMode, signal, followSymlinks = false, exclusive = false) {
504
+ throwIfAborted(signal);
505
+ const source = followSymlinks
506
+ ? validateLocalUploadSource(localPath, true, 'Refusing to upload symlink at top level (use ssh_sync with followSymlinks=true to traverse)')
507
+ : validateUploadFileSource(localPath);
508
+ localPath = source.expandedPath;
509
+ const stats = source.stats;
510
+ if (stats.isDirectory()) {
323
511
  throw new Error('UPLOAD_PATH_IS_DIRECTORY: localPath is a directory, use ssh_sync for directory upload');
324
512
  }
325
- if (!lstats.isFile()) {
513
+ if (!stats.isFile()) {
326
514
  throw new Error('UPLOAD_PATH_IS_NOT_FILE: localPath is not a regular file');
327
515
  }
328
516
  const sftp = sharedSftp ?? (await sessionManager.getSftp(alias));
329
- const totalSize = lstats.size;
330
- return pipeWithProgress(fs.createReadStream(localPath), sftp.createWriteStream(remotePath), sftp, totalSize, onProgress, !sharedSftp);
517
+ const totalSize = stats.size;
518
+ const safeCreateMode = (createMode ?? stats.mode) & 0o777;
519
+ const writeStream = sftp.createWriteStream(remotePath, { mode: safeCreateMode, flags: exclusive ? 'wx' : 'w' });
520
+ let remoteCreated = false;
521
+ writeStream.once('open', () => {
522
+ remoteCreated = true;
523
+ });
524
+ try {
525
+ const result = await pipeWithProgress(fs.createReadStream(localPath), writeStream, sftp, totalSize, onProgress, !sharedSftp, signal);
526
+ return { ...result, createMode: safeCreateMode.toString(8).padStart(4, '0') };
527
+ }
528
+ catch (error) {
529
+ throw new UploadFileError(error instanceof Error ? error.message : String(error), remoteCreated, {
530
+ cause: error,
531
+ });
532
+ }
331
533
  }
332
534
  /**
333
535
  * 下载文件
334
536
  */
335
- export async function downloadFile(alias, remotePath, localPath, onProgress, sharedSftp) {
537
+ export async function downloadFile(alias, remotePath, localPath, onProgress, sharedSftp, signal) {
538
+ throwIfAborted(signal);
336
539
  localPath = expandTilde(localPath);
337
540
  validateLocalPathAgainstAllowList(localPath);
338
541
  const sftp = sharedSftp ?? (await sessionManager.getSftp(alias));
542
+ const localDir = path.dirname(localPath);
543
+ const temporaryPath = path.join(localDir, `.${path.basename(localPath)}.mcp-tmp-${process.pid}-${randomBytes(8).toString('hex')}`);
544
+ let transferStarted = false;
339
545
  try {
340
546
  const totalSize = (await sftpStat(sftp, remotePath)).size;
341
- // 确保本地目录存在
342
- const localDir = path.dirname(localPath);
343
547
  if (!fs.existsSync(localDir)) {
344
548
  fs.mkdirSync(localDir, { recursive: true });
345
549
  }
346
- return await pipeWithProgress(sftp.createReadStream(remotePath), fs.createWriteStream(localPath), sftp, totalSize, onProgress, !sharedSftp);
550
+ transferStarted = true;
551
+ const result = await pipeWithProgress(sftp.createReadStream(remotePath), fs.createWriteStream(temporaryPath, { flags: 'wx' }), sftp, totalSize, onProgress, !sharedSftp, signal);
552
+ await fs.promises.rename(temporaryPath, localPath);
553
+ return { ...result, atomic: true };
347
554
  }
348
555
  catch (error) {
349
- if (!sharedSftp) {
556
+ if (!sharedSftp && !transferStarted) {
350
557
  sftp.end();
351
558
  }
559
+ try {
560
+ await fs.promises.unlink(temporaryPath);
561
+ }
562
+ catch (cleanupError) {
563
+ if (!isLocalMissingError(cleanupError)) {
564
+ throw new AggregateError([error, cleanupError], 'Download failed and temporary file cleanup failed', {
565
+ cause: cleanupError,
566
+ });
567
+ }
568
+ }
352
569
  throw error;
353
570
  }
354
571
  }
@@ -399,25 +616,49 @@ function emptyReadResult(totalSize, sampleKind, readOffset = 0) {
399
616
  truncated: readOffset < totalSize,
400
617
  };
401
618
  }
619
+ export function buildLineRangeAwkProgram(start, end) {
620
+ return [
621
+ 'BEGIN { written = 0; actual_start = 0; actual_end = 0; byte_limit = 0; saw_after = 0; full_body = 0 }',
622
+ `NR >= ${start} && NR <= ${end} {`,
623
+ ' if (actual_start == 0) { actual_start = NR }',
624
+ ' actual_end = NR',
625
+ ' remaining = max - written',
626
+ ' if (remaining <= 0) { byte_limit = 1; exit }',
627
+ ' body_length = length($0)',
628
+ ' if (body_length >= remaining) {',
629
+ ' printf "%s", substr($0, 1, remaining)',
630
+ ' written += remaining',
631
+ ' if (body_length > remaining) { byte_limit = 1; exit }',
632
+ ' full_body = 1',
633
+ ' next',
634
+ ' }',
635
+ ' printf "%s\\n", $0',
636
+ ' written += body_length + 1',
637
+ ' full_body = 0',
638
+ '}',
639
+ `NR > ${end} { if (full_body) { byte_limit = 1 } saw_after = 1; exit }`,
640
+ 'END {',
641
+ ' if (full_body && final_terminated) { byte_limit = 1 }',
642
+ ' printf "__MCP_LINE_META__%d:%d:%d:%d:%d\\n", actual_start, actual_end, written, byte_limit, saw_after > "/dev/stderr"',
643
+ '}',
644
+ ].join('\n');
645
+ }
402
646
  async function readLineRange(alias, remotePath, totalSize, maxBytes, lineRange) {
403
647
  const { start, end } = parseLineRange(lineRange);
404
- const awkProgram = [
405
- 'BEGIN { written = 0 }',
406
- `NR>=${start} && NR<=${end} {`,
407
- 'line = $0 "\\n"',
408
- 'remaining = max - written',
409
- 'if (remaining <= 0) { exit }',
410
- 'if (length(line) > remaining) { printf "%s", substr(line, 1, remaining); exit }',
411
- 'printf "%s", line',
412
- 'written += length(line)',
413
- '}',
414
- `NR>${end} { exit }`,
415
- ].join(' ');
416
- const command = `LC_ALL=C awk -v max=${maxBytes} ${escapeShellArg(awkProgram)} ${escapeShellArg(remotePath)}`;
417
- const result = await sessionManager.exec(alias, command, {
648
+ const awkProgram = buildLineRangeAwkProgram(start, end);
649
+ const escapedPath = escapeShellArg(remotePath);
650
+ const command = [
651
+ `last_byte=$(tail -c 1 -- ${escapedPath} 2>/dev/null | od -An -tu1 | tr -d ' ')`,
652
+ 'if [ "$last_byte" = "10" ]; then terminated=1; else terminated=0; fi',
653
+ `LC_ALL=C awk -v max=${maxBytes} -v final_terminated="$terminated" ${escapeShellArg(awkProgram)} ${escapedPath} | base64 | tr -d '\\n'`,
654
+ 'status=${PIPESTATUS[0]}',
655
+ 'printf "__MCP_LINE_END__%s\\n" "$terminated" >&2',
656
+ 'exit "$status"',
657
+ ].join('; ');
658
+ const result = await sessionManager.exec(alias, `bash -c ${escapeShellArg(command)}`, {
418
659
  timeout: 30000,
419
660
  useLoginUser: true,
420
- maxOutputSize: maxBytes + 4096,
661
+ maxOutputSize: Math.ceil(maxBytes / 3) * 4 + 4096,
421
662
  });
422
663
  if (!result.success) {
423
664
  const message = result.stderr.trim() ||
@@ -425,18 +666,43 @@ async function readLineRange(alias, remotePath, totalSize, maxBytes, lineRange)
425
666
  `remote lineRange command failed with exit code ${result.exitCode}`;
426
667
  throw new Error(message);
427
668
  }
428
- const readBytes = Buffer.byteLength(result.stdout, 'utf-8');
669
+ const metadataMatch = result.stderr.match(/__MCP_LINE_META__(\d+):(\d+):(\d+):([01]):([01])/);
670
+ const terminatedMatch = result.stderr.match(/__MCP_LINE_END__([01])/);
671
+ if (!metadataMatch || !terminatedMatch) {
672
+ throw new Error('remote lineRange command returned incomplete metadata');
673
+ }
674
+ const actualStart = Number(metadataMatch[1]);
675
+ const actualEnd = Number(metadataMatch[2]);
676
+ const encoded = result.stdout.trim();
677
+ if (encoded && (!/^[A-Za-z0-9+/]+={0,2}$/.test(encoded) || encoded.length % 4 !== 0)) {
678
+ throw new Error('remote lineRange command returned malformed base64 content');
679
+ }
680
+ let rawContent = Buffer.from(encoded, 'base64');
681
+ const metadataByteLimitReached = metadataMatch[4] === '1';
682
+ const sawAfter = metadataMatch[5] === '1';
683
+ const finalLineTerminated = terminatedMatch[1] === '1';
684
+ if (!sawAfter && !metadataByteLimitReached && !finalLineTerminated && rawContent.at(-1) === 0x0a) {
685
+ rawContent = rawContent.subarray(0, -1);
686
+ }
687
+ const decoder = new StringDecoder('utf8');
688
+ const content = decoder.write(rawContent);
689
+ const incompleteUtf8 = decoder.end().length > 0;
690
+ const readBytes = Buffer.byteLength(content, 'utf8');
691
+ const byteLimitReached = metadataByteLimitReached || incompleteUtf8;
692
+ const eof = !sawAfter && !byteLimitReached;
429
693
  return {
430
- content: result.stdout,
694
+ content,
431
695
  size: totalSize,
432
696
  total_size: totalSize,
433
697
  read_offset: 0,
434
698
  read_bytes: readBytes,
435
- remaining_bytes: Math.max(0, totalSize - readBytes),
436
699
  sample_kind: 'line_range',
437
- truncated: readBytes >= maxBytes,
438
- line_start: start,
439
- line_end: end,
700
+ truncated: byteLimitReached || sawAfter,
701
+ line_start: actualStart || undefined,
702
+ line_end: actualEnd || undefined,
703
+ eof,
704
+ byte_limit_reached: byteLimitReached,
705
+ final_line_terminated: actualEnd === 0 ? undefined : finalLineTerminated,
440
706
  };
441
707
  }
442
708
  /**
@@ -528,6 +794,16 @@ export async function writeFile(alias, remotePath, content, append = false) {
528
794
  writeStream.end();
529
795
  });
530
796
  }
797
+ function validateSftpEntryName(filename) {
798
+ if (filename.length === 0 ||
799
+ filename === '.' ||
800
+ filename === '..' ||
801
+ filename.includes('\0') ||
802
+ filename.includes('/') ||
803
+ filename.includes('\\')) {
804
+ throw new Error('SFTP directory listing returned an unsafe entry name');
805
+ }
806
+ }
531
807
  /**
532
808
  * 列出目录内容
533
809
  */
@@ -543,6 +819,18 @@ export async function listDir(alias, remotePath, showHidden = false, sharedSftp)
543
819
  reject(err);
544
820
  return;
545
821
  }
822
+ try {
823
+ for (const item of list) {
824
+ validateSftpEntryName(item.filename);
825
+ }
826
+ }
827
+ catch (error) {
828
+ if (closeWhenDone) {
829
+ sftp.end();
830
+ }
831
+ reject(error);
832
+ return;
833
+ }
546
834
  const files = list
547
835
  .filter((item) => showHidden || !item.filename.startsWith('.'))
548
836
  .map((item) => ({
@@ -600,49 +888,167 @@ export async function getFileInfo(alias, remotePath) {
600
888
  });
601
889
  });
602
890
  }
603
- /**
604
- * 创建目录
605
- */
606
- export async function mkdir(alias, remotePath, recursive = false) {
607
- if (recursive) {
608
- // 通过 exec 实现递归创建
609
- const result = await sessionManager.exec(alias, `mkdir -p ${escapeShellArg(remotePath)}`);
610
- return result.exitCode === 0;
611
- }
612
- const sftp = await sessionManager.getSftp(alias);
891
+ function sftpMkdir(sftp, remotePath) {
613
892
  return new Promise((resolve, reject) => {
614
- sftp.mkdir(remotePath, (err) => {
615
- sftp.end();
616
- if (err) {
617
- reject(err);
893
+ sftp.mkdir(remotePath, (error) => {
894
+ if (error) {
895
+ reject(error);
618
896
  }
619
897
  else {
620
- resolve(true);
898
+ resolve();
621
899
  }
622
900
  });
623
901
  });
624
902
  }
903
+ async function ensureSftpDirectory(sftp, remotePath) {
904
+ const normalizedPath = path.posix.normalize(remotePath);
905
+ if (normalizedPath === '/' || normalizedPath === '.') {
906
+ return;
907
+ }
908
+ try {
909
+ const stats = await sftpStat(sftp, normalizedPath);
910
+ if ((stats.mode & S_IFMT) !== S_IFDIR) {
911
+ throw new Error(`Remote path exists and is not a directory: ${normalizedPath}`);
912
+ }
913
+ return;
914
+ }
915
+ catch (error) {
916
+ if (!isSftpMissingError(error)) {
917
+ throw error;
918
+ }
919
+ }
920
+ const parent = path.posix.dirname(normalizedPath);
921
+ if (parent !== normalizedPath) {
922
+ await ensureSftpDirectory(sftp, parent);
923
+ }
924
+ try {
925
+ await sftpMkdir(sftp, normalizedPath);
926
+ }
927
+ catch (error) {
928
+ try {
929
+ const stats = await sftpStat(sftp, normalizedPath);
930
+ if ((stats.mode & S_IFMT) === S_IFDIR) {
931
+ return;
932
+ }
933
+ }
934
+ catch {
935
+ // 保留原 mkdir 错误
936
+ }
937
+ throw error;
938
+ }
939
+ }
940
+ /**
941
+ * 创建目录
942
+ */
943
+ export async function mkdir(alias, remotePath, recursive = false, sharedSftp) {
944
+ const sftp = sharedSftp ?? (await sessionManager.getSftp(alias));
945
+ try {
946
+ if (recursive) {
947
+ await ensureSftpDirectory(sftp, remotePath);
948
+ }
949
+ else {
950
+ await sftpMkdir(sftp, remotePath);
951
+ }
952
+ return true;
953
+ }
954
+ finally {
955
+ if (!sharedSftp) {
956
+ sftp.end();
957
+ }
958
+ }
959
+ }
625
960
  /**
626
961
  * 检查远程是否安装 rsync
627
962
  */
628
- export async function checkRsync(alias) {
963
+ export async function checkRsync(alias, timeout = 10_000) {
964
+ const generation = getRsyncGeneration(alias);
629
965
  const cached = rsyncCache.get(alias);
630
966
  if (cached !== undefined && cached.expiresAt > Date.now()) {
631
967
  return cached.value;
632
968
  }
969
+ const startedAt = Date.now();
633
970
  try {
634
- const result = await sessionManager.exec(alias, 'which rsync');
635
- const available = result.exitCode === 0 && result.stdout.trim().length > 0;
636
- rsyncCache.set(alias, { value: available, expiresAt: Date.now() + RSYNC_CACHE_TTL_MS });
637
- return available;
971
+ const result = await sessionManager.exec(alias, 'command -v rsync', {
972
+ timeout,
973
+ useLoginUser: true,
974
+ maxOutputSize: 4096,
975
+ });
976
+ const duration = Date.now() - startedAt;
977
+ if (!isCurrentRsyncGeneration(alias, generation)) {
978
+ return {
979
+ available: false,
980
+ status: 'error',
981
+ duration,
982
+ retryable: true,
983
+ error: `SSH session '${alias}' changed during rsync probe`,
984
+ };
985
+ }
986
+ if (result.success && result.exitCode === 0 && result.stdout.trim().length > 0) {
987
+ const probe = {
988
+ available: true,
989
+ status: 'available',
990
+ duration,
991
+ retryable: false,
992
+ exitCode: result.exitCode,
993
+ };
994
+ rsyncCache.set(alias, { value: probe, expiresAt: Date.now() + RSYNC_CACHE_TTL_MS });
995
+ return probe;
996
+ }
997
+ if (result.failureKind === 'timeout' || result.timedOut) {
998
+ return {
999
+ available: false,
1000
+ status: 'timeout',
1001
+ duration,
1002
+ retryable: true,
1003
+ exitCode: result.exitCode,
1004
+ failureKind: result.failureKind,
1005
+ error: result.stderr.trim() || result.stdout.trim() || `rsync probe timed out after ${timeout}ms`,
1006
+ };
1007
+ }
1008
+ if (result.failureKind === 'ssh_transport') {
1009
+ return {
1010
+ available: false,
1011
+ status: 'error',
1012
+ duration,
1013
+ retryable: true,
1014
+ exitCode: result.exitCode,
1015
+ failureKind: result.failureKind,
1016
+ error: result.stderr.trim() || result.stdout.trim() || 'SSH transport failed during rsync probe',
1017
+ };
1018
+ }
1019
+ if (result.exitCode === 1 && result.stderr.trim().length === 0) {
1020
+ const probe = {
1021
+ available: false,
1022
+ status: 'unavailable',
1023
+ duration,
1024
+ retryable: false,
1025
+ exitCode: result.exitCode,
1026
+ };
1027
+ rsyncCache.set(alias, { value: probe, expiresAt: Date.now() + RSYNC_CACHE_TTL_MS });
1028
+ return probe;
1029
+ }
1030
+ return {
1031
+ available: false,
1032
+ status: 'error',
1033
+ duration,
1034
+ retryable: result.failureKind !== 'remote_command',
1035
+ exitCode: result.exitCode,
1036
+ failureKind: result.failureKind,
1037
+ error: result.stderr.trim() || result.stdout.trim() || `rsync probe failed with exit code ${result.exitCode}`,
1038
+ };
638
1039
  }
639
- catch {
640
- rsyncCache.set(alias, { value: false, expiresAt: Date.now() + RSYNC_CACHE_TTL_MS });
641
- return false;
1040
+ catch (error) {
1041
+ return {
1042
+ available: false,
1043
+ status: 'error',
1044
+ duration: Date.now() - startedAt,
1045
+ retryable: true,
1046
+ error: error instanceof Error ? error.message : String(error),
1047
+ };
642
1048
  }
643
1049
  }
644
1050
  /**
645
- * 智能文件同步(优先使用 rsync)
1051
+ * 智能文件同步(根据连接能力选择 rsync 或 SFTP)
646
1052
  *
647
1053
  * @param alias SSH 连接别名
648
1054
  * @param localPath 本地路径
@@ -654,13 +1060,72 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
654
1060
  const startedAt = Date.now();
655
1061
  localPath = expandTilde(localPath);
656
1062
  validateLocalPathAgainstAllowList(localPath);
657
- const hasRsync = await checkRsync(alias);
658
- const result = hasRsync
659
- ? await syncWithRsync(alias, localPath, remotePath, direction, options)
660
- : await syncWithSftp(alias, localPath, remotePath, direction, options);
1063
+ if (remotePath === '~' || remotePath.startsWith('~/')) {
1064
+ throw new Error('remotePath must be absolute or relative to the SSH working directory; ~ is not supported');
1065
+ }
1066
+ let sourceIsDirectory;
1067
+ if (direction === 'upload') {
1068
+ const source = validateSyncUploadSource(localPath, options.followSymlinks === true, options.exclude);
1069
+ localPath = source.expandedPath;
1070
+ sourceIsDirectory = source.stats.isDirectory();
1071
+ }
1072
+ else {
1073
+ sourceIsDirectory = (await getFileInfo(alias, remotePath)).isDirectory;
1074
+ }
1075
+ if (sourceIsDirectory && options.recursive === false) {
1076
+ throw new Error('recursive=false is not supported for directory sources; use a file source or enable recursion');
1077
+ }
1078
+ const capabilityStartedAt = Date.now();
1079
+ const rsyncGeneration = getRsyncGeneration(alias);
1080
+ const capability = sessionManager.getExternalTransferCapability(alias);
1081
+ const preflightTimeout = options.preflightTimeout ?? 10_000;
1082
+ const rsyncProbe = capability.rsyncEligible
1083
+ ? await checkRsync(alias, preflightTimeout)
1084
+ : {
1085
+ available: false,
1086
+ status: 'skipped',
1087
+ duration: 0,
1088
+ retryable: false,
1089
+ reason: capability.decisionReason,
1090
+ };
1091
+ assertCurrentRsyncGeneration(alias, rsyncGeneration);
1092
+ const resolvedOptions = { ...options, sourceIsDirectory };
1093
+ const capabilityDuration = Date.now() - capabilityStartedAt;
1094
+ const selectedTransport = capability.rsyncEligible && rsyncProbe?.available ? 'rsync' : 'sftp';
1095
+ const decisionReason = !capability.rsyncEligible
1096
+ ? capability.decisionReason
1097
+ : rsyncProbe?.status === 'available'
1098
+ ? capability.decisionReason
1099
+ : rsyncProbe?.status === 'unavailable'
1100
+ ? 'remote_rsync_unavailable'
1101
+ : rsyncProbe?.status === 'timeout'
1102
+ ? 'remote_rsync_probe_timeout'
1103
+ : 'remote_rsync_probe_failed';
1104
+ const operationStartedAt = Date.now();
1105
+ const result = selectedTransport === 'rsync'
1106
+ ? await syncWithRsync(alias, localPath, remotePath, direction, resolvedOptions, capability, rsyncGeneration)
1107
+ : await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, resolvedOptions);
1108
+ const operationDuration = Date.now() - operationStartedAt;
1109
+ const operationStatus = result.operationStatus ?? (result.success ? 'completed' : 'failed');
1110
+ const connectStatus = result.connectStatus ?? (selectedTransport === 'rsync' ? 'completed' : 'not_applicable');
661
1111
  return {
662
1112
  ...result,
663
1113
  transport: result.method,
1114
+ selectedTransport,
1115
+ decisionReason,
1116
+ rsyncProbe,
1117
+ fallbackReason: result.fallbackReason,
1118
+ phaseDurations: {
1119
+ capabilityPreflight: capabilityDuration,
1120
+ connect: result.connectDuration,
1121
+ operation: operationDuration,
1122
+ total: Date.now() - startedAt,
1123
+ },
1124
+ phaseStatus: {
1125
+ capabilityPreflight: rsyncProbe?.status === 'timeout' || rsyncProbe?.status === 'error' ? 'failed' : 'completed',
1126
+ connect: connectStatus,
1127
+ operation: operationStatus,
1128
+ },
664
1129
  direction,
665
1130
  dryRun: options.dryRun === true,
666
1131
  duration: Date.now() - startedAt,
@@ -670,22 +1135,18 @@ export async function syncFiles(alias, localPath, remotePath, direction, options
670
1135
  * 使用 rsync 同步文件
671
1136
  * 通过本地执行 rsync 连接到远程(需要密钥认证或 ssh-agent)
672
1137
  */
673
- async function syncWithRsync(alias, localPath, remotePath, direction, options) {
1138
+ async function syncWithRsync(alias, localPath, remotePath, direction, options, capability, generation) {
1139
+ assertCurrentRsyncGeneration(alias, generation);
674
1140
  const checkCommand = os.platform() === 'win32' ? 'where' : 'which';
675
- const checkResult = await runLocalProcess(checkCommand, ['rsync'], 10_000);
1141
+ const checkResult = await runLocalProcess(checkCommand, ['rsync'], options.preflightTimeout ?? 10_000);
1142
+ assertCurrentRsyncGeneration(alias, generation);
676
1143
  if (checkResult.status !== 0) {
677
- // 本地没有 rsync,回退到 SFTP
678
- return syncWithSftp(alias, localPath, remotePath, direction, options);
679
- }
680
- // 获取会话信息以构建 rsync 命令
681
- const sessions = sessionManager.listSessions();
682
- const sessionInfo = sessions.find((s) => s.alias === alias);
683
- if (!sessionInfo) {
684
- throw new Error(`Session '${alias}' not found`);
685
- }
686
- // 密码认证无法透传给 rsync 的 ssh 子进程,直接走 SFTP
687
- if (sessionInfo.authMethod === 'password') {
688
- return syncWithSftp(alias, localPath, remotePath, direction, options);
1144
+ const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1145
+ return {
1146
+ ...fallback,
1147
+ connectStatus: 'not_applicable',
1148
+ fallbackReason: checkResult.timedOut ? 'local_rsync_preflight_timeout' : 'local_rsync_unavailable',
1149
+ };
689
1150
  }
690
1151
  // 构建 rsync 参数
691
1152
  const args = ['-avz', '--itemize-changes', '--stats'];
@@ -695,66 +1156,125 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options) {
695
1156
  if (options.dryRun) {
696
1157
  args.push('--dry-run');
697
1158
  }
698
- if (options.recursive === false) {
699
- args.push('--dirs'); // 不递归,只传输目录本身
700
- }
701
1159
  if (options.exclude) {
702
1160
  for (const pattern of options.exclude) {
703
1161
  args.push(`--exclude=${pattern}`);
704
1162
  }
705
1163
  }
706
1164
  if (options.followSymlinks) {
707
- // -L / --copy-links: 跟随 symlink,上传链接目标内容(与 SFTP 路径 followSymlinks=true 行为对齐)
708
- // 默认不传则用 rsync -a 自带的 -l:保留 symlink 本身,不上传目标内容
1165
+ // -L / --copy-links: 跟随 symlink,上传链接目标内容(与 SFTP 路径 followSymlinks=true 行为一致)
709
1166
  args.push('-L');
710
1167
  }
1168
+ else {
1169
+ // -a 隐含 -l,显式关闭后默认跳过 symlink,与 SFTP 默认行为一致
1170
+ args.push('--no-links');
1171
+ }
1172
+ args.push('--no-devices', '--no-specials');
711
1173
  // path 安全校验:以 `-` 起始的路径会被 rsync 当成选项解析(即便有 `--` 兜底,也避免 OS/rsync 实现差异)
712
1174
  if (localPath.startsWith('-') || remotePath.startsWith('-')) {
713
1175
  throw new Error('Invalid path: localPath and remotePath must not start with -');
714
1176
  }
715
1177
  // username/host 安全校验:拒绝 shell 元字符与 ssh 选项注入
716
1178
  const sshIdentifierAllowed = /^[a-zA-Z0-9.:_-]+$/;
717
- if (!sshIdentifierAllowed.test(sessionInfo.username)) {
718
- throw new Error(`Invalid username for rsync: "${sessionInfo.username}" 含非法字符`);
1179
+ if (!sshIdentifierAllowed.test(capability.username)) {
1180
+ throw new Error(`Invalid username for rsync: "${capability.username}" 含非法字符`);
719
1181
  }
720
- if (!sshIdentifierAllowed.test(sessionInfo.host)) {
721
- throw new Error(`Invalid host for rsync: "${sessionInfo.host}" 含非法字符`);
1182
+ if (!sshIdentifierAllowed.test(capability.host)) {
1183
+ throw new Error(`Invalid host for rsync: "${capability.host}" 含非法字符`);
722
1184
  }
723
1185
  // 构建 rsync argv(不用 shell=true,避免 username/host 含 shell 元字符时注入)
724
1186
  // ProxyCommand=none 显式禁用 ssh-config 中可能定义的 ProxyCommand,防止外部命令注入
725
- const sshParts = [
726
- 'ssh',
1187
+ const connectTimeout = options.connectTimeout ?? 30_000;
1188
+ const sshOptions = [
727
1189
  '-p',
728
- String(sessionInfo.port),
1190
+ String(capability.port),
729
1191
  '-o',
730
1192
  'StrictHostKeyChecking=accept-new',
731
1193
  '-o',
732
1194
  'BatchMode=yes',
733
1195
  '-o',
1196
+ `ConnectTimeout=${Math.max(1, Math.ceil(connectTimeout / 1000))}`,
1197
+ '-o',
734
1198
  'ProxyCommand=none',
735
1199
  ];
736
- if (sessionInfo.keyPath) {
737
- sshParts.push('-i', sessionInfo.keyPath);
1200
+ if (capability.keyPath) {
1201
+ sshOptions.push('-i', capability.keyPath);
1202
+ }
1203
+ const destination = `${capability.username}@${capability.host}`;
1204
+ assertCurrentRsyncGeneration(alias, generation);
1205
+ const connectStartedAt = Date.now();
1206
+ const connectResult = await runLocalProcess('ssh', [...sshOptions, destination, 'true'], connectTimeout);
1207
+ const connectDuration = Date.now() - connectStartedAt;
1208
+ assertCurrentRsyncGeneration(alias, generation);
1209
+ if (connectResult.error || connectResult.status !== 0) {
1210
+ const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
1211
+ const fallbackReason = connectResult.timedOut ? 'rsync_connect_timeout' : 'rsync_connect_failed';
1212
+ const detail = connectResult.stderr.trim() || connectResult.error?.message;
1213
+ return {
1214
+ ...fallback,
1215
+ connectDuration,
1216
+ connectStatus: 'failed',
1217
+ fallbackReason,
1218
+ output: detail
1219
+ ? `OpenSSH connect preflight failed: ${detail}\n${fallback.output ?? ''}`.trim()
1220
+ : fallback.output,
1221
+ };
738
1222
  }
739
- const sshCmd = sshParts.map(quoteRsyncSshArg).join(' ');
1223
+ const sshCmd = ['ssh', ...sshOptions].map(quoteRsyncSshArg).join(' ');
1224
+ const rsyncLocalPath = options.sourceIsDirectory && direction === 'upload' && !localPath.endsWith(path.sep)
1225
+ ? `${localPath}${path.sep}`
1226
+ : localPath;
1227
+ const rsyncRemotePath = options.sourceIsDirectory && direction === 'download' && !remotePath.endsWith('/')
1228
+ ? `${remotePath}/`
1229
+ : remotePath;
740
1230
  // remoteSpec 作为单独 argv 元素,不经 shell 解析
741
- const remoteSpec = `${sessionInfo.username}@${sessionInfo.host}:${remotePath}`;
1231
+ const remoteSpec = `${destination}:${rsyncRemotePath}`;
742
1232
  // 在 path 参数前插 `--`,明确剩余为路径而非选项(双重保险)
743
1233
  const rsyncArgs = direction === 'upload'
744
- ? [...args, '-e', sshCmd, '--', localPath, remoteSpec]
1234
+ ? [...args, '-e', sshCmd, '--', rsyncLocalPath, remoteSpec]
745
1235
  : [...args, '-e', sshCmd, '--', remoteSpec, localPath];
746
1236
  try {
747
- const result = await runLocalProcess('rsync', rsyncArgs, 600000);
1237
+ assertCurrentRsyncGeneration(alias, generation);
1238
+ const operationTimeout = options.operationTimeout ?? 600_000;
1239
+ const result = await runLocalProcess('rsync', rsyncArgs, operationTimeout);
1240
+ if (!isCurrentRsyncGeneration(alias, generation)) {
1241
+ return {
1242
+ success: false,
1243
+ method: 'rsync',
1244
+ connectDuration,
1245
+ connectStatus: 'completed',
1246
+ operationStatus: 'unknown',
1247
+ retryable: true,
1248
+ fallbackReason: 'ssh_session_changed',
1249
+ output: `SSH session '${alias}' changed during rsync; remote transfer state is unknown`,
1250
+ };
1251
+ }
748
1252
  if (result.error || result.status !== 0) {
749
1253
  const stderr = result.stderr.trim();
750
1254
  const reason = result.timedOut
751
- ? 'timed out after 600000ms'
1255
+ ? `timed out after ${operationTimeout}ms`
752
1256
  : (result.error?.message ?? `exit code ${result.status}`);
753
- const fallback = await syncWithSftp(alias, localPath, remotePath, direction, options);
1257
+ if (result.timedOut) {
1258
+ return {
1259
+ success: false,
1260
+ method: 'rsync',
1261
+ connectDuration,
1262
+ connectStatus: 'completed',
1263
+ operationStatus: 'unknown',
1264
+ timedOut: true,
1265
+ retryable: true,
1266
+ fallbackReason: 'rsync_operation_timeout',
1267
+ output: `rsync ${reason}${stderr ? ` | stderr: ${stderr}` : ''}; remote transfer state is unknown`,
1268
+ };
1269
+ }
1270
+ const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
754
1271
  const warning = `rsync 失败原因: ${reason}${stderr ? ` | stderr: ${stderr}` : ''} | 已 fallback 到 SFTP(性能可能较差)`;
755
1272
  console.warn(`[mcp-ssh] ${warning}`);
756
1273
  return {
757
1274
  ...fallback,
1275
+ connectDuration,
1276
+ connectStatus: 'completed',
1277
+ fallbackReason: 'rsync_operation_failed',
758
1278
  output: fallback.output ? `${warning}\n\n${fallback.output}` : warning,
759
1279
  };
760
1280
  }
@@ -764,6 +1284,9 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options) {
764
1284
  return {
765
1285
  success: true,
766
1286
  method: 'rsync',
1287
+ connectDuration,
1288
+ connectStatus: 'completed',
1289
+ operationStatus: 'completed',
767
1290
  filesTransferred,
768
1291
  stats,
769
1292
  commandSummary: {
@@ -773,18 +1296,24 @@ async function syncWithRsync(alias, localPath, remotePath, direction, options) {
773
1296
  recursive: options.recursive !== false,
774
1297
  followSymlinks: options.followSymlinks === true,
775
1298
  excludeCount: options.exclude?.length ?? 0,
776
- usedKeyPath: Boolean(sessionInfo.keyPath),
1299
+ usedKeyPath: Boolean(capability.keyPath),
777
1300
  },
778
1301
  output,
779
1302
  };
780
1303
  }
781
1304
  catch (e) {
782
- // rsync 执行异常,回退到 SFTP(保留原因供调用方追溯)
783
- const fallback = await syncWithSftp(alias, localPath, remotePath, direction, options);
1305
+ if (!isCurrentRsyncGeneration(alias, generation)) {
1306
+ throw e;
1307
+ }
1308
+ // rsync 启动异常发生在传输进程建立前,可安全改用 SFTP
1309
+ const fallback = await syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options);
784
1310
  const warning = `rsync 异常: ${e instanceof Error ? e.message : String(e)} | 已 fallback 到 SFTP(性能可能较差)`;
785
1311
  console.warn(`[mcp-ssh] ${warning}`);
786
1312
  return {
787
1313
  ...fallback,
1314
+ connectDuration,
1315
+ connectStatus: 'completed',
1316
+ fallbackReason: 'rsync_execution_error',
788
1317
  output: fallback.output ? `${warning}\n\n${fallback.output}` : warning,
789
1318
  };
790
1319
  }
@@ -794,6 +1323,8 @@ class DirectoryTransferAccumulator {
794
1323
  fileCount: 0,
795
1324
  totalSize: 0,
796
1325
  skippedSymlinks: 0,
1326
+ skippedUnsupported: 0,
1327
+ unsupportedSamples: [],
797
1328
  added: 0,
798
1329
  updated: 0,
799
1330
  failed: 0,
@@ -811,6 +1342,12 @@ class DirectoryTransferAccumulator {
811
1342
  addSkippedSymlink() {
812
1343
  this.result.skippedSymlinks++;
813
1344
  }
1345
+ addSkippedUnsupported(itemPath) {
1346
+ this.result.skippedUnsupported++;
1347
+ if (this.result.unsupportedSamples.length < 10) {
1348
+ this.result.unsupportedSamples.push(itemPath);
1349
+ }
1350
+ }
814
1351
  addFailed() {
815
1352
  this.result.failed++;
816
1353
  }
@@ -818,6 +1355,12 @@ class DirectoryTransferAccumulator {
818
1355
  this.result.fileCount += result.fileCount;
819
1356
  this.result.totalSize += result.totalSize;
820
1357
  this.result.skippedSymlinks += result.skippedSymlinks;
1358
+ this.result.skippedUnsupported += result.skippedUnsupported;
1359
+ for (const itemPath of result.unsupportedSamples) {
1360
+ if (this.result.unsupportedSamples.length < 10) {
1361
+ this.result.unsupportedSamples.push(itemPath);
1362
+ }
1363
+ }
821
1364
  this.result.added += result.added;
822
1365
  this.result.updated += result.updated;
823
1366
  this.result.failed += result.failed;
@@ -827,11 +1370,17 @@ class DirectoryTransferAccumulator {
827
1370
  }
828
1371
  }
829
1372
  function buildDirectorySyncResult(result, warnings) {
830
- return buildSyncResult(result.fileCount, result.totalSize, warnings, result.skippedSymlinks, {
1373
+ const syncResult = buildSyncResult(result.fileCount, result.totalSize, warnings, result.skippedSymlinks, {
831
1374
  added: result.added,
832
1375
  updated: result.updated,
1376
+ skipped: result.skippedSymlinks + result.skippedUnsupported,
833
1377
  failed: result.failed,
834
1378
  });
1379
+ return {
1380
+ ...syncResult,
1381
+ skippedUnsupported: result.skippedUnsupported > 0 ? result.skippedUnsupported : undefined,
1382
+ unsupportedSamples: result.unsupportedSamples.length > 0 ? result.unsupportedSamples : undefined,
1383
+ };
835
1384
  }
836
1385
  function buildSyncResult(fileCount, totalSize, warnings, skippedSymlinks = 0, stats) {
837
1386
  if (skippedSymlinks > 0) {
@@ -882,41 +1431,61 @@ function hashLocalFile(filePath) {
882
1431
  });
883
1432
  }
884
1433
  async function hashRemoteFile(alias, remotePath) {
885
- const result = await sessionManager.exec(alias, `sha256sum ${escapeShellArg(remotePath)} | awk '{print $1}'`, {
886
- timeout: 30000,
887
- useLoginUser: true,
888
- maxOutputSize: 4096,
889
- });
890
- if (!result.success) {
891
- return undefined;
1434
+ try {
1435
+ const digest = await hashRemoteFileDigest(alias, remotePath, 'sha256');
1436
+ return { status: 'available', digest, retryable: false };
1437
+ }
1438
+ catch (error) {
1439
+ return {
1440
+ status: 'error',
1441
+ retryable: true,
1442
+ error: error instanceof Error ? error.message : String(error),
1443
+ };
892
1444
  }
893
- const digest = result.stdout.trim().split(/\s+/)[0];
894
- return /^[a-fA-F0-9]{64}$/.test(digest) ? digest : undefined;
895
1445
  }
896
1446
  async function buildSingleFileVerification(alias, localPath, remotePath, direction) {
897
- const localStats = fs.statSync(localPath);
898
- const remoteInfo = await getFileInfo(alias, remotePath);
899
- const [localSha256, remoteSha256] = await Promise.all([hashLocalFile(localPath), hashRemoteFile(alias, remotePath)]);
900
- return {
901
- kind: 'single_file',
902
- direction,
903
- hashAlgorithm: 'sha256',
904
- hashMatch: remoteSha256 ? localSha256 === remoteSha256 : undefined,
905
- local: {
906
- size: localStats.size,
907
- mode: (localStats.mode & 0o777).toString(8),
908
- mtimeMs: Math.round(localStats.mtimeMs),
909
- sha256: localSha256,
910
- },
911
- remote: {
912
- size: remoteInfo.size,
913
- permissions: remoteInfo.permissions,
914
- owner: remoteInfo.owner,
915
- group: remoteInfo.group,
916
- mtime: remoteInfo.mtime,
917
- sha256: remoteSha256,
918
- },
919
- };
1447
+ try {
1448
+ const localStats = fs.statSync(localPath);
1449
+ const [remoteInfo, localSha256, remoteHash] = await Promise.all([
1450
+ getFileInfo(alias, remotePath),
1451
+ hashLocalFile(localPath),
1452
+ hashRemoteFile(alias, remotePath),
1453
+ ]);
1454
+ return {
1455
+ kind: 'single_file',
1456
+ direction,
1457
+ status: remoteHash.status === 'available' ? 'completed' : 'error',
1458
+ failureStage: remoteHash.status === 'error' ? 'remote_hash' : undefined,
1459
+ retryable: remoteHash.retryable,
1460
+ error: remoteHash.error,
1461
+ hashAlgorithm: 'sha256',
1462
+ hashMatch: remoteHash.digest ? localSha256 === remoteHash.digest : undefined,
1463
+ local: {
1464
+ size: localStats.size,
1465
+ mode: (localStats.mode & 0o777).toString(8),
1466
+ mtimeMs: Math.round(localStats.mtimeMs),
1467
+ sha256: localSha256,
1468
+ },
1469
+ remote: {
1470
+ size: remoteInfo.size,
1471
+ permissions: remoteInfo.permissions,
1472
+ owner: remoteInfo.owner,
1473
+ group: remoteInfo.group,
1474
+ mtime: remoteInfo.mtime,
1475
+ sha256: remoteHash.digest,
1476
+ },
1477
+ };
1478
+ }
1479
+ catch (error) {
1480
+ return {
1481
+ kind: 'single_file',
1482
+ direction,
1483
+ status: 'error',
1484
+ failureStage: 'remote_metadata_or_hash',
1485
+ retryable: true,
1486
+ error: error instanceof Error ? error.message : String(error),
1487
+ };
1488
+ }
920
1489
  }
921
1490
  async function resolveSftpUploadFileTarget(alias, localPath, remotePath) {
922
1491
  if (remotePath.endsWith('/')) {
@@ -928,12 +1497,53 @@ async function resolveSftpUploadFileTarget(alias, localPath, remotePath) {
928
1497
  return path.posix.join(remotePath, path.basename(localPath));
929
1498
  }
930
1499
  }
931
- catch {
1500
+ catch (error) {
1501
+ if (!isSftpMissingError(error)) {
1502
+ throw error;
1503
+ }
932
1504
  // Missing remote targets are handled by uploadFile below.
933
1505
  }
934
1506
  return remotePath;
935
1507
  }
1508
+ function resolveSftpDownloadFileTarget(remotePath, localPath) {
1509
+ if (localPath.endsWith(path.sep)) {
1510
+ fs.mkdirSync(localPath, { recursive: true });
1511
+ return path.join(localPath, path.posix.basename(remotePath));
1512
+ }
1513
+ if (fs.existsSync(localPath) && fs.statSync(localPath).isDirectory()) {
1514
+ return path.join(localPath, path.posix.basename(remotePath));
1515
+ }
1516
+ return localPath;
1517
+ }
1518
+ async function syncWithSftpWithTimeout(alias, localPath, remotePath, direction, options) {
1519
+ const timeout = options.operationTimeout ?? 600_000;
1520
+ const controller = new AbortController();
1521
+ const operation = syncWithSftp(alias, localPath, remotePath, direction, {
1522
+ ...options,
1523
+ signal: controller.signal,
1524
+ });
1525
+ let timer;
1526
+ const timedOut = new Promise((resolve) => {
1527
+ timer = setTimeout(() => {
1528
+ controller.abort(new SftpOperationTimeoutError(timeout));
1529
+ resolve({
1530
+ success: false,
1531
+ method: 'sftp',
1532
+ operationStatus: 'unknown',
1533
+ timedOut: true,
1534
+ retryable: true,
1535
+ output: `SFTP operation timed out after ${timeout}ms; remote transfer state is unknown`,
1536
+ });
1537
+ }, timeout);
1538
+ });
1539
+ const result = await Promise.race([operation, timedOut]);
1540
+ if (timer) {
1541
+ clearTimeout(timer);
1542
+ }
1543
+ return result;
1544
+ }
936
1545
  async function syncWithSftp(alias, localPath, remotePath, direction, options) {
1546
+ throwIfAborted(options.signal);
937
1547
  const warnings = [];
938
1548
  if (options.delete) {
939
1549
  warnings.push('delete option is not supported in SFTP mode (requires rsync)');
@@ -941,7 +1551,7 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
941
1551
  if (options.dryRun) {
942
1552
  const localExists = fs.existsSync(localPath);
943
1553
  const localStats = localExists ? fs.lstatSync(localPath) : undefined;
944
- const remoteTargetExists = await remoteExists(alias, remotePath).catch(() => false);
1554
+ const remoteTargetExists = await remoteExists(alias, remotePath);
945
1555
  return {
946
1556
  success: true,
947
1557
  method: 'sftp',
@@ -982,13 +1592,16 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
982
1592
  output: 'Refusing to upload symlink at top level (set followSymlinks=true to traverse)',
983
1593
  };
984
1594
  }
985
- if (stats.isDirectory() && options.recursive !== false) {
986
- const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0);
1595
+ if (stats.isDirectory() && options.recursive === false) {
1596
+ throw new Error('recursive=false is not supported for directory sources; use a file source or enable recursion');
1597
+ }
1598
+ if (stats.isDirectory()) {
1599
+ const result = await uploadDirectory(alias, localPath, remotePath, options.exclude, undefined, options.followSymlinks ?? false, { files: 0, directories: 0, bytes: 0 }, 0, options.signal);
987
1600
  return buildDirectorySyncResult(result, warnings);
988
1601
  }
989
1602
  const targetRemotePath = await resolveSftpUploadFileTarget(alias, localPath, remotePath);
990
1603
  const existed = await remoteExists(alias, targetRemotePath);
991
- const { size } = await uploadFile(alias, localPath, targetRemotePath);
1604
+ const { size } = await uploadFile(alias, localPath, targetRemotePath, undefined, undefined, undefined, options.signal, options.followSymlinks === true);
992
1605
  return {
993
1606
  ...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
994
1607
  verification: await buildSingleFileVerification(alias, localPath, targetRemotePath, 'upload'),
@@ -996,25 +1609,41 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
996
1609
  }
997
1610
  // download
998
1611
  const info = await getFileInfo(alias, remotePath);
999
- if (info.isDirectory && options.recursive !== false) {
1612
+ if (info.isDirectory && options.recursive === false) {
1613
+ throw new Error('recursive=false is not supported for directory sources; use a file source or enable recursion');
1614
+ }
1615
+ if (info.isDirectory) {
1000
1616
  const result = await downloadDirectory(alias, remotePath, localPath, options.exclude, undefined, {
1001
1617
  files: 0,
1002
1618
  directories: 0,
1003
1619
  bytes: 0,
1004
- }, 0);
1620
+ }, 0, options.signal);
1005
1621
  return buildDirectorySyncResult(result, warnings);
1006
1622
  }
1007
- const existed = fs.existsSync(localPath);
1008
- const { size } = await downloadFile(alias, remotePath, localPath);
1623
+ const targetLocalPath = resolveSftpDownloadFileTarget(remotePath, localPath);
1624
+ const existed = fs.existsSync(targetLocalPath);
1625
+ const { size } = await downloadFile(alias, remotePath, targetLocalPath, undefined, undefined, options.signal);
1009
1626
  return {
1010
1627
  ...buildSyncResult(1, size, warnings, 0, { added: existed ? 0 : 1, updated: existed ? 1 : 0 }),
1011
- verification: await buildSingleFileVerification(alias, localPath, remotePath, 'download'),
1628
+ verification: await buildSingleFileVerification(alias, targetLocalPath, remotePath, 'download'),
1012
1629
  };
1013
1630
  }
1014
1631
  catch (err) {
1632
+ if (err instanceof SftpOperationTimeoutError) {
1633
+ return {
1634
+ success: false,
1635
+ method: 'sftp',
1636
+ operationStatus: 'unknown',
1637
+ timedOut: true,
1638
+ retryable: true,
1639
+ stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
1640
+ output: err.message,
1641
+ };
1642
+ }
1015
1643
  return {
1016
1644
  success: false,
1017
1645
  method: 'sftp',
1646
+ operationStatus: 'failed',
1018
1647
  stats: { added: 0, updated: 0, deleted: 0, skipped: 0, failed: 1 },
1019
1648
  output: err instanceof Error ? err.message : String(err),
1020
1649
  };
@@ -1023,19 +1652,33 @@ async function syncWithSftp(alias, localPath, remotePath, direction, options) {
1023
1652
  /**
1024
1653
  * 递归上传目录
1025
1654
  */
1026
- async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0) {
1655
+ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp, followSymlinks = false, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '') {
1656
+ throwIfAborted(signal);
1027
1657
  const accumulator = new DirectoryTransferAccumulator();
1028
1658
  recordSftpDirectory(traversal, depth, localPath);
1029
1659
  const ownSftp = !sharedSftp;
1030
1660
  const sftp = sharedSftp ?? (await sessionManager.getSftp(alias));
1661
+ const activeDirectories = (traversal.activeDirectories ??= new Set());
1662
+ const realPath = fs.realpathSync(localPath);
1663
+ if (activeDirectories.has(realPath)) {
1664
+ accumulator.addSkippedSymlink();
1665
+ if (ownSftp) {
1666
+ sftp.end();
1667
+ }
1668
+ return accumulator.toResult();
1669
+ }
1670
+ activeDirectories.add(realPath);
1031
1671
  try {
1032
- await mkdir(alias, remotePath, true);
1672
+ validateLocalPathAgainstAllowList(localPath);
1673
+ await mkdir(alias, remotePath, true, sftp);
1674
+ throwIfAborted(signal);
1033
1675
  const items = fs.readdirSync(localPath);
1034
1676
  // 先分类:目录串行(递归会自己并发文件);文件并发上传,避免单 SSH session 上 channel 过多
1035
1677
  const subDirItems = [];
1036
1678
  const fileItems = [];
1037
1679
  for (const item of items) {
1038
- if (exclude && exclude.some((pattern) => matchPattern(item, pattern))) {
1680
+ const itemRelativePath = relativeBase ? `${relativeBase}/${item}` : item;
1681
+ if (matchesDirectoryExclude(itemRelativePath, exclude)) {
1039
1682
  continue;
1040
1683
  }
1041
1684
  const itemLocalPath = path.join(localPath, item);
@@ -1052,6 +1695,9 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
1052
1695
  recordSftpFile(traversal, stats.size, depth, itemLocalPath);
1053
1696
  fileItems.push({ name: item, stats });
1054
1697
  }
1698
+ else {
1699
+ accumulator.addSkippedUnsupported(itemRelativePath);
1700
+ }
1055
1701
  }
1056
1702
  // 文件并发上传
1057
1703
  await runWithConcurrency(fileItems, SFTP_PARALLEL_LIMIT, async ({ name, stats }) => {
@@ -1059,7 +1705,7 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
1059
1705
  const itemRemotePath = path.posix.join(remotePath, name);
1060
1706
  const existed = await sftpExists(sftp, itemRemotePath);
1061
1707
  try {
1062
- await uploadFile(alias, itemLocalPath, itemRemotePath, undefined, sftp);
1708
+ await uploadFile(alias, itemLocalPath, itemRemotePath, undefined, sftp, undefined, signal, followSymlinks);
1063
1709
  accumulator.addFile(stats.size, existed);
1064
1710
  }
1065
1711
  catch (error) {
@@ -1071,11 +1717,12 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
1071
1717
  for (const item of subDirItems) {
1072
1718
  const itemLocalPath = path.join(localPath, item);
1073
1719
  const itemRemotePath = path.posix.join(remotePath, item);
1074
- const result = await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1);
1720
+ const result = await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude, sftp, followSymlinks, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item}` : item);
1075
1721
  accumulator.merge(result);
1076
1722
  }
1077
1723
  }
1078
1724
  finally {
1725
+ activeDirectories.delete(realPath);
1079
1726
  if (ownSftp) {
1080
1727
  sftp.end();
1081
1728
  }
@@ -1085,7 +1732,8 @@ async function uploadDirectory(alias, localPath, remotePath, exclude, sharedSftp
1085
1732
  /**
1086
1733
  * 递归下载目录
1087
1734
  */
1088
- async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0) {
1735
+ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSftp, traversal = { files: 0, directories: 0, bytes: 0 }, depth = 0, signal, relativeBase = '') {
1736
+ throwIfAborted(signal);
1089
1737
  const accumulator = new DirectoryTransferAccumulator();
1090
1738
  recordSftpDirectory(traversal, depth, remotePath);
1091
1739
  const ownSftp = !sharedSftp;
@@ -1095,12 +1743,14 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
1095
1743
  fs.mkdirSync(localPath, { recursive: true });
1096
1744
  }
1097
1745
  const items = await listDir(alias, remotePath, true, sftp);
1746
+ throwIfAborted(signal);
1098
1747
  // 分类:文件并发下载,目录串行递归
1099
1748
  const fileItems = [];
1100
1749
  const dirItems = [];
1101
1750
  const symlinkItems = [];
1102
1751
  for (const item of items) {
1103
- if (exclude && exclude.some((pattern) => matchPattern(item.name, pattern))) {
1752
+ const itemRelativePath = relativeBase ? `${relativeBase}/${item.name}` : item.name;
1753
+ if (matchesDirectoryExclude(itemRelativePath, exclude)) {
1104
1754
  continue;
1105
1755
  }
1106
1756
  if (item.isDirectory) {
@@ -1114,14 +1764,14 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
1114
1764
  symlinkItems.push(item);
1115
1765
  }
1116
1766
  else {
1117
- console.warn(`[ssh_sync] skipping unknown item type: ${item.path}`);
1767
+ accumulator.addSkippedUnsupported(itemRelativePath);
1118
1768
  }
1119
1769
  }
1120
1770
  await runWithConcurrency(fileItems, SFTP_PARALLEL_LIMIT, async (item) => {
1121
1771
  const itemLocalPath = path.join(localPath, item.name);
1122
1772
  const existed = fs.existsSync(itemLocalPath);
1123
1773
  try {
1124
- await downloadFile(alias, item.path, itemLocalPath, undefined, sftp);
1774
+ await downloadFile(alias, item.path, itemLocalPath, undefined, sftp, signal);
1125
1775
  accumulator.addFile(item.size, existed);
1126
1776
  }
1127
1777
  catch (error) {
@@ -1131,7 +1781,7 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
1131
1781
  });
1132
1782
  for (const item of dirItems) {
1133
1783
  const itemLocalPath = path.join(localPath, item.name);
1134
- const result = await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1);
1784
+ const result = await downloadDirectory(alias, item.path, itemLocalPath, exclude, sftp, traversal, depth + 1, signal, relativeBase ? `${relativeBase}/${item.name}` : item.name);
1135
1785
  accumulator.merge(result);
1136
1786
  }
1137
1787
  for (const item of symlinkItems) {
@@ -1146,22 +1796,6 @@ async function downloadDirectory(alias, remotePath, localPath, exclude, sharedSf
1146
1796
  }
1147
1797
  return accumulator.toResult();
1148
1798
  }
1149
- /**
1150
- * 简单的模式匹配(支持 * 和 ?)
1151
- */
1152
- const matchPatternCache = new Map();
1153
- function matchPattern(name, pattern) {
1154
- let regex = matchPatternCache.get(pattern);
1155
- if (!regex) {
1156
- const regexPattern = pattern
1157
- .replace(/[.+^${}()|[\]\\]/g, '\\$&') // 转义特殊字符
1158
- .replace(/\*/g, '.*')
1159
- .replace(/\?/g, '.');
1160
- regex = new RegExp(`^${regexPattern}$`);
1161
- matchPatternCache.set(pattern, regex);
1162
- }
1163
- return regex.test(name);
1164
- }
1165
1799
  /**
1166
1800
  * 格式化权限字符串
1167
1801
  */