-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathZoweExplorerFtpMvsApi.ts
More file actions
452 lines (426 loc) · 19.4 KB
/
Copy pathZoweExplorerFtpMvsApi.ts
File metadata and controls
452 lines (426 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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
268
269
270
271
272
273
274
275
276
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/**
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*
*/
import * as fs from "fs";
import * as crypto from "crypto";
import * as path from "path";
import * as os from "os";
import * as zosfiles from "@zowe/zos-files-for-zowe-sdk";
import { BufferBuilder, Gui, imperative, MainframeInteraction, MessageSeverity } from "@zowe/zowe-explorer-api";
import { CoreUtils, DataSetUtils } from "@zowe/zos-ftp-for-zowe-cli";
import { AbstractFtpApi } from "./ZoweExplorerAbstractFtpApi";
import { LOGGER } from "./globals";
import { ZoweFtpExtensionError } from "./ZoweFtpExtensionError";
// The Zowe FTP CLI plugin is written and uses mostly JavaScript, so relax the rules here.
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
export class FtpMvsApi extends AbstractFtpApi implements MainframeInteraction.IMvs {
public async dataSet(filter: string, options?: zosfiles.IListOptions): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
const session = this.getSession(this.profile);
try {
if (!session.mvsListConnection?.isConnected()) {
session.mvsListConnection = await this.ftpClient(this.checkedProfile());
}
if (session.mvsListConnection.isConnected()) {
const response = await DataSetUtils.listDataSets(session.mvsListConnection, filter);
if (response) {
result.success = true;
result.apiResponse.items = response.map((element) => ({
dsname: element.name,
dsorg: element.dsOrg,
vols: element.volume,
recfm: element.recordFormat,
blksz: element.blockSize,
lrecl: element.recordLength,
migr: element.isMigrated ? "YES" : "NO",
}));
if (options?.start) {
let startIndex = undefined;
result.apiResponse.items = result.apiResponse.items.filter((item, i) => {
if (item.dsname === options.start) {
startIndex = i;
}
return startIndex == null ? false : i >= startIndex;
});
}
if (options?.maxLength) {
result.apiResponse.items = result.apiResponse.items.slice(0, options.maxLength);
}
}
}
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
}
}
public async allMembers(dataSetName: string, options?: zosfiles.IListOptions): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
let connection;
try {
connection = await this.ftpClient(this.checkedProfile());
if (connection) {
const response = await DataSetUtils.listMembers(connection, dataSetName);
if (response) {
result.success = true;
// Ideally we could just do `result.apiResponse.items = response;`
result.apiResponse.items = response.map((element) => ({
member: element.name,
m4date: element.changed,
c4date: element.created,
size: element.size,
version: element.version,
// id: element.id, // Removed in zos-node-accessor v2
}));
if (options?.start) {
let startIndex = undefined;
result.apiResponse.items = result.apiResponse.items.filter((item, i) => {
if (item.member === options.start) {
startIndex = i;
}
return startIndex == null ? false : i >= startIndex;
});
}
if (options?.maxLength) {
result.apiResponse.items = result.apiResponse.items.slice(0, options.maxLength);
}
}
}
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public async getContents(dataSetName: string, options: zosfiles.IDownloadSingleOptions): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
const transferOptions = {
encoding: options.encoding,
localFile: options.file,
transferType: CoreUtils.getBinaryTransferModeOrDefault(options.binary),
};
const fileOrStreamSpecified = options.file != null || options.stream != null;
let connection;
try {
connection = await this.ftpClient(this.checkedProfile());
if (!connection || !fileOrStreamSpecified) {
LOGGER.logImperativeMessage(result.commandResponse, MessageSeverity.ERROR);
throw new Error(result.commandResponse);
}
if (options.file) {
transferOptions.localFile = options.file;
imperative.IO.createDirsSyncFromFilePath(transferOptions.localFile);
await DataSetUtils.downloadDataSet(connection, dataSetName, transferOptions);
result.apiResponse.etag = await this.hashFile(transferOptions.localFile);
} else if (options.stream) {
const buffer = await DataSetUtils.downloadDataSet(connection, dataSetName, transferOptions);
result.apiResponse.etag = this.hashBuffer(buffer);
options.stream.write(buffer);
options.stream.end();
}
result.success = true;
result.commandResponse = "";
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public async uploadFromBuffer(buffer: Buffer, dataSetName: string, options?: zosfiles.IUploadOptions): Promise<zosfiles.IZosFilesResponse> {
const result = await this.putContents(buffer, dataSetName, options);
return result;
}
public async putContents(input: string | Buffer, dataSetName: string, options: zosfiles.IUploadOptions): Promise<zosfiles.IZosFilesResponse> {
const openParens = dataSetName.indexOf("(");
const dataSetNameWithoutMember = openParens > 0 ? dataSetName.substring(0, openParens) : dataSetName;
const dsAtrribute = await this.dataSet(dataSetNameWithoutMember);
const result = this.getDefaultResponse();
const profile = this.checkedProfile();
const dsorg = dsAtrribute.apiResponse.items[0]?.dsorg;
const isPds = dsorg === "PO" || dsorg === "PO-E";
/**
* Determine the data set name for uploading.
*
* For PDS: When the input is a file path and the provided data set name doesn't include the member name,
* we'll need to generate a member name.
*/
const uploadName =
isPds && openParens == -1 && typeof input === "string"
? `${dataSetName}(${zosfiles.ZosFilesUtils.generateMemberName(input)})`
: dataSetName;
const inputIsBuffer = input instanceof Buffer;
// Save-Save with FTP requires loading the file first
// (moved this block above connection request so only one connection is active at a time)
if (options.returnEtag && options.etag) {
const contentsTag = await this.getContentsTag(uploadName, inputIsBuffer);
if (contentsTag && contentsTag !== options.etag) {
throw Error("Rest API failure with HTTP(S) status 412: Save conflict");
}
}
let connection;
try {
connection = await this.ftpClient(profile);
if (!connection) {
LOGGER.logImperativeMessage(result.commandResponse, MessageSeverity.ERROR);
throw new Error(result.commandResponse);
}
const lrecl: number = dsAtrribute.apiResponse.items[0].lrecl;
const data = inputIsBuffer ? input.toString() : fs.readFileSync(input, { encoding: "utf8" });
const transferOptions: Record<string, any> = {
content: inputIsBuffer ? input : undefined,
encoding: options.encoding,
localFile: inputIsBuffer ? undefined : input,
transferType: CoreUtils.getBinaryTransferModeOrDefault(options.binary),
};
if (profile.profile.secureFtp && data === "") {
// substitute single space for empty DS contents when saving (avoids FTPS error)
transferOptions.content = " ";
delete transferOptions.localFile;
}
const lines = data.split(/\r?\n/);
const foundIndex = lines.findIndex((line) => line.length > lrecl);
if (foundIndex !== -1) {
const message1 = `zftp Warning: At least one line, like line ${foundIndex + 1},
is longer than dataset LRECL, ${lrecl}.`;
const message2 = "The exceeding part will be truncated.";
const message3 = "Do you want to continue?";
const warningMessage = `${message1} ${message2}\n${message3}`;
const select = await Gui.warningMessage(warningMessage, {
items: ["Yes", "No"],
});
if (select === "No") {
result.commandResponse = "";
return result;
}
}
await DataSetUtils.uploadDataSet(connection, uploadName, transferOptions);
result.success = true;
if (options.returnEtag) {
// release this connection instance because a new one will be made with getContentsTag
this.releaseConnection(connection);
connection = null;
const etag = await this.getContentsTag(uploadName, inputIsBuffer);
result.apiResponse = {
etag,
};
}
result.commandResponse = "Data set uploaded successfully.";
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public async createDataSet(
dataSetType: zosfiles.CreateDataSetTypeEnum,
dataSetName: string,
options?: Partial<zosfiles.ICreateDataSetOptions>
): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
const dcbList = [];
if (options?.alcunit) {
dcbList.push(`ALCUNIT=${options.alcunit}`);
}
if (options?.blksize) {
dcbList.push(`BLKSIZE=${options.blksize}`);
}
if (options?.dirblk) {
dcbList.push(`DIRECTORY=${options.dirblk}`);
}
if (options?.dsorg) {
dcbList.push(`DSORG=${options.dsorg}`);
}
if (options?.lrecl) {
dcbList.push(`LRECL=${options.lrecl}`);
}
if (options?.primary) {
dcbList.push(`PRIMARY=${options.primary}`);
}
if (options?.recfm) {
dcbList.push(`RECFM=${options.recfm}`);
}
if (options?.secondary) {
dcbList.push(`SECONDARY=${options.secondary}`);
}
const allocateOptions = {
dcb: dcbList.join(" "),
};
let connection;
try {
connection = await this.ftpClient(this.checkedProfile());
if (connection) {
await DataSetUtils.allocateDataSet(connection, dataSetName, allocateOptions);
result.success = true;
result.commandResponse = "Data set created successfully.";
} else {
throw new Error(result.commandResponse);
}
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public async createDataSetMember(dataSetName: string, options?: zosfiles.IUploadOptions): Promise<zosfiles.IZosFilesResponse> {
const profile = this.checkedProfile();
const transferOptions = {
transferType: CoreUtils.getBinaryTransferModeOrDefault(options.binary),
// we have to provide a single space for content over FTPS, or it will fail to upload
content: profile.profile.secureFtp ? " " : "",
encoding: options.encoding,
};
const result = this.getDefaultResponse();
let connection;
try {
connection = await this.ftpClient(profile);
if (!connection) {
throw new Error(result.commandResponse);
}
await DataSetUtils.uploadDataSet(connection, dataSetName, transferOptions);
result.success = true;
result.commandResponse = "Member created successfully.";
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public allocateLikeDataSet(_dataSetName: string, _likeDataSetName: string): Promise<zosfiles.IZosFilesResponse> {
throw new ZoweFtpExtensionError("Allocate like dataset is not supported in ftp extension.");
}
public copyDataSetMember(
{ dsn: _fromDataSetName, member: _fromMemberName }: zosfiles.IDataSet,
{ dsn: _toDataSetName, member: _toMemberName }: zosfiles.IDataSet,
_options?: { replace?: boolean }
): Promise<zosfiles.IZosFilesResponse> {
throw new ZoweFtpExtensionError("Copy dataset member is not supported in ftp extension.");
}
public copyDataSet(_fromDataSetName: string, _toDataSetName: string, _enq?: string, _replace?: boolean): Promise<zosfiles.IZosFilesResponse> {
throw new ZoweFtpExtensionError("Copy dataset is not supported in ftp extension.");
}
public async renameDataSet(currentDataSetName: string, newDataSetName: string): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
let connection;
try {
connection = await this.ftpClient(this.checkedProfile());
if (connection) {
await DataSetUtils.renameDataSet(connection, currentDataSetName, newDataSetName);
result.success = true;
result.commandResponse = "Rename completed successfully.";
} else {
throw new Error(result.commandResponse);
}
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public async renameDataSetMember(dataSetName: string, currentMemberName: string, newMemberName: string): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
const currentName = dataSetName + "(" + currentMemberName + ")";
const newName = dataSetName + "(" + newMemberName + ")";
let connection;
try {
connection = await this.ftpClient(this.checkedProfile());
if (connection) {
await DataSetUtils.renameDataSet(connection, currentName, newName);
result.success = true;
result.commandResponse = "Rename completed successfully.";
} else {
throw new Error(result.commandResponse);
}
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
public hMigrateDataSet(_dataSetName: string): Promise<zosfiles.IZosFilesResponse> {
throw new ZoweFtpExtensionError("Migrate dataset is not supported in ftp extension.");
}
public hRecallDataSet(_dataSetName: string): Promise<zosfiles.IZosFilesResponse> {
throw new ZoweFtpExtensionError("Recall dataset is not supported in ftp extension.");
}
public async deleteDataSet(dataSetName: string, _options?: zosfiles.IDeleteDatasetOptions): Promise<zosfiles.IZosFilesResponse> {
const result = this.getDefaultResponse();
let connection;
try {
connection = await this.ftpClient(this.checkedProfile());
if (connection) {
await DataSetUtils.deleteDataSet(connection, dataSetName);
result.success = true;
result.commandResponse = "Delete completed successfully.";
} else {
throw new Error(result.commandResponse);
}
return result;
} catch (err) {
throw new ZoweFtpExtensionError(err.message);
} finally {
this.releaseConnection(connection);
}
}
private async getContentsTag(dataSetName: string, buffer?: boolean): Promise<string> {
if (buffer) {
const builder = new BufferBuilder();
const loadResult = await this.getContents(dataSetName, { binary: false, stream: builder });
return loadResult.apiResponse.etag as string;
}
// Create a temporary directory and unique filename
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "zowe-ftp-mvs-"));
const tmpFileName = path.join(tmpDir, `temp-${crypto.randomUUID()}.dat`);
try {
const options: zosfiles.IDownloadOptions = {
binary: false,
file: tmpFileName,
};
const loadResult = await this.getContents(dataSetName, options);
return loadResult.apiResponse.etag as string;
} finally {
// Clean up temporary file and directory
try {
fs.rmSync(tmpDir, { force: true, recursive: true });
} catch (cleanupError) {
if (cleanupError instanceof Error) {
LOGGER.logImperativeMessage(`Failed to clean up temporary files: ${cleanupError.message}`, MessageSeverity.WARN);
}
}
}
}
private getDefaultResponse(): zosfiles.IZosFilesResponse {
return {
success: false,
commandResponse: "Could not get a valid FTP connection.",
apiResponse: {},
};
}
private hashFile(filename: string): Promise<string> {
return new Promise((resolve) => {
const hash = crypto.createHash("sha256");
const input = fs.createReadStream(filename);
input.on("readable", () => {
const data = input.read();
if (data) {
hash.update(data as unknown as crypto.BinaryLike);
} else {
resolve(`${hash.digest("hex")}`);
}
});
});
}
}