]> git.eshelyaron.com Git - emacs.git/commitdiff
Check directory in Tramp's {copy,rename}-file
authorMichael Albinus <michael.albinus@gmx.de>
Fri, 12 Jul 2019 13:10:57 +0000 (15:10 +0200)
committerMichael Albinus <michael.albinus@gmx.de>
Fri, 12 Jul 2019 13:10:57 +0000 (15:10 +0200)
* lisp/net/tramp-adb.el (tramp-adb-handle-copy-file)
(tramp-adb-handle-rename-file):
* lisp/net/tramp-gvfs.el (tramp-gvfs-do-copy-or-rename-file):
* lisp/net/tramp-rclone.el (tramp-rclone-do-copy-or-rename-file):
* lisp/net/tramp-sh.el (tramp-do-copy-or-rename-file):
* lisp/net/tramp-smb.el (tramp-smb-handle-copy-file)
(tramp-smb-handle-rename-file):
* lisp/net/tramp-sudoedit.el (tramp-sudoedit-do-copy-or-rename-file):
Check, that NEWNAME is not a directory given as file name.

* test/lisp/net/tramp-tests.el (tramp-test11-copy-file)
(tramp-test12-rename-file): Extend tests.

lisp/net/tramp-adb.el
lisp/net/tramp-gvfs.el
lisp/net/tramp-rclone.el
lisp/net/tramp-sh.el
lisp/net/tramp-smb.el
lisp/net/tramp-sudoedit.el
test/lisp/net/tramp-tests.el

index 3a5f4732ade108fb187ff1334e212489ac299eab..fb84aa11085c9b04a220d48a90a156198bd2db09 100644 (file)
@@ -709,15 +709,16 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
     (let ((t1 (tramp-tramp-file-p filename))
          (t2 (tramp-tramp-file-p newname)))
       (with-parsed-tramp-file-name (if t1 filename newname) nil
+       (when (and (not ok-if-already-exists) (file-exists-p newname))
+         (tramp-error v 'file-already-exists newname))
+       (when (and (file-directory-p newname) (not (directory-name-p newname)))
+         (tramp-error v 'file-error "File is a directory %s" newname))
+
        (with-tramp-progress-reporter
            v 0 (format "Copying %s to %s" filename newname)
-
          (if (and t1 t2 (tramp-equal-remote filename newname))
              (let ((l1 (tramp-compat-file-local-name filename))
                    (l2 (tramp-compat-file-local-name newname)))
-               (when (and (not ok-if-already-exists)
-                          (file-exists-p newname))
-                 (tramp-error v 'file-already-exists newname))
                ;; We must also flush the cache of the directory,
                ;; because `file-attributes' reads the values from
                ;; there.
@@ -788,17 +789,18 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
     (let ((t1 (tramp-tramp-file-p filename))
          (t2 (tramp-tramp-file-p newname)))
       (with-parsed-tramp-file-name (if t1 filename newname) nil
+       (when (and (not ok-if-already-exists) (file-exists-p newname))
+         (tramp-error v 'file-already-exists newname))
+       (when (and (file-directory-p newname) (not (directory-name-p newname)))
+         (tramp-error v 'file-error "File is a directory %s" newname))
+
        (with-tramp-progress-reporter
            v 0 (format "Renaming %s to %s" filename newname)
-
          (if (and t1 t2
                   (tramp-equal-remote filename newname)
                   (not (file-directory-p filename)))
              (let ((l1 (tramp-compat-file-local-name filename))
                    (l2 (tramp-compat-file-local-name newname)))
-               (when (and (not ok-if-already-exists)
-                          (file-exists-p newname))
-                 (tramp-error v 'file-already-exists newname))
                ;; We must also flush the cache of the directory, because
                ;; `file-attributes' reads the values from there.
                (tramp-flush-file-properties v (file-name-directory l1))
index c9ed674bc275e8922c1c6654f0010084d38ec38b..9d45e6a8ce9b22c6cdf30224c8ddf18fcf0f9304 100644 (file)
@@ -765,6 +765,8 @@ file names."
       (with-parsed-tramp-file-name (if t1 filename newname) nil
        (when (and (not ok-if-already-exists) (file-exists-p newname))
          (tramp-error v 'file-already-exists newname))
+       (when (and (file-directory-p newname) (not (directory-name-p newname)))
+         (tramp-error v 'file-error "File is a directory %s" newname))
 
        (if (or (and equal-remote
                     (tramp-get-connection-property v "direct-copy-failed" nil))
index 22a8ca3ac6ceed5553b61e15b3196ea16a7359b1..9b3eab3477111588b002d5c825f8a93f36d5fd23 100644 (file)
@@ -215,6 +215,8 @@ file names."
       (with-parsed-tramp-file-name (if t1 filename newname) nil
        (when (and (not ok-if-already-exists) (file-exists-p newname))
          (tramp-error v 'file-already-exists newname))
+       (when (and (file-directory-p newname) (not (directory-name-p newname)))
+         (tramp-error v 'file-error "File is a directory %s" newname))
 
        (if (or (and t1 (not (tramp-rclone-file-name-p filename)))
                (and t2 (not (tramp-rclone-file-name-p newname))))
index d488e5ccf97e976d1189e3355ed2f7c83f93332f..b8b981bc1a669c356eb38a3efc884c17980f6885 100644 (file)
@@ -1995,6 +1995,8 @@ file names."
       (with-parsed-tramp-file-name (if t1 filename newname) nil
        (when (and (not ok-if-already-exists) (file-exists-p newname))
          (tramp-error v 'file-already-exists newname))
+       (when (and (file-directory-p newname) (not (directory-name-p newname)))
+         (tramp-error v 'file-error "File is a directory %s" newname))
 
        (with-tramp-progress-reporter
            v 0 (format "%s %s to %s"
index 099277d496a69195e4af113600d57d1f03d6673f..9b87ed40cb04aef9efc7369aecb8e13fbfd4df80 100644 (file)
@@ -588,9 +588,11 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
                  (expand-file-name (file-name-nondirectory filename) newname)))
 
          (with-parsed-tramp-file-name newname nil
-           (when (and (not ok-if-already-exists)
-                      (file-exists-p newname))
+           (when (and (not ok-if-already-exists) (file-exists-p newname))
              (tramp-error v 'file-already-exists newname))
+           (when (and (file-directory-p newname)
+                      (not (directory-name-p newname)))
+             (tramp-error v 'file-error "File is a directory %s" newname))
 
            ;; We must also flush the cache of the directory, because
            ;; `file-attributes' reads the values from there.
@@ -1335,48 +1337,49 @@ component is used as the target of the symlink."
   (setq filename (expand-file-name filename)
        newname (expand-file-name newname))
 
-  (when (and (not ok-if-already-exists)
-            (file-exists-p newname))
-    (tramp-error
-     (tramp-dissect-file-name
-      (if (tramp-tramp-file-p filename) filename newname))
-     'file-already-exists newname))
-
-  (with-tramp-progress-reporter
-      (tramp-dissect-file-name
-       (if (tramp-tramp-file-p filename) filename newname))
-      0 (format "Renaming %s to %s" filename newname)
-
-    (if (and (not (file-exists-p newname))
-            (tramp-equal-remote filename newname)
-            (string-equal
-             (tramp-smb-get-share (tramp-dissect-file-name filename))
-             (tramp-smb-get-share (tramp-dissect-file-name newname))))
-       ;; We can rename directly.
-       (with-parsed-tramp-file-name filename v1
-         (with-parsed-tramp-file-name newname v2
-
-           ;; We must also flush the cache of the directory, because
-           ;; `file-attributes' reads the values from there.
-           (tramp-flush-file-properties v1 (file-name-directory v1-localname))
-           (tramp-flush-file-properties v1 v1-localname)
-           (tramp-flush-file-properties v2 (file-name-directory v2-localname))
-           (tramp-flush-file-properties v2 v2-localname)
-           (unless (tramp-smb-get-share v2)
-             (tramp-error
-              v2 'file-error "Target `%s' must contain a share name" newname))
-           (unless (tramp-smb-send-command
-                    v2 (format "rename \"%s\" \"%s\""
-                               (tramp-smb-get-localname v1)
-                               (tramp-smb-get-localname v2)))
-             (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
-
-      ;; We must rename via copy.
-      (copy-file
-       filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
-      (if (file-directory-p filename)
-         (delete-directory filename 'recursive)
-       (delete-file filename)))))
+  (with-parsed-tramp-file-name
+      (if (tramp-tramp-file-p filename) filename newname) nil
+    (when (and (not ok-if-already-exists) (file-exists-p newname))
+      (tramp-error v 'file-already-exists newname))
+    (when (and (file-directory-p newname) (not (directory-name-p newname)))
+      (tramp-error v 'file-error "File is a directory %s" newname))
+
+    (with-tramp-progress-reporter
+       v 0 (format "Renaming %s to %s" filename newname)
+
+      (if (and (not (file-exists-p newname))
+              (tramp-equal-remote filename newname)
+              (string-equal
+               (tramp-smb-get-share (tramp-dissect-file-name filename))
+               (tramp-smb-get-share (tramp-dissect-file-name newname))))
+         ;; We can rename directly.
+         (with-parsed-tramp-file-name filename v1
+           (with-parsed-tramp-file-name newname v2
+
+             ;; We must also flush the cache of the directory, because
+             ;; `file-attributes' reads the values from there.
+             (tramp-flush-file-properties
+              v1 (file-name-directory v1-localname))
+             (tramp-flush-file-properties v1 v1-localname)
+             (tramp-flush-file-properties
+              v2 (file-name-directory v2-localname))
+             (tramp-flush-file-properties v2 v2-localname)
+             (unless (tramp-smb-get-share v2)
+               (tramp-error
+                v2 'file-error
+                "Target `%s' must contain a share name" newname))
+             (unless (tramp-smb-send-command
+                      v2 (format "rename \"%s\" \"%s\""
+                                 (tramp-smb-get-localname v1)
+                                 (tramp-smb-get-localname v2)))
+               (tramp-error v2 'file-error "Cannot rename `%s'" filename))))
+
+       ;; We must rename via copy.
+       (copy-file
+        filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
+       (if (file-directory-p filename)
+           (delete-directory filename 'recursive)
+         (delete-file filename))))))
 
 (defun tramp-smb-action-set-acl (proc vec)
   "Set ACL data."
index bbe780099d5f5678354008d018ada6a7793e7d9b..0ded85fb5546406b05afe9db68c6a85e4582aa9d 100644 (file)
@@ -244,6 +244,8 @@ absolute file names."
       (with-parsed-tramp-file-name (if t1 filename newname) nil
        (when (and (not ok-if-already-exists) (file-exists-p newname))
          (tramp-error v 'file-already-exists newname))
+       (when (and (file-directory-p newname) (not (directory-name-p newname)))
+         (tramp-error v 'file-error "File is a directory %s" newname))
 
        (if (or (and (file-remote-p filename) (not t1))
                (and (file-remote-p newname)  (not t2)))
index 77b2c263ff83661b6bc999ae453a9e41048a55bc..a12e41a882287c06bef80ac5a021df7fac41b90a 100644 (file)
@@ -2394,7 +2394,10 @@ This checks also `file-name-as-directory', `file-name-directory',
                (when (and (tramp--test-expensive-test) (tramp--test-emacs26-p))
                  (should-error
                   (copy-file source target)
-                  :type 'file-already-exists))
+                  :type 'file-already-exists)
+                 (should-error
+                  (copy-file source target 'ok)
+                  :type 'file-error))
                (copy-file source (file-name-as-directory target))
                (should
                 (file-exists-p
@@ -2508,7 +2511,10 @@ This checks also `file-name-as-directory', `file-name-directory',
                (when (and (tramp--test-expensive-test) (tramp--test-emacs26-p))
                  (should-error
                   (rename-file source target)
-                  :type 'file-already-exists))
+                  :type 'file-already-exists)
+                 (should-error
+                  (rename-file source target 'ok)
+                  :type 'file-error))
                (rename-file source (file-name-as-directory target))
                (should-not (file-exists-p source))
                (should