LCOV - code coverage report
Current view: top level - lisp/net - tramp-adb.el (source / functions) Hit Total Coverage
Test: tramp-tests.info Lines: 6 763 0.8 %
Date: 2017-08-27 09:44:50 Functions: 2 44 4.5 %

          Line data    Source code
       1             : ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp  -*- lexical-binding:t -*-
       2             : 
       3             : ;; Copyright (C) 2011-2017 Free Software Foundation, Inc.
       4             : 
       5             : ;; Author: Jürgen Hötzel <juergen@archlinux.org>
       6             : ;; Keywords: comm, processes
       7             : ;; Package: tramp
       8             : 
       9             : ;; This file is part of GNU Emacs.
      10             : 
      11             : ;; GNU Emacs is free software: you can redistribute it and/or modify
      12             : ;; it under the terms of the GNU General Public License as published by
      13             : ;; the Free Software Foundation, either version 3 of the License, or
      14             : ;; (at your option) any later version.
      15             : 
      16             : ;; GNU Emacs is distributed in the hope that it will be useful,
      17             : ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
      18             : ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      19             : ;; GNU General Public License for more details.
      20             : 
      21             : ;; You should have received a copy of the GNU General Public License
      22             : ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
      23             : 
      24             : ;;; Commentary:
      25             : 
      26             : ;; The Android Debug Bridge "adb" must be installed on your local
      27             : ;; machine.  If it is not in your $PATH, add the following form into
      28             : ;; your .emacs:
      29             : ;;
      30             : ;;   (setq tramp-adb-program "/path/to/adb")
      31             : ;;
      32             : ;; Due to security it is not possible to access non-root devices.
      33             : 
      34             : ;;; Code:
      35             : 
      36             : (require 'tramp)
      37             : 
      38             : ;;;###tramp-autoload
      39             : (defcustom tramp-adb-program "adb"
      40             :   "Name of the Android Debug Bridge program."
      41             :   :group 'tramp
      42             :   :version "24.4"
      43             :   :type 'string
      44             :   :require 'tramp)
      45             : 
      46             : ;;;###tramp-autoload
      47             : (defcustom tramp-adb-connect-if-not-connected nil
      48             :   "Try to run `adb connect' if provided device is not connected currently.
      49             : It is used for TCP/IP devices."
      50             :   :group 'tramp
      51             :   :version "25.1"
      52             :   :type 'boolean
      53             :   :require 'tramp)
      54             : 
      55             : ;;;###tramp-autoload
      56             : (defconst tramp-adb-method "adb"
      57             :   "When this method name is used, forward all calls to Android Debug Bridge.")
      58             : 
      59             : ;;;###tramp-autoload
      60             : (defcustom tramp-adb-prompt
      61             :   "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]\e;[]*@?[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
      62             :   "Regexp used as prompt in almquist shell."
      63             :   :type 'string
      64             :   :version "24.4"
      65             :   :group 'tramp
      66             :   :require 'tramp)
      67             : 
      68             : (defconst tramp-adb-ls-date-regexp
      69             :   "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]"
      70             :   "Regexp for date format in ls output.")
      71             : 
      72             : (defconst tramp-adb-ls-toolbox-regexp
      73             :   (concat
      74             :    "^[[:space:]]*\\([-[:alpha:]]+\\)"         ; \1 permissions
      75             :    "\\(?:[[:space:]]+[[:digit:]]+\\)?"        ; links (Android 7/toybox)
      76             :    "[[:space:]]*\\([^[:space:]]+\\)"  ; \2 username
      77             :    "[[:space:]]+\\([^[:space:]]+\\)"  ; \3 group
      78             :    "[[:space:]]+\\([[:digit:]]+\\)"   ; \4 size
      79             :    "[[:space:]]+\\([-[:digit:]]+[[:space:]][:[:digit:]]+\\)" ; \5 date
      80             :    "[[:space:]]\\(.*\\)$")            ; \6 filename
      81             :   "Regexp for ls output.")
      82             : 
      83             : ;;;###tramp-autoload
      84             : (add-to-list 'tramp-methods
      85             :              `(,tramp-adb-method
      86             :                (tramp-tmpdir "/data/local/tmp")
      87             :                (tramp-default-port 5555)))
      88             : 
      89             : ;;;###tramp-autoload
      90             : (add-to-list 'tramp-default-host-alist `(,tramp-adb-method nil ""))
      91             : 
      92             : ;;;###tramp-autoload
      93             : (eval-after-load 'tramp
      94             :   '(tramp-set-completion-function
      95             :     tramp-adb-method '((tramp-adb-parse-device-names ""))))
      96             : 
      97             : ;;;###tramp-autoload
      98             : (defconst tramp-adb-file-name-handler-alist
      99             :   '((access-file . ignore)
     100             :     (add-name-to-file . tramp-adb-handle-copy-file)
     101             :     ;; `byte-compiler-base-file-name' performed by default handler.
     102             :     ;; `copy-directory' performed by default handler.
     103             :     (copy-file . tramp-adb-handle-copy-file)
     104             :     (delete-directory . tramp-adb-handle-delete-directory)
     105             :     (delete-file . tramp-adb-handle-delete-file)
     106             :     ;; `diff-latest-backup-file' performed by default handler.
     107             :     (directory-file-name . tramp-handle-directory-file-name)
     108             :     (directory-files . tramp-handle-directory-files)
     109             :     (directory-files-and-attributes
     110             :      . tramp-adb-handle-directory-files-and-attributes)
     111             :     (dired-compress-file . ignore)
     112             :     (dired-uncache . tramp-handle-dired-uncache)
     113             :     (expand-file-name . tramp-adb-handle-expand-file-name)
     114             :     (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
     115             :     (file-acl . ignore)
     116             :     (file-attributes . tramp-adb-handle-file-attributes)
     117             :     (file-directory-p . tramp-adb-handle-file-directory-p)
     118             :     (file-equal-p . tramp-handle-file-equal-p)
     119             :     ;; FIXME: This is too sloppy.
     120             :     (file-executable-p . tramp-handle-file-exists-p)
     121             :     (file-exists-p . tramp-handle-file-exists-p)
     122             :     (file-in-directory-p . tramp-handle-file-in-directory-p)
     123             :     (file-local-copy . tramp-adb-handle-file-local-copy)
     124             :     (file-modes . tramp-handle-file-modes)
     125             :     (file-name-all-completions . tramp-adb-handle-file-name-all-completions)
     126             :     (file-name-as-directory . tramp-handle-file-name-as-directory)
     127             :     (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
     128             :     (file-name-completion . tramp-handle-file-name-completion)
     129             :     (file-name-directory . tramp-handle-file-name-directory)
     130             :     (file-name-nondirectory . tramp-handle-file-name-nondirectory)
     131             :     ;; `file-name-sans-versions' performed by default handler.
     132             :     (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
     133             :     (file-notify-add-watch . tramp-handle-file-notify-add-watch)
     134             :     (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
     135             :     (file-notify-valid-p . tramp-handle-file-notify-valid-p)
     136             :     (file-ownership-preserved-p . ignore)
     137             :     (file-readable-p . tramp-handle-file-exists-p)
     138             :     (file-regular-p . tramp-handle-file-regular-p)
     139             :     (file-remote-p . tramp-handle-file-remote-p)
     140             :     (file-selinux-context . ignore)
     141             :     (file-symlink-p . tramp-handle-file-symlink-p)
     142             :     (file-truename . tramp-adb-handle-file-truename)
     143             :     (file-writable-p . tramp-adb-handle-file-writable-p)
     144             :     (find-backup-file-name . tramp-handle-find-backup-file-name)
     145             :     ;; `find-file-noselect' performed by default handler.
     146             :     ;; `get-file-buffer' performed by default handler.
     147             :     (insert-directory . tramp-handle-insert-directory)
     148             :     (insert-file-contents . tramp-handle-insert-file-contents)
     149             :     (load . tramp-handle-load)
     150             :     (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
     151             :     (make-directory . tramp-adb-handle-make-directory)
     152             :     (make-directory-internal . ignore)
     153             :     (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
     154             :     (make-symbolic-link . tramp-handle-make-symbolic-link)
     155             :     (process-file . tramp-adb-handle-process-file)
     156             :     (rename-file . tramp-adb-handle-rename-file)
     157             :     (set-file-acl . ignore)
     158             :     (set-file-modes . tramp-adb-handle-set-file-modes)
     159             :     (set-file-selinux-context . ignore)
     160             :     (set-file-times . tramp-adb-handle-set-file-times)
     161             :     (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
     162             :     (shell-command . tramp-adb-handle-shell-command)
     163             :     (start-file-process . tramp-adb-handle-start-file-process)
     164             :     (substitute-in-file-name . tramp-handle-substitute-in-file-name)
     165             :     (temporary-file-directory . tramp-handle-temporary-file-directory)
     166             :     (unhandled-file-name-directory . ignore)
     167             :     (vc-registered . ignore)
     168             :     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     169             :     (write-region . tramp-adb-handle-write-region))
     170             :   "Alist of handler functions for Tramp ADB method.")
     171             : 
     172             : ;; It must be a `defsubst' in order to push the whole code into
     173             : ;; tramp-loaddefs.el.  Otherwise, there would be recursive autoloading.
     174             : ;;;###tramp-autoload
     175             : (defsubst tramp-adb-file-name-p (filename)
     176             :   "Check if it's a filename for ADB."
     177       44919 :   (let ((v (tramp-dissect-file-name filename)))
     178       44919 :     (string= (tramp-file-name-method v) tramp-adb-method)))
     179             : 
     180             : ;;;###tramp-autoload
     181             : (defun tramp-adb-file-name-handler (operation &rest args)
     182             :   "Invoke the ADB handler for OPERATION.
     183             : First arg specifies the OPERATION, second arg is a list of arguments to
     184             : pass to the OPERATION."
     185           1 :   (let ((fn (assoc operation tramp-adb-file-name-handler-alist)))
     186           1 :     (if fn
     187           1 :         (save-match-data (apply (cdr fn) args))
     188           1 :       (tramp-run-real-handler operation args))))
     189             : 
     190             : ;;;###tramp-autoload
     191             : (tramp-register-foreign-file-name-handler
     192             :  'tramp-adb-file-name-p 'tramp-adb-file-name-handler)
     193             : 
     194             : ;;;###tramp-autoload
     195             : (defun tramp-adb-parse-device-names (_ignore)
     196             :   "Return a list of (nil host) tuples allowed to access."
     197           0 :   (with-timeout (10)
     198           0 :     (with-temp-buffer
     199             :       ;; `call-process' does not react on timer under MS Windows.
     200             :       ;; That's why we use `start-process'.
     201           0 :       (let ((p (start-process
     202           0 :                 tramp-adb-program (current-buffer) tramp-adb-program "devices"))
     203           0 :             (v (make-tramp-file-name
     204           0 :                 :method tramp-adb-method :user tramp-current-user
     205           0 :                 :host tramp-current-host))
     206             :             result)
     207           0 :         (tramp-message v 6 "%s" (mapconcat 'identity (process-command p) " "))
     208           0 :         (process-put p 'adjust-window-size-function 'ignore)
     209           0 :         (set-process-query-on-exit-flag p nil)
     210           0 :         (while (process-live-p p)
     211           0 :           (accept-process-output p 0.1))
     212           0 :         (accept-process-output p 0.1)
     213           0 :         (tramp-message v 6 "\n%s" (buffer-string))
     214           0 :         (goto-char (point-min))
     215           0 :         (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t)
     216           0 :           (push (list nil (match-string 1)) result))
     217             : 
     218             :         ;; Replace ":" by "#".
     219           0 :         (mapc
     220             :          (lambda (elt)
     221           0 :            (setcar
     222           0 :             (cdr elt)
     223           0 :             (replace-regexp-in-string
     224           0 :              ":" tramp-prefix-port-format (car (cdr elt)))))
     225           0 :          result)
     226           0 :         result))))
     227             : 
     228             : (defun tramp-adb-handle-expand-file-name (name &optional dir)
     229             :   "Like `expand-file-name' for Tramp files."
     230             :   ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
     231           0 :   (setq dir (or dir default-directory "/"))
     232             :   ;; Unless NAME is absolute, concat DIR and NAME.
     233           0 :   (unless (file-name-absolute-p name)
     234           0 :     (setq name (concat (file-name-as-directory dir) name)))
     235             :   ;; If NAME is not a Tramp file, run the real handler.
     236           0 :   (if (not (tramp-tramp-file-p name))
     237           0 :       (tramp-run-real-handler 'expand-file-name (list name nil))
     238             :     ;; Dissect NAME.
     239           0 :     (with-parsed-tramp-file-name name nil
     240           0 :       (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
     241           0 :         (setq localname (concat "/" localname)))
     242             :       ;; Do normal `expand-file-name' (this does "/./" and "/../").
     243             :       ;; `default-directory' is bound, because on Windows there would
     244             :       ;; be problems with UNC shares or Cygwin mounts.
     245           0 :       (let ((default-directory (tramp-compat-temporary-file-directory)))
     246           0 :         (tramp-make-tramp-file-name
     247           0 :          method user domain host port
     248           0 :          (tramp-drop-volume-letter
     249           0 :           (tramp-run-real-handler
     250           0 :            'expand-file-name (list localname))))))))
     251             : 
     252             : (defun tramp-adb-handle-file-directory-p (filename)
     253             :   "Like `file-directory-p' for Tramp files."
     254           0 :   (eq (tramp-compat-file-attribute-type
     255           0 :        (file-attributes (file-truename filename)))
     256           0 :       t))
     257             : 
     258             : ;; This is derived from `tramp-sh-handle-file-truename'.  Maybe the
     259             : ;; code could be shared?
     260             : (defun tramp-adb-handle-file-truename (filename)
     261             :   "Like `file-truename' for Tramp files."
     262           0 :   (format
     263             :    "%s%s"
     264           0 :    (with-parsed-tramp-file-name (expand-file-name filename) nil
     265           0 :      (tramp-make-tramp-file-name
     266           0 :       method user domain host port
     267           0 :       (with-tramp-file-property v localname "file-truename"
     268           0 :         (let ((result nil))                     ; result steps in reverse order
     269           0 :           (tramp-message v 4 "Finding true name for `%s'" filename)
     270           0 :           (let* ((steps (split-string localname "/" 'omit))
     271           0 :                  (localnamedir (tramp-run-real-handler
     272           0 :                                 'file-name-as-directory (list localname)))
     273           0 :                  (is-dir (string= localname localnamedir))
     274             :                  (thisstep nil)
     275             :                  (numchase 0)
     276             :                  ;; Don't make the following value larger than
     277             :                  ;; necessary.  People expect an error message in a
     278             :                  ;; timely fashion when something is wrong; otherwise
     279             :                  ;; they might think that Emacs is hung.  Of course,
     280             :                  ;; correctness has to come first.
     281             :                  (numchase-limit 20)
     282             :                  symlink-target)
     283           0 :             (while (and steps (< numchase numchase-limit))
     284           0 :               (setq thisstep (pop steps))
     285           0 :               (tramp-message
     286           0 :                v 5 "Check %s"
     287           0 :                (mapconcat 'identity
     288           0 :                           (append '("") (reverse result) (list thisstep))
     289           0 :                           "/"))
     290           0 :               (setq symlink-target
     291           0 :                     (tramp-compat-file-attribute-type
     292           0 :                      (file-attributes
     293           0 :                       (tramp-make-tramp-file-name
     294           0 :                        method user domain host port
     295           0 :                        (mapconcat 'identity
     296           0 :                                   (append '("")
     297           0 :                                           (reverse result)
     298           0 :                                           (list thisstep))
     299           0 :                                   "/")))))
     300           0 :               (cond ((string= "." thisstep)
     301           0 :                      (tramp-message v 5 "Ignoring step `.'"))
     302           0 :                     ((string= ".." thisstep)
     303           0 :                      (tramp-message v 5 "Processing step `..'")
     304           0 :                      (pop result))
     305           0 :                     ((stringp symlink-target)
     306             :                      ;; It's a symlink, follow it.
     307           0 :                      (tramp-message v 5 "Follow symlink to %s" symlink-target)
     308           0 :                      (setq numchase (1+ numchase))
     309           0 :                      (when (file-name-absolute-p symlink-target)
     310           0 :                        (setq result nil))
     311             :                      ;; If the symlink was absolute, we'll get a string
     312             :                      ;; like "/user@host:/some/target"; extract the
     313             :                      ;; "/some/target" part from it.
     314           0 :                      (when (tramp-tramp-file-p symlink-target)
     315           0 :                        (unless (tramp-equal-remote filename symlink-target)
     316           0 :                          (tramp-error
     317           0 :                           v 'file-error
     318           0 :                           "Symlink target `%s' on wrong host" symlink-target))
     319           0 :                        (setq symlink-target localname))
     320           0 :                      (setq steps
     321           0 :                            (append (split-string symlink-target "/" 'omit)
     322           0 :                                    steps)))
     323             :                     (t
     324             :                      ;; It's a file.
     325           0 :                      (setq result (cons thisstep result)))))
     326           0 :             (when (>= numchase numchase-limit)
     327           0 :               (tramp-error
     328           0 :                v 'file-error
     329           0 :                "Maximum number (%d) of symlinks exceeded" numchase-limit))
     330           0 :             (setq result (reverse result))
     331             :             ;; Combine list to form string.
     332           0 :             (setq result
     333           0 :                   (if result
     334           0 :                       (mapconcat 'identity (cons "" result) "/")
     335           0 :                     "/"))
     336           0 :             (when (and is-dir (or (string= "" result)
     337           0 :                                   (not (string= (substring result -1) "/"))))
     338           0 :               (setq result (concat result "/"))))
     339             : 
     340           0 :           (tramp-message v 4 "True name of `%s' is `%s'" localname result)
     341           0 :           result))))
     342             : 
     343             :    ;; Preserve trailing "/".
     344           0 :    (if (string-equal (file-name-nondirectory filename) "") "/" "")))
     345             : 
     346             : (defun tramp-adb-handle-file-attributes (filename &optional id-format)
     347             :   "Like `file-attributes' for Tramp files."
     348           0 :   (unless id-format (setq id-format 'integer))
     349           0 :   (ignore-errors
     350           0 :     (with-parsed-tramp-file-name filename nil
     351           0 :       (with-tramp-file-property
     352           0 :           v localname (format "file-attributes-%s" id-format)
     353           0 :         (and
     354           0 :          (tramp-adb-send-command-and-check
     355           0 :           v (format "%s -d -l %s"
     356           0 :                     (tramp-adb-get-ls-command v)
     357           0 :                     (tramp-shell-quote-argument localname)))
     358           0 :          (with-current-buffer (tramp-get-buffer v)
     359           0 :            (tramp-adb-sh-fix-ls-output)
     360           0 :            (cdar (tramp-do-parse-file-attributes-with-ls v id-format))))))))
     361             : 
     362             : (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format)
     363             :   "Parse `file-attributes' for Tramp files using the ls(1) command."
     364           0 :   (with-current-buffer (tramp-get-buffer vec)
     365           0 :     (goto-char (point-min))
     366           0 :     (let ((file-properties nil))
     367           0 :       (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t)
     368           0 :         (let* ((mod-string (match-string 1))
     369           0 :                (is-dir (eq ?d (aref mod-string 0)))
     370           0 :                (is-symlink (eq ?l (aref mod-string 0)))
     371           0 :                (uid (match-string 2))
     372           0 :                (gid (match-string 3))
     373           0 :                (size (string-to-number (match-string 4)))
     374           0 :                (date (match-string 5))
     375           0 :                (name (match-string 6))
     376             :                (symlink-target
     377           0 :                 (and is-symlink
     378           0 :                      (cadr (split-string name "\\( -> \\|\n\\)")))))
     379           0 :           (push (list
     380           0 :                  (if is-symlink
     381           0 :                      (car (split-string name "\\( -> \\|\n\\)"))
     382           0 :                    name)
     383           0 :                  (or is-dir symlink-target)
     384             :                  1     ;link-count
     385             :                  ;; no way to handle numeric ids in Androids ash
     386           0 :                  (if (eq id-format 'integer) 0 uid)
     387           0 :                  (if (eq id-format 'integer) 0 gid)
     388             :                  '(0 0)                 ; atime
     389           0 :                  (date-to-time date)    ; mtime
     390             :                  '(0 0)                 ; ctime
     391           0 :                  size
     392           0 :                  mod-string
     393             :                  ;; fake
     394             :                  t 1
     395           0 :                  (tramp-get-device vec))
     396           0 :                 file-properties)))
     397           0 :       file-properties)))
     398             : 
     399             : (defun tramp-adb-handle-directory-files-and-attributes
     400             :   (directory &optional full match nosort id-format)
     401             :   "Like `directory-files-and-attributes' for Tramp files."
     402           0 :   (when (file-directory-p directory)
     403           0 :     (with-parsed-tramp-file-name (expand-file-name directory) nil
     404           0 :       (copy-tree
     405           0 :        (with-tramp-file-property
     406           0 :            v localname (format "directory-files-and-attributes-%s-%s-%s-%s"
     407           0 :                                full match id-format nosort)
     408           0 :          (with-current-buffer (tramp-get-buffer v)
     409           0 :            (when (tramp-adb-send-command-and-check
     410           0 :                   v (format "%s -a -l %s"
     411           0 :                             (tramp-adb-get-ls-command v)
     412           0 :                             (tramp-shell-quote-argument localname)))
     413             :              ;; We insert also filename/. and filename/.., because "ls" doesn't.
     414             :              ;; Looks like it does include them in toybox, since Android 6.
     415           0 :              (unless (re-search-backward "\\.$" nil t)
     416           0 :                (narrow-to-region (point-max) (point-max))
     417           0 :                (tramp-adb-send-command
     418           0 :                 v (format "%s -d -a -l %s %s"
     419           0 :                           (tramp-adb-get-ls-command v)
     420           0 :                           (tramp-shell-quote-argument
     421           0 :                            (concat (file-name-as-directory localname) "."))
     422           0 :                           (tramp-shell-quote-argument
     423           0 :                            (concat (file-name-as-directory localname) ".."))))
     424           0 :                (widen)))
     425           0 :            (tramp-adb-sh-fix-ls-output)
     426           0 :            (let ((result (tramp-do-parse-file-attributes-with-ls
     427           0 :                           v (or id-format 'integer))))
     428           0 :              (when full
     429           0 :                (setq result
     430           0 :                      (mapcar
     431             :                       (lambda (x)
     432           0 :                         (cons (expand-file-name (car x) directory) (cdr x)))
     433           0 :                       result)))
     434           0 :              (unless nosort
     435           0 :                (setq result
     436           0 :                      (sort result (lambda (x y) (string< (car x) (car y))))))
     437           0 :              (delq nil
     438           0 :                    (mapcar (lambda (x)
     439           0 :                              (if (or (not match) (string-match match (car x)))
     440           0 :                                  x))
     441           0 :                            result)))))))))
     442             : 
     443             : (defun tramp-adb-get-ls-command (vec)
     444             :   "Determine `ls' command at its arguments."
     445           0 :   (with-tramp-connection-property vec "ls"
     446           0 :     (tramp-message vec 5 "Finding a suitable `ls' command")
     447           0 :     (cond
     448             :      ;; Can't disable coloring explicitly for toybox ls command.  We
     449             :      ;; must force "ls" to print just one column.
     450           0 :      ((tramp-adb-send-command-and-check vec "toybox") "env COLUMNS=1 ls")
     451             :      ;; On CyanogenMod based system BusyBox is used and "ls" output
     452             :      ;; coloring is enabled by default.  So we try to disable it when
     453             :      ;; possible.
     454           0 :      ((tramp-adb-send-command-and-check vec "ls --color=never -al /dev/null")
     455             :       "ls --color=never")
     456           0 :      (t "ls"))))
     457             : 
     458             : (defun tramp-adb--gnu-switches-to-ash (switches)
     459             :   "Almquist shell can't handle multiple arguments.
     460             : Convert (\"-al\") to (\"-a\" \"-l\").  Remove arguments like \"--dired\"."
     461           0 :   (split-string
     462           0 :    (apply 'concat
     463           0 :           (mapcar (lambda (s)
     464           0 :                     (replace-regexp-in-string
     465           0 :                      "\\(.\\)"  " -\\1" (replace-regexp-in-string "^-" "" s)))
     466             :                   ;; FIXME: Warning about removed switches (long and non-dash).
     467           0 :                   (delq nil
     468           0 :                         (mapcar
     469             :                          (lambda (s)
     470           0 :                            (and (not (string-match "\\(^--\\|^[^-]\\)" s)) s))
     471           0 :                          switches))))))
     472             : 
     473             : (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time)
     474             :   "Insert dummy 0 in empty size columns.
     475             : Androids \"ls\" command doesn't insert size column for directories:
     476             : Emacs dired can't find files."
     477           0 :   (save-excursion
     478             :     ;; Insert missing size.
     479           0 :     (goto-char (point-min))
     480           0 :     (while
     481           0 :         (search-forward-regexp
     482           0 :          "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t)
     483           0 :       (replace-match "0\\1" "\\1" nil)
     484             :       ;; Insert missing "/".
     485           0 :       (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
     486           0 :         (end-of-line)
     487           0 :         (insert "/")))
     488             :     ;; Sort entries.
     489           0 :     (let* ((lines (split-string (buffer-string) "\n" t))
     490             :            (sorted-lines
     491           0 :             (sort
     492           0 :              lines
     493           0 :              (if sort-by-time
     494             :                  'tramp-adb-ls-output-time-less-p
     495           0 :                'tramp-adb-ls-output-name-less-p))))
     496           0 :       (delete-region (point-min) (point-max))
     497           0 :       (insert "  " (mapconcat 'identity sorted-lines "\n  ")))
     498             :     ;; Add final newline.
     499           0 :     (goto-char (point-max))
     500           0 :     (unless (bolp) (insert "\n"))))
     501             : 
     502             : (defun tramp-adb-ls-output-time-less-p (a b)
     503             :   "Sort \"ls\" output by time, descending."
     504           0 :   (let (time-a time-b)
     505           0 :     (string-match tramp-adb-ls-date-regexp a)
     506           0 :     (setq time-a (apply 'encode-time (parse-time-string (match-string 0 a))))
     507           0 :     (string-match tramp-adb-ls-date-regexp b)
     508           0 :     (setq time-b (apply 'encode-time (parse-time-string (match-string 0 b))))
     509           0 :     (time-less-p time-b time-a)))
     510             : 
     511             : (defun tramp-adb-ls-output-name-less-p (a b)
     512             :   "Sort \"ls\" output by name, ascending."
     513           0 :   (if (string-match directory-listing-before-filename-regexp a)
     514           0 :       (let ((posa (match-end 0)))
     515           0 :         (if (string-match directory-listing-before-filename-regexp b)
     516           0 :             (let ((posb (match-end 0)))
     517           0 :               (string-lessp (substring a posa) (substring b posb)))))))
     518             : 
     519             : (defun tramp-adb-handle-make-directory (dir &optional parents)
     520             :   "Like `make-directory' for Tramp files."
     521           0 :   (setq dir (expand-file-name dir))
     522           0 :   (with-parsed-tramp-file-name dir nil
     523           0 :     (when parents
     524           0 :       (let ((par (expand-file-name ".." dir)))
     525           0 :         (unless (file-directory-p par)
     526           0 :           (make-directory par parents))))
     527           0 :     (tramp-adb-barf-unless-okay
     528           0 :      v (format "mkdir %s" (tramp-shell-quote-argument localname))
     529           0 :      "Couldn't make directory %s" dir)
     530           0 :     (tramp-flush-file-property v (file-name-directory localname))
     531           0 :     (tramp-flush-directory-property v localname)))
     532             : 
     533             : (defun tramp-adb-handle-delete-directory (directory &optional recursive _trash)
     534             :   "Like `delete-directory' for Tramp files."
     535           0 :   (setq directory (expand-file-name directory))
     536           0 :   (with-parsed-tramp-file-name (file-truename directory) nil
     537           0 :     (tramp-flush-file-property v (file-name-directory localname))
     538           0 :     (tramp-flush-directory-property v localname))
     539           0 :   (with-parsed-tramp-file-name directory nil
     540           0 :     (tramp-flush-file-property v (file-name-directory localname))
     541           0 :     (tramp-flush-directory-property v localname)
     542           0 :     (tramp-adb-barf-unless-okay
     543           0 :      v (format "%s %s"
     544           0 :                (if recursive "rm -r" "rmdir")
     545           0 :                (tramp-shell-quote-argument localname))
     546           0 :      "Couldn't delete %s" directory)))
     547             : 
     548             : (defun tramp-adb-handle-delete-file (filename &optional _trash)
     549             :   "Like `delete-file' for Tramp files."
     550           0 :   (setq filename (expand-file-name filename))
     551           0 :   (with-parsed-tramp-file-name filename nil
     552           0 :     (tramp-flush-file-property v (file-name-directory localname))
     553           0 :     (tramp-flush-file-property v localname)
     554           0 :     (tramp-adb-barf-unless-okay
     555           0 :      v (format "rm %s" (tramp-shell-quote-argument localname))
     556           0 :      "Couldn't delete %s" filename)))
     557             : 
     558             : (defun tramp-adb-handle-file-name-all-completions (filename directory)
     559             :   "Like `file-name-all-completions' for Tramp files."
     560           0 :   (all-completions
     561           0 :    filename
     562           0 :    (with-parsed-tramp-file-name (expand-file-name directory) nil
     563           0 :      (with-tramp-file-property v localname "file-name-all-completions"
     564           0 :        (save-match-data
     565           0 :          (tramp-adb-send-command
     566           0 :           v (format "%s -a %s"
     567           0 :                     (tramp-adb-get-ls-command v)
     568           0 :                     (tramp-shell-quote-argument localname)))
     569           0 :          (mapcar
     570             :           (lambda (f)
     571           0 :             (if (file-directory-p (expand-file-name f directory))
     572           0 :                 (file-name-as-directory f)
     573           0 :               f))
     574           0 :           (with-current-buffer (tramp-get-buffer v)
     575           0 :             (delete-dups
     576           0 :              (append
     577             :               ;; In older Android versions, "." and ".." are not
     578             :               ;; included.  In newer versions (toybox, since Android
     579             :               ;; 6) they are.  We fix this by `delete-dups'.
     580             :               '("." "..")
     581           0 :               (delq
     582             :                nil
     583           0 :                (mapcar
     584           0 :                 (lambda (l) (and (not (string-match  "^[[:space:]]*$" l)) l))
     585           0 :                 (split-string (buffer-string) "\n"))))))))))))
     586             : 
     587             : (defun tramp-adb-handle-file-local-copy (filename)
     588             :   "Like `file-local-copy' for Tramp files."
     589           0 :   (with-parsed-tramp-file-name filename nil
     590           0 :     (unless (file-exists-p (file-truename filename))
     591           0 :       (tramp-error
     592           0 :        v tramp-file-missing
     593           0 :        "Cannot make local copy of non-existing file `%s'" filename))
     594           0 :     (let ((tmpfile (tramp-compat-make-temp-file filename)))
     595           0 :       (with-tramp-progress-reporter
     596           0 :           v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
     597             :         ;; "adb pull ..." does not always return an error code.
     598           0 :         (when (or (tramp-adb-execute-adb-command
     599           0 :                    v "pull" (tramp-compat-file-name-unquote localname) tmpfile)
     600           0 :                   (not (file-exists-p tmpfile)))
     601           0 :           (ignore-errors (delete-file tmpfile))
     602           0 :           (tramp-error
     603           0 :            v 'file-error "Cannot make local copy of file `%s'" filename))
     604           0 :         (set-file-modes
     605           0 :          tmpfile
     606           0 :          (logior (or (file-modes filename) 0) (string-to-number "0400" 8))))
     607           0 :       tmpfile)))
     608             : 
     609             : (defun tramp-adb-handle-file-writable-p (filename)
     610             :   "Like `tramp-sh-handle-file-writable-p'.
     611             : But handle the case, if the \"test\" command is not available."
     612           0 :   (with-parsed-tramp-file-name filename nil
     613           0 :     (with-tramp-file-property v localname "file-writable-p"
     614           0 :       (if (tramp-adb-find-test-command v)
     615           0 :           (if (file-exists-p filename)
     616           0 :               (tramp-adb-send-command-and-check
     617           0 :                v (format "test -w %s" (tramp-shell-quote-argument localname)))
     618           0 :             (and
     619           0 :              (file-directory-p (file-name-directory filename))
     620           0 :              (file-writable-p (file-name-directory filename))))
     621             : 
     622             :         ;; Missing "test" command on Android < 4.
     623           0 :        (let ((rw-path "/data/data"))
     624           0 :          (tramp-message
     625           0 :           v 5
     626             :           "Not implemented yet (assuming \"/data/data\" is writable): %s"
     627           0 :           localname)
     628           0 :          (and (>= (length localname) (length rw-path))
     629           0 :               (string= (substring localname 0 (length rw-path))
     630           0 :                        rw-path)))))))
     631             : 
     632             : (defun tramp-adb-handle-write-region
     633             :   (start end filename &optional append visit lockname mustbenew)
     634             :   "Like `write-region' for Tramp files."
     635           0 :   (setq filename (expand-file-name filename))
     636           0 :   (with-parsed-tramp-file-name filename nil
     637           0 :     (when (and mustbenew (file-exists-p filename)
     638           0 :                (or (eq mustbenew 'excl)
     639           0 :                    (not
     640           0 :                     (y-or-n-p
     641           0 :                      (format "File %s exists; overwrite anyway? " filename)))))
     642           0 :       (tramp-error v 'file-already-exists filename))
     643             : 
     644             :     ;; We must also flush the cache of the directory, because
     645             :     ;; `file-attributes' reads the values from there.
     646           0 :     (tramp-flush-file-property v (file-name-directory localname))
     647           0 :     (tramp-flush-file-property v localname)
     648           0 :     (let* ((curbuf (current-buffer))
     649           0 :            (tmpfile (tramp-compat-make-temp-file filename)))
     650           0 :       (when (and append (file-exists-p filename))
     651           0 :         (copy-file filename tmpfile 'ok)
     652           0 :         (set-file-modes
     653           0 :          tmpfile
     654           0 :          (logior (or (file-modes tmpfile) 0) (string-to-number "0600" 8))))
     655           0 :       (tramp-run-real-handler
     656           0 :        'write-region (list start end tmpfile append 'no-message lockname))
     657           0 :       (with-tramp-progress-reporter
     658           0 :         v 3 (format-message
     659           0 :              "Moving tmp file `%s' to `%s'" tmpfile filename)
     660           0 :         (unwind-protect
     661           0 :             (when (tramp-adb-execute-adb-command
     662           0 :                    v "push" tmpfile (tramp-compat-file-name-unquote localname))
     663           0 :               (tramp-error v 'file-error "Cannot write: `%s'" filename))
     664           0 :           (delete-file tmpfile)))
     665             : 
     666           0 :       (when (or (eq visit t) (stringp visit))
     667           0 :         (set-visited-file-modtime))
     668             : 
     669           0 :       (unless (equal curbuf (current-buffer))
     670           0 :         (tramp-error
     671           0 :          v 'file-error
     672           0 :          "Buffer has changed from `%s' to `%s'" curbuf (current-buffer))))))
     673             : 
     674             : (defun tramp-adb-handle-set-file-modes (filename mode)
     675             :   "Like `set-file-modes' for Tramp files."
     676           0 :   (with-parsed-tramp-file-name filename nil
     677           0 :     (tramp-flush-file-property v (file-name-directory localname))
     678           0 :     (tramp-flush-file-property v localname)
     679           0 :     (tramp-adb-send-command-and-check v (format "chmod %o %s" mode localname))))
     680             : 
     681             : (defun tramp-adb-handle-set-file-times (filename &optional time)
     682             :   "Like `set-file-times' for Tramp files."
     683           0 :   (with-parsed-tramp-file-name filename nil
     684           0 :     (tramp-flush-file-property v (file-name-directory localname))
     685           0 :     (tramp-flush-file-property v localname)
     686           0 :     (let ((time (if (or (null time) (equal time '(0 0)))
     687           0 :                     (current-time)
     688           0 :                   time)))
     689           0 :       (tramp-adb-send-command-and-check
     690             :        ;; Use shell arithmetic because of Emacs integer size limit.
     691           0 :        v (format "touch -t $(( %d * 65536 + %d )) %s"
     692           0 :                  (car time) (cadr time)
     693           0 :                  (tramp-shell-quote-argument localname))))))
     694             : 
     695             : (defun tramp-adb-handle-copy-file
     696             :   (filename newname &optional ok-if-already-exists keep-date
     697             :    _preserve-uid-gid _preserve-extended-attributes)
     698             :   "Like `copy-file' for Tramp files.
     699             : PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
     700           0 :   (setq filename (expand-file-name filename)
     701           0 :         newname (expand-file-name newname))
     702             : 
     703           0 :   (if (file-directory-p filename)
     704           0 :       (copy-directory filename newname keep-date t)
     705             : 
     706           0 :     (let ((t1 (tramp-tramp-file-p filename))
     707           0 :           (t2 (tramp-tramp-file-p newname)))
     708           0 :       (with-parsed-tramp-file-name (if t1 filename newname) nil
     709           0 :         (with-tramp-progress-reporter
     710           0 :             v 0 (format "Copying %s to %s" filename newname)
     711             : 
     712           0 :           (if (and t1 t2 (tramp-equal-remote filename newname))
     713           0 :               (let ((l1 (file-remote-p filename 'localname))
     714           0 :                     (l2 (file-remote-p newname 'localname)))
     715           0 :                 (when (and (not ok-if-already-exists)
     716           0 :                            (file-exists-p newname))
     717           0 :                   (tramp-error v 'file-already-exists newname))
     718             :                 ;; We must also flush the cache of the directory,
     719             :                 ;; because `file-attributes' reads the values from
     720             :                 ;; there.
     721           0 :                 (tramp-flush-file-property v (file-name-directory l2))
     722           0 :                 (tramp-flush-file-property v l2)
     723             :                 ;; Short track.
     724           0 :                 (tramp-adb-barf-unless-okay
     725           0 :                  v (format
     726             :                     "cp -f %s %s"
     727           0 :                     (tramp-shell-quote-argument l1)
     728           0 :                     (tramp-shell-quote-argument l2))
     729           0 :                  "Error copying %s to %s" filename newname))
     730             : 
     731           0 :             (let ((tmpfile (file-local-copy filename)))
     732             : 
     733           0 :               (if tmpfile
     734             :                   ;; Remote filename.
     735           0 :                   (condition-case err
     736           0 :                       (rename-file tmpfile newname ok-if-already-exists)
     737             :                     ((error quit)
     738           0 :                      (delete-file tmpfile)
     739           0 :                      (signal (car err) (cdr err))))
     740             : 
     741             :                 ;; Remote newname.
     742           0 :                 (when (file-directory-p newname)
     743           0 :                   (setq newname
     744           0 :                         (expand-file-name
     745           0 :                          (file-name-nondirectory filename) newname)))
     746             : 
     747           0 :                 (with-parsed-tramp-file-name newname nil
     748           0 :                   (when (and (not ok-if-already-exists)
     749           0 :                              (file-exists-p newname))
     750           0 :                     (tramp-error v 'file-already-exists newname))
     751             : 
     752             :                   ;; We must also flush the cache of the directory,
     753             :                   ;; because `file-attributes' reads the values from
     754             :                   ;; there.
     755           0 :                   (tramp-flush-file-property v (file-name-directory localname))
     756           0 :                   (tramp-flush-file-property v localname)
     757           0 :                   (when (tramp-adb-execute-adb-command
     758           0 :                          v "push"
     759           0 :                          (tramp-compat-file-name-unquote filename)
     760           0 :                          (tramp-compat-file-name-unquote localname))
     761           0 :                     (tramp-error
     762           0 :                      v 'file-error
     763           0 :                      "Cannot copy `%s' `%s'" filename newname)))))))))
     764             : 
     765             :     ;; KEEP-DATE handling.
     766           0 :     (when keep-date
     767           0 :       (set-file-times
     768           0 :        newname
     769           0 :        (tramp-compat-file-attribute-modification-time
     770           0 :         (file-attributes filename))))))
     771             : 
     772             : (defun tramp-adb-handle-rename-file
     773             :   (filename newname &optional ok-if-already-exists)
     774             :   "Like `rename-file' for Tramp files."
     775           0 :   (setq filename (expand-file-name filename)
     776           0 :         newname (expand-file-name newname))
     777             : 
     778           0 :   (let ((t1 (tramp-tramp-file-p filename))
     779           0 :         (t2 (tramp-tramp-file-p newname)))
     780           0 :     (with-parsed-tramp-file-name (if t1 filename newname) nil
     781           0 :       (with-tramp-progress-reporter
     782           0 :           v 0 (format "Renaming %s to %s" filename newname)
     783             : 
     784           0 :         (if (and t1 t2
     785           0 :                  (tramp-equal-remote filename newname)
     786           0 :                  (not (file-directory-p filename)))
     787           0 :             (let ((l1 (file-remote-p filename 'localname))
     788           0 :                   (l2 (file-remote-p newname 'localname)))
     789           0 :               (when (and (not ok-if-already-exists)
     790           0 :                          (file-exists-p newname))
     791           0 :                 (tramp-error v 'file-already-exists newname))
     792             :               ;; We must also flush the cache of the directory, because
     793             :               ;; `file-attributes' reads the values from there.
     794           0 :               (tramp-flush-file-property v (file-name-directory l1))
     795           0 :               (tramp-flush-file-property v l1)
     796           0 :               (tramp-flush-file-property v (file-name-directory l2))
     797           0 :               (tramp-flush-file-property v l2)
     798             :               ;; Short track.
     799           0 :               (tramp-adb-barf-unless-okay
     800           0 :                v (format
     801             :                   "mv -f %s %s"
     802           0 :                   (tramp-shell-quote-argument l1)
     803           0 :                   (tramp-shell-quote-argument l2))
     804           0 :                "Error renaming %s to %s" filename newname))
     805             : 
     806             :           ;; Rename by copy.
     807           0 :           (copy-file
     808           0 :            filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
     809           0 :           (delete-file filename))))))
     810             : 
     811             : (defun tramp-adb-handle-process-file
     812             :   (program &optional infile destination display &rest args)
     813             :   "Like `process-file' for Tramp files."
     814             :   ;; The implementation is not complete yet.
     815           0 :   (when (and (numberp destination) (zerop destination))
     816           0 :     (error "Implementation does not handle immediate return"))
     817             : 
     818           0 :   (with-parsed-tramp-file-name default-directory nil
     819           0 :     (let (command input tmpinput stderr tmpstderr outbuf ret)
     820             :       ;; Compute command.
     821           0 :       (setq command (mapconcat 'tramp-shell-quote-argument
     822           0 :                                (cons program args) " "))
     823             :       ;; Determine input.
     824           0 :       (if (null infile)
     825           0 :           (setq input "/dev/null")
     826           0 :         (setq infile (expand-file-name infile))
     827           0 :         (if (tramp-equal-remote default-directory infile)
     828             :             ;; INFILE is on the same remote host.
     829           0 :             (setq input (with-parsed-tramp-file-name infile nil localname))
     830             :           ;; INFILE must be copied to remote host.
     831           0 :           (setq input (tramp-make-tramp-temp-file v)
     832           0 :                 tmpinput (tramp-make-tramp-file-name
     833           0 :                           method user domain host port input))
     834           0 :           (copy-file infile tmpinput t)))
     835           0 :       (when input (setq command (format "%s <%s" command input)))
     836             : 
     837             :       ;; Determine output.
     838           0 :       (cond
     839             :        ;; Just a buffer.
     840           0 :        ((bufferp destination)
     841           0 :         (setq outbuf destination))
     842             :        ;; A buffer name.
     843           0 :        ((stringp destination)
     844           0 :         (setq outbuf (get-buffer-create destination)))
     845             :        ;; (REAL-DESTINATION ERROR-DESTINATION)
     846           0 :        ((consp destination)
     847             :         ;; output.
     848           0 :         (cond
     849           0 :          ((bufferp (car destination))
     850           0 :           (setq outbuf (car destination)))
     851           0 :          ((stringp (car destination))
     852           0 :           (setq outbuf (get-buffer-create (car destination))))
     853           0 :          ((car destination)
     854           0 :           (setq outbuf (current-buffer))))
     855             :         ;; stderr.
     856           0 :         (cond
     857           0 :          ((stringp (cadr destination))
     858           0 :           (setcar (cdr destination) (expand-file-name (cadr destination)))
     859           0 :           (if (tramp-equal-remote default-directory (cadr destination))
     860             :               ;; stderr is on the same remote host.
     861           0 :               (setq stderr (with-parsed-tramp-file-name
     862           0 :                                (cadr destination) nil localname))
     863             :             ;; stderr must be copied to remote host.  The temporary
     864             :             ;; file must be deleted after execution.
     865           0 :             (setq stderr (tramp-make-tramp-temp-file v)
     866           0 :                   tmpstderr (tramp-make-tramp-file-name
     867           0 :                              method user domain host port stderr))))
     868             :          ;; stderr to be discarded.
     869           0 :          ((null (cadr destination))
     870           0 :           (setq stderr "/dev/null"))))
     871             :        ;; 't
     872           0 :        (destination
     873           0 :         (setq outbuf (current-buffer))))
     874           0 :       (when stderr (setq command (format "%s 2>%s" command stderr)))
     875             : 
     876             :       ;; Send the command.  It might not return in time, so we protect
     877             :       ;; it.  Call it in a subshell, in order to preserve working
     878             :       ;; directory.
     879           0 :       (condition-case nil
     880           0 :           (progn
     881           0 :             (setq ret
     882           0 :                   (if (tramp-adb-send-command-and-check
     883           0 :                        v
     884           0 :                        (format "(cd %s; %s)"
     885           0 :                                (tramp-shell-quote-argument localname) command))
     886             :                       ;; Set return status accordingly.
     887           0 :                       0 1))
     888             :             ;; We should add the output anyway.
     889           0 :             (when outbuf
     890           0 :               (with-current-buffer outbuf
     891           0 :                 (insert-buffer-substring (tramp-get-connection-buffer v)))
     892           0 :               (when (and display (get-buffer-window outbuf t)) (redisplay))))
     893             :         ;; When the user did interrupt, we should do it also.  We use
     894             :         ;; return code -1 as marker.
     895             :         (quit
     896           0 :          (kill-buffer (tramp-get-connection-buffer v))
     897           0 :          (setq ret -1))
     898             :         ;; Handle errors.
     899             :         (error
     900           0 :          (kill-buffer (tramp-get-connection-buffer v))
     901           0 :          (setq ret 1)))
     902             : 
     903             :       ;; Provide error file.
     904           0 :       (when tmpstderr (rename-file tmpstderr (cadr destination) t))
     905             : 
     906             :       ;; Cleanup.  We remove all file cache values for the connection,
     907             :       ;; because the remote process could have changed them.
     908           0 :       (when tmpinput (delete-file tmpinput))
     909             : 
     910           0 :       (unless process-file-side-effects
     911           0 :         (tramp-flush-directory-property v ""))
     912             : 
     913             :       ;; Return exit status.
     914           0 :       (if (equal ret -1)
     915           0 :           (keyboard-quit)
     916           0 :         ret))))
     917             : 
     918             : (defun tramp-adb-handle-shell-command
     919             :   (command &optional output-buffer error-buffer)
     920             :   "Like `shell-command' for Tramp files."
     921           0 :   (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
     922             :          ;; We cannot use `shell-file-name' and `shell-command-switch',
     923             :          ;; they are variables of the local host.
     924           0 :          (args (list "sh" "-c" (substring command 0 asynchronous)))
     925             :          current-buffer-p
     926             :          (output-buffer
     927           0 :           (cond
     928           0 :            ((bufferp output-buffer) output-buffer)
     929           0 :            ((stringp output-buffer) (get-buffer-create output-buffer))
     930           0 :            (output-buffer
     931           0 :             (setq current-buffer-p t)
     932           0 :             (current-buffer))
     933           0 :            (t (get-buffer-create
     934           0 :                (if asynchronous
     935             :                    "*Async Shell Command*"
     936           0 :                  "*Shell Command Output*")))))
     937             :          (error-buffer
     938           0 :           (cond
     939           0 :            ((bufferp error-buffer) error-buffer)
     940           0 :            ((stringp error-buffer) (get-buffer-create error-buffer))))
     941             :          (buffer
     942           0 :           (if (and (not asynchronous) error-buffer)
     943           0 :               (with-parsed-tramp-file-name default-directory nil
     944           0 :                 (list output-buffer (tramp-make-tramp-temp-file v)))
     945           0 :             output-buffer))
     946           0 :          (p (get-buffer-process output-buffer)))
     947             : 
     948             :     ;; Check whether there is another process running.  Tramp does not
     949             :     ;; support 2 (asynchronous) processes in parallel.
     950           0 :     (when p
     951           0 :       (if (yes-or-no-p "A command is running.  Kill it? ")
     952           0 :           (ignore-errors (kill-process p))
     953           0 :         (tramp-compat-user-error p "Shell command in progress")))
     954             : 
     955           0 :     (if current-buffer-p
     956           0 :         (progn
     957           0 :           (barf-if-buffer-read-only)
     958           0 :           (push-mark nil t))
     959           0 :       (with-current-buffer output-buffer
     960           0 :         (setq buffer-read-only nil)
     961           0 :         (erase-buffer)))
     962             : 
     963           0 :     (if (and (not current-buffer-p) (integerp asynchronous))
     964           0 :         (prog1
     965             :             ;; Run the process.
     966           0 :             (apply 'start-file-process "*Async Shell*" buffer args)
     967             :           ;; Display output.
     968           0 :           (pop-to-buffer output-buffer)
     969           0 :           (setq mode-line-process '(":%s"))
     970           0 :           (shell-mode))
     971             : 
     972           0 :       (prog1
     973             :           ;; Run the process.
     974           0 :           (apply 'process-file (car args) nil buffer nil (cdr args))
     975             :         ;; Insert error messages if they were separated.
     976           0 :         (when (listp buffer)
     977           0 :           (with-current-buffer error-buffer
     978           0 :             (insert-file-contents (cadr buffer)))
     979           0 :           (delete-file (cadr buffer)))
     980           0 :         (if current-buffer-p
     981             :             ;; This is like exchange-point-and-mark, but doesn't
     982             :             ;; activate the mark.  It is cleaner to avoid activation,
     983             :             ;; even though the command loop would deactivate the mark
     984             :             ;; because we inserted text.
     985           0 :             (goto-char (prog1 (mark t)
     986           0 :                          (set-marker (mark-marker) (point)
     987           0 :                                      (current-buffer))))
     988             :           ;; There's some output, display it.
     989           0 :           (when (with-current-buffer output-buffer (> (point-max) (point-min)))
     990           0 :             (display-message-or-buffer output-buffer)))))))
     991             : 
     992             : ;; We use BUFFER also as connection buffer during setup.  Because of
     993             : ;; this, its original contents must be saved, and restored once
     994             : ;; connection has been setup.
     995             : (defun tramp-adb-handle-start-file-process (name buffer program &rest args)
     996             :   "Like `start-file-process' for Tramp files."
     997           0 :   (with-parsed-tramp-file-name default-directory nil
     998             :     ;; When PROGRAM is nil, we should provide a tty.  This is not
     999             :     ;; possible here.
    1000           0 :     (unless (stringp program)
    1001           0 :       (tramp-error v 'file-error "PROGRAM must be a string"))
    1002             : 
    1003           0 :     (let* ((buffer
    1004           0 :             (if buffer
    1005           0 :                 (get-buffer-create buffer)
    1006             :               ;; BUFFER can be nil.  We use a temporary buffer.
    1007           0 :               (generate-new-buffer tramp-temp-buffer-name)))
    1008             :            (command
    1009           0 :             (format "cd %s; %s"
    1010           0 :                     (tramp-shell-quote-argument localname)
    1011           0 :                     (mapconcat 'tramp-shell-quote-argument
    1012           0 :                                (cons program args) " ")))
    1013             :            (tramp-process-connection-type
    1014           0 :             (or (null program) tramp-process-connection-type))
    1015           0 :            (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
    1016           0 :            (name1 name)
    1017             :            (i 0))
    1018             : 
    1019           0 :       (while (get-process name1)
    1020             :         ;; NAME must be unique as process name.
    1021           0 :         (setq i (1+ i)
    1022           0 :               name1 (format "%s<%d>" name i)))
    1023           0 :       (setq name name1)
    1024             :       ;; Set the new process properties.
    1025           0 :       (tramp-set-connection-property v "process-name" name)
    1026           0 :       (tramp-set-connection-property v "process-buffer" buffer)
    1027             : 
    1028           0 :       (with-current-buffer (tramp-get-connection-buffer v)
    1029           0 :         (unwind-protect
    1030             :             ;; We catch this event.  Otherwise, `start-process' could
    1031             :             ;; be called on the local host.
    1032           0 :             (save-excursion
    1033           0 :               (save-restriction
    1034             :                 ;; Activate narrowing in order to save BUFFER
    1035             :                 ;; contents.  Clear also the modification time;
    1036             :                 ;; otherwise we might be interrupted by
    1037             :                 ;; `verify-visited-file-modtime'.
    1038           0 :                 (let ((buffer-undo-list t)
    1039             :                       (buffer-read-only nil)
    1040           0 :                       (mark (point)))
    1041           0 :                   (clear-visited-file-modtime)
    1042           0 :                   (narrow-to-region (point-max) (point-max))
    1043             :                   ;; We call `tramp-adb-maybe-open-connection', in
    1044             :                   ;; order to cleanup the prompt afterwards.
    1045           0 :                   (tramp-adb-maybe-open-connection v)
    1046           0 :                   (widen)
    1047           0 :                   (delete-region mark (point))
    1048           0 :                   (narrow-to-region (point-max) (point-max))
    1049             :                   ;; Send the command.
    1050           0 :                   (let ((tramp-adb-prompt (regexp-quote command)))
    1051           0 :                     (tramp-adb-send-command v command))
    1052           0 :                   (let ((p (tramp-get-connection-process v)))
    1053             :                     ;; Set query flag and process marker for this
    1054             :                     ;; process.  We ignore errors, because the process
    1055             :                     ;; could have finished already.
    1056           0 :                     (ignore-errors
    1057           0 :                       (set-process-query-on-exit-flag p t)
    1058           0 :                       (set-marker (process-mark p) (point)))
    1059             :                     ;; Return process.
    1060           0 :                     p))))
    1061             : 
    1062             :           ;; Save exit.
    1063           0 :           (if (string-match tramp-temp-buffer-name (buffer-name))
    1064           0 :               (ignore-errors
    1065           0 :                 (set-process-buffer (tramp-get-connection-process v) nil)
    1066           0 :                 (kill-buffer (current-buffer)))
    1067           0 :             (set-buffer-modified-p bmp))
    1068           0 :           (tramp-set-connection-property v "process-name" nil)
    1069           0 :           (tramp-set-connection-property v "process-buffer" nil))))))
    1070             : 
    1071             : (defun tramp-adb-get-device (vec)
    1072             :   "Return full host name from VEC to be used in shell execution.
    1073             : E.g. a host name \"192.168.1.1#5555\" returns \"192.168.1.1:5555\"
    1074             :      a host name \"R38273882DE\" returns \"R38273882DE\"."
    1075             :   ;; Sometimes this is called before there is a connection process
    1076             :   ;; yet.  In order to work with the connection cache, we flush all
    1077             :   ;; unwanted entries first.
    1078           0 :   (tramp-flush-connection-property nil)
    1079           0 :   (with-tramp-connection-property (tramp-get-connection-process vec) "device"
    1080           0 :     (let* ((host (tramp-file-name-host vec))
    1081           0 :            (port (tramp-file-name-port-or-default vec))
    1082           0 :            (devices (mapcar 'cadr (tramp-adb-parse-device-names nil))))
    1083           0 :       (replace-regexp-in-string
    1084           0 :        tramp-prefix-port-format ":"
    1085           0 :        (cond ((member host devices) host)
    1086             :              ;; This is the case when the host is connected to the default port.
    1087           0 :              ((member (format "%s%s%d" host tramp-prefix-port-format port)
    1088           0 :                       devices)
    1089           0 :               (format "%s:%d" host port))
    1090             :              ;; An empty host name shall be mapped as well, when there
    1091             :              ;; is exactly one entry in `devices'.
    1092           0 :              ((and (zerop (length host)) (= (length devices) 1))
    1093           0 :               (car devices))
    1094             :              ;; Try to connect device.
    1095           0 :              ((and tramp-adb-connect-if-not-connected
    1096           0 :                    (not (zerop (length host)))
    1097           0 :                    (not (tramp-adb-execute-adb-command
    1098           0 :                          vec "connect"
    1099           0 :                          (replace-regexp-in-string
    1100           0 :                           tramp-prefix-port-format ":" host))))
    1101             :               ;; When new device connected, running other adb command (e.g.
    1102             :               ;; adb shell) immediately will fail.  To get around this
    1103             :               ;; problem, add sleep 0.1 second here.
    1104           0 :               (sleep-for 0.1)
    1105           0 :               host)
    1106           0 :              (t (tramp-error
    1107           0 :                  vec 'file-error "Could not find device %s" host)))))))
    1108             : 
    1109             : (defun tramp-adb-execute-adb-command (vec &rest args)
    1110             :   "Returns nil on success error-output on failure."
    1111           0 :   (when (and (> (length (tramp-file-name-host vec)) 0)
    1112             :              ;; The -s switch is only available for ADB device commands.
    1113           0 :              (not (member (car args) '("connect" "disconnect"))))
    1114           0 :     (setq args (append (list "-s" (tramp-adb-get-device vec)) args)))
    1115           0 :   (with-temp-buffer
    1116           0 :     (prog1
    1117           0 :         (unless
    1118           0 :             (zerop
    1119           0 :              (apply 'tramp-call-process vec tramp-adb-program nil t nil args))
    1120           0 :           (buffer-string))
    1121           0 :       (tramp-message vec 6 "%s" (buffer-string)))))
    1122             : 
    1123             : (defun tramp-adb-find-test-command (vec)
    1124             :   "Checks, whether the ash has a builtin \"test\" command.
    1125             : This happens for Android >= 4.0."
    1126           0 :   (with-tramp-connection-property vec "test"
    1127           0 :     (tramp-adb-send-command-and-check vec "type test")))
    1128             : 
    1129             : ;; Connection functions
    1130             : 
    1131             : (defun tramp-adb-send-command (vec command)
    1132             :   "Send the COMMAND to connection VEC."
    1133           0 :   (tramp-adb-maybe-open-connection vec)
    1134           0 :   (tramp-message vec 6 "%s" command)
    1135           0 :   (tramp-send-string vec command)
    1136             :   ;; fixme: Race condition
    1137           0 :   (tramp-adb-wait-for-output (tramp-get-connection-process vec))
    1138           0 :   (with-current-buffer (tramp-get-connection-buffer vec)
    1139           0 :     (save-excursion
    1140           0 :       (goto-char (point-min))
    1141             :       ;; We can't use stty to disable echo of command.
    1142           0 :       (delete-matching-lines (regexp-quote command))
    1143             :       ;; When the local machine is W32, there are still trailing ^M.
    1144             :       ;; There must be a better solution by setting the correct coding
    1145             :       ;; system, but this requires changes in core Tramp.
    1146           0 :       (goto-char (point-min))
    1147           0 :       (while (re-search-forward "\r+$" nil t)
    1148           0 :         (replace-match "" nil nil)))))
    1149             : 
    1150             : (defun tramp-adb-send-command-and-check (vec command)
    1151             :   "Run COMMAND and check its exit status.
    1152             : Sends `echo $?' along with the COMMAND for checking the exit
    1153             : status.  If COMMAND is nil, just sends `echo $?'.  Returns nil if
    1154             : the exit status is not equal 0, and t otherwise."
    1155           0 :   (tramp-adb-send-command
    1156           0 :    vec (if command
    1157           0 :            (format "%s; echo tramp_exit_status $?" command)
    1158           0 :          "echo tramp_exit_status $?"))
    1159           0 :   (with-current-buffer (tramp-get-connection-buffer vec)
    1160           0 :     (goto-char (point-max))
    1161           0 :     (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
    1162           0 :       (tramp-error
    1163           0 :        vec 'file-error "Couldn't find exit status of `%s'" command))
    1164           0 :     (skip-chars-forward "^ ")
    1165           0 :     (prog1
    1166           0 :         (zerop (read (current-buffer)))
    1167           0 :       (let (buffer-read-only)
    1168           0 :         (delete-region (match-beginning 0) (point-max))))))
    1169             : 
    1170             : (defun tramp-adb-barf-unless-okay (vec command fmt &rest args)
    1171             :   "Run COMMAND, check exit status, throw error if exit status not okay.
    1172             : FMT and ARGS are passed to `error'."
    1173           0 :   (unless (tramp-adb-send-command-and-check vec command)
    1174           0 :     (apply 'tramp-error vec 'file-error fmt args)))
    1175             : 
    1176             : (defun tramp-adb-wait-for-output (proc &optional timeout)
    1177             :   "Wait for output from remote command."
    1178           0 :   (unless (buffer-live-p (process-buffer proc))
    1179           0 :     (delete-process proc)
    1180           0 :     (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
    1181           0 :   (with-current-buffer (process-buffer proc)
    1182           0 :     (if (tramp-wait-for-regexp
    1183           0 :          proc timeout
    1184           0 :          (tramp-get-connection-property proc "prompt" tramp-adb-prompt))
    1185           0 :         (let (buffer-read-only)
    1186           0 :           (goto-char (point-min))
    1187             :           ;; ADB terminal sends "^H" sequences.
    1188           0 :           (when (re-search-forward "<\b+" (point-at-eol) t)
    1189           0 :             (forward-line 1)
    1190           0 :             (delete-region (point-min) (point)))
    1191             :           ;; Delete the prompt.
    1192           0 :          (goto-char (point-min))
    1193           0 :          (when (re-search-forward
    1194           0 :                 (tramp-get-connection-property proc "prompt" tramp-adb-prompt)
    1195           0 :                 (point-at-eol) t)
    1196           0 :            (forward-line 1)
    1197           0 :            (delete-region (point-min) (point)))
    1198           0 :           (goto-char (point-max))
    1199           0 :           (re-search-backward
    1200           0 :            (tramp-get-connection-property proc "prompt" tramp-adb-prompt) nil t)
    1201           0 :           (delete-region (point) (point-max)))
    1202           0 :       (if timeout
    1203           0 :           (tramp-error
    1204           0 :            proc 'file-error
    1205             :            "[[Remote adb prompt `%s' not found in %d secs]]"
    1206           0 :            (tramp-get-connection-property proc "prompt" tramp-adb-prompt)
    1207           0 :            timeout)
    1208           0 :         (tramp-error
    1209           0 :          proc 'file-error
    1210             :          "[[Remote prompt `%s' not found]]"
    1211           0 :          (tramp-get-connection-property proc "prompt" tramp-adb-prompt))))))
    1212             : 
    1213             : (defun tramp-adb-maybe-open-connection (vec)
    1214             :   "Maybe open a connection VEC.
    1215             : Does not do anything if a connection is already open, but re-opens the
    1216             : connection if a previous connection has died for some reason."
    1217           0 :   (let* ((buf (tramp-get-connection-buffer vec))
    1218           0 :          (p (get-buffer-process buf))
    1219           0 :          (host (tramp-file-name-host vec))
    1220           0 :          (user (tramp-file-name-user vec))
    1221           0 :          (device (tramp-adb-get-device vec)))
    1222             : 
    1223             :     ;; Set variables for proper tracing in `tramp-adb-parse-device-names'.
    1224           0 :     (setq tramp-current-user   (tramp-file-name-user vec)
    1225           0 :           tramp-current-host   (tramp-file-name-host vec))
    1226             : 
    1227             :     ;; Maybe we know already that "su" is not supported.  We cannot
    1228             :     ;; use a connection property, because we have not checked yet
    1229             :     ;; whether it is still the same device.
    1230           0 :     (when (and user (not (tramp-get-file-property vec "" "su-command-p" t)))
    1231           0 :       (tramp-error vec 'file-error "Cannot switch to user `%s'" user))
    1232             : 
    1233           0 :     (unless (process-live-p p)
    1234           0 :       (save-match-data
    1235           0 :         (when (and p (processp p)) (delete-process p))
    1236           0 :         (if (zerop (length device))
    1237           0 :             (tramp-error vec 'file-error "Device %s not connected" host))
    1238           0 :         (with-tramp-progress-reporter vec 3 "Opening adb shell connection"
    1239           0 :           (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
    1240           0 :                  (process-connection-type tramp-process-connection-type)
    1241           0 :                  (args (if (> (length host) 0)
    1242           0 :                            (list "-s" device "shell")
    1243           0 :                          (list "shell")))
    1244           0 :                  (p (let ((default-directory
    1245           0 :                             (tramp-compat-temporary-file-directory)))
    1246           0 :                       (apply 'start-process (tramp-get-connection-name vec) buf
    1247           0 :                              tramp-adb-program args)))
    1248           0 :                  (prompt (md5 (concat (prin1-to-string process-environment)
    1249           0 :                                       (current-time-string)))))
    1250           0 :             (tramp-message
    1251           0 :              vec 6 "%s" (mapconcat 'identity (process-command p) " "))
    1252             :             ;; Wait for initial prompt.
    1253           0 :             (tramp-adb-wait-for-output p 30)
    1254           0 :             (unless (process-live-p p)
    1255           0 :               (tramp-error  vec 'file-error "Terminated!"))
    1256           0 :             (tramp-set-connection-property p "vector" vec)
    1257           0 :             (process-put p 'adjust-window-size-function 'ignore)
    1258           0 :             (set-process-query-on-exit-flag p nil)
    1259             : 
    1260             :             ;; Change prompt.
    1261           0 :             (tramp-set-connection-property
    1262           0 :              p "prompt" (regexp-quote (format "///%s#$" prompt)))
    1263           0 :             (tramp-adb-send-command
    1264           0 :              vec (format "PS1=\"///\"\"%s\"\"#$\"" prompt))
    1265             : 
    1266             :             ;; Check whether the properties have been changed.  If
    1267             :             ;; yes, this is a strong indication that we must expire all
    1268             :             ;; connection properties.  We start again.
    1269           0 :             (tramp-message vec 5 "Checking system information")
    1270           0 :             (tramp-adb-send-command
    1271           0 :              vec "echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
    1272           0 :             (let ((old-getprop
    1273           0 :                    (tramp-get-connection-property vec "getprop" nil))
    1274             :                   (new-getprop
    1275           0 :                    (tramp-set-connection-property
    1276           0 :                     vec "getprop"
    1277           0 :                     (with-current-buffer (tramp-get-connection-buffer vec)
    1278             :                       ;; Read the expression.
    1279           0 :                       (goto-char (point-min))
    1280           0 :                       (read (current-buffer))))))
    1281           0 :               (when (and (stringp old-getprop)
    1282           0 :                          (not (string-equal old-getprop new-getprop)))
    1283           0 :                 (tramp-message
    1284           0 :                  vec 3
    1285             :                  "Connection reset, because remote host changed from `%s' to `%s'"
    1286           0 :                  old-getprop new-getprop)
    1287           0 :                 (tramp-cleanup-connection vec t)
    1288           0 :                 (tramp-adb-maybe-open-connection vec)))
    1289             : 
    1290             :             ;; Change user if indicated.
    1291           0 :             (when user
    1292           0 :               (tramp-adb-send-command vec (format "su %s" user))
    1293           0 :               (unless (tramp-adb-send-command-and-check vec nil)
    1294           0 :                 (delete-process p)
    1295           0 :                 (tramp-set-file-property vec "" "su-command-p" nil)
    1296           0 :                 (tramp-error
    1297           0 :                  vec 'file-error "Cannot switch to user `%s'" user)))
    1298             : 
    1299             :             ;; Set "remote-path" connection property.  This is needed
    1300             :             ;; for eshell.
    1301           0 :             (tramp-adb-send-command vec "echo \\\"$PATH\\\"")
    1302           0 :             (tramp-set-connection-property
    1303           0 :              vec "remote-path"
    1304           0 :              (split-string
    1305           0 :               (with-current-buffer (tramp-get-connection-buffer vec)
    1306             :                 ;; Read the expression.
    1307           0 :                 (goto-char (point-min))
    1308           0 :                 (read (current-buffer)))
    1309           0 :               ":" 'omit))
    1310             : 
    1311             :             ;; Set connection-local variables.
    1312           0 :             (tramp-set-connection-local-variables vec)
    1313             : 
    1314             :             ;; Mark it as connected.
    1315           0 :             (tramp-set-connection-property p "connected" t)))))))
    1316             : 
    1317             : (add-hook 'tramp-unload-hook
    1318             :           (lambda ()
    1319             :             (unload-feature 'tramp-adb 'force)))
    1320             : 
    1321             : (provide 'tramp-adb)
    1322             : 
    1323             : ;;; tramp-adb.el ends here

Generated by: LCOV version 1.12