Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add checking the uss file path when upload file or stdin to uss file. #145

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to the z/OS FTP Plug-in for Zowe CLI will be documented in this file.

## Recent Changes

- Add checking the uss file path when upload file or stdin to uss file.

## `2.1.4`
- BugFix: Provide new utility function that checks file names for valid characters [143](https://github.com/zowe/zowe-cli-ftp-plugin/issues/143).

Expand Down
9 changes: 9 additions & 0 deletions __tests__/__unit__/api/UssUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*
*/

import { ImperativeError } from "@zowe/imperative";
import { UssUtils } from "../../../src/api/UssUtils";

describe("UssUtils", () => {
Expand All @@ -28,4 +29,12 @@ describe("UssUtils", () => {
path = UssUtils.normalizeUnixPath("//home/user1/hello.text");
expect(path).toBe("/home/user1/hello.text");
});

it("should throw error when the uss file path is not absolute file Path", () => {
const path = "test.txt";
const result = () => {
UssUtils.checkAbsoluteFilePath(path);
};
expect(result).toThrow(ImperativeError);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ describe("Upload file to data set handler", () => {
expect(mockResponse.console.log.mock.calls[0][1]).toBe("local file '/u/user/file1'");
expect(mockResponse.console.log.mock.calls[0][2]).toBe("/u/user/ussfile1");
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,4 @@ describe("Upload file to data set handler", () => {
expect(mockResponse.console.log.mock.calls[0][1]).toBe("stdin");
expect(mockResponse.console.log.mock.calls[0][2]).toBe("/u/user/ussfile1");
});

});
14 changes: 7 additions & 7 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@typescript-eslint/eslint-plugin": "^5.33.0",
"@typescript-eslint/parser": "^5.33.0",
"@zowe/cli": "^7.18.8",
"@zowe/cli-test-utils": "^7.18.7",
"@zowe/cli-test-utils": "^7.18.11",
"@zowe/imperative": "^5.18.3",
"chalk": "^2.3.0",
"env-cmd": "^10.1.0",
Expand Down
10 changes: 9 additions & 1 deletion src/api/UssUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import * as PATH from "path";
import * as fs from "fs";

import { IHandlerResponseConsoleApi, IO, Logger } from "@zowe/imperative";
import { IHandlerResponseConsoleApi, IO, ImperativeError, Logger } from "@zowe/imperative";
import { CoreUtils, TRANSFER_TYPE_ASCII } from "./CoreUtils";
import { StreamUtils } from "./StreamUtils";
import { IDeleteFileOption, IDownloadFileOption, IUploadFileOption } from "./UssInterface";
Expand Down Expand Up @@ -201,6 +201,14 @@ export class UssUtils {
}
}

public static checkAbsoluteFilePath(filePath: string) {
if (!filePath.startsWith('/')) {
throw new ImperativeError({ msg: "Please check the uss file path. The full file path is required."});
} else {
return filePath;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the current usage of the function, I don't think we need to return the filePath, but it doesn't hurt (in case we want to use it in the near future)

No changes requested here 😋

}
}

private static get log(): Logger {
return Logger.getAppLogger();
}
Expand Down
1 change: 1 addition & 0 deletions src/cli/upload/file-to-uss-file/FileToUssFile.Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class UploadFileToUssFileHandler extends FTPBaseHandler {

public async processFTP(params: IFTPHandlerParams): Promise<void> {
const ussFile = UssUtils.normalizeUnixPath(params.arguments.ussFile);
UssUtils.checkAbsoluteFilePath(ussFile);
const options = {
localFile: params.arguments.file,
transferType: params.arguments.binary ? TRANSFER_TYPE_BINARY : TRANSFER_TYPE_ASCII,
Expand Down
3 changes: 2 additions & 1 deletion src/cli/upload/stdin-to-uss-file/StdinToUssFile.Handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export default class UploadStdinToUssFileHandler extends FTPBaseHandler {
public async processFTP(params: IFTPHandlerParams): Promise<void> {

const ussFile = UssUtils.normalizeUnixPath(params.arguments.ussFile);
UssUtils.checkAbsoluteFilePath(ussFile);
const content: Buffer | string = await CoreUtils.readStdin(params.stdin);

const options = {
Expand All @@ -30,7 +31,7 @@ export default class UploadStdinToUssFileHandler extends FTPBaseHandler {
const successMsg = params.response.console.log("Uploaded from %s to %s ", uploadSource, ussFile);
params.response.data.setMessage(successMsg);
this.log.info(successMsg);

}

}