@pyrokine/mcp-ssh 1.1.0 → 1.1.3
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 +81 -72
- package/README_zh.md +81 -73
- package/dist/file-ops.d.ts +7 -10
- package/dist/file-ops.js +94 -176
- package/dist/index.js +8 -938
- package/dist/session-manager.d.ts +72 -67
- package/dist/session-manager.js +228 -240
- package/dist/ssh-config.d.ts +0 -4
- package/dist/ssh-config.js +3 -10
- package/dist/tools/connection.d.ts +7 -0
- package/dist/tools/connection.js +215 -0
- package/dist/tools/exec.d.ts +8 -0
- package/dist/tools/exec.js +221 -0
- package/dist/tools/file.d.ts +8 -0
- package/dist/tools/file.js +192 -0
- package/dist/tools/forward.d.ts +7 -0
- package/dist/tools/forward.js +116 -0
- package/dist/tools/index.d.ts +8 -0
- package/dist/tools/index.js +8 -0
- package/dist/tools/pty.d.ts +7 -0
- package/dist/tools/pty.js +178 -0
- package/dist/tools/utils.d.ts +22 -0
- package/dist/tools/utils.js +21 -0
- package/dist/types.d.ts +0 -7
- package/package.json +3 -2
- package/src/file-ops.ts +526 -596
- package/src/index.ts +25 -1084
- package/src/session-manager.ts +938 -945
- package/src/ssh-config.ts +183 -190
- package/src/tools/connection.ts +240 -0
- package/src/tools/exec.ts +260 -0
- package/src/tools/file.ts +219 -0
- package/src/tools/forward.ts +141 -0
- package/src/tools/index.ts +9 -0
- package/src/tools/pty.ts +203 -0
- package/src/tools/utils.ts +23 -0
- package/src/types.ts +84 -93
- package/tsconfig.json +7 -2
package/src/file-ops.ts
CHANGED
|
@@ -2,374 +2,329 @@
|
|
|
2
2
|
* SSH File Operations - 文件操作
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import
|
|
6
|
-
import * as
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
5
|
+
import {execSync} from 'child_process'
|
|
6
|
+
import * as fs from 'fs'
|
|
7
|
+
import * as path from 'path'
|
|
8
|
+
import {SFTPWrapper, Stats} from 'ssh2'
|
|
9
|
+
import {sessionManager} from './session-manager.js'
|
|
10
|
+
import {FileInfo, TransferProgress} from './types.js'
|
|
11
|
+
|
|
12
|
+
// 文件类型 mode 常量
|
|
13
|
+
const S_IFDIR = 0o40000
|
|
14
|
+
const S_IFREG = 0o100000
|
|
15
|
+
const S_IFLNK = 0o120000
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* sftp.stat 的 Promise 包装
|
|
19
|
+
*/
|
|
20
|
+
function sftpStat(sftp: SFTPWrapper, remotePath: string): Promise<Stats> {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
sftp.stat(remotePath, (err, stats) => {
|
|
23
|
+
if (err) {
|
|
24
|
+
reject(err)
|
|
25
|
+
} else {
|
|
26
|
+
resolve(stats)
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
})
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 带进度追踪的流传输
|
|
34
|
+
*/
|
|
35
|
+
function pipeWithProgress(
|
|
36
|
+
readStream: NodeJS.ReadableStream,
|
|
37
|
+
writeStream: NodeJS.WritableStream,
|
|
38
|
+
sftp: SFTPWrapper,
|
|
39
|
+
totalSize: number,
|
|
40
|
+
onProgress?: (progress: TransferProgress) => void,
|
|
41
|
+
): Promise<{ success: boolean; size: number }> {
|
|
42
|
+
return new Promise((resolve, reject) => {
|
|
43
|
+
let settled = false
|
|
44
|
+
|
|
45
|
+
const cleanup = (err?: Error) => {
|
|
46
|
+
if (settled) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
settled = true
|
|
50
|
+
sftp.end()
|
|
51
|
+
if (err) {
|
|
52
|
+
reject(err)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let transferred = 0
|
|
57
|
+
|
|
58
|
+
readStream.on('data', (chunk: Buffer) => {
|
|
59
|
+
transferred += chunk.length
|
|
60
|
+
onProgress?.({
|
|
61
|
+
transferred,
|
|
62
|
+
total: totalSize,
|
|
63
|
+
percent: totalSize > 0 ? Math.round((transferred / totalSize) * 100) : 100,
|
|
64
|
+
})
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
readStream.on('error', (err: Error) => cleanup(err))
|
|
68
|
+
writeStream.on('error', (err: Error) => cleanup(err))
|
|
69
|
+
|
|
70
|
+
writeStream.on('close', () => {
|
|
71
|
+
if (!settled) {
|
|
72
|
+
settled = true
|
|
73
|
+
sftp.end()
|
|
74
|
+
resolve({success: true, size: totalSize})
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
readStream.pipe(writeStream)
|
|
79
|
+
})
|
|
80
|
+
}
|
|
11
81
|
|
|
12
82
|
/**
|
|
13
83
|
* 上传文件
|
|
14
84
|
*/
|
|
15
85
|
export async function uploadFile(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
86
|
+
alias: string,
|
|
87
|
+
localPath: string,
|
|
88
|
+
remotePath: string,
|
|
89
|
+
onProgress?: (progress: TransferProgress) => void,
|
|
20
90
|
): Promise<{ success: boolean; size: number }> {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const cleanup = (err?: Error) => {
|
|
35
|
-
if (settled) return;
|
|
36
|
-
settled = true;
|
|
37
|
-
sftp.end();
|
|
38
|
-
if (err) reject(err);
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
let transferred = 0;
|
|
42
|
-
|
|
43
|
-
readStream.on('data', (chunk: Buffer) => {
|
|
44
|
-
transferred += chunk.length;
|
|
45
|
-
if (onProgress) {
|
|
46
|
-
onProgress({
|
|
47
|
-
transferred,
|
|
48
|
-
total: totalSize,
|
|
49
|
-
percent: totalSize > 0 ? Math.round((transferred / totalSize) * 100) : 100,
|
|
50
|
-
});
|
|
51
|
-
}
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
readStream.on('error', (err: Error) => cleanup(err));
|
|
55
|
-
writeStream.on('error', (err: Error) => cleanup(err));
|
|
56
|
-
|
|
57
|
-
writeStream.on('close', () => {
|
|
58
|
-
if (!settled) {
|
|
59
|
-
settled = true;
|
|
60
|
-
sftp.end();
|
|
61
|
-
resolve({ success: true, size: totalSize });
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
readStream.pipe(writeStream);
|
|
66
|
-
});
|
|
91
|
+
if (!fs.existsSync(localPath)) {
|
|
92
|
+
throw new Error(`Local file not found: ${localPath}`)
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
96
|
+
const totalSize = fs.statSync(localPath).size
|
|
97
|
+
|
|
98
|
+
return pipeWithProgress(
|
|
99
|
+
fs.createReadStream(localPath),
|
|
100
|
+
sftp.createWriteStream(remotePath),
|
|
101
|
+
sftp, totalSize, onProgress,
|
|
102
|
+
)
|
|
67
103
|
}
|
|
68
104
|
|
|
69
105
|
/**
|
|
70
106
|
* 下载文件
|
|
71
107
|
*/
|
|
72
108
|
export async function downloadFile(
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
109
|
+
alias: string,
|
|
110
|
+
remotePath: string,
|
|
111
|
+
localPath: string,
|
|
112
|
+
onProgress?: (progress: TransferProgress) => void,
|
|
77
113
|
): Promise<{ success: boolean; size: number }> {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
fs.mkdirSync(localDir, { recursive: true });
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
return new Promise((resolve, reject) => {
|
|
96
|
-
const readStream = sftp.createReadStream(remotePath);
|
|
97
|
-
const writeStream = fs.createWriteStream(localPath);
|
|
98
|
-
let settled = false;
|
|
99
|
-
|
|
100
|
-
const cleanup = (err?: Error) => {
|
|
101
|
-
if (settled) return;
|
|
102
|
-
settled = true;
|
|
103
|
-
sftp.end();
|
|
104
|
-
if (err) reject(err);
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
let transferred = 0;
|
|
108
|
-
|
|
109
|
-
readStream.on('data', (chunk: Buffer) => {
|
|
110
|
-
transferred += chunk.length;
|
|
111
|
-
if (onProgress) {
|
|
112
|
-
onProgress({
|
|
113
|
-
transferred,
|
|
114
|
-
total: totalSize,
|
|
115
|
-
percent: totalSize > 0 ? Math.round((transferred / totalSize) * 100) : 100,
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
readStream.on('error', (err: Error) => cleanup(err));
|
|
121
|
-
writeStream.on('error', (err: Error) => cleanup(err));
|
|
122
|
-
|
|
123
|
-
writeStream.on('close', () => {
|
|
124
|
-
if (!settled) {
|
|
125
|
-
settled = true;
|
|
126
|
-
sftp.end();
|
|
127
|
-
resolve({ success: true, size: totalSize });
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
readStream.pipe(writeStream);
|
|
132
|
-
});
|
|
114
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
115
|
+
const totalSize = (await sftpStat(sftp, remotePath)).size
|
|
116
|
+
|
|
117
|
+
// 确保本地目录存在
|
|
118
|
+
const localDir = path.dirname(localPath)
|
|
119
|
+
if (!fs.existsSync(localDir)) {
|
|
120
|
+
fs.mkdirSync(localDir, {recursive: true})
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return pipeWithProgress(
|
|
124
|
+
sftp.createReadStream(remotePath),
|
|
125
|
+
fs.createWriteStream(localPath),
|
|
126
|
+
sftp, totalSize, onProgress,
|
|
127
|
+
)
|
|
133
128
|
}
|
|
134
129
|
|
|
135
130
|
/**
|
|
136
131
|
* 读取远程文件内容
|
|
137
132
|
*/
|
|
138
133
|
export async function readFile(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
134
|
+
alias: string,
|
|
135
|
+
remotePath: string,
|
|
136
|
+
maxBytes: number = 1024 * 1024, // 默认最大 1MB
|
|
142
137
|
): Promise<{ content: string; size: number; truncated: boolean }> {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
truncated,
|
|
183
|
-
});
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
readStream.on('error', (err: Error) => {
|
|
187
|
-
sftp.end();
|
|
188
|
-
reject(err);
|
|
189
|
-
});
|
|
190
|
-
});
|
|
138
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
139
|
+
const actualSize = (await sftpStat(sftp, remotePath)).size
|
|
140
|
+
const truncated = actualSize > maxBytes
|
|
141
|
+
|
|
142
|
+
// 处理空文件
|
|
143
|
+
if (actualSize === 0) {
|
|
144
|
+
sftp.end()
|
|
145
|
+
return {content: '', size: 0, truncated: false}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const readSize = Math.min(actualSize, maxBytes)
|
|
149
|
+
|
|
150
|
+
return new Promise((resolve, reject) => {
|
|
151
|
+
const chunks: Buffer[] = []
|
|
152
|
+
|
|
153
|
+
const readStream = sftp.createReadStream(remotePath, {
|
|
154
|
+
start: 0,
|
|
155
|
+
end: readSize - 1,
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
readStream.on('data', (chunk: Buffer) => {
|
|
159
|
+
chunks.push(chunk)
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
readStream.on('end', () => {
|
|
163
|
+
sftp.end()
|
|
164
|
+
const content = Buffer.concat(chunks).toString('utf-8')
|
|
165
|
+
resolve({
|
|
166
|
+
content,
|
|
167
|
+
size: actualSize,
|
|
168
|
+
truncated,
|
|
169
|
+
})
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
readStream.on('error', (err: Error) => {
|
|
173
|
+
sftp.end()
|
|
174
|
+
reject(err)
|
|
175
|
+
})
|
|
176
|
+
})
|
|
191
177
|
}
|
|
192
178
|
|
|
193
179
|
/**
|
|
194
180
|
* 写入远程文件
|
|
195
181
|
*/
|
|
196
182
|
export async function writeFile(
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
183
|
+
alias: string,
|
|
184
|
+
remotePath: string,
|
|
185
|
+
content: string,
|
|
186
|
+
append: boolean = false,
|
|
201
187
|
): Promise<{ success: boolean; size: number }> {
|
|
202
|
-
|
|
203
|
-
|
|
188
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
189
|
+
const flags = append ? 'a' : 'w'
|
|
204
190
|
|
|
205
|
-
|
|
206
|
-
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
const writeStream = sftp.createWriteStream(remotePath, {flags})
|
|
207
193
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
194
|
+
writeStream.on('close', () => {
|
|
195
|
+
sftp.end()
|
|
196
|
+
resolve({success: true, size: content.length})
|
|
197
|
+
})
|
|
212
198
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
199
|
+
writeStream.on('error', (err: Error) => {
|
|
200
|
+
sftp.end()
|
|
201
|
+
reject(err)
|
|
202
|
+
})
|
|
217
203
|
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
204
|
+
writeStream.write(content)
|
|
205
|
+
writeStream.end()
|
|
206
|
+
})
|
|
221
207
|
}
|
|
222
208
|
|
|
223
209
|
/**
|
|
224
210
|
* 列出目录内容
|
|
225
211
|
*/
|
|
226
212
|
export async function listDir(
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
213
|
+
alias: string,
|
|
214
|
+
remotePath: string,
|
|
215
|
+
showHidden: boolean = false,
|
|
230
216
|
): Promise<FileInfo[]> {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
217
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
218
|
+
|
|
219
|
+
return new Promise((resolve, reject) => {
|
|
220
|
+
sftp.readdir(remotePath, (err, list) => {
|
|
221
|
+
if (err) {
|
|
222
|
+
sftp.end()
|
|
223
|
+
reject(err)
|
|
224
|
+
return
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const files: FileInfo[] = list
|
|
228
|
+
.filter((item) => showHidden || !item.filename.startsWith('.'))
|
|
229
|
+
.map((item) => ({
|
|
230
|
+
name: item.filename,
|
|
231
|
+
path: path.posix.join(remotePath, item.filename),
|
|
232
|
+
size: item.attrs.size,
|
|
233
|
+
isDirectory: (item.attrs.mode & S_IFDIR) !== 0,
|
|
234
|
+
isFile: (item.attrs.mode & S_IFREG) !== 0,
|
|
235
|
+
isSymlink: (item.attrs.mode & S_IFLNK) !== 0,
|
|
236
|
+
permissions: formatPermissions(item.attrs.mode),
|
|
237
|
+
owner: item.attrs.uid,
|
|
238
|
+
group: item.attrs.gid,
|
|
239
|
+
mtime: new Date(item.attrs.mtime * 1000),
|
|
240
|
+
atime: new Date(item.attrs.atime * 1000),
|
|
241
|
+
}))
|
|
242
|
+
.sort((a, b) => {
|
|
243
|
+
// 目录在前
|
|
244
|
+
if (a.isDirectory !== b.isDirectory) {
|
|
245
|
+
return a.isDirectory ? -1 : 1
|
|
246
|
+
}
|
|
247
|
+
return a.name.localeCompare(b.name)
|
|
248
|
+
})
|
|
249
|
+
|
|
250
|
+
sftp.end()
|
|
251
|
+
resolve(files)
|
|
252
|
+
})
|
|
253
|
+
})
|
|
268
254
|
}
|
|
269
255
|
|
|
270
256
|
/**
|
|
271
257
|
* 获取文件信息
|
|
272
258
|
*/
|
|
273
259
|
export async function getFileInfo(
|
|
274
|
-
|
|
275
|
-
|
|
260
|
+
alias: string,
|
|
261
|
+
remotePath: string,
|
|
276
262
|
): Promise<FileInfo> {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
/**
|
|
306
|
-
* 检查文件是否存在
|
|
307
|
-
*/
|
|
308
|
-
export async function fileExists(
|
|
309
|
-
alias: string,
|
|
310
|
-
remotePath: string
|
|
311
|
-
): Promise<boolean> {
|
|
312
|
-
const sftp = await sessionManager.getSftp(alias);
|
|
313
|
-
|
|
314
|
-
return new Promise((resolve) => {
|
|
315
|
-
sftp.stat(remotePath, (err) => {
|
|
316
|
-
sftp.end();
|
|
317
|
-
resolve(!err);
|
|
318
|
-
});
|
|
319
|
-
});
|
|
263
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
264
|
+
|
|
265
|
+
return new Promise((resolve, reject) => {
|
|
266
|
+
sftp.stat(remotePath, (err, stats) => {
|
|
267
|
+
sftp.end()
|
|
268
|
+
|
|
269
|
+
if (err) {
|
|
270
|
+
reject(err)
|
|
271
|
+
return
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
resolve({
|
|
275
|
+
name: path.posix.basename(remotePath),
|
|
276
|
+
path: remotePath,
|
|
277
|
+
size: stats.size,
|
|
278
|
+
isDirectory: (stats.mode & S_IFDIR) !== 0,
|
|
279
|
+
isFile: (stats.mode & S_IFREG) !== 0,
|
|
280
|
+
isSymlink: (stats.mode & S_IFLNK) !== 0,
|
|
281
|
+
permissions: formatPermissions(stats.mode),
|
|
282
|
+
owner: stats.uid,
|
|
283
|
+
group: stats.gid,
|
|
284
|
+
mtime: new Date(stats.mtime * 1000),
|
|
285
|
+
atime: new Date(stats.atime * 1000),
|
|
286
|
+
})
|
|
287
|
+
})
|
|
288
|
+
})
|
|
320
289
|
}
|
|
321
290
|
|
|
322
291
|
/**
|
|
323
292
|
* 创建目录
|
|
324
293
|
*/
|
|
325
294
|
export async function mkdir(
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
295
|
+
alias: string,
|
|
296
|
+
remotePath: string,
|
|
297
|
+
recursive: boolean = false,
|
|
329
298
|
): Promise<boolean> {
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
const sftp = await sessionManager.getSftp(alias);
|
|
337
|
-
return new Promise((resolve, reject) => {
|
|
338
|
-
sftp.mkdir(remotePath, (err) => {
|
|
339
|
-
sftp.end();
|
|
340
|
-
if (err) reject(err);
|
|
341
|
-
else resolve(true);
|
|
342
|
-
});
|
|
343
|
-
});
|
|
344
|
-
}
|
|
299
|
+
if (recursive) {
|
|
300
|
+
// 通过 exec 实现递归创建
|
|
301
|
+
const result = await sessionManager.exec(alias, `mkdir -p "${remotePath}"`)
|
|
302
|
+
return result.exitCode === 0
|
|
303
|
+
}
|
|
345
304
|
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
if (err) reject(err);
|
|
358
|
-
else resolve(true);
|
|
359
|
-
});
|
|
360
|
-
});
|
|
305
|
+
const sftp = await sessionManager.getSftp(alias)
|
|
306
|
+
return new Promise((resolve, reject) => {
|
|
307
|
+
sftp.mkdir(remotePath, (err) => {
|
|
308
|
+
sftp.end()
|
|
309
|
+
if (err) {
|
|
310
|
+
reject(err)
|
|
311
|
+
} else {
|
|
312
|
+
resolve(true)
|
|
313
|
+
}
|
|
314
|
+
})
|
|
315
|
+
})
|
|
361
316
|
}
|
|
362
317
|
|
|
363
318
|
/**
|
|
364
319
|
* 检查远程是否安装 rsync
|
|
365
320
|
*/
|
|
366
321
|
export async function checkRsync(alias: string): Promise<boolean> {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
322
|
+
try {
|
|
323
|
+
const result = await sessionManager.exec(alias, 'which rsync')
|
|
324
|
+
return result.exitCode === 0 && result.stdout.trim().length > 0
|
|
325
|
+
} catch {
|
|
326
|
+
return false
|
|
327
|
+
}
|
|
373
328
|
}
|
|
374
329
|
|
|
375
330
|
/**
|
|
@@ -382,40 +337,30 @@ export async function checkRsync(alias: string): Promise<boolean> {
|
|
|
382
337
|
* @param options 同步选项
|
|
383
338
|
*/
|
|
384
339
|
export async function syncFiles(
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
): Promise<{
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
// 检查远程 rsync
|
|
403
|
-
const hasRsync = await checkRsync(alias);
|
|
404
|
-
|
|
405
|
-
if (hasRsync) {
|
|
406
|
-
// 使用 rsync(通过远程端执行)
|
|
407
|
-
return syncWithRsync(alias, localPath, remotePath, direction, options);
|
|
408
|
-
} else {
|
|
409
|
-
// 回退到 SFTP
|
|
410
|
-
return syncWithSftp(alias, localPath, remotePath, direction, options);
|
|
411
|
-
}
|
|
340
|
+
alias: string,
|
|
341
|
+
localPath: string,
|
|
342
|
+
remotePath: string,
|
|
343
|
+
direction: 'upload' | 'download',
|
|
344
|
+
options: {
|
|
345
|
+
delete?: boolean; // 删除目标端多余文件
|
|
346
|
+
dryRun?: boolean; // 仅显示将执行的操作
|
|
347
|
+
exclude?: string[]; // 排除模式
|
|
348
|
+
recursive?: boolean; // 递归同步目录
|
|
349
|
+
} = {},
|
|
350
|
+
): Promise<SyncResult> {
|
|
351
|
+
const hasRsync = await checkRsync(alias)
|
|
352
|
+
|
|
353
|
+
if (hasRsync) {
|
|
354
|
+
return syncWithRsync(alias, localPath, remotePath, direction, options)
|
|
355
|
+
}
|
|
356
|
+
return syncWithSftp(alias, localPath, remotePath, direction, options)
|
|
412
357
|
}
|
|
413
358
|
|
|
414
359
|
/**
|
|
415
360
|
* 转义 shell 路径参数
|
|
416
361
|
*/
|
|
417
362
|
function escapeShellPath(p: string): string {
|
|
418
|
-
|
|
363
|
+
return `'${p.replace(/'/g, '\'\\\'\'')}'`
|
|
419
364
|
}
|
|
420
365
|
|
|
421
366
|
/**
|
|
@@ -423,308 +368,293 @@ function escapeShellPath(p: string): string {
|
|
|
423
368
|
* 通过本地执行 rsync 连接到远程(需要密钥认证或 ssh-agent)
|
|
424
369
|
*/
|
|
425
370
|
async function syncWithRsync(
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
): Promise<{
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
}
|
|
443
|
-
// 检查本地是否有 rsync
|
|
444
|
-
let hasLocalRsync = false;
|
|
445
|
-
try {
|
|
446
|
-
execSync('which rsync', { stdio: 'pipe' });
|
|
447
|
-
hasLocalRsync = true;
|
|
448
|
-
} catch {}
|
|
449
|
-
|
|
450
|
-
if (!hasLocalRsync) {
|
|
451
|
-
// 本地没有 rsync,回退到 SFTP
|
|
452
|
-
return syncWithSftp(alias, localPath, remotePath, direction, options);
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
// 获取会话信息以构建 rsync 命令
|
|
456
|
-
const sessions = sessionManager.listSessions();
|
|
457
|
-
const sessionInfo = sessions.find(s => s.alias === alias);
|
|
458
|
-
if (!sessionInfo) {
|
|
459
|
-
throw new Error(`Session '${alias}' not found`);
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
// 构建 rsync 参数
|
|
463
|
-
const args: string[] = ['-avz', '--progress'];
|
|
464
|
-
|
|
465
|
-
if (options.delete) {
|
|
466
|
-
args.push('--delete');
|
|
467
|
-
}
|
|
468
|
-
if (options.dryRun) {
|
|
469
|
-
args.push('--dry-run');
|
|
470
|
-
}
|
|
471
|
-
if (options.recursive === false) {
|
|
472
|
-
args.push('--dirs'); // 不递归,只传输目录本身
|
|
473
|
-
}
|
|
474
|
-
if (options.exclude) {
|
|
475
|
-
for (const pattern of options.exclude) {
|
|
476
|
-
args.push(`--exclude=${escapeShellPath(pattern)}`);
|
|
371
|
+
alias: string,
|
|
372
|
+
localPath: string,
|
|
373
|
+
remotePath: string,
|
|
374
|
+
direction: 'upload' | 'download',
|
|
375
|
+
options: {
|
|
376
|
+
delete?: boolean;
|
|
377
|
+
dryRun?: boolean;
|
|
378
|
+
exclude?: string[];
|
|
379
|
+
recursive?: boolean;
|
|
380
|
+
},
|
|
381
|
+
): Promise<SyncResult> {
|
|
382
|
+
// 检查本地是否有 rsync
|
|
383
|
+
let hasLocalRsync = false
|
|
384
|
+
try {
|
|
385
|
+
execSync('which rsync', {stdio: 'pipe'})
|
|
386
|
+
hasLocalRsync = true
|
|
387
|
+
} catch {
|
|
477
388
|
}
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
const sshCmd = `ssh -p ${sessionInfo.port} -o StrictHostKeyChecking=no -o BatchMode=yes`;
|
|
483
|
-
const remoteSpec = `${sessionInfo.username}@${sessionInfo.host}:${escapeShellPath(remotePath)}`;
|
|
484
|
-
const rsyncCmd = direction === 'upload'
|
|
485
|
-
? `rsync ${args.join(' ')} -e "${sshCmd}" ${escapeShellPath(localPath)} ${remoteSpec}`
|
|
486
|
-
: `rsync ${args.join(' ')} -e "${sshCmd}" ${remoteSpec} ${escapeShellPath(localPath)}`;
|
|
487
|
-
|
|
488
|
-
try {
|
|
489
|
-
const result = execSync(rsyncCmd, {
|
|
490
|
-
encoding: 'utf-8',
|
|
491
|
-
timeout: 600000, // 10 分钟超时
|
|
492
|
-
stdio: ['pipe', 'pipe', 'pipe']
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
// 解析 rsync 输出统计文件数
|
|
496
|
-
const lines = result.split('\n');
|
|
497
|
-
let filesTransferred = 0;
|
|
498
|
-
for (const line of lines) {
|
|
499
|
-
if (line.trim() && !line.startsWith('sending') && !line.startsWith('receiving') && !line.startsWith('total')) {
|
|
500
|
-
filesTransferred++;
|
|
501
|
-
}
|
|
389
|
+
|
|
390
|
+
if (!hasLocalRsync) {
|
|
391
|
+
// 本地没有 rsync,回退到 SFTP
|
|
392
|
+
return syncWithSftp(alias, localPath, remotePath, direction, options)
|
|
502
393
|
}
|
|
503
394
|
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
// rsync
|
|
512
|
-
|
|
513
|
-
|
|
395
|
+
// 获取会话信息以构建 rsync 命令
|
|
396
|
+
const sessions = sessionManager.listSessions()
|
|
397
|
+
const sessionInfo = sessions.find(s => s.alias === alias)
|
|
398
|
+
if (!sessionInfo) {
|
|
399
|
+
throw new Error(`Session '${alias}' not found`)
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// 构建 rsync 参数
|
|
403
|
+
const args: string[] = ['-avz', '--progress']
|
|
404
|
+
|
|
405
|
+
if (options.delete) {
|
|
406
|
+
args.push('--delete')
|
|
407
|
+
}
|
|
408
|
+
if (options.dryRun) {
|
|
409
|
+
args.push('--dry-run')
|
|
410
|
+
}
|
|
411
|
+
if (options.recursive === false) {
|
|
412
|
+
args.push('--dirs') // 不递归,只传输目录本身
|
|
413
|
+
}
|
|
414
|
+
if (options.exclude) {
|
|
415
|
+
for (const pattern of options.exclude) {
|
|
416
|
+
args.push(`--exclude=${escapeShellPath(pattern)}`)
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
// 构建 rsync 命令(本地执行)
|
|
421
|
+
// 注意:这需要密钥认证或 ssh-agent,密码认证不支持
|
|
422
|
+
const sshCmd = `ssh -p ${sessionInfo.port} -o StrictHostKeyChecking=no -o BatchMode=yes`
|
|
423
|
+
const remoteSpec = `${sessionInfo.username}@${sessionInfo.host}:${escapeShellPath(remotePath)}`
|
|
424
|
+
const rsyncCmd = direction === 'upload'
|
|
425
|
+
? `rsync ${args.join(' ')} -e "${sshCmd}" ${escapeShellPath(localPath)} ${remoteSpec}`
|
|
426
|
+
: `rsync ${args.join(' ')} -e "${sshCmd}" ${remoteSpec} ${escapeShellPath(localPath)}`
|
|
427
|
+
|
|
428
|
+
try {
|
|
429
|
+
const result = execSync(rsyncCmd, {
|
|
430
|
+
encoding: 'utf-8',
|
|
431
|
+
timeout: 600000, // 10 分钟超时
|
|
432
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
433
|
+
})
|
|
434
|
+
|
|
435
|
+
// 解析 rsync 输出统计文件数
|
|
436
|
+
const lines = result.split('\n')
|
|
437
|
+
let filesTransferred = 0
|
|
438
|
+
for (const line of lines) {
|
|
439
|
+
if (line.trim() &&
|
|
440
|
+
!line.startsWith('sending') &&
|
|
441
|
+
!line.startsWith('receiving') &&
|
|
442
|
+
!line.startsWith('total')) {
|
|
443
|
+
filesTransferred++
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return {
|
|
448
|
+
success: true,
|
|
449
|
+
method: 'rsync',
|
|
450
|
+
filesTransferred,
|
|
451
|
+
output: result,
|
|
452
|
+
}
|
|
453
|
+
} catch {
|
|
454
|
+
// rsync 失败(可能是密码认证),回退到 SFTP
|
|
455
|
+
return syncWithSftp(alias, localPath, remotePath, direction, options)
|
|
456
|
+
}
|
|
514
457
|
}
|
|
515
458
|
|
|
516
459
|
/**
|
|
517
460
|
* 使用 SFTP 同步文件
|
|
518
461
|
*/
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
method: 'rsync' | 'sftp';
|
|
533
|
-
filesTransferred?: number;
|
|
534
|
-
bytesTransferred?: number;
|
|
535
|
-
output?: string;
|
|
536
|
-
}> {
|
|
537
|
-
// SFTP 模式不支持 delete 选项
|
|
538
|
-
const warnings: string[] = [];
|
|
539
|
-
if (options.delete) {
|
|
540
|
-
warnings.push('delete option is not supported in SFTP mode (requires rsync)');
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
if (options.dryRun) {
|
|
462
|
+
type SyncResult = {
|
|
463
|
+
success: boolean;
|
|
464
|
+
method: 'rsync' | 'sftp';
|
|
465
|
+
filesTransferred?: number;
|
|
466
|
+
bytesTransferred?: number;
|
|
467
|
+
output?: string;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
function buildSyncResult(
|
|
471
|
+
fileCount: number,
|
|
472
|
+
totalSize: number,
|
|
473
|
+
warnings: string[],
|
|
474
|
+
): SyncResult {
|
|
544
475
|
return {
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
filesTransferred: 1,
|
|
572
|
-
bytesTransferred: result.size,
|
|
573
|
-
output: warnings.length ? `Warning: ${warnings.join('; ')}` : undefined,
|
|
574
|
-
};
|
|
575
|
-
}
|
|
576
|
-
} else {
|
|
577
|
-
// 下载
|
|
578
|
-
const info = await getFileInfo(alias, remotePath);
|
|
579
|
-
if (info.isDirectory && options.recursive !== false) {
|
|
580
|
-
result = await downloadDirectory(alias, remotePath, localPath, options.exclude);
|
|
476
|
+
success: true,
|
|
477
|
+
method: 'sftp',
|
|
478
|
+
filesTransferred: fileCount,
|
|
479
|
+
bytesTransferred: totalSize,
|
|
480
|
+
output: warnings.length ? `Warning: ${warnings.join('; ')}` : undefined,
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
async function syncWithSftp(
|
|
485
|
+
alias: string,
|
|
486
|
+
localPath: string,
|
|
487
|
+
remotePath: string,
|
|
488
|
+
direction: 'upload' | 'download',
|
|
489
|
+
options: {
|
|
490
|
+
delete?: boolean;
|
|
491
|
+
dryRun?: boolean;
|
|
492
|
+
exclude?: string[];
|
|
493
|
+
recursive?: boolean;
|
|
494
|
+
},
|
|
495
|
+
): Promise<SyncResult> {
|
|
496
|
+
const warnings: string[] = []
|
|
497
|
+
if (options.delete) {
|
|
498
|
+
warnings.push('delete option is not supported in SFTP mode (requires rsync)')
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
if (options.dryRun) {
|
|
581
502
|
return {
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
503
|
+
success: true,
|
|
504
|
+
method: 'sftp',
|
|
505
|
+
output: 'Dry run mode: would transfer files via SFTP' +
|
|
506
|
+
(warnings.length ? `. Warning: ${warnings.join('; ')}` : ''),
|
|
507
|
+
}
|
|
508
|
+
}
|
|
509
|
+
|
|
510
|
+
try {
|
|
511
|
+
if (direction === 'upload') {
|
|
512
|
+
const stats = fs.statSync(localPath)
|
|
513
|
+
if (stats.isDirectory() && options.recursive !== false) {
|
|
514
|
+
const {fileCount, totalSize} = await uploadDirectory(alias, localPath, remotePath, options.exclude)
|
|
515
|
+
return buildSyncResult(fileCount, totalSize, warnings)
|
|
516
|
+
}
|
|
517
|
+
const {size} = await uploadFile(alias, localPath, remotePath)
|
|
518
|
+
return buildSyncResult(1, size, warnings)
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// download
|
|
522
|
+
const info = await getFileInfo(alias, remotePath)
|
|
523
|
+
if (info.isDirectory && options.recursive !== false) {
|
|
524
|
+
const {fileCount, totalSize} = await downloadDirectory(alias, remotePath, localPath, options.exclude)
|
|
525
|
+
return buildSyncResult(fileCount, totalSize, warnings)
|
|
526
|
+
}
|
|
527
|
+
const {size} = await downloadFile(alias, remotePath, localPath)
|
|
528
|
+
return buildSyncResult(1, size, warnings)
|
|
529
|
+
} catch (err: any) {
|
|
590
530
|
return {
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
output: warnings.length ? `Warning: ${warnings.join('; ')}` : undefined,
|
|
596
|
-
};
|
|
597
|
-
}
|
|
531
|
+
success: false,
|
|
532
|
+
method: 'sftp',
|
|
533
|
+
output: err.message,
|
|
534
|
+
}
|
|
598
535
|
}
|
|
599
|
-
} catch (err: any) {
|
|
600
|
-
return {
|
|
601
|
-
success: false,
|
|
602
|
-
method: 'sftp',
|
|
603
|
-
output: err.message,
|
|
604
|
-
};
|
|
605
|
-
}
|
|
606
536
|
}
|
|
607
537
|
|
|
608
538
|
/**
|
|
609
539
|
* 递归上传目录
|
|
610
540
|
*/
|
|
611
541
|
async function uploadDirectory(
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
542
|
+
alias: string,
|
|
543
|
+
localPath: string,
|
|
544
|
+
remotePath: string,
|
|
545
|
+
exclude?: string[],
|
|
616
546
|
): Promise<{ fileCount: number; totalSize: number }> {
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
547
|
+
let fileCount = 0
|
|
548
|
+
let totalSize = 0
|
|
549
|
+
|
|
550
|
+
// 确保远程目录存在
|
|
551
|
+
await mkdir(alias, remotePath, true)
|
|
552
|
+
|
|
553
|
+
const items = fs.readdirSync(localPath)
|
|
554
|
+
for (const item of items) {
|
|
555
|
+
// 检查排除模式
|
|
556
|
+
if (exclude && exclude.some(pattern => matchPattern(item, pattern))) {
|
|
557
|
+
continue
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
const itemLocalPath = path.join(localPath, item)
|
|
561
|
+
const itemRemotePath = path.posix.join(remotePath, item)
|
|
562
|
+
const stats = fs.statSync(itemLocalPath)
|
|
563
|
+
|
|
564
|
+
if (stats.isDirectory()) {
|
|
565
|
+
const result = await uploadDirectory(alias, itemLocalPath, itemRemotePath, exclude)
|
|
566
|
+
fileCount += result.fileCount
|
|
567
|
+
totalSize += result.totalSize
|
|
568
|
+
} else if (stats.isFile()) {
|
|
569
|
+
await uploadFile(alias, itemLocalPath, itemRemotePath)
|
|
570
|
+
fileCount++
|
|
571
|
+
totalSize += stats.size
|
|
572
|
+
}
|
|
642
573
|
}
|
|
643
|
-
}
|
|
644
574
|
|
|
645
|
-
|
|
575
|
+
return {fileCount, totalSize}
|
|
646
576
|
}
|
|
647
577
|
|
|
648
578
|
/**
|
|
649
579
|
* 递归下载目录
|
|
650
580
|
*/
|
|
651
581
|
async function downloadDirectory(
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
582
|
+
alias: string,
|
|
583
|
+
remotePath: string,
|
|
584
|
+
localPath: string,
|
|
585
|
+
exclude?: string[],
|
|
656
586
|
): Promise<{ fileCount: number; totalSize: number }> {
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
// 确保本地目录存在
|
|
661
|
-
if (!fs.existsSync(localPath)) {
|
|
662
|
-
fs.mkdirSync(localPath, { recursive: true });
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
const items = await listDir(alias, remotePath, true);
|
|
666
|
-
for (const item of items) {
|
|
667
|
-
// 检查排除模式
|
|
668
|
-
if (exclude && exclude.some(pattern => matchPattern(item.name, pattern))) {
|
|
669
|
-
continue;
|
|
670
|
-
}
|
|
587
|
+
let fileCount = 0
|
|
588
|
+
let totalSize = 0
|
|
671
589
|
|
|
672
|
-
|
|
590
|
+
// 确保本地目录存在
|
|
591
|
+
if (!fs.existsSync(localPath)) {
|
|
592
|
+
fs.mkdirSync(localPath, {recursive: true})
|
|
593
|
+
}
|
|
673
594
|
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
595
|
+
const items = await listDir(alias, remotePath, true)
|
|
596
|
+
for (const item of items) {
|
|
597
|
+
// 检查排除模式
|
|
598
|
+
if (exclude && exclude.some(pattern => matchPattern(item.name, pattern))) {
|
|
599
|
+
continue
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
const itemLocalPath = path.join(localPath, item.name)
|
|
603
|
+
|
|
604
|
+
if (item.isDirectory) {
|
|
605
|
+
const result = await downloadDirectory(alias, item.path, itemLocalPath, exclude)
|
|
606
|
+
fileCount += result.fileCount
|
|
607
|
+
totalSize += result.totalSize
|
|
608
|
+
} else if (item.isFile) {
|
|
609
|
+
await downloadFile(alias, item.path, itemLocalPath)
|
|
610
|
+
fileCount++
|
|
611
|
+
totalSize += item.size
|
|
612
|
+
}
|
|
682
613
|
}
|
|
683
|
-
}
|
|
684
614
|
|
|
685
|
-
|
|
615
|
+
return {fileCount, totalSize}
|
|
686
616
|
}
|
|
687
617
|
|
|
688
618
|
/**
|
|
689
619
|
* 简单的模式匹配(支持 * 和 ?)
|
|
690
620
|
*/
|
|
691
621
|
function matchPattern(name: string, pattern: string): boolean {
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
622
|
+
const regexPattern = pattern
|
|
623
|
+
.replace(/[.+^${}()|[\]\\]/g, '\\$&') // 转义特殊字符
|
|
624
|
+
.replace(/\*/g, '.*')
|
|
625
|
+
.replace(/\?/g, '.')
|
|
626
|
+
return new RegExp(`^${regexPattern}$`).test(name)
|
|
697
627
|
}
|
|
698
628
|
|
|
699
629
|
/**
|
|
700
630
|
* 格式化权限字符串
|
|
701
631
|
*/
|
|
702
632
|
function formatPermissions(mode: number): string {
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
633
|
+
const types: [number, string][] = [
|
|
634
|
+
[S_IFDIR, 'd'],
|
|
635
|
+
[S_IFLNK, 'l'],
|
|
636
|
+
[S_IFREG, '-'],
|
|
637
|
+
]
|
|
638
|
+
|
|
639
|
+
let type = '-'
|
|
640
|
+
for (const [mask, char] of types) {
|
|
641
|
+
if ((mode & mask) !== 0) {
|
|
642
|
+
type = char
|
|
643
|
+
break
|
|
644
|
+
}
|
|
714
645
|
}
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
return type + perms.join('');
|
|
646
|
+
|
|
647
|
+
const perms = [
|
|
648
|
+
(mode & 0o400) ? 'r' : '-',
|
|
649
|
+
(mode & 0o200) ? 'w' : '-',
|
|
650
|
+
(mode & 0o100) ? 'x' : '-',
|
|
651
|
+
(mode & 0o040) ? 'r' : '-',
|
|
652
|
+
(mode & 0o020) ? 'w' : '-',
|
|
653
|
+
(mode & 0o010) ? 'x' : '-',
|
|
654
|
+
(mode & 0o004) ? 'r' : '-',
|
|
655
|
+
(mode & 0o002) ? 'w' : '-',
|
|
656
|
+
(mode & 0o001) ? 'x' : '-',
|
|
657
|
+
]
|
|
658
|
+
|
|
659
|
+
return type + perms.join('')
|
|
730
660
|
}
|