]> code.delx.au - gnu-emacs/blobdiff - lisp/net/tramp-sh.el
Merge from emacs-24; up to 2012-12-17T11:17:34Z!rgm@gnu.org
[gnu-emacs] / lisp / net / tramp-sh.el
index ec321d0050639342be52e2c0089b31444273a7dc..0c2a0aa385a5c0109bf1f5e41179cefb400ddde2 100644 (file)
@@ -1,6 +1,6 @@
 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
 
-;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
+;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
 
 ;; (copyright statements below in code to be updated with the above notice)
 
@@ -805,7 +805,7 @@ on the remote host.")
 (defconst tramp-perl-encode
   "%s -e '
 # This script contributed by Juanma Barranquero <lektu@terra.es>.
-# Copyright (C) 2002-2012 Free Software Foundation, Inc.
+# Copyright (C) 2002-2013 Free Software Foundation, Inc.
 use strict;
 
 my %%trans = do {
@@ -813,14 +813,11 @@ my %%trans = do {
     map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
       split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
 };
-
-binmode(\\*STDIN);
+my $data;
 
 # We read in chunks of 54 bytes, to generate output lines
 # of 72 chars (plus end of line)
-$/ = \\54;
-
-while (my $data = <STDIN>) {
+while (read STDIN, $data, 54) {
     my $pad = q();
 
     # Only for the last chunk, and only if did not fill the last three-byte packet
@@ -846,7 +843,7 @@ This string is passed to `format', so percent characters need to be doubled.")
 (defconst tramp-perl-decode
   "%s -e '
 # This script contributed by Juanma Barranquero <lektu@terra.es>.
-# Copyright (C) 2002-2012 Free Software Foundation, Inc.
+# Copyright (C) 2002-2013 Free Software Foundation, Inc.
 use strict;
 
 my %%trans = do {
@@ -938,6 +935,7 @@ This is used to map a mode number to a permission string.")
     (file-name-nondirectory . tramp-handle-file-name-nondirectory)
     (file-truename . tramp-sh-handle-file-truename)
     (file-exists-p . tramp-sh-handle-file-exists-p)
+    (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
     (file-directory-p . tramp-sh-handle-file-directory-p)
     (file-executable-p . tramp-sh-handle-file-executable-p)
     (file-readable-p . tramp-sh-handle-file-readable-p)
@@ -988,6 +986,8 @@ This is used to map a mode number to a permission string.")
     (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
     (file-selinux-context . tramp-sh-handle-file-selinux-context)
     (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
+    (file-acl . tramp-sh-handle-file-acl)
+    (set-file-acl . tramp-sh-handle-set-file-acl)
     (vc-registered . tramp-sh-handle-vc-registered))
   "Alist of handler functions.
 Operations not mentioned here will be handled by the normal Emacs functions.")
@@ -1270,9 +1270,10 @@ target of the symlink differ."
          res-uid
          ;; 3. File gid.
          res-gid
-         ;; 4. Last access time, as a list of two integers. First
-         ;; integer has high-order 16 bits of time, second has low 16
-         ;; bits.
+         ;; 4. Last access time, as a list of integers.  Normally this
+         ;; would be in the same format as `current-time', but the
+         ;; subseconds part is not currently implemented, and (0 0)
+         ;; denotes an unknown time.
          ;; 5. Last modification time, likewise.
          ;; 6. Last status change time, likewise.
          '(0 0) '(0 0) '(0 0)          ;CCC how to find out?
@@ -1329,7 +1330,8 @@ target of the symlink differ."
     (let ((f (buffer-file-name))
          coding-system-used)
       (with-parsed-tramp-file-name f nil
-       (let* ((attr (file-attributes f))
+       (let* ((remote-file-name-inhibit-cache t)
+              (attr (file-attributes f))
               ;; '(-1 65535) means file doesn't exists yet.
               (modtime (or (nth 5 attr) '(-1 65535))))
          (when (boundp 'last-coding-system-used)
@@ -1451,7 +1453,8 @@ of."
 (defun tramp-set-file-uid-gid (filename &optional uid gid)
   "Set the ownership for FILENAME.
 If UID and GID are provided, these values are used; otherwise uid
-and gid of the corresponding user is taken.  Both parameters must be integers."
+and gid of the corresponding user is taken.  Both parameters must
+be non-negative integers."
   ;; Modern Unices allow chown only for root.  So we might need
   ;; another implementation, see `dired-do-chown'.  OTOH, it is mostly
   ;; working with su(do)? when it is needed, so it shall succeed in
@@ -1463,9 +1466,9 @@ and gid of the corresponding user is taken.  Both parameters must be integers."
          (if (and (zerop (user-uid)) (tramp-local-host-p v))
              ;; If we are root on the local host, we can do it directly.
              (tramp-set-file-uid-gid localname uid gid)
-           (let ((uid (or (and (integerp uid) uid)
+           (let ((uid (or (and (natnump uid) uid)
                           (tramp-get-remote-uid v 'integer)))
-                 (gid (or (and (integerp gid) gid)
+                 (gid (or (and (natnump gid) gid)
                           (tramp-get-remote-gid v 'integer))))
              (tramp-send-command
               v (format
@@ -1474,8 +1477,8 @@ and gid of the corresponding user is taken.  Both parameters must be integers."
 
       ;; We handle also the local part, because there doesn't exist
       ;; `set-file-uid-gid'.  On W32 "chown" might not work.
-      (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
-           (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
+      (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
+           (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
        (tramp-compat-call-process
         "chown" nil nil nil
          (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
@@ -1529,10 +1532,49 @@ and gid of the corresponding user is taken.  Both parameters must be integers."
                        (if (stringp (nth 3 context))
                            (format "--range=%s" (nth 3 context)) "")
                        (tramp-shell-quote-argument localname))))
-       (tramp-set-file-property v localname "file-selinux-context" context)
-      (tramp-set-file-property v localname "file-selinux-context" 'undef)))
-  ;; We always return nil.
-  nil)
+       (progn
+         (tramp-set-file-property v localname "file-selinux-context" context)
+         t)
+      (tramp-set-file-property v localname "file-selinux-context" 'undef)
+      nil)))
+
+(defun tramp-remote-acl-p (vec)
+  "Check, whether ACL is enabled on the remote host."
+  (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
+    (tramp-send-command-and-check vec "getfacl /")))
+
+(defun tramp-sh-handle-file-acl (filename)
+  "Like `file-acl' for Tramp files."
+  (with-parsed-tramp-file-name filename nil
+    (with-tramp-file-property v localname "file-acl"
+      (when (and (tramp-remote-acl-p v)
+                (tramp-send-command-and-check
+                 v (format
+                    "getfacl -ac %s 2>/dev/null"
+                    (tramp-shell-quote-argument localname))))
+       (with-current-buffer (tramp-get-connection-buffer v)
+         (goto-char (point-max))
+         (delete-blank-lines)
+         (when (> (point-max) (point-min))
+           (tramp-compat-funcall
+            'substring-no-properties (buffer-string))))))))
+
+(defun tramp-sh-handle-set-file-acl (filename acl-string)
+  "Like `set-file-acl' for Tramp files."
+  (with-parsed-tramp-file-name (expand-file-name filename) nil
+    (if (and (stringp acl-string) (tramp-remote-acl-p v)
+            (progn
+              (tramp-send-command
+               v (format "setfacl --set-file=- %s <<'EOF'\n%s\nEOF\n"
+                         (tramp-shell-quote-argument localname) acl-string))
+              (tramp-send-command-and-check v nil)))
+       ;; Success.
+       (progn
+         (tramp-set-file-property v localname "file-acl" acl-string)
+         t)
+      ;; In case of errors, we return `nil'.
+      (tramp-set-file-property v localname "file-acl-string" 'undef)
+      nil)))
 
 ;; Simple functions using the `test' command.
 
@@ -1618,7 +1660,7 @@ and gid of the corresponding user is taken.  Both parameters must be integers."
        (and (tramp-run-test "-d" (file-name-directory filename))
             (tramp-run-test "-w" (file-name-directory filename)))))))
 
-(defun tramp-sh-handle-file-ownership-preserved-p (filename)
+(defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
   "Like `file-ownership-preserved-p' for Tramp files."
   (with-parsed-tramp-file-name filename nil
     (with-tramp-file-property v localname "file-ownership-preserved-p"
@@ -1626,7 +1668,10 @@ and gid of the corresponding user is taken.  Both parameters must be integers."
        ;; Return t if the file doesn't exist, since it's true that no
        ;; information would be lost by an (attempted) delete and create.
        (or (null attributes)
-           (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
+           (and
+            (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
+            (or (not group)
+                (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
 
 ;; Directory listings.
 
@@ -1882,7 +1927,7 @@ tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
 
 (defun tramp-sh-handle-copy-file
   (filename newname &optional ok-if-already-exists keep-date
-           preserve-uid-gid preserve-selinux-context)
+           preserve-uid-gid preserve-extended-attributes)
   "Like `copy-file' for Tramp files."
   (setq filename (expand-file-name filename))
   (setq newname (expand-file-name newname))
@@ -1892,13 +1937,13 @@ tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
        (tramp-tramp-file-p newname))
     (tramp-do-copy-or-rename-file
      'copy filename newname ok-if-already-exists keep-date
-     preserve-uid-gid preserve-selinux-context))
+     preserve-uid-gid preserve-extended-attributes))
    ;; Compat section.
-   (preserve-selinux-context
+   (preserve-extended-attributes
     (tramp-run-real-handler
      'copy-file
      (list filename newname ok-if-already-exists keep-date
-          preserve-uid-gid preserve-selinux-context)))
+          preserve-uid-gid preserve-extended-attributes)))
    (preserve-uid-gid
     (tramp-run-real-handler
      'copy-file
@@ -1961,7 +2006,7 @@ tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
 
 (defun tramp-do-copy-or-rename-file
   (op filename newname &optional ok-if-already-exists keep-date
-      preserve-uid-gid preserve-selinux-context)
+      preserve-uid-gid preserve-extended-attributes)
   "Copy or rename a remote file.
 OP must be `copy' or `rename' and indicates the operation to perform.
 FILENAME specifies the file to copy or rename, NEWNAME is the name of
@@ -1970,7 +2015,7 @@ OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
 KEEP-DATE means to make sure that NEWNAME has the same timestamp
 as FILENAME.  PRESERVE-UID-GID, when non-nil, instructs to keep
 the uid and gid if both files are on the same host.
-PRESERVE-SELINUX-CONTEXT activates selinux commands.
+PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
 
 This function is invoked by `tramp-sh-handle-copy-file' and
 `tramp-sh-handle-rename-file'.  It is an error if OP is neither
@@ -1980,8 +2025,9 @@ file names."
     (error "Unknown operation `%s', must be `copy' or `rename'" op))
   (let ((t1 (tramp-tramp-file-p filename))
        (t2 (tramp-tramp-file-p newname))
-       (context (and preserve-selinux-context
-                     (apply 'file-selinux-context (list filename))))
+       (length (nth 7 (file-attributes (file-truename filename))))
+       (attributes (and preserve-extended-attributes
+                        (apply 'file-extended-attributes (list filename))))
        pr tm)
 
     (with-parsed-tramp-file-name (if t1 filename newname) nil
@@ -2009,8 +2055,9 @@ file names."
                 ok-if-already-exists keep-date preserve-uid-gid))
 
               ;; Try out-of-band operation.
-              ((tramp-method-out-of-band-p
-                v1 (nth 7 (file-attributes (file-truename filename))))
+              ((and
+                (tramp-method-out-of-band-p v1 length)
+                (tramp-method-out-of-band-p v2 length))
                (tramp-do-copy-or-rename-file-out-of-band
                 op filename newname keep-date))
 
@@ -2038,8 +2085,7 @@ file names."
 
           ;; If the Tramp file has an out-of-band method, the
           ;; corresponding copy-program can be invoked.
-          ((tramp-method-out-of-band-p
-            v (nth 7 (file-attributes (file-truename filename))))
+          ((tramp-method-out-of-band-p v length)
            (tramp-do-copy-or-rename-file-out-of-band
             op filename newname keep-date))
 
@@ -2051,8 +2097,11 @@ file names."
          ;; One of them must be a Tramp file.
          (error "Tramp implementation says this cannot happen")))
 
-       ;; Handle `preserve-selinux-context'.
-       (when context (apply 'set-file-selinux-context (list newname context)))
+       ;; Handle `preserve-extended-attributes'.  We ignore possible
+       ;; errors, because ACL strings could be incompatible.
+       (when attributes
+         (ignore-errors
+           (apply 'set-file-extended-attributes (list newname attributes))))
 
        ;; In case of `rename', we must flush the cache of the source file.
        (when (and t1 (eq op 'rename))
@@ -2380,17 +2429,38 @@ The method used must be an out-of-band method."
                ;; last longer than 60 secs.
                (let ((p (let ((default-directory
                                 (tramp-compat-temporary-file-directory)))
-                          (apply 'start-process
+                          (apply 'start-process-shell-command
                                  (tramp-get-connection-name v)
                                  (tramp-get-connection-buffer v)
                                  copy-program
-                                 (append copy-args (list source target))))))
+                                 (append
+                                  copy-args
+                                  (list
+                                   (shell-quote-argument source)
+                                   (shell-quote-argument target)
+                                   "&&" "echo" "tramp_exit_status" "0"
+                                   "||" "echo" "tramp_exit_status" "1"))))))
                  (tramp-message
                   orig-vec 6 "%s"
                   (mapconcat 'identity (process-command p) " "))
                  (tramp-compat-set-process-query-on-exit-flag p nil)
                  (tramp-process-actions
-                  p v nil tramp-actions-copy-out-of-band)))
+                  p v nil tramp-actions-copy-out-of-band)
+
+                 ;; Check the return code.
+                 (goto-char (point-max))
+                 (unless
+                     (re-search-backward "tramp_exit_status [0-9]+" nil t)
+                   (tramp-error
+                    orig-vec 'file-error
+                    "Couldn't find exit status of `%s'" (process-command p)))
+                 (skip-chars-forward "^ ")
+                 (unless (zerop (read (current-buffer)))
+                   (forward-line -1)
+                   (tramp-error
+                    orig-vec 'file-error
+                    "Error copying: `%s'"
+                    (buffer-substring (point-min) (point-at-eol))))))
 
            ;; Reset the transfer process properties.
            (tramp-message orig-vec 6 "\n%s" (buffer-string))
@@ -2755,6 +2825,8 @@ the result will be a local, non-Tramp, filename."
 
       (with-current-buffer (tramp-get-connection-buffer v)
        (unwind-protect
+           ;; We catch this event.  Otherwise, `start-process' could
+           ;; be called on the local host.
            (save-excursion
              (save-restriction
                ;; Activate narrowing in order to save BUFFER
@@ -2768,31 +2840,34 @@ the result will be a local, non-Tramp, filename."
                  (narrow-to-region (point-max) (point-max))
                  ;; We call `tramp-maybe-open-connection', in order
                  ;; to cleanup the prompt afterwards.
-                 (tramp-maybe-open-connection v)
-                 (widen)
-                 (delete-region mark (point))
-                 (narrow-to-region (point-max) (point-max))
-                 ;; Now do it.
-                 (if command
-                     ;; Send the command.
-                     (tramp-send-command v command nil t) ; nooutput
-                   ;; Check, whether a pty is associated.
-                   (unless (tramp-compat-process-get
-                            (tramp-get-connection-process v) 'remote-tty)
-                     (tramp-error
-                      v 'file-error
-                      "pty association is not supported for `%s'" name))))
-               (let ((p (tramp-get-connection-process v)))
-                 ;; Set query flag for this process.  We ignore errors,
-                 ;; because the process could have finished already.
-                 (ignore-errors
-                   (tramp-compat-set-process-query-on-exit-flag p t))
-                 ;; Return process.
-                 p)))
+                 (catch 'suppress
+                   (tramp-maybe-open-connection v)
+                   (widen)
+                   (delete-region mark (point))
+                   (narrow-to-region (point-max) (point-max))
+                   ;; Now do it.
+                   (if command
+                       ;; Send the command.
+                       (tramp-send-command v command nil t) ; nooutput
+                     ;; Check, whether a pty is associated.
+                     (unless (tramp-compat-process-get
+                              (tramp-get-connection-process v) 'remote-tty)
+                       (tramp-error
+                        v 'file-error
+                        "pty association is not supported for `%s'" name))))
+                 (let ((p (tramp-get-connection-process v)))
+                   ;; Set query flag and process marker for this
+                   ;; process.  We ignore errors, because the process
+                   ;; could have finished already.
+                   (ignore-errors
+                     (tramp-compat-set-process-query-on-exit-flag p t)
+                     (set-marker (process-mark p) (point)))
+                   ;; Return process.
+                   p))))
 
          ;; Save exit.
          (if (string-match tramp-temp-buffer-name (buffer-name))
-             (progn
+             (ignore-errors
                (set-process-buffer (tramp-get-connection-process v) nil)
                (kill-buffer (current-buffer)))
            (set-buffer-modified-p bmp))
@@ -2912,16 +2987,6 @@ the result will be a local, non-Tramp, filename."
          (keyboard-quit)
        ret))))
 
-(defun tramp-sh-handle-call-process-region
-  (start end program &optional delete buffer display &rest args)
-  "Like `call-process-region' for Tramp files."
-  (let ((tmpfile (tramp-compat-make-temp-file "")))
-    (write-region start end tmpfile)
-    (when delete (delete-region start end))
-    (unwind-protect
-       (apply 'call-process program tmpfile buffer display args)
-      (delete-file tmpfile))))
-
 (defun tramp-sh-handle-file-local-copy (filename)
   "Like `file-local-copy' for Tramp files."
   (with-parsed-tramp-file-name filename nil
@@ -4147,6 +4212,9 @@ Goes through the list `tramp-inline-compress-commands'."
        (tramp-message
         vec 2 "Couldn't find an inline transfer compress command")))))
 
+(defvar tramp-gw-tunnel-method)
+(defvar tramp-gw-socks-method)
+
 (defun tramp-compute-multi-hops (vec)
   "Expands VEC according to `tramp-default-proxies-alist'.
 Gateway hops are already opened."
@@ -4207,10 +4275,11 @@ Gateway hops are already opened."
            (setq choices tramp-default-proxies-alist)))))
 
     ;; Handle gateways.
-    (when (string-match
-          (format
-           "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
-          (tramp-file-name-method (car target-alist)))
+    (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
+              (string-match
+               (format
+                "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
+               (tramp-file-name-method (car target-alist))))
       (let ((gw (pop target-alist))
            (hop (pop target-alist)))
        ;; Is the method prepared for gateways?
@@ -4291,7 +4360,7 @@ connection if a previous connection has died for some reason."
                              (car tramp-current-connection)))
                  (> (tramp-time-diff
                      (current-time) (cdr tramp-current-connection))
-                    5))
+                    (or tramp-connection-min-time-diff 0)))
        (throw 'suppress 'suppress))
 
       ;; If too much time has passed since last command was sent, look
@@ -4605,7 +4674,7 @@ raises an error."
                  command (buffer-string))))))))
 
 (defun tramp-convert-file-attributes (vec attr)
-  "Convert file-attributes ATTR generated by perl script, stat or ls.
+  "Convert `file-attributes' ATTR generated by perl script, stat or ls.
 Convert file mode bits to string and set virtual device number.
 Return ATTR."
   (when attr
@@ -4613,6 +4682,17 @@ Return ATTR."
     (when (stringp (car attr))
       (while (string-match tramp-color-escape-sequence-regexp (car attr))
        (setcar attr (replace-match "" nil nil (car attr)))))
+    ;; Convert uid and gid.  Use -1 as indication of unusable value.
+    (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
+      (setcar (nthcdr 2 attr) -1))
+    (when (and (floatp (nth 2 attr))
+               (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
+      (setcar (nthcdr 2 attr) (round (nth 2 attr))))
+    (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
+      (setcar (nthcdr 3 attr) -1))
+    (when (and (floatp (nth 3 attr))
+               (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
+      (setcar (nthcdr 3 attr) (round (nth 3 attr))))
     ;; Convert last access time.
     (unless (listp (nth 4 attr))
       (setcar (nthcdr 4 attr)
@@ -5011,7 +5091,9 @@ This is used internally by `tramp-file-mode-from-int'."
   (if (equal id-format 'integer) (user-uid) (user-login-name)))
 
 (defun tramp-get-local-gid (id-format)
-  (nth 3 (tramp-compat-file-attributes "~/" id-format)))
+  (if (and (fboundp 'group-gid) (equal id-format 'integer))
+      (tramp-compat-funcall 'group-gid)
+    (nth 3 (tramp-compat-file-attributes "~/" id-format))))
 
 ;; Some predefined connection properties.
 (defun tramp-get-inline-compress (vec prop size)
@@ -5109,34 +5191,6 @@ function cell is returned to be applied on a buffer."
         (t
          (format "%s <%%s" coding)))))))
 
-;;; Integration of eshell.el:
-
-(eval-when-compile
-  (defvar eshell-path-env))
-
-;; eshell.el keeps the path in `eshell-path-env'.  We must change it
-;; when `default-directory' points to another host.
-(defun tramp-eshell-directory-change ()
-  "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
-  (setq eshell-path-env
-       (if (file-remote-p default-directory)
-           (with-parsed-tramp-file-name default-directory nil
-             (mapconcat
-              'identity
-              (tramp-get-remote-path v)
-              ":"))
-         (getenv "PATH"))))
-
-(eval-after-load "esh-util"
-  '(progn
-     (tramp-eshell-directory-change)
-     (add-hook 'eshell-directory-change-hook
-              'tramp-eshell-directory-change)
-     (add-hook 'tramp-unload-hook
-              (lambda ()
-                (remove-hook 'eshell-directory-change-hook
-                             'tramp-eshell-directory-change)))))
-
 (add-hook 'tramp-unload-hook
          (lambda ()
            (unload-feature 'tramp-sh 'force)))