diff options
| author | Michael Albinus | 2019-07-12 15:10:57 +0200 |
|---|---|---|
| committer | Michael Albinus | 2019-07-12 15:10:57 +0200 |
| commit | 955db220053778bd1e47ff9daf87896b7fd52a67 (patch) | |
| tree | d7e7b138ee69930467f6eb40e6d211c3521c2ac3 | |
| parent | 9f76913184c900534fa17311ff5b1af747ff6264 (diff) | |
| download | emacs-955db220053778bd1e47ff9daf87896b7fd52a67.tar.gz emacs-955db220053778bd1e47ff9daf87896b7fd52a67.zip | |
Check directory in Tramp's {copy,rename}-file
* 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.
| -rw-r--r-- | lisp/net/tramp-adb.el | 18 | ||||
| -rw-r--r-- | lisp/net/tramp-gvfs.el | 2 | ||||
| -rw-r--r-- | lisp/net/tramp-rclone.el | 2 | ||||
| -rw-r--r-- | lisp/net/tramp-sh.el | 2 | ||||
| -rw-r--r-- | lisp/net/tramp-smb.el | 91 | ||||
| -rw-r--r-- | lisp/net/tramp-sudoedit.el | 2 | ||||
| -rw-r--r-- | test/lisp/net/tramp-tests.el | 10 |
7 files changed, 73 insertions, 54 deletions
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el index 3a5f4732ade..fb84aa11085 100644 --- a/lisp/net/tramp-adb.el +++ b/lisp/net/tramp-adb.el | |||
| @@ -709,15 +709,16 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." | |||
| 709 | (let ((t1 (tramp-tramp-file-p filename)) | 709 | (let ((t1 (tramp-tramp-file-p filename)) |
| 710 | (t2 (tramp-tramp-file-p newname))) | 710 | (t2 (tramp-tramp-file-p newname))) |
| 711 | (with-parsed-tramp-file-name (if t1 filename newname) nil | 711 | (with-parsed-tramp-file-name (if t1 filename newname) nil |
| 712 | (when (and (not ok-if-already-exists) (file-exists-p newname)) | ||
| 713 | (tramp-error v 'file-already-exists newname)) | ||
| 714 | (when (and (file-directory-p newname) (not (directory-name-p newname))) | ||
| 715 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 716 | |||
| 712 | (with-tramp-progress-reporter | 717 | (with-tramp-progress-reporter |
| 713 | v 0 (format "Copying %s to %s" filename newname) | 718 | v 0 (format "Copying %s to %s" filename newname) |
| 714 | |||
| 715 | (if (and t1 t2 (tramp-equal-remote filename newname)) | 719 | (if (and t1 t2 (tramp-equal-remote filename newname)) |
| 716 | (let ((l1 (tramp-compat-file-local-name filename)) | 720 | (let ((l1 (tramp-compat-file-local-name filename)) |
| 717 | (l2 (tramp-compat-file-local-name newname))) | 721 | (l2 (tramp-compat-file-local-name newname))) |
| 718 | (when (and (not ok-if-already-exists) | ||
| 719 | (file-exists-p newname)) | ||
| 720 | (tramp-error v 'file-already-exists newname)) | ||
| 721 | ;; We must also flush the cache of the directory, | 722 | ;; We must also flush the cache of the directory, |
| 722 | ;; because `file-attributes' reads the values from | 723 | ;; because `file-attributes' reads the values from |
| 723 | ;; there. | 724 | ;; there. |
| @@ -788,17 +789,18 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." | |||
| 788 | (let ((t1 (tramp-tramp-file-p filename)) | 789 | (let ((t1 (tramp-tramp-file-p filename)) |
| 789 | (t2 (tramp-tramp-file-p newname))) | 790 | (t2 (tramp-tramp-file-p newname))) |
| 790 | (with-parsed-tramp-file-name (if t1 filename newname) nil | 791 | (with-parsed-tramp-file-name (if t1 filename newname) nil |
| 792 | (when (and (not ok-if-already-exists) (file-exists-p newname)) | ||
| 793 | (tramp-error v 'file-already-exists newname)) | ||
| 794 | (when (and (file-directory-p newname) (not (directory-name-p newname))) | ||
| 795 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 796 | |||
| 791 | (with-tramp-progress-reporter | 797 | (with-tramp-progress-reporter |
| 792 | v 0 (format "Renaming %s to %s" filename newname) | 798 | v 0 (format "Renaming %s to %s" filename newname) |
| 793 | |||
| 794 | (if (and t1 t2 | 799 | (if (and t1 t2 |
| 795 | (tramp-equal-remote filename newname) | 800 | (tramp-equal-remote filename newname) |
| 796 | (not (file-directory-p filename))) | 801 | (not (file-directory-p filename))) |
| 797 | (let ((l1 (tramp-compat-file-local-name filename)) | 802 | (let ((l1 (tramp-compat-file-local-name filename)) |
| 798 | (l2 (tramp-compat-file-local-name newname))) | 803 | (l2 (tramp-compat-file-local-name newname))) |
| 799 | (when (and (not ok-if-already-exists) | ||
| 800 | (file-exists-p newname)) | ||
| 801 | (tramp-error v 'file-already-exists newname)) | ||
| 802 | ;; We must also flush the cache of the directory, because | 804 | ;; We must also flush the cache of the directory, because |
| 803 | ;; `file-attributes' reads the values from there. | 805 | ;; `file-attributes' reads the values from there. |
| 804 | (tramp-flush-file-properties v (file-name-directory l1)) | 806 | (tramp-flush-file-properties v (file-name-directory l1)) |
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el index c9ed674bc27..9d45e6a8ce9 100644 --- a/lisp/net/tramp-gvfs.el +++ b/lisp/net/tramp-gvfs.el | |||
| @@ -765,6 +765,8 @@ file names." | |||
| 765 | (with-parsed-tramp-file-name (if t1 filename newname) nil | 765 | (with-parsed-tramp-file-name (if t1 filename newname) nil |
| 766 | (when (and (not ok-if-already-exists) (file-exists-p newname)) | 766 | (when (and (not ok-if-already-exists) (file-exists-p newname)) |
| 767 | (tramp-error v 'file-already-exists newname)) | 767 | (tramp-error v 'file-already-exists newname)) |
| 768 | (when (and (file-directory-p newname) (not (directory-name-p newname))) | ||
| 769 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 768 | 770 | ||
| 769 | (if (or (and equal-remote | 771 | (if (or (and equal-remote |
| 770 | (tramp-get-connection-property v "direct-copy-failed" nil)) | 772 | (tramp-get-connection-property v "direct-copy-failed" nil)) |
diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el index 22a8ca3ac6c..9b3eab34771 100644 --- a/lisp/net/tramp-rclone.el +++ b/lisp/net/tramp-rclone.el | |||
| @@ -215,6 +215,8 @@ file names." | |||
| 215 | (with-parsed-tramp-file-name (if t1 filename newname) nil | 215 | (with-parsed-tramp-file-name (if t1 filename newname) nil |
| 216 | (when (and (not ok-if-already-exists) (file-exists-p newname)) | 216 | (when (and (not ok-if-already-exists) (file-exists-p newname)) |
| 217 | (tramp-error v 'file-already-exists newname)) | 217 | (tramp-error v 'file-already-exists newname)) |
| 218 | (when (and (file-directory-p newname) (not (directory-name-p newname))) | ||
| 219 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 218 | 220 | ||
| 219 | (if (or (and t1 (not (tramp-rclone-file-name-p filename))) | 221 | (if (or (and t1 (not (tramp-rclone-file-name-p filename))) |
| 220 | (and t2 (not (tramp-rclone-file-name-p newname)))) | 222 | (and t2 (not (tramp-rclone-file-name-p newname)))) |
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el index d488e5ccf97..b8b981bc1a6 100644 --- a/lisp/net/tramp-sh.el +++ b/lisp/net/tramp-sh.el | |||
| @@ -1995,6 +1995,8 @@ file names." | |||
| 1995 | (with-parsed-tramp-file-name (if t1 filename newname) nil | 1995 | (with-parsed-tramp-file-name (if t1 filename newname) nil |
| 1996 | (when (and (not ok-if-already-exists) (file-exists-p newname)) | 1996 | (when (and (not ok-if-already-exists) (file-exists-p newname)) |
| 1997 | (tramp-error v 'file-already-exists newname)) | 1997 | (tramp-error v 'file-already-exists newname)) |
| 1998 | (when (and (file-directory-p newname) (not (directory-name-p newname))) | ||
| 1999 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 1998 | 2000 | ||
| 1999 | (with-tramp-progress-reporter | 2001 | (with-tramp-progress-reporter |
| 2000 | v 0 (format "%s %s to %s" | 2002 | v 0 (format "%s %s to %s" |
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el index 099277d496a..9b87ed40cb0 100644 --- a/lisp/net/tramp-smb.el +++ b/lisp/net/tramp-smb.el | |||
| @@ -588,9 +588,11 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." | |||
| 588 | (expand-file-name (file-name-nondirectory filename) newname))) | 588 | (expand-file-name (file-name-nondirectory filename) newname))) |
| 589 | 589 | ||
| 590 | (with-parsed-tramp-file-name newname nil | 590 | (with-parsed-tramp-file-name newname nil |
| 591 | (when (and (not ok-if-already-exists) | 591 | (when (and (not ok-if-already-exists) (file-exists-p newname)) |
| 592 | (file-exists-p newname)) | ||
| 593 | (tramp-error v 'file-already-exists newname)) | 592 | (tramp-error v 'file-already-exists newname)) |
| 593 | (when (and (file-directory-p newname) | ||
| 594 | (not (directory-name-p newname))) | ||
| 595 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 594 | 596 | ||
| 595 | ;; We must also flush the cache of the directory, because | 597 | ;; We must also flush the cache of the directory, because |
| 596 | ;; `file-attributes' reads the values from there. | 598 | ;; `file-attributes' reads the values from there. |
| @@ -1335,48 +1337,49 @@ component is used as the target of the symlink." | |||
| 1335 | (setq filename (expand-file-name filename) | 1337 | (setq filename (expand-file-name filename) |
| 1336 | newname (expand-file-name newname)) | 1338 | newname (expand-file-name newname)) |
| 1337 | 1339 | ||
| 1338 | (when (and (not ok-if-already-exists) | 1340 | (with-parsed-tramp-file-name |
| 1339 | (file-exists-p newname)) | 1341 | (if (tramp-tramp-file-p filename) filename newname) nil |
| 1340 | (tramp-error | 1342 | (when (and (not ok-if-already-exists) (file-exists-p newname)) |
| 1341 | (tramp-dissect-file-name | 1343 | (tramp-error v 'file-already-exists newname)) |
| 1342 | (if (tramp-tramp-file-p filename) filename newname)) | 1344 | (when (and (file-directory-p newname) (not (directory-name-p newname))) |
| 1343 | 'file-already-exists newname)) | 1345 | (tramp-error v 'file-error "File is a directory %s" newname)) |
| 1344 | 1346 | ||
| 1345 | (with-tramp-progress-reporter | 1347 | (with-tramp-progress-reporter |
| 1346 | (tramp-dissect-file-name | 1348 | v 0 (format "Renaming %s to %s" filename newname) |
| 1347 | (if (tramp-tramp-file-p filename) filename newname)) | 1349 | |
| 1348 | 0 (format "Renaming %s to %s" filename newname) | 1350 | (if (and (not (file-exists-p newname)) |
| 1349 | 1351 | (tramp-equal-remote filename newname) | |
| 1350 | (if (and (not (file-exists-p newname)) | 1352 | (string-equal |
| 1351 | (tramp-equal-remote filename newname) | 1353 | (tramp-smb-get-share (tramp-dissect-file-name filename)) |
| 1352 | (string-equal | 1354 | (tramp-smb-get-share (tramp-dissect-file-name newname)))) |
| 1353 | (tramp-smb-get-share (tramp-dissect-file-name filename)) | 1355 | ;; We can rename directly. |
| 1354 | (tramp-smb-get-share (tramp-dissect-file-name newname)))) | 1356 | (with-parsed-tramp-file-name filename v1 |
| 1355 | ;; We can rename directly. | 1357 | (with-parsed-tramp-file-name newname v2 |
| 1356 | (with-parsed-tramp-file-name filename v1 | 1358 | |
| 1357 | (with-parsed-tramp-file-name newname v2 | 1359 | ;; We must also flush the cache of the directory, because |
| 1358 | 1360 | ;; `file-attributes' reads the values from there. | |
| 1359 | ;; We must also flush the cache of the directory, because | 1361 | (tramp-flush-file-properties |
| 1360 | ;; `file-attributes' reads the values from there. | 1362 | v1 (file-name-directory v1-localname)) |
| 1361 | (tramp-flush-file-properties v1 (file-name-directory v1-localname)) | 1363 | (tramp-flush-file-properties v1 v1-localname) |
| 1362 | (tramp-flush-file-properties v1 v1-localname) | 1364 | (tramp-flush-file-properties |
| 1363 | (tramp-flush-file-properties v2 (file-name-directory v2-localname)) | 1365 | v2 (file-name-directory v2-localname)) |
| 1364 | (tramp-flush-file-properties v2 v2-localname) | 1366 | (tramp-flush-file-properties v2 v2-localname) |
| 1365 | (unless (tramp-smb-get-share v2) | 1367 | (unless (tramp-smb-get-share v2) |
| 1366 | (tramp-error | 1368 | (tramp-error |
| 1367 | v2 'file-error "Target `%s' must contain a share name" newname)) | 1369 | v2 'file-error |
| 1368 | (unless (tramp-smb-send-command | 1370 | "Target `%s' must contain a share name" newname)) |
| 1369 | v2 (format "rename \"%s\" \"%s\"" | 1371 | (unless (tramp-smb-send-command |
| 1370 | (tramp-smb-get-localname v1) | 1372 | v2 (format "rename \"%s\" \"%s\"" |
| 1371 | (tramp-smb-get-localname v2))) | 1373 | (tramp-smb-get-localname v1) |
| 1372 | (tramp-error v2 'file-error "Cannot rename `%s'" filename)))) | 1374 | (tramp-smb-get-localname v2))) |
| 1373 | 1375 | (tramp-error v2 'file-error "Cannot rename `%s'" filename)))) | |
| 1374 | ;; We must rename via copy. | 1376 | |
| 1375 | (copy-file | 1377 | ;; We must rename via copy. |
| 1376 | filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid) | 1378 | (copy-file |
| 1377 | (if (file-directory-p filename) | 1379 | filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid) |
| 1378 | (delete-directory filename 'recursive) | 1380 | (if (file-directory-p filename) |
| 1379 | (delete-file filename))))) | 1381 | (delete-directory filename 'recursive) |
| 1382 | (delete-file filename)))))) | ||
| 1380 | 1383 | ||
| 1381 | (defun tramp-smb-action-set-acl (proc vec) | 1384 | (defun tramp-smb-action-set-acl (proc vec) |
| 1382 | "Set ACL data." | 1385 | "Set ACL data." |
diff --git a/lisp/net/tramp-sudoedit.el b/lisp/net/tramp-sudoedit.el index bbe780099d5..0ded85fb554 100644 --- a/lisp/net/tramp-sudoedit.el +++ b/lisp/net/tramp-sudoedit.el | |||
| @@ -244,6 +244,8 @@ absolute file names." | |||
| 244 | (with-parsed-tramp-file-name (if t1 filename newname) nil | 244 | (with-parsed-tramp-file-name (if t1 filename newname) nil |
| 245 | (when (and (not ok-if-already-exists) (file-exists-p newname)) | 245 | (when (and (not ok-if-already-exists) (file-exists-p newname)) |
| 246 | (tramp-error v 'file-already-exists newname)) | 246 | (tramp-error v 'file-already-exists newname)) |
| 247 | (when (and (file-directory-p newname) (not (directory-name-p newname))) | ||
| 248 | (tramp-error v 'file-error "File is a directory %s" newname)) | ||
| 247 | 249 | ||
| 248 | (if (or (and (file-remote-p filename) (not t1)) | 250 | (if (or (and (file-remote-p filename) (not t1)) |
| 249 | (and (file-remote-p newname) (not t2))) | 251 | (and (file-remote-p newname) (not t2))) |
diff --git a/test/lisp/net/tramp-tests.el b/test/lisp/net/tramp-tests.el index 77b2c263ff8..a12e41a8822 100644 --- a/test/lisp/net/tramp-tests.el +++ b/test/lisp/net/tramp-tests.el | |||
| @@ -2394,7 +2394,10 @@ This checks also `file-name-as-directory', `file-name-directory', | |||
| 2394 | (when (and (tramp--test-expensive-test) (tramp--test-emacs26-p)) | 2394 | (when (and (tramp--test-expensive-test) (tramp--test-emacs26-p)) |
| 2395 | (should-error | 2395 | (should-error |
| 2396 | (copy-file source target) | 2396 | (copy-file source target) |
| 2397 | :type 'file-already-exists)) | 2397 | :type 'file-already-exists) |
| 2398 | (should-error | ||
| 2399 | (copy-file source target 'ok) | ||
| 2400 | :type 'file-error)) | ||
| 2398 | (copy-file source (file-name-as-directory target)) | 2401 | (copy-file source (file-name-as-directory target)) |
| 2399 | (should | 2402 | (should |
| 2400 | (file-exists-p | 2403 | (file-exists-p |
| @@ -2508,7 +2511,10 @@ This checks also `file-name-as-directory', `file-name-directory', | |||
| 2508 | (when (and (tramp--test-expensive-test) (tramp--test-emacs26-p)) | 2511 | (when (and (tramp--test-expensive-test) (tramp--test-emacs26-p)) |
| 2509 | (should-error | 2512 | (should-error |
| 2510 | (rename-file source target) | 2513 | (rename-file source target) |
| 2511 | :type 'file-already-exists)) | 2514 | :type 'file-already-exists) |
| 2515 | (should-error | ||
| 2516 | (rename-file source target 'ok) | ||
| 2517 | :type 'file-error)) | ||
| 2512 | (rename-file source (file-name-as-directory target)) | 2518 | (rename-file source (file-name-as-directory target)) |
| 2513 | (should-not (file-exists-p source)) | 2519 | (should-not (file-exists-p source)) |
| 2514 | (should | 2520 | (should |