X-Git-Url: https://git.hcoop.net/bpt/emacs.git/blobdiff_plain/b6cfbcd0fcd40fd41112477b62f1d819ee3d2998..970ad972b936db89b62798a0abb2401c61f404df:/lisp/net/tramp-adb.el diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el index 13e17ed3cd..14fb8575ff 100644 --- a/lisp/net/tramp-adb.el +++ b/lisp/net/tramp-adb.el @@ -23,31 +23,33 @@ ;;; Commentary: -;; The Android Debug Bridge must be installed on your local machine. -;; Add the following form into your .emacs: +;; The Android Debug Bridge "adb" must be installed on your local +;; machine. If it is not in your $PATH, add the following form into +;; your .emacs: ;; -;; (setq tramp-adb-sdk-dir "/path/to/android/sdk") +;; (setq tramp-adb-program "/path/to/adb") ;; ;; Due to security it is not possible to access non-root devices. ;;; Code: (require 'tramp) +(require 'time-date) (defvar dired-move-to-filename-regexp) -(defcustom tramp-adb-sdk-dir "~/Android/sdk" - "Set to the directory containing the Android SDK." - :type 'string +(defcustom tramp-adb-program "adb" + "Name of the Android Debug Bridge program." + :group 'tramp :version "24.4" - :group 'tramp) + :type 'string) ;;;###tramp-autoload (defconst tramp-adb-method "adb" "*When this method name is used, forward all calls to Android Debug Bridge.") (defcustom tramp-adb-prompt - "^\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]" + "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]" "Regexp used as prompt in almquist shell." :type 'string :version "24.4" @@ -66,7 +68,12 @@ "[[:space:]]+\\(.*\\)$")) ; \6 filename ;;;###tramp-autoload -(add-to-list 'tramp-methods `(,tramp-adb-method)) +(add-to-list 'tramp-methods + `(,tramp-adb-method + (tramp-tmpdir "/data/local/tmp"))) + +;;;###tramp-autoload +(add-to-list 'tramp-default-host-alist `(,tramp-adb-method nil "")) ;;;###tramp-autoload (eval-after-load 'tramp @@ -106,7 +113,6 @@ (directory-files . tramp-handle-directory-files) (directory-files-and-attributes . tramp-adb-handle-directory-files-and-attributes) - (file-name-all-completions . tramp-sh-handle-file-name-all-completions) (make-directory . tramp-adb-handle-make-directory) (delete-directory . tramp-adb-handle-delete-directory) (delete-file . tramp-adb-handle-delete-file) @@ -118,7 +124,7 @@ (vc-registered . ignore) ;no vc control files on Android devices (write-region . tramp-adb-handle-write-region) (set-file-modes . tramp-adb-handle-set-file-modes) - (set-file-times . ignore) + (set-file-times . tramp-adb-handle-set-file-times) (copy-file . tramp-adb-handle-copy-file) (rename-file . tramp-adb-handle-rename-file) (process-file . tramp-adb-handle-process-file) @@ -144,17 +150,19 @@ pass to the OPERATION." (save-match-data (apply (cdr fn) args)) (tramp-run-real-handler operation args)))) -;; This cannot be a constant, because `tramp-adb-sdk-dir' is customizable. -(defun tramp-adb-program () - "The Android Debug Bridge." - (expand-file-name "platform-tools/adb" tramp-adb-sdk-dir)) - ;;;###tramp-autoload (defun tramp-adb-parse-device-names (ignore) "Return a list of (nil host) tuples allowed to access." - (with-temp-buffer - (when (zerop (call-process (tramp-adb-program) nil t nil "devices")) - (let (result) + (with-timeout (10) + (with-temp-buffer + ;; `call-process' does not react on timer under MS Windows. + ;; That's why we use `start-process'. + (let ((p (start-process + tramp-adb-program (current-buffer) tramp-adb-program "devices")) + result) + (tramp-compat-set-process-query-on-exit-flag p nil) + (while (eq 'run (process-status p)) + (sleep-for 0.1)) (goto-char (point-min)) (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t) (add-to-list 'result (list nil (match-string 1)))) @@ -306,7 +314,9 @@ pass to the OPERATION." (and is-symlink (cadr (split-string name "\\( -> \\|\n\\)"))))) (push (list - name + (if is-symlink + (car (split-string name "\\( -> \\|\n\\)")) + name) (or is-dir symlink-target) 1 ;link-count ;; no way to handle numeric ids in Androids ash @@ -365,16 +375,6 @@ pass to the OPERATION." "ls --color=never" "ls"))) -(defun tramp-adb-get-toolbox (vec) - "Get shell toolbox implementation: `toolbox' for orginal distributions -or `busybox' for CynagenMode based distributions" - (with-tramp-connection-property vec "toolbox" - (tramp-message vec 5 "Checking shell toolbox implementation") - (cond - ((zerop (tramp-adb-command-exit-status vec "busybox")) 'busybox) - ((zerop (tramp-adb-command-exit-status vec "toolbox")) 'toolbox) - (t 'unkown)))) - (defun tramp-adb--gnu-switches-to-ash (switches) "Almquist shell can't handle multiple arguments. @@ -459,7 +459,7 @@ Emacs dired can't find files." (setq time-a (apply 'encode-time (parse-time-string (match-string 0 a)))) (string-match tramp-adb-ls-date-regexp b) (setq time-b (apply 'encode-time (parse-time-string (match-string 0 b)))) - (time-less-p time-b time-a))) + (tramp-time-less-p time-b time-a))) (defun tramp-adb-ls-output-name-less-p (a b) "Sort \"ls\" output by name, ascending." @@ -610,6 +610,19 @@ But handle the case, if the \"test\" command is not available." v (format "chmod %s %s" (tramp-compat-decimal-to-octal mode) localname) "Error while changing file's mode %s" filename))) +(defun tramp-adb-handle-set-file-times (filename &optional time) + "Like `set-file-times' for Tramp files." + (with-parsed-tramp-file-name filename nil + (tramp-flush-file-property v localname) + (let ((time (if (or (null time) (equal time '(0 0))) + (current-time) + time))) + (tramp-adb-command-exit-status + ;; use shell arithmetic because of Emacs integer size limit + v (format "touch -t $(( %d * 65536 + %d )) %s" + (car time) (cadr time) + (tramp-shell-quote-argument localname)))))) + (defun tramp-adb-handle-copy-file (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid preserve-extended-attributes) @@ -619,7 +632,7 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." newname (expand-file-name newname)) (if (file-directory-p filename) - (copy-directory filename newname keep-date t) + (tramp-file-name-handler 'copy-directory filename newname keep-date t) (with-tramp-progress-reporter (tramp-dissect-file-name (if (file-remote-p filename) filename newname)) 0 (format "Copying %s to %s" filename newname) @@ -679,7 +692,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." (tramp-flush-file-property v localname) ;; Short track. (tramp-adb-barf-unless-okay - v (format "mv %s %s" (file-remote-p filename 'localname) localname) + v (format + "mv %s %s" + (tramp-file-name-handler 'file-remote-p filename 'localname) + localname) "Error renaming %s to %s" filename newname)) ;; Rename by copy. @@ -755,13 +771,11 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." ;; directory. (condition-case nil (progn - (setq ret 0 - ret - (tramp-adb-barf-unless-okay - v (format "(cd %s; %s)" - (tramp-shell-quote-argument localname) - command) - "")) + (setq ret 0) + (tramp-adb-barf-unless-okay + v (format "(cd %s; %s)" + (tramp-shell-quote-argument localname) command) + "") ;; We should show the output anyway. (when outbuf (with-current-buffer outbuf @@ -832,7 +846,7 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." (when p (if (yes-or-no-p "A command is running. Kill it? ") (ignore-errors (kill-process p)) - (error "Shell command in progress"))) + (tramp-compat-user-error "Shell command in progress"))) (if current-buffer-p (progn @@ -879,81 +893,89 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." (defun tramp-adb-handle-start-file-process (name buffer program &rest args) "Like `start-file-process' for Tramp files." (with-parsed-tramp-file-name default-directory nil - ;; When PROGRAM is nil, we just provide a tty. + ;; When PROGRAM is nil, we should provide a tty. This is not + ;; possible here. + (unless (stringp program) + (tramp-error v 'file-error "PROGRAM must be a string")) + (let ((command - (when (stringp program) - (format "cd %s; %s" - (tramp-shell-quote-argument localname) - (mapconcat 'tramp-shell-quote-argument - (cons program args) " ")))) + (format "cd %s; %s" + (tramp-shell-quote-argument localname) + (mapconcat 'tramp-shell-quote-argument + (cons program args) " "))) (tramp-process-connection-type (or (null program) tramp-process-connection-type)) (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer))) (name1 name) (i 0)) - (unwind-protect - (save-excursion - (save-restriction - (unless buffer - ;; BUFFER can be nil. We use a temporary buffer. - (setq buffer (generate-new-buffer tramp-temp-buffer-name))) - (while (get-process name1) - ;; NAME must be unique as process name. - (setq i (1+ i) - name1 (format "%s<%d>" name i))) - (setq name name1) - ;; Set the new process properties. - (tramp-set-connection-property v "process-name" name) - (tramp-set-connection-property v "process-buffer" buffer) - ;; Activate narrowing in order to save BUFFER contents. - ;; Clear also the modification time; otherwise we might - ;; be interrupted by `verify-visited-file-modtime'. - (with-current-buffer (tramp-get-connection-buffer v) - (let ((buffer-undo-list t)) + + (unless buffer + ;; BUFFER can be nil. We use a temporary buffer. + (setq buffer (generate-new-buffer tramp-temp-buffer-name))) + (while (get-process name1) + ;; NAME must be unique as process name. + (setq i (1+ i) + name1 (format "%s<%d>" name i))) + (setq name name1) + ;; Set the new process properties. + (tramp-set-connection-property v "process-name" name) + (tramp-set-connection-property v "process-buffer" buffer) + + (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 + ;; contents. Clear also the modification time; + ;; otherwise we might be interrupted by + ;; `verify-visited-file-modtime'. + (let ((buffer-undo-list t) + (buffer-read-only nil) + (mark (point))) (clear-visited-file-modtime) (narrow-to-region (point-max) (point-max)) - (if command - ;; Send the command. - (tramp-adb-send-command v command) - ;; Open the connection. - (tramp-adb-maybe-open-connection v)))) - (let ((p (tramp-get-connection-process v))) - ;; Set sentinel and query flag for this process. - (tramp-set-connection-property p "vector" v) - (set-process-sentinel p 'tramp-process-sentinel) - (tramp-compat-set-process-query-on-exit-flag p t) - ;; Return process. - p))) - ;; Save exit. - (with-current-buffer (tramp-get-connection-buffer v) + ;; We call `tramp-adb-maybe-open-connection', in + ;; order to cleanup the prompt afterwards. + (tramp-adb-maybe-open-connection v) + (widen) + (delete-region mark (point)) + (narrow-to-region (point-max) (point-max)) + ;; Send the command. + (let ((tramp-adb-prompt (regexp-quote command))) + (tramp-adb-send-command v command)) + (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))) - (tramp-set-connection-property v "process-name" nil) - (tramp-set-connection-property v "process-buffer" nil))))) + (set-buffer-modified-p bmp)) + (tramp-set-connection-property v "process-name" nil) + (tramp-set-connection-property v "process-buffer" nil)))))) ;; Helper functions. -(defun tramp-adb-file-name-host (vec) - "Return host component of VEC. -If it is equal to the default value of `tramp-default-host', `nil' is returned." - (let ((host (tramp-file-name-host vec))) - (unless (equal host (eval (car (get 'tramp-default-host 'standard-value)))) - host))) - (defun tramp-adb-execute-adb-command (vec &rest args) "Returns nil on success error-output on failure." - (when (tramp-adb-file-name-host vec) - (setq args (append (list "-s" (tramp-adb-file-name-host vec)) args))) + (when (> (length (tramp-file-name-host vec)) 0) + (setq args (append (list "-s" (tramp-file-name-host vec)) args))) (with-temp-buffer (prog1 - (unless (zerop (apply 'call-process (tramp-adb-program) nil t nil args)) + (unless + (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args)) (buffer-string)) - (tramp-message - vec 6 "%s %s\n%s" - (tramp-adb-program) (mapconcat 'identity args " ") (buffer-string))))) + (tramp-message vec 6 "%s" (buffer-string))))) (defun tramp-adb-find-test-command (vec) "Checks, whether the ash has a builtin \"test\" command. @@ -982,34 +1004,31 @@ This happens for Android >= 4.0." (while (re-search-forward "\r+$" nil t) (replace-match "" nil nil))))) -(defun tramp-adb-barf-unless-okay (vec command fmt &rest args) - "Run COMMAND, check exit status, throw error if exit status not okay. -FMT and ARGS are passed to `error'." - (tramp-adb-send-command vec (format "%s; echo tramp_exit_status $?" command)) - (with-current-buffer (tramp-get-connection-buffer vec) - (goto-char (point-max)) - (unless (re-search-backward "tramp_exit_status [0-9]+" nil t) - (tramp-error - vec 'file-error "Couldn't find exit status of `%s'" command)) - (skip-chars-forward "^ ") - (unless (zerop (read (current-buffer))) - (apply 'tramp-error vec 'file-error fmt args)) - (let (buffer-read-only) - (delete-region (match-beginning 0) (point-max))))) - (defun tramp-adb-command-exit-status (vec command) "Run COMMAND and return its exit status. Sends `echo $?' along with the COMMAND for checking the exit status. If COMMAND is nil, just sends `echo $?'. Returns the exit status found." - (tramp-adb-send-command vec (format "%s; echo tramp_exit_status $?" command)) + (tramp-adb-send-command + vec (if command + (format "%s; echo tramp_exit_status $?" command) + "echo tramp_exit_status $?")) (with-current-buffer (tramp-get-connection-buffer vec) (goto-char (point-max)) (unless (re-search-backward "tramp_exit_status [0-9]+" nil t) (tramp-error vec 'file-error "Couldn't find exit status of `%s'" command)) (skip-chars-forward "^ ") - (read (current-buffer)))) + (prog1 + (read (current-buffer)) + (let (buffer-read-only) + (delete-region (match-beginning 0) (point-max)))))) + +(defun tramp-adb-barf-unless-okay (vec command fmt &rest args) + "Run COMMAND, check exit status, throw error if exit status not okay. +FMT and ARGS are passed to `error'." + (unless (zerop (tramp-adb-command-exit-status vec command)) + (apply 'tramp-error vec 'file-error fmt args))) (defun tramp-adb-wait-for-output (proc &optional timeout) "Wait for output from remote command." @@ -1047,40 +1066,45 @@ Does not do anything if a connection is already open, but re-opens the connection if a previous connection has died for some reason." (let* ((buf (tramp-get-connection-buffer vec)) (p (get-buffer-process buf)) + (host (tramp-file-name-host vec)) + (user (tramp-file-name-user vec)) (devices (mapcar 'cadr (tramp-adb-parse-device-names nil)))) + + ;; Maybe we know already that "su" is not supported. We cannot + ;; use a connection property, because we have not checked yet + ;; whether it is still the same device. + (when (and user (not (tramp-get-file-property vec "" "su-command-p" t))) + (tramp-error vec 'file-error "Cannot switch to user `%s'" user)) + (unless (and p (processp p) (memq (process-status p) '(run open))) (save-match-data (when (and p (processp p)) (delete-process p)) (if (not devices) (tramp-error vec 'file-error "No device connected")) - (if (and (tramp-adb-file-name-host vec) - (not (member (tramp-adb-file-name-host vec) devices))) - (tramp-error - vec 'file-error - "Device %s not connected" (tramp-adb-file-name-host vec))) - (if (and (not (eq (length devices) 1)) - (not (tramp-adb-file-name-host vec))) + (if (and (> (length host) 0) (not (member host devices))) + (tramp-error vec 'file-error "Device %s not connected" host)) + (if (and (> (length devices) 1) (zerop (length host))) (tramp-error vec 'file-error "Multiple Devices connected: No Host/Device specified")) (with-tramp-progress-reporter vec 3 "Opening adb shell connection" (let* ((coding-system-for-read 'utf-8-dos) ;is this correct? (process-connection-type tramp-process-connection-type) - (args (if (tramp-adb-file-name-host vec) - (list "-s" (tramp-adb-file-name-host vec) "shell") + (args (if (> (length host) 0) + (list "-s" host "shell") (list "shell"))) (p (let ((default-directory (tramp-compat-temporary-file-directory))) (apply 'start-process (tramp-get-connection-name vec) buf - (tramp-adb-program) args)))) + tramp-adb-program args)))) (tramp-message vec 6 "%s" (mapconcat 'identity (process-command p) " ")) ;; Wait for initial prompt. - (tramp-adb-wait-for-output p) + (tramp-adb-wait-for-output p 30) (unless (eq 'run (process-status p)) (tramp-error vec 'file-error "Terminated!")) - (set-process-query-on-exit-flag p nil) + (tramp-compat-set-process-query-on-exit-flag p nil) ;; Check whether the properties have been changed. If ;; yes, this is a strong indication that we must expire all @@ -1106,6 +1130,15 @@ connection if a previous connection has died for some reason." old-getprop new-getprop) (tramp-adb-maybe-open-connection vec))) + ;; Change user if indicated. + (when user + (tramp-adb-send-command vec (format "su %s" user)) + (unless (zerop (tramp-adb-command-exit-status vec nil)) + (delete-process p) + (tramp-set-file-property vec "" "su-command-p" nil) + (tramp-error + vec 'file-error "Cannot switch to user `%s'" user))) + ;; Set "remote-path" connection property. This is needed ;; for eshell. (tramp-adb-send-command vec "echo \\\"$PATH\\\"")