Commit dd19cc3a authored by Michael Albinus's avatar Michael Albinus

Improve Tramp error handling

* lisp/net/tramp.el (tramp-set-syntax): Add missing argument.
(tramp-signal-hook-function): Make it more robust.
(tramp-handle-directory-files):
* lisp/net/tramp-adb.el
(tramp-adb-handle-directory-files-and-attributes)
(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)
(tramp-rclone-handle-directory-files):
* lisp/net/tramp-sh.el (tramp-sh-handle-directory-files-and-attributes)
(tramp-sh-handle-copy-directory, tramp-do-copy-or-rename-file):
* lisp/net/tramp-smb.el (tramp-smb-handle-copy-directory)
(tramp-smb-handle-copy-file, tramp-smb-handle-directory-files)
(tramp-smb-handle-rename-file):
* lisp/net/tramp-sudoedit.el (tramp-sudoedit-do-copy-or-rename-file):
Improve error handling.

* test/lisp/net/tramp-tests.el (tramp-test11-copy-file)
(tramp-test12-rename-file, tramp-test14-delete-directory)
(tramp-test15-copy-directory, tramp-test16-directory-files)
(tramp-test19-directory-files-and-attributes): Extend tests.
parent a256e03b
Pipeline #3932 failed with stage
in 59 minutes and 44 seconds
...@@ -383,6 +383,10 @@ ARGUMENTS to pass to the OPERATION." ...@@ -383,6 +383,10 @@ ARGUMENTS to pass to the OPERATION."
(defun tramp-adb-handle-directory-files-and-attributes (defun tramp-adb-handle-directory-files-and-attributes
(directory &optional full match nosort id-format) (directory &optional full match nosort id-format)
"Like `directory-files-and-attributes' for Tramp files." "Like `directory-files-and-attributes' for Tramp files."
(unless (file-exists-p directory)
(tramp-error
(tramp-dissect-file-name directory) tramp-file-missing
"No such file or directory" directory))
(when (file-directory-p directory) (when (file-directory-p directory)
(with-parsed-tramp-file-name (expand-file-name directory) nil (with-parsed-tramp-file-name (expand-file-name directory) nil
(copy-tree (copy-tree
...@@ -706,6 +710,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." ...@@ -706,6 +710,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
(let ((t1 (tramp-tramp-file-p filename)) (let ((t1 (tramp-tramp-file-p filename))
(t2 (tramp-tramp-file-p newname))) (t2 (tramp-tramp-file-p newname)))
(with-parsed-tramp-file-name (if t1 filename newname) nil (with-parsed-tramp-file-name (if t1 filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing
"Copying file" "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
...@@ -784,6 +792,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." ...@@ -784,6 +792,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
(let ((t1 (tramp-tramp-file-p filename)) (let ((t1 (tramp-tramp-file-p filename))
(t2 (tramp-tramp-file-p newname))) (t2 (tramp-tramp-file-p newname)))
(with-parsed-tramp-file-name (if t1 filename newname) nil (with-parsed-tramp-file-name (if t1 filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing
"Renaming file" "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
......
...@@ -765,6 +765,10 @@ file names." ...@@ -765,6 +765,10 @@ file names."
(msg-operation (if (eq op 'copy) "Copying" "Renaming"))) (msg-operation (if (eq op 'copy) "Copying" "Renaming")))
(with-parsed-tramp-file-name (if t1 filename newname) nil (with-parsed-tramp-file-name (if t1 filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing
"%s file" msg-operation "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
......
...@@ -213,6 +213,10 @@ file names." ...@@ -213,6 +213,10 @@ file names."
(msg-operation (if (eq op 'copy) "Copying" "Renaming"))) (msg-operation (if (eq op 'copy) "Copying" "Renaming")))
(with-parsed-tramp-file-name (if t1 filename newname) nil (with-parsed-tramp-file-name (if t1 filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing
"%s file" msg-operation "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
...@@ -298,6 +302,10 @@ file names." ...@@ -298,6 +302,10 @@ file names."
(defun tramp-rclone-handle-directory-files (defun tramp-rclone-handle-directory-files
(directory &optional full match nosort) (directory &optional full match nosort)
"Like `directory-files' for Tramp files." "Like `directory-files' for Tramp files."
(unless (file-exists-p directory)
(tramp-error
(tramp-dissect-file-name directory) tramp-file-missing
"No such file or directory" directory))
(when (file-directory-p directory) (when (file-directory-p directory)
(setq directory (file-name-as-directory (expand-file-name directory))) (setq directory (file-name-as-directory (expand-file-name directory)))
(with-parsed-tramp-file-name directory nil (with-parsed-tramp-file-name directory nil
......
...@@ -1713,6 +1713,10 @@ of." ...@@ -1713,6 +1713,10 @@ of."
(directory &optional full match nosort id-format) (directory &optional full match nosort id-format)
"Like `directory-files-and-attributes' for Tramp files." "Like `directory-files-and-attributes' for Tramp files."
(unless id-format (setq id-format 'integer)) (unless id-format (setq id-format 'integer))
(unless (file-exists-p directory)
(tramp-error
(tramp-dissect-file-name directory) tramp-file-missing
"No such file or directory" directory))
(when (file-directory-p directory) (when (file-directory-p directory)
(setq directory (expand-file-name directory)) (setq directory (expand-file-name directory))
(let* ((temp (let* ((temp
...@@ -1923,6 +1927,10 @@ tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'" ...@@ -1923,6 +1927,10 @@ tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
(let ((t1 (tramp-tramp-file-p dirname)) (let ((t1 (tramp-tramp-file-p dirname))
(t2 (tramp-tramp-file-p newname))) (t2 (tramp-tramp-file-p newname)))
(with-parsed-tramp-file-name (if t1 dirname newname) nil (with-parsed-tramp-file-name (if t1 dirname newname) nil
(unless (file-exists-p dirname)
(tramp-error
v tramp-file-missing
"Copying directory" "No such file or directory" dirname))
(if (and (not copy-contents) (if (and (not copy-contents)
(tramp-get-method-parameter v 'tramp-copy-recursive) (tramp-get-method-parameter v 'tramp-copy-recursive)
;; When DIRNAME and NEWNAME are remote, they must have ;; When DIRNAME and NEWNAME are remote, they must have
...@@ -2011,6 +2019,9 @@ file names." ...@@ -2011,6 +2019,9 @@ file names."
(apply #'file-extended-attributes (list filename))))) (apply #'file-extended-attributes (list filename)))))
(with-parsed-tramp-file-name (if t1 filename newname) nil (with-parsed-tramp-file-name (if t1 filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
......
...@@ -415,6 +415,10 @@ pass to the OPERATION." ...@@ -415,6 +415,10 @@ pass to the OPERATION."
(with-parsed-tramp-file-name (if t1 dirname newname) nil (with-parsed-tramp-file-name (if t1 dirname newname) nil
(with-tramp-progress-reporter (with-tramp-progress-reporter
v 0 (format "Copying %s to %s" dirname newname) v 0 (format "Copying %s to %s" dirname newname)
(unless (file-exists-p dirname)
(tramp-error
v tramp-file-missing
"Copying directory" "No such file or directory" dirname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
(not (tramp-compat-directory-name-p newname))) (not (tramp-compat-directory-name-p newname)))
(tramp-error v 'file-already-exists newname)) (tramp-error v 'file-already-exists newname))
...@@ -570,6 +574,13 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." ...@@ -570,6 +574,13 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
(if (file-directory-p filename) (if (file-directory-p filename)
(copy-directory filename newname keep-date 'parents 'copy-contents) (copy-directory filename newname keep-date 'parents 'copy-contents)
(unless (file-exists-p filename)
(tramp-error
(tramp-dissect-file-name
(if (tramp-tramp-file-p filename) filename newname))
tramp-file-missing
"Copying file" "No such file or directory" filename))
(let ((tmpfile (file-local-copy filename))) (let ((tmpfile (file-local-copy filename)))
(if tmpfile (if tmpfile
;; Remote filename. ;; Remote filename.
...@@ -669,6 +680,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." ...@@ -669,6 +680,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
(defun tramp-smb-handle-directory-files (defun tramp-smb-handle-directory-files
(directory &optional full match nosort) (directory &optional full match nosort)
"Like `directory-files' for Tramp files." "Like `directory-files' for Tramp files."
(unless (file-exists-p directory)
(tramp-error
(tramp-dissect-file-name directory) tramp-file-missing
"No such file or directory" directory))
(let ((result (mapcar #'directory-file-name (let ((result (mapcar #'directory-file-name
(file-name-all-completions "" directory)))) (file-name-all-completions "" directory))))
;; Discriminate with regexp. ;; Discriminate with regexp.
...@@ -1333,6 +1348,10 @@ component is used as the target of the symlink." ...@@ -1333,6 +1348,10 @@ component is used as the target of the symlink."
(with-parsed-tramp-file-name (with-parsed-tramp-file-name
(if (tramp-tramp-file-p filename) filename newname) nil (if (tramp-tramp-file-p filename) filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing
"Renaming file" "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
......
...@@ -241,6 +241,10 @@ absolute file names." ...@@ -241,6 +241,10 @@ absolute file names."
(msg-operation (if (eq op 'copy) "Copying" "Renaming"))) (msg-operation (if (eq op 'copy) "Copying" "Renaming")))
(with-parsed-tramp-file-name (if t1 filename newname) nil (with-parsed-tramp-file-name (if t1 filename newname) nil
(unless (file-exists-p filename)
(tramp-error
v tramp-file-missing
"%s file" msg-operation "No such file or directory" filename))
(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)) (tramp-error v 'file-already-exists newname))
(when (and (file-directory-p newname) (when (and (file-directory-p newname)
......
...@@ -719,7 +719,7 @@ Used in user option `tramp-syntax'. There are further variables ...@@ -719,7 +719,7 @@ Used in user option `tramp-syntax'. There are further variables
to be set, depending on VALUE." to be set, depending on VALUE."
;; Check allowed values. ;; Check allowed values.
(unless (memq value (tramp-syntax-values)) (unless (memq value (tramp-syntax-values))
(tramp-user-error "Wrong `tramp-syntax' %s" value)) (tramp-user-error nil "Wrong `tramp-syntax' %s" value))
;; Cleanup existing buffers. ;; Cleanup existing buffers.
(unless (eq (symbol-value symbol) value) (unless (eq (symbol-value symbol) value)
(tramp-cleanup-all-buffers)) (tramp-cleanup-all-buffers))
...@@ -1889,8 +1889,13 @@ the resulting error message." ...@@ -1889,8 +1889,13 @@ the resulting error message."
;; This function provides traces in case of errors not triggered by ;; This function provides traces in case of errors not triggered by
;; Tramp functions. ;; Tramp functions.
(defun tramp-signal-hook-function (error-symbol data) (defun tramp-signal-hook-function (error-symbol data)
"Funtion to be called via `signal-hook-function'." "Function to be called via `signal-hook-function'."
(tramp-error (car tramp-current-connection) error-symbol "%s" data)) ;; `custom-initialize-*' functions provoke `void-variable' errors.
;; We don't want to see them in the backtrace.
(unless (eq error-symbol 'void-variable)
(tramp-error
(car tramp-current-connection) error-symbol
"%s" (mapconcat (lambda (x) (format "%s" x)) data " "))))
(defmacro with-parsed-tramp-file-name (filename var &rest body) (defmacro with-parsed-tramp-file-name (filename var &rest body)
"Parse a Tramp filename and make components available in the body. "Parse a Tramp filename and make components available in the body.
...@@ -3025,6 +3030,10 @@ User is always nil." ...@@ -3025,6 +3030,10 @@ User is always nil."
(defun tramp-handle-directory-files (directory &optional full match nosort) (defun tramp-handle-directory-files (directory &optional full match nosort)
"Like `directory-files' for Tramp files." "Like `directory-files' for Tramp files."
(unless (file-exists-p directory)
(tramp-error
(tramp-dissect-file-name directory) tramp-file-missing
"No such file or directory" directory))
(when (file-directory-p directory) (when (file-directory-p directory)
(setq directory (file-name-as-directory (expand-file-name directory))) (setq directory (file-name-as-directory (expand-file-name directory)))
(let ((temp (nreverse (file-name-all-completions "" directory))) (let ((temp (nreverse (file-name-all-completions "" directory)))
......
...@@ -2370,6 +2370,9 @@ This checks also `file-name-as-directory', `file-name-directory', ...@@ -2370,6 +2370,9 @@ This checks also `file-name-as-directory', `file-name-directory',
;; Copy simple file. ;; Copy simple file.
(unwind-protect (unwind-protect
(progn (progn
(should-error
(copy-file source target)
:type tramp-file-missing)
(write-region "foo" nil source) (write-region "foo" nil source)
(should (file-exists-p source)) (should (file-exists-p source))
(copy-file source target) (copy-file source target)
...@@ -2482,6 +2485,9 @@ This checks also `file-name-as-directory', `file-name-directory', ...@@ -2482,6 +2485,9 @@ This checks also `file-name-as-directory', `file-name-directory',
;; Rename simple file. ;; Rename simple file.
(unwind-protect (unwind-protect
(progn (progn
(should-error
(rename-file source target)
:type tramp-file-missing)
(write-region "foo" nil source) (write-region "foo" nil source)
(should (file-exists-p source)) (should (file-exists-p source))
(rename-file source target) (rename-file source target)
...@@ -2605,20 +2611,25 @@ This tests also `file-directory-p' and `file-accessible-directory-p'." ...@@ -2605,20 +2611,25 @@ This tests also `file-directory-p' and `file-accessible-directory-p'."
(skip-unless (tramp--test-enabled)) (skip-unless (tramp--test-enabled))
(dolist (quoted (if (tramp--test-expensive-test) '(nil t) '(nil))) (dolist (quoted (if (tramp--test-expensive-test) '(nil t) '(nil)))
(let ((tmp-name (tramp--test-make-temp-name nil quoted))) (let* ((tmp-name1 (tramp--test-make-temp-name nil quoted))
(tmp-name2 (expand-file-name "foo" tmp-name1)))
;; Delete empty directory. ;; Delete empty directory.
(make-directory tmp-name) (make-directory tmp-name1)
(should (file-directory-p tmp-name)) (should (file-directory-p tmp-name1))
(delete-directory tmp-name) (delete-directory tmp-name1)
(should-not (file-directory-p tmp-name)) (should-not (file-directory-p tmp-name1))
;; Delete non-empty directory. ;; Delete non-empty directory.
(make-directory tmp-name) (make-directory tmp-name1)
(should (file-directory-p tmp-name)) (should (file-directory-p tmp-name1))
(write-region "foo" nil (expand-file-name "bla" tmp-name)) (write-region "foo" nil (expand-file-name "bla" tmp-name1))
(should (file-exists-p (expand-file-name "bla" tmp-name))) (should (file-exists-p (expand-file-name "bla" tmp-name1)))
(should-error (delete-directory tmp-name) :type 'file-error) (make-directory tmp-name2)
(delete-directory tmp-name 'recursive) (should (file-directory-p tmp-name2))
(should-not (file-directory-p tmp-name))))) (write-region "foo" nil (expand-file-name "bla" tmp-name2))
(should (file-exists-p (expand-file-name "bla" tmp-name2)))
(should-error (delete-directory tmp-name1) :type 'file-error)
(delete-directory tmp-name1 'recursive)
(should-not (file-directory-p tmp-name1)))))
(ert-deftest tramp-test15-copy-directory () (ert-deftest tramp-test15-copy-directory ()
"Check `copy-directory'." "Check `copy-directory'."
...@@ -2636,6 +2647,9 @@ This tests also `file-directory-p' and `file-accessible-directory-p'." ...@@ -2636,6 +2647,9 @@ This tests also `file-directory-p' and `file-accessible-directory-p'."
;; Copy complete directory. ;; Copy complete directory.
(unwind-protect (unwind-protect
(progn (progn
(should-error
(copy-directory tmp-name1 tmp-name2)
:type tramp-file-missing)
;; Copy empty directory. ;; Copy empty directory.
(make-directory tmp-name1) (make-directory tmp-name1)
(write-region "foo" nil tmp-name4) (write-region "foo" nil tmp-name4)
...@@ -2696,6 +2710,9 @@ This tests also `file-directory-p' and `file-accessible-directory-p'." ...@@ -2696,6 +2710,9 @@ This tests also `file-directory-p' and `file-accessible-directory-p'."
(tmp-name3 (expand-file-name "foo" tmp-name1))) (tmp-name3 (expand-file-name "foo" tmp-name1)))
(unwind-protect (unwind-protect
(progn (progn
(should-error
(directory-files tmp-name1)
:type tramp-file-missing)
(make-directory tmp-name1) (make-directory tmp-name1)
(write-region "foo" nil tmp-name2) (write-region "foo" nil tmp-name2)
(write-region "bla" nil tmp-name3) (write-region "bla" nil tmp-name3)
...@@ -3174,6 +3191,9 @@ They might differ only in time attributes or directory size." ...@@ -3174,6 +3191,9 @@ They might differ only in time attributes or directory size."
attr) attr)
(unwind-protect (unwind-protect
(progn (progn
(should-error
(directory-files-and-attributes tmp-name1)
:type tramp-file-missing)
(make-directory tmp-name1) (make-directory tmp-name1)
(should (file-directory-p tmp-name1)) (should (file-directory-p tmp-name1))
(setq tramp--test-start-time (setq tramp--test-start-time
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment