]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cephfs-shell: put cmd must accept both paths and validate local_path
authordparmar18 <dparmar@redhat.com>
Fri, 8 Apr 2022 14:25:24 +0000 (19:55 +0530)
committerdparmar18 <dparmar@redhat.com>
Tue, 17 May 2022 08:15:48 +0000 (13:45 +0530)
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 <dparmar@redhat.com>
(cherry picked from commit cfeab92ec8d5a258d7eb48318e3e083d90383bfb)

src/tools/cephfs/cephfs-shell

index 4c88cb8cd71ba8cf3dfece92d6a167eeff8721a0..7cd5cb73c86a76b151264b61332f248516f6bbc1 100755 (executable)
@@ -605,8 +605,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.')
 
@@ -615,6 +614,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'./':
@@ -648,14 +662,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)