-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathZoweExplorerFtpMvsApi.ts
More file actions
299 lines (282 loc) · 12 KB
/
Copy pathZoweExplorerFtpMvsApi.ts
File metadata and controls
299 lines (282 loc) · 12 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
/*
* 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 tmp from "tmp";
import * as zowe from "@zowe/cli";
import * as imperative from "@zowe/imperative";
import * as path from "path";
import { ZoweExplorerApi } from "@zowe/zowe-explorer-api";
import { DataSetUtils, TRANSFER_TYPE_ASCII, TRANSFER_TYPE_BINARY } from "@zowe/zos-ftp-for-zowe-cli";
import { AbstractFtpApi } from "./ZoweExplorerAbstractFtpApi";
// 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 ZoweExplorerApi.IMvs {
public async dataSet(filter: string, options?: zowe.IListOptions): Promise<zowe.IZosFilesResponse> {
const result = this.getDefaultResponse();
const connection = await this.ftpClient(this.checkedProfile());
if (connection) {
const response: any[] = await DataSetUtils.listDataSets(connection, filter);
if (response) {
result.success = true;
result.apiResponse.items = response.map((element) => ({
dsname: element.dsname,
dsorg: element.dsorg,
volume: element.volume,
recfm: element.recfm,
blksz: element.blksz,
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
migr: element.volume && element.volume.toUpperCase() === "MIGRATED" ? "YES" : "NO",
}));
}
}
return result;
}
public async allMembers(dataSetName: string, options?: zowe.IListOptions): Promise<zowe.IZosFilesResponse> {
const result = this.getDefaultResponse();
const connection = await this.ftpClient(this.checkedProfile());
if (connection) {
const response: any[] = await DataSetUtils.listMembers(connection, dataSetName);
if (response) {
result.success = true;
result.apiResponse.items = response.map((element) => ({
member: element.name,
changed: element.changed,
created: element.created,
id: element.id,
}));
}
}
return result;
}
public async getContents(dataSetName: string, options: zowe.IDownloadOptions): Promise<zowe.IZosFilesResponse> {
const connection = await this.ftpClient(this.checkedProfile());
const result = this.getDefaultResponse();
const targetFile = options.file;
const transferOptions = {
transferType: options.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
localFile: targetFile,
};
if (connection && targetFile) {
imperative.IO.createDirsSyncFromFilePath(targetFile);
await DataSetUtils.downloadDataSet(connection, dataSetName, transferOptions);
result.success = true;
result.commandResponse = "";
result.apiResponse.etag = await this.hashFile(targetFile);
} else {
throw new Error(result.commandResponse);
}
return result;
}
public async putContents(
inputFilePath: string,
dataSetName: string,
options: zowe.IUploadOptions
): Promise<zowe.IZosFilesResponse> {
const transferOptions = {
transferType: options.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
localFile: inputFilePath,
};
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const file = path.basename(inputFilePath).replace(/[^a-z0-9]+/gi, "");
const member = file.substr(0, 8);
let targetDataset;
const dsAtrribute = await this.dataSet(dataSetName);
const dsorg = dsAtrribute.apiResponse.items[0].dsorg;
if (dsorg === "PS" || dataSetName.substr(dataSetName.length - 1) == ")") {
targetDataset = dataSetName;
} else {
targetDataset = dataSetName + "(" + member + ")";
}
const result = this.getDefaultResponse();
const connection = await this.ftpClient(this.checkedProfile());
if (!connection) {
throw new Error(result.commandResponse);
}
// Save-Save with FTP requires loading the file first
if (options.returnEtag && options.etag) {
const contentsTag = await this.getContentsTag(dataSetName);
if (contentsTag && contentsTag !== options.etag) {
// TODO: extension.ts should not check for zosmf errors.
throw new Error("Save conflict. Please pull the latest content from mainfram first.");
}
}
await DataSetUtils.uploadDataSet(connection, targetDataset, transferOptions);
result.success = true;
if (options.returnEtag) {
const contentsTag = await this.getContentsTag(dataSetName);
result.apiResponse = [
{
etag: contentsTag,
},
];
}
result.commandResponse = "Data set uploaded successfully.";
return result;
}
public async createDataSet(
dataSetType: zowe.CreateDataSetTypeEnum,
dataSetName: string,
options?: Partial<zowe.ICreateDataSetOptions>
): Promise<zowe.IZosFilesResponse> {
const result = this.getDefaultResponse();
const connection = await this.ftpClient(this.checkedProfile());
/* eslint-disable @typescript-eslint/restrict-plus-operands */
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 dcb = dcbList.join(" ");
const allocateOptions = {
dcb: dcb,
};
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;
}
public async createDataSetMember(
dataSetName: string,
options?: zowe.IUploadOptions
): Promise<zowe.IZosFilesResponse> {
const transferOptions = {
transferType: options ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
content: "",
};
const result = this.getDefaultResponse();
const connection = await this.ftpClient(this.checkedProfile());
if (!connection) {
throw new Error(result.commandResponse);
}
await DataSetUtils.uploadDataSet(connection, dataSetName, transferOptions);
result.success = true;
result.commandResponse = "Member created successfully.";
return result;
}
public allocateLikeDataSet(dataSetName: string, likeDataSetName: string): Promise<zowe.IZosFilesResponse> {
throw new Error("Allocate like dataset is not supported in ftp extension.");
}
public copyDataSetMember(
{ dataSetName: fromDataSetName, memberName: fromMemberName }: zowe.IDataSet,
{ dataSetName: toDataSetName, memberName: toMemberName }: zowe.IDataSet,
options?: { replace?: boolean }
): Promise<zowe.IZosFilesResponse> {
throw new Error("Copy dataset is not supported in ftp extension.");
}
public async renameDataSet(currentDataSetName: string, newDataSetName: string): Promise<zowe.IZosFilesResponse> {
const result = this.getDefaultResponse();
const 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;
}
public async renameDataSetMember(
dataSetName: string,
currentMemberName: string,
newMemberName: string
): Promise<zowe.IZosFilesResponse> {
const result = this.getDefaultResponse();
const connection = await this.ftpClient(this.checkedProfile());
const currentName = dataSetName + "(" + currentMemberName + ")";
const newName = dataSetName + "(" + newMemberName + ")";
if (connection) {
await DataSetUtils.renameDataSet(connection, currentName, newName);
result.success = true;
result.commandResponse = "Rename completed successfully.";
} else {
throw new Error(result.commandResponse);
}
return result;
}
public hMigrateDataSet(dataSetName: string): Promise<zowe.IZosFilesResponse> {
throw new Error("Migrate dataset is not supported in ftp extension.");
}
public hRecallDataSet(dataSetName: string): Promise<zowe.IZosFilesResponse> {
throw new Error("Recall dataset is not supported in ftp extension.");
}
public async deleteDataSet(
dataSetName: string,
options?: zowe.IDeleteDatasetOptions
): Promise<zowe.IZosFilesResponse> {
const result = this.getDefaultResponse();
const 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;
}
private async getContentsTag(dataSetName: string): Promise<string> {
const tmpFileName = tmp.tmpNameSync();
const options: zowe.IDownloadOptions = {
binary: false,
file: tmpFileName,
};
const loadResult = await this.getContents(dataSetName, options);
const etag: string = loadResult.apiResponse.etag;
return etag;
}
private getDefaultResponse(): zowe.IZosFilesResponse {
return {
success: false,
commandResponse: "Could not get a valid FTP connection.",
apiResponse: {},
};
}
private async hashFile(filename: string): Promise<string> {
return await new Promise((resolve) => {
const hash = crypto.createHash("sha1");
const input = fs.createReadStream(filename);
input.on("readable", () => {
const data = input.read();
if (data) {
hash.update(data);
} else {
resolve(`${hash.digest("hex")}`);
}
});
});
}
}