From cfeab92ec8d5a258d7eb48318e3e083d90383bfb Mon Sep 17 00:00:00 2001 From: dparmar18 Date: Fri, 8 Apr 2022 19:55:24 +0530 Subject: [PATCH] cephfs-shell: put cmd must accept both paths and validate local_path Description: - `put` command didn't display any error when file at local_path was not found. This PR intends to add that check. - Rationale: Till now, there used to be a default path of `remote_path` as `default='.'` but wasn't mentioned anywhere. It could lead to confusion. On top of it, considering put command to be a ssh inspired utlity, or any other CLI tool that copies file between filesystems, source and destination path are always mandatory. Therefore in order to simulate this behavior in cephfs-shell`s command(s), my opinion is to make put command accept both the paths. Fixes: https://tracker.ceph.com/issues/55242 Signed-off-by: dparmar18 --- src/tools/cephfs/cephfs-shell | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/tools/cephfs/cephfs-shell b/src/tools/cephfs/cephfs-shell index c1830dfe6f5..83be25c528d 100755 --- a/src/tools/cephfs/cephfs-shell +++ b/src/tools/cephfs/cephfs-shell @@ -611,8 +611,7 @@ class CephFSShell(Cmd): put_parser.add_argument('local_path', type=str, action=path_to_bytes, help='Path of the file in the local system') put_parser.add_argument('remote_path', type=str, action=path_to_bytes, - help='Path of the file in the remote system.', - nargs='?', default='.') + help='Path of the file in the remote system') put_parser.add_argument('-f', '--force', action='store_true', help='Overwrites the destination if it already exists.') @@ -621,6 +620,21 @@ class CephFSShell(Cmd): """ Copy a local file/directory to CephFS. """ + if args.local_path != b'-' and not os.path.isfile(args.local_path) \ + and not os.path.isdir(args.local_path): + set_exit_code_msg(errno.ENOENT, + msg=f"error: " + f"{args.local_path.decode('utf-8')}: " + f"No such file or directory") + return + + if (is_file_exists(args.remote_path) or is_dir_exists( + args.remote_path)) and not args.force: + set_exit_code_msg(msg=f"error: file/directory " + f"{args.remote_path.decode('utf-8')} " + f"exists, use --force to overwrite") + return + root_src_dir = args.local_path root_dst_dir = args.remote_path if args.local_path == b'.' or args.local_path == b'./': @@ -654,14 +668,6 @@ class CephFSShell(Cmd): root_dst_dir += b'/' if args.local_path == b'-' or os.path.isfile(root_src_dir): - if not args.force: - if os.path.isfile(root_src_dir): - dst_file = root_dst_dir - if is_file_exists(dst_file): - set_exit_code_msg(errno.EEXIST, - f"{dst_file.decode('utf-8')}: file " - "exists! use --force to overwrite") - return if args.local_path == b'-': root_src_dir = b'-' copy_from_local(root_src_dir, root_dst_dir) -- 2.39.5