tramp-adb.el 44.1 KB
Newer Older
1 2
;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

;; Author: Juergen Hoetzel <juergen@archlinux.org>
;; Keywords: comm, processes
;; Package: tramp

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

26 27 28
;; 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:
29
;;
30
;;   (setq tramp-adb-program "/path/to/adb")
31 32 33 34 35 36
;;
;; Due to security it is not possible to access non-root devices.

;;; Code:

(require 'tramp)
37
(require 'time-date)
38

Michael Albinus's avatar
Michael Albinus committed
39 40 41 42
;; Pacify byte-compiler.
(eval-when-compile
  (defvar directory-sep-char)
  (defvar dired-move-to-filename-regexp))
43

44 45 46
(defcustom tramp-adb-program "adb"
  "Name of the Android Debug Bridge program."
  :group 'tramp
47
  :version "24.4"
48
  :type 'string)
49 50 51 52 53

;;;###tramp-autoload
(defconst tramp-adb-method "adb"
  "*When this method name is used, forward all calls to Android Debug Bridge.")

54
(defcustom tramp-adb-prompt
55
  "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
56
  "Regexp used as prompt in almquist shell."
57
  :type 'string
58 59
  :version "24.4"
  :group 'tramp)
60

61 62
(defconst tramp-adb-ls-date-regexp
  "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]")
63

64 65 66 67 68
(defconst tramp-adb-ls-toolbox-regexp
  (concat
   "^[[:space:]]*\\([-[:alpha:]]+\\)" 	; \1 permissions
   "[[:space:]]*\\([^[:space:]]+\\)"	; \2 username
   "[[:space:]]+\\([^[:space:]]+\\)"	; \3 group
69
   "[[:space:]]+\\([[:digit:]]+\\)"	; \4 size
70 71 72
   "[[:space:]]+\\([-[:digit:]]+[[:space:]][:[:digit:]]+\\)" ; \5 date
   "[[:space:]]+\\(.*\\)$"))		; \6 filename

73
;;;###tramp-autoload
74 75 76 77 78 79
(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 ""))
80 81 82 83 84 85 86 87 88 89 90

;;;###tramp-autoload
(eval-after-load 'tramp
  '(tramp-set-completion-function
    tramp-adb-method '((tramp-adb-parse-device-names ""))))

;;;###tramp-autoload
(add-to-list 'tramp-foreign-file-name-handler-alist
	     (cons 'tramp-adb-file-name-p 'tramp-adb-file-name-handler))

(defconst tramp-adb-file-name-handler-alist
Michael Albinus's avatar
Michael Albinus committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104
  '((access-file . ignore)
    (add-name-to-file . tramp-adb-handle-copy-file)
    ;; `byte-compiler-base-file-name' performed by default handler.
    ;; `copy-directory' performed by default handler.
    (copy-file . tramp-adb-handle-copy-file)
    (delete-directory . tramp-adb-handle-delete-directory)
    (delete-file . tramp-adb-handle-delete-file)
    ;; `diff-latest-backup-file' performed by default handler.
    (directory-file-name . tramp-handle-directory-file-name)
    (directory-files . tramp-handle-directory-files)
    (directory-files-and-attributes
     . tramp-adb-handle-directory-files-and-attributes)
    (dired-call-process . ignore)
    (dired-compress-file . ignore)
105
    (dired-uncache . tramp-handle-dired-uncache)
Michael Albinus's avatar
Michael Albinus committed
106 107 108 109 110 111 112 113 114 115 116 117 118
    (expand-file-name . tramp-adb-handle-expand-file-name)
    (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
    (file-acl . ignore)
    (file-attributes . tramp-adb-handle-file-attributes)
    (file-directory-p . tramp-adb-handle-file-directory-p)
    ;; `file-equal-p' performed by default handler.
    ;; FIXME: This is too sloppy.
    (file-executable-p . tramp-handle-file-exists-p)
    (file-exists-p . tramp-handle-file-exists-p)
    ;; `file-in-directory-p' performed by default handler.
    (file-local-copy . tramp-adb-handle-file-local-copy)
    (file-modes . tramp-handle-file-modes)
    (file-name-all-completions . tramp-adb-handle-file-name-all-completions)
119 120 121 122
    (file-name-as-directory . tramp-handle-file-name-as-directory)
    (file-name-completion . tramp-handle-file-name-completion)
    (file-name-directory . tramp-handle-file-name-directory)
    (file-name-nondirectory . tramp-handle-file-name-nondirectory)
Michael Albinus's avatar
Michael Albinus committed
123
    ;; `file-name-sans-versions' performed by default handler.
124
    (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
Michael Albinus's avatar
Michael Albinus committed
125 126 127 128
    (file-notify-add-watch . tramp-handle-file-notify-add-watch)
    (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
    (file-ownership-preserved-p . ignore)
    (file-readable-p . tramp-handle-file-exists-p)
129 130
    (file-regular-p . tramp-handle-file-regular-p)
    (file-remote-p . tramp-handle-file-remote-p)
Michael Albinus's avatar
Michael Albinus committed
131
    (file-selinux-context . ignore)
132
    (file-symlink-p . tramp-handle-file-symlink-p)
Michael Albinus's avatar
Michael Albinus committed
133
    (file-truename . tramp-adb-handle-file-truename)
134 135
    (file-writable-p . tramp-adb-handle-file-writable-p)
    (find-backup-file-name . tramp-handle-find-backup-file-name)
Michael Albinus's avatar
Michael Albinus committed
136 137
    ;; `find-file-noselect' performed by default handler.
    ;; `get-file-buffer' performed by default handler.
138 139
    (insert-directory . tramp-adb-handle-insert-directory)
    (insert-file-contents . tramp-handle-insert-file-contents)
Michael Albinus's avatar
Michael Albinus committed
140 141 142 143 144 145 146 147
    (load . tramp-handle-load)
    ;; `make-auto-save-file-name' performed by default handler.
    (make-directory . tramp-adb-handle-make-directory)
    (make-directory-internal . ignore)
    (make-symbolic-link . ignore)
    (process-file . tramp-adb-handle-process-file)
    (rename-file . tramp-adb-handle-rename-file)
    (set-file-acl . ignore)
148
    (set-file-modes . tramp-adb-handle-set-file-modes)
Michael Albinus's avatar
Michael Albinus committed
149
    (set-file-selinux-context . ignore)
150
    (set-file-times . tramp-adb-handle-set-file-times)
Michael Albinus's avatar
Michael Albinus committed
151
    (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
152
    (shell-command . tramp-adb-handle-shell-command)
Michael Albinus's avatar
Michael Albinus committed
153 154 155 156 157 158
    (start-file-process . tramp-adb-handle-start-file-process)
    (substitute-in-file-name . tramp-handle-substitute-in-file-name)
    (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
    (vc-registered . ignore)
    (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
    (write-region . tramp-adb-handle-write-region))
159 160
  "Alist of handler functions for Tramp ADB method.")

161 162
;; It must be a `defsubst' in order to push the whole code into
;; tramp-loaddefs.el.  Otherwise, there would be recursive autoloading.
163
;;;###tramp-autoload
164
(defsubst tramp-adb-file-name-p (filename)
165 166 167 168 169 170 171 172 173
  "Check if it's a filename for ADB."
  (let ((v (tramp-dissect-file-name filename)))
    (string= (tramp-file-name-method v) tramp-adb-method)))

;;;###tramp-autoload
(defun tramp-adb-file-name-handler (operation &rest args)
  "Invoke the ADB handler for OPERATION.
First arg specifies the OPERATION, second arg is a list of arguments to
pass to the OPERATION."
174
  (let ((fn (assoc operation tramp-adb-file-name-handler-alist)))
175 176 177 178 179
    (if fn
	(save-match-data (apply (cdr fn) args))
      (tramp-run-real-handler operation args))))

;;;###tramp-autoload
180
(defun tramp-adb-parse-device-names (_ignore)
181
  "Return a list of (nil host) tuples allowed to access."
182 183
  (with-timeout (10)
    (with-temp-buffer
184 185 186 187 188 189 190 191 192 193 194 195
      ;; `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))))
	result))))
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

(defun tramp-adb-handle-expand-file-name (name &optional dir)
  "Like `expand-file-name' for Tramp files."
  ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
  (setq dir (or dir default-directory "/"))
  ;; Unless NAME is absolute, concat DIR and NAME.
  (unless (file-name-absolute-p name)
    (setq name (concat (file-name-as-directory dir) name)))
  ;; If NAME is not a Tramp file, run the real handler.
  (if (not (tramp-tramp-file-p name))
      (tramp-run-real-handler 'expand-file-name (list name nil))
    ;; Dissect NAME.
    (with-parsed-tramp-file-name name nil
      (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
	(setq localname (concat "/" localname)))
      ;; Do normal `expand-file-name' (this does "/./" and "/../").
      ;; We bind `directory-sep-char' here for XEmacs on Windows,
      ;; which would otherwise use backslash.  `default-directory' is
      ;; bound, because on Windows there would be problems with UNC
      ;; shares or Cygwin mounts.
      (let ((directory-sep-char ?/)
	    (default-directory (tramp-compat-temporary-file-directory)))
	(tramp-make-tramp-file-name
	 method user host
	 (tramp-drop-volume-letter
	  (tramp-run-real-handler
	   'expand-file-name (list localname))))))))

(defun tramp-adb-handle-file-directory-p (filename)
  "Like `file-directory-p' for Tramp files."
  (car (file-attributes (file-truename filename))))

;; This is derived from `tramp-sh-handle-file-truename'.  Maybe the
;; code could be shared?
230
(defun tramp-adb-handle-file-truename (filename)
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  "Like `file-truename' for Tramp files."
  (with-parsed-tramp-file-name (expand-file-name filename) nil
    (with-tramp-file-property v localname "file-truename"
      (let ((result nil))			; result steps in reverse order
	(tramp-message v 4 "Finding true name for `%s'" filename)
	(let* ((directory-sep-char ?/)
	       (steps (tramp-compat-split-string localname "/"))
	       (localnamedir (tramp-run-real-handler
			      'file-name-as-directory (list localname)))
	       (is-dir (string= localname localnamedir))
	       (thisstep nil)
	       (numchase 0)
	       ;; Don't make the following value larger than
	       ;; necessary.  People expect an error message in a
	       ;; timely fashion when something is wrong; otherwise
	       ;; they might think that Emacs is hung.  Of course,
	       ;; correctness has to come first.
	       (numchase-limit 20)
	       symlink-target)
	  (while (and steps (< numchase numchase-limit))
	    (setq thisstep (pop steps))
	    (tramp-message
	     v 5 "Check %s"
	     (mapconcat 'identity
			(append '("") (reverse result) (list thisstep))
			"/"))
	    (setq symlink-target
		  (nth 0 (file-attributes
			  (tramp-make-tramp-file-name
			   method user host
			   (mapconcat 'identity
				      (append '("")
					      (reverse result)
					      (list thisstep))
				      "/")))))
	    (cond ((string= "." thisstep)
		   (tramp-message v 5 "Ignoring step `.'"))
		  ((string= ".." thisstep)
		   (tramp-message v 5 "Processing step `..'")
		   (pop result))
		  ((stringp symlink-target)
		   ;; It's a symlink, follow it.
		   (tramp-message v 5 "Follow symlink to %s" symlink-target)
		   (setq numchase (1+ numchase))
		   (when (file-name-absolute-p symlink-target)
		     (setq result nil))
		   ;; If the symlink was absolute, we'll get a string
		   ;; like "/user@host:/some/target"; extract the
		   ;; "/some/target" part from it.
		   (when (tramp-tramp-file-p symlink-target)
		     (unless (tramp-equal-remote filename symlink-target)
		       (tramp-error
			v 'file-error
			"Symlink target `%s' on wrong host" symlink-target))
		     (setq symlink-target localname))
		   (setq steps
			 (append (tramp-compat-split-string
				  symlink-target "/")
				 steps)))
		  (t
		   ;; It's a file.
		   (setq result (cons thisstep result)))))
	  (when (>= numchase numchase-limit)
	    (tramp-error
	     v 'file-error
	     "Maximum number (%d) of symlinks exceeded" numchase-limit))
	  (setq result (reverse result))
	  ;; Combine list to form string.
	  (setq result
		(if result
		    (mapconcat 'identity (cons "" result) "/")
		  "/"))
	  (when (and is-dir (or (string= "" result)
				(not (string= (substring result -1) "/"))))
	    (setq result (concat result "/"))))

        (tramp-message v 4 "True name of `%s' is `%s'" filename result)
        (tramp-make-tramp-file-name method user host result)))))

(defun tramp-adb-handle-file-attributes (filename &optional id-format)
  "Like `file-attributes' for Tramp files."
  (unless id-format (setq id-format 'integer))
  (ignore-errors
    (with-parsed-tramp-file-name filename nil
315 316
      (with-tramp-file-property
	  v localname (format "file-attributes-%s" id-format)
317
	(tramp-adb-barf-unless-okay
318 319 320
	 v (format "%s -d -l %s"
		   (tramp-adb-get-ls-command v)
		   (tramp-shell-quote-argument localname)) "")
321 322
	(with-current-buffer (tramp-get-buffer v)
	  (tramp-adb-sh-fix-ls-output)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	  (cdar (tramp-do-parse-file-attributes-with-ls v id-format)))))))

(defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format)
  "Parse `file-attributes' for Tramp files using the ls(1) command."
  (with-current-buffer (tramp-get-buffer vec)
    (goto-char (point-min))
    (let ((file-properties nil))
      (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t)
	(let* ((mod-string (match-string 1))
	       (is-dir (eq ?d (aref mod-string 0)))
	       (is-symlink (eq ?l (aref mod-string 0)))
	       (uid (match-string 2))
	       (gid (match-string 3))
	       (size (string-to-number (match-string 4)))
	       (date (match-string 5))
	       (name (match-string 6))
	       (symlink-target
		(and is-symlink
		     (cadr (split-string name "\\( -> \\|\n\\)")))))
	  (push (list
343 344 345
		 (if is-symlink
		     (car (split-string name "\\( -> \\|\n\\)"))
		   name)
346 347 348 349 350 351 352 353 354 355 356 357
		 (or is-dir symlink-target)
		 1     ;link-count
		 ;; no way to handle numeric ids in Androids ash
		 (if (eq id-format 'integer) 0 uid)
		 (if (eq id-format 'integer) 0 gid)
		 '(0 0)			; atime
		 (date-to-time date)	; mtime
		 '(0 0)			; ctime
		 size
		 mod-string
		 ;; fake
		 t 1
358
		 (tramp-get-device vec))
359 360 361 362 363 364 365 366 367
		file-properties)))
      file-properties)))

(defun tramp-adb-handle-directory-files-and-attributes
  (directory &optional full match nosort id-format)
  "Like `directory-files-and-attributes' for Tramp files."
  (when (file-directory-p directory)
    (with-parsed-tramp-file-name (expand-file-name directory) nil
      (with-tramp-file-property
368
	  v localname (format "directory-files-attributes-%s-%s-%s-%s"
369 370 371 372 373 374 375
			      full match id-format nosort)
	(tramp-adb-barf-unless-okay
	 v (format "%s -a -l %s"
		   (tramp-adb-get-ls-command v)
		   (tramp-shell-quote-argument localname)) "")
	(with-current-buffer (tramp-get-buffer v)
	  (tramp-adb-sh-fix-ls-output)
376 377
	  (let ((result (tramp-do-parse-file-attributes-with-ls
			 v (or id-format 'integer))))
378
	    (when full
379 380 381 382 383
	      (setq result
		    (mapcar
		     (lambda (x)
		       (cons (expand-file-name (car x) directory) (cdr x)))
		     result)))
384
	    (unless nosort
385 386
	      (setq result
		    (sort result (lambda (x y) (string< (car x) (car y))))))
387 388 389 390 391
	    (delq nil
		  (mapcar (lambda (x)
			    (if (or (not match) (string-match match (car x)))
				x))
			  result))))))))
392

393 394 395 396 397 398 399 400 401 402 403
(defun tramp-adb-get-ls-command (vec)
  (with-tramp-connection-property vec "ls"
    (tramp-message vec 5 "Finding a suitable `ls' command")
    (if	(zerop (tramp-adb-command-exit-status
		vec "ls --color=never -al /dev/null"))
	;; On CyanogenMod based system BusyBox is used and "ls" output
	;; coloring is enabled by default.  So we try to disable it
	;; when possible.
	"ls --color=never"
      "ls")))

404 405 406 407 408 409 410 411 412 413 414 415 416
(defun tramp-adb--gnu-switches-to-ash
  (switches)
  "Almquist shell can't handle multiple arguments.
Convert (\"-al\") to (\"-a\" \"-l\").  Remove arguments like \"--dired\"."
  (split-string
   (apply 'concat
	  (mapcar (lambda (s)
		    (replace-regexp-in-string
		     "\\(.\\)"  " -\\1"
		     (replace-regexp-in-string "^-" "" s)))
		  ;; FIXME: Warning about removed switches (long and non-dash).
		  (delq nil
			(mapcar
417 418
			 (lambda (s)
			   (and (not (string-match "\\(^--\\|^[^-]\\)" s)) s))
419 420 421
			 switches))))))

(defun tramp-adb-handle-insert-directory
422
  (filename switches &optional _wildcard _full-directory-p)
423 424 425 426 427 428 429 430 431 432
  "Like `insert-directory' for Tramp files."
  (when (stringp switches)
    (setq switches (tramp-adb--gnu-switches-to-ash (split-string switches))))
  (with-parsed-tramp-file-name (file-truename filename) nil
    (with-current-buffer (tramp-get-buffer v)
      (let ((name (tramp-shell-quote-argument (directory-file-name localname)))
	    (switch-d (member "-d" switches))
	    (switch-t (member "-t" switches))
	    (switches (mapconcat 'identity (remove "-t" switches) " ")))
	(tramp-adb-barf-unless-okay
433
	 v (format "%s %s %s" (tramp-adb-get-ls-command v) switches name)
434 435 436 437 438 439
	 "Cannot insert directory listing: %s" filename)
	(unless switch-d
	  ;; We insert also filename/. and filename/.., because "ls" doesn't.
	  (narrow-to-region (point) (point))
	  (ignore-errors
	    (tramp-adb-barf-unless-okay
440 441
	     v (format "%s -d %s %s %s"
		       (tramp-adb-get-ls-command v)
442 443 444 445 446 447 448 449 450
		       switches
		       (concat (file-name-as-directory name) ".")
		       (concat (file-name-as-directory name) ".."))
	     "Cannot insert directory listing: %s" filename))
	  (widen))
	(tramp-adb-sh-fix-ls-output switch-t)))
    (insert-buffer-substring (tramp-get-buffer v))))

(defun tramp-adb-sh-fix-ls-output (&optional sort-by-time)
451 452 453
  "Insert dummy 0 in empty size columns.
Androids \"ls\" command doesn't insert size column for directories:
Emacs dired can't find files."
454 455 456
  (save-excursion
    ;; Insert missing size.
    (goto-char (point-min))
457 458 459
    (while
	(search-forward-regexp
	 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t)
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
      (replace-match "0\\1" "\\1" nil)
      ;; Insert missing "/".
      (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
	(end-of-line)
	(insert "/")))
    ;; Sort entries.
    (let* ((lines (split-string (buffer-string) "\n" t))
	   (sorted-lines
	    (sort
	     lines
	     (if sort-by-time
		 'tramp-adb-ls-output-time-less-p
	       'tramp-adb-ls-output-name-less-p))))
      (delete-region (point-min) (point-max))
      (insert "  " (mapconcat 'identity sorted-lines "\n  ")))
    ;; Add final newline.
    (goto-char (point-max))
    (unless (= (point) (line-beginning-position))
      (insert "\n"))))


(defun tramp-adb-ls-output-time-less-p (a b)
  "Sort \"ls\" output by time, descending."
  (let (time-a time-b)
    (string-match tramp-adb-ls-date-regexp a)
    (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))))
488
    (tramp-time-less-p time-b time-a)))
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523

(defun tramp-adb-ls-output-name-less-p (a b)
  "Sort \"ls\" output by name, ascending."
  (let (posa posb)
    (string-match dired-move-to-filename-regexp a)
    (setq posa (match-end 0))
    (string-match dired-move-to-filename-regexp b)
    (setq posb (match-end 0))
    (string-lessp (substring a posa) (substring b posb))))

(defun tramp-adb-handle-make-directory (dir &optional parents)
  "Like `make-directory' for Tramp files."
  (setq dir (expand-file-name dir))
  (with-parsed-tramp-file-name dir nil
    (when parents
      (let ((par (expand-file-name ".." dir)))
	(unless (file-directory-p par)
	  (make-directory par parents))))
    (tramp-adb-barf-unless-okay
     v (format "mkdir %s" (tramp-shell-quote-argument localname))
     "Couldn't make directory %s" dir)
    (tramp-flush-directory-property v (file-name-directory localname))))

(defun tramp-adb-handle-delete-directory (directory &optional recursive)
  "Like `delete-directory' for Tramp files."
  (setq directory (expand-file-name directory))
  (with-parsed-tramp-file-name directory nil
    (tramp-flush-file-property v (file-name-directory localname))
    (tramp-flush-directory-property v localname)
    (tramp-adb-barf-unless-okay
     v (format "%s %s"
	       (if recursive "rm -r" "rmdir")
	       (tramp-shell-quote-argument localname))
     "Couldn't delete %s" directory)))

524
(defun tramp-adb-handle-delete-file (filename &optional _trash)
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
  "Like `delete-file' for Tramp files."
  (setq filename (expand-file-name filename))
  (with-parsed-tramp-file-name filename nil
    (tramp-flush-file-property v (file-name-directory localname))
    (tramp-flush-file-property v localname)
    (tramp-adb-barf-unless-okay
     v (format "rm %s" (tramp-shell-quote-argument localname))
     "Couldn't delete %s" filename)))

(defun tramp-adb-handle-file-name-all-completions (filename directory)
  "Like `file-name-all-completions' for Tramp files."
  (all-completions
   filename
   (with-parsed-tramp-file-name directory nil
     (with-tramp-file-property v localname "file-name-all-completions"
       (save-match-data
	 (tramp-adb-send-command
542 543 544
	  v (format "%s %s"
		    (tramp-adb-get-ls-command v)
		    (tramp-shell-quote-argument localname)))
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625
	 (mapcar
	  (lambda (f)
	    (if (file-directory-p f)
		(file-name-as-directory f)
	      f))
	  (with-current-buffer (tramp-get-buffer v)
	    (delq
	     nil
	     (mapcar
	      (lambda (l) (and (not (string-match  "^[[:space:]]*$" l)) l))
	      (split-string (buffer-string) "\n"))))))))))

(defun tramp-adb-handle-file-local-copy (filename)
  "Like `file-local-copy' for Tramp files."
  (with-parsed-tramp-file-name filename nil
    (unless (file-exists-p (file-truename filename))
      (tramp-error
       v 'file-error
       "Cannot make local copy of non-existing file `%s'" filename))
    (let ((tmpfile (tramp-compat-make-temp-file filename)))
      (with-tramp-progress-reporter
	  v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
	(when (tramp-adb-execute-adb-command v "pull" localname tmpfile)
	  (delete-file tmpfile)
	  (tramp-error
	   v 'file-error "Cannot make local copy of file `%s'" filename))
	(set-file-modes tmpfile (file-modes filename)))
      tmpfile)))

(defun tramp-adb-handle-file-writable-p (filename)
  "Like `tramp-sh-handle-file-writable-p'.
But handle the case, if the \"test\" command is not available."
  (with-parsed-tramp-file-name filename nil
    (with-tramp-file-property v localname "file-writable-p"
      (if (tramp-adb-find-test-command v)
	  (if (file-exists-p filename)
	      (zerop
	       (tramp-adb-command-exit-status
		v (format "test -w %s" (tramp-shell-quote-argument localname))))
	    (and
	     (file-directory-p (file-name-directory filename))
	     (file-writable-p (file-name-directory filename))))

	;; Missing "test" command on Android < 4.
       (let ((rw-path "/data/data"))
	 (tramp-message
	  v 5
	  "Not implemented yet (assuming \"/data/data\" is writable): %s"
	  localname)
	 (and (>= (length localname) (length rw-path))
	      (string= (substring localname 0 (length rw-path))
		       rw-path)))))))

(defun tramp-adb-handle-write-region
  (start end filename &optional append visit lockname confirm)
  "Like `write-region' for Tramp files."
  (setq filename (expand-file-name filename))
  (with-parsed-tramp-file-name filename nil
    (when append
      (tramp-error
       v 'file-error "Cannot append to file using Tramp (`%s')" filename))
    (when (and confirm (file-exists-p filename))
      (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
				filename))
	(tramp-error v 'file-error "File not overwritten")))
    ;; We must also flush the cache of the directory, because
    ;; `file-attributes' reads the values from there.
    (tramp-flush-file-property v (file-name-directory localname))
    (tramp-flush-file-property v localname)
    (let* ((curbuf (current-buffer))
	   (tmpfile (tramp-compat-make-temp-file filename)))
      (tramp-run-real-handler
       'write-region
       (list start end tmpfile append 'no-message lockname confirm))
      (with-tramp-progress-reporter
	  v 3 (format "Moving tmp file %s to %s" tmpfile filename)
	(unwind-protect
	    (when (tramp-adb-execute-adb-command v "push" tmpfile localname)
	      (tramp-error v 'file-error "Cannot write: `%s' filename"))
	  (delete-file tmpfile)))

Michael Albinus's avatar
Michael Albinus committed
626 627 628
      (when (or (eq visit t) (stringp visit))
	(set-visited-file-modtime))

629 630 631 632 633 634 635 636 637 638 639 640 641
      (unless (equal curbuf (current-buffer))
	(tramp-error
	 v 'file-error
	 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer))))))

(defun tramp-adb-handle-set-file-modes (filename mode)
  "Like `set-file-modes' for Tramp files."
  (with-parsed-tramp-file-name filename nil
    (tramp-flush-file-property v localname)
    (tramp-adb-barf-unless-okay
     v (format "chmod %s %s" (tramp-compat-decimal-to-octal mode) localname)
     "Error while changing file's mode %s" filename)))

642 643 644 645 646 647 648 649 650 651 652 653 654
(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))))))

655 656
(defun tramp-adb-handle-copy-file
  (filename newname &optional ok-if-already-exists keep-date
657
	    _preserve-uid-gid _preserve-extended-attributes)
658
  "Like `copy-file' for Tramp files.
659
PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
660 661 662 663
  (setq filename (expand-file-name filename)
	newname (expand-file-name newname))

  (if (file-directory-p filename)
664
      (tramp-file-name-handler 'copy-directory filename newname keep-date t)
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    (with-tramp-progress-reporter
	(tramp-dissect-file-name (if (file-remote-p filename) filename newname))
	0 (format "Copying %s to %s" filename newname)

      (let ((tmpfile (file-local-copy filename)))

	(if tmpfile
	    ;; Remote filename.
	    (condition-case err
		(rename-file tmpfile newname ok-if-already-exists)
	      ((error quit)
	       (delete-file tmpfile)
	       (signal (car err) (cdr err))))

	  ;; Remote newname.
	  (when (file-directory-p newname)
	    (setq newname
		  (expand-file-name (file-name-nondirectory filename) newname)))

	  (with-parsed-tramp-file-name newname nil
	    (when (and (not ok-if-already-exists)
		       (file-exists-p newname))
	      (tramp-error v 'file-already-exists newname))

	    ;; We must also flush the cache of the directory, because
	    ;; `file-attributes' reads the values from there.
	    (tramp-flush-file-property v (file-name-directory localname))
	    (tramp-flush-file-property v localname)
	    (when (tramp-adb-execute-adb-command v "push" filename localname)
	      (tramp-error
	       v 'file-error "Cannot copy `%s' `%s'" filename newname))))))

    ;; KEEP-DATE handling.
    (when keep-date
      (set-file-times newname (nth 5 (file-attributes filename))))))

(defun tramp-adb-handle-rename-file
  (filename newname &optional ok-if-already-exists)
  "Like `rename-file' for Tramp files."
  (setq filename (expand-file-name filename)
	newname (expand-file-name newname))

  (with-parsed-tramp-file-name
      (if (file-remote-p filename) filename newname) nil
    (with-tramp-progress-reporter
	v 0 (format "Renaming %s to %s" newname filename)

      (if (and (tramp-equal-remote filename newname)
	       (not (file-directory-p filename)))
	  (progn
	    (when (and (not ok-if-already-exists)
		       (file-exists-p newname))
	      (tramp-error v 'file-already-exists newname))
	    ;; We must also flush the cache of the directory, because
	    ;; `file-attributes' reads the values from there.
	    (tramp-flush-file-property v (file-name-directory localname))
	    (tramp-flush-file-property v localname)
	    ;; Short track.
	    (tramp-adb-barf-unless-okay
724 725 726 727
	     v (format
		"mv %s %s"
		(tramp-file-name-handler 'file-remote-p filename 'localname)
		localname)
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
	     "Error renaming %s to %s" filename newname))

	;; Rename by copy.
	(copy-file filename newname ok-if-already-exists t t)
	(delete-file filename)))))

(defun tramp-adb-handle-process-file
  (program &optional infile destination display &rest args)
  "Like `process-file' for Tramp files."
  ;; The implementation is not complete yet.
  (when (and (numberp destination) (zerop destination))
    (error "Implementation does not handle immediate return"))

  (with-parsed-tramp-file-name default-directory nil
    (let (command input tmpinput stderr tmpstderr outbuf ret)
      ;; Compute command.
      (setq command (mapconcat 'tramp-shell-quote-argument
			       (cons program args) " "))
      ;; Determine input.
      (if (null infile)
	  (setq input "/dev/null")
	(setq infile (expand-file-name infile))
	(if (tramp-equal-remote default-directory infile)
	    ;; INFILE is on the same remote host.
	    (setq input (with-parsed-tramp-file-name infile nil localname))
	  ;; INFILE must be copied to remote host.
	  (setq input (tramp-make-tramp-temp-file v)
		tmpinput (tramp-make-tramp-file-name method user host input))
	  (copy-file infile tmpinput t)))
      (when input (setq command (format "%s <%s" command input)))

      ;; Determine output.
      (cond
       ;; Just a buffer.
       ((bufferp destination)
	(setq outbuf destination))
       ;; A buffer name.
       ((stringp destination)
	(setq outbuf (get-buffer-create destination)))
       ;; (REAL-DESTINATION ERROR-DESTINATION)
       ((consp destination)
	;; output.
	(cond
	 ((bufferp (car destination))
	  (setq outbuf (car destination)))
	 ((stringp (car destination))
	  (setq outbuf (get-buffer-create (car destination))))
	 ((car destination)
	  (setq outbuf (current-buffer))))
	;; stderr.
	(cond
	 ((stringp (cadr destination))
	  (setcar (cdr destination) (expand-file-name (cadr destination)))
	  (if (tramp-equal-remote default-directory (cadr destination))
	      ;; stderr is on the same remote host.
	      (setq stderr (with-parsed-tramp-file-name
			       (cadr destination) nil localname))
	    ;; stderr must be copied to remote host.  The temporary
	    ;; file must be deleted after execution.
	    (setq stderr (tramp-make-tramp-temp-file v)
		  tmpstderr (tramp-make-tramp-file-name
			     method user host stderr))))
	 ;; stderr to be discarded.
	 ((null (cadr destination))
	  (setq stderr "/dev/null"))))
       ;; 't
       (destination
	(setq outbuf (current-buffer))))
      (when stderr (setq command (format "%s 2>%s" command stderr)))

      ;; Send the command.  It might not return in time, so we protect
      ;; it.  Call it in a subshell, in order to preserve working
      ;; directory.
      (condition-case nil
	  (progn
803 804 805 806 807
	    (setq ret 0)
	    (tramp-adb-barf-unless-okay
	     v (format "(cd %s; %s)"
		       (tramp-shell-quote-argument localname) command)
	     "")
808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
	    ;; We should show the output anyway.
	    (when outbuf
	      (with-current-buffer outbuf
		(insert-buffer-substring (tramp-get-connection-buffer v)))
	      (when display (display-buffer outbuf))))
	;; When the user did interrupt, we should do it also.  We use
	;; return code -1 as marker.
	(quit
	 (kill-buffer (tramp-get-connection-buffer v))
	 (setq ret -1))
	;; Handle errors.
	(error
	 (kill-buffer (tramp-get-connection-buffer v))
	 (setq ret 1)))

      ;; Provide error file.
      (when tmpstderr (rename-file tmpstderr (cadr destination) t))

      ;; Cleanup.  We remove all file cache values for the connection,
      ;; because the remote process could have changed them.
      (when tmpinput (delete-file tmpinput))

      ;; `process-file-side-effects' has been introduced with GNU
      ;; Emacs 23.2.  If set to `nil', no remote file will be changed
      ;; by `program'.  If it doesn't exist, we assume its default
      ;; value 't'.
      (unless (and (boundp 'process-file-side-effects)
		   (not (symbol-value 'process-file-side-effects)))
        (tramp-flush-directory-property v ""))

      ;; Return exit status.
      (if (equal ret -1)
	  (keyboard-quit)
	ret))))

(defun tramp-adb-handle-shell-command
  (command &optional output-buffer error-buffer)
  "Like `shell-command' for Tramp files."
  (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
	 ;; We cannot use `shell-file-name' and `shell-command-switch',
	 ;; they are variables of the local host.
	 (args (list "sh" "-c" (substring command 0 asynchronous)))
	 current-buffer-p
	 (output-buffer
	  (cond
	   ((bufferp output-buffer) output-buffer)
	   ((stringp output-buffer) (get-buffer-create output-buffer))
	   (output-buffer
	    (setq current-buffer-p t)
	    (current-buffer))
	   (t (get-buffer-create
	       (if asynchronous
		   "*Async Shell Command*"
		 "*Shell Command Output*")))))
	 (error-buffer
	  (cond
	   ((bufferp error-buffer) error-buffer)
	   ((stringp error-buffer) (get-buffer-create error-buffer))))
	 (buffer
	  (if (and (not asynchronous) error-buffer)
	      (with-parsed-tramp-file-name default-directory nil
		(list output-buffer (tramp-make-tramp-temp-file v)))
	    output-buffer))
	 (p (get-buffer-process output-buffer)))

    ;; Check whether there is another process running.  Tramp does not
    ;; support 2 (asynchronous) processes in parallel.
    (when p
      (if (yes-or-no-p "A command is running.  Kill it? ")
	  (ignore-errors (kill-process p))
878
	(tramp-compat-user-error "Shell command in progress")))
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918

    (if current-buffer-p
	(progn
	  (barf-if-buffer-read-only)
	  (push-mark nil t))
      (with-current-buffer output-buffer
	(setq buffer-read-only nil)
	(erase-buffer)))

    (if (and (not current-buffer-p) (integerp asynchronous))
	(prog1
	    ;; Run the process.
	    (apply 'start-file-process "*Async Shell*" buffer args)
	  ;; Display output.
	  (pop-to-buffer output-buffer)
	  (setq mode-line-process '(":%s"))
	  (shell-mode))

      (prog1
	  ;; Run the process.
	  (apply 'process-file (car args) nil buffer nil (cdr args))
	;; Insert error messages if they were separated.
	(when (listp buffer)
	  (with-current-buffer error-buffer
	    (insert-file-contents (cadr buffer)))
	  (delete-file (cadr buffer)))
	(if current-buffer-p
	    ;; This is like exchange-point-and-mark, but doesn't
	    ;; activate the mark.  It is cleaner to avoid activation,
	    ;; even though the command loop would deactivate the mark
	    ;; because we inserted text.
	    (goto-char (prog1 (mark t)
			 (set-marker (mark-marker) (point)
				     (current-buffer))))
	  ;; There's some output, display it.
	  (when (with-current-buffer output-buffer (> (point-max) (point-min)))
	    (if (functionp 'display-message-or-buffer)
		(tramp-compat-funcall 'display-message-or-buffer output-buffer)
	      (pop-to-buffer output-buffer))))))))

919
;; We use BUFFER also as connection buffer during setup.  Because of
920 921 922 923 924
;; this, its original contents must be saved, and restored once
;; connection has been setup.
(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
925 926 927 928 929
    ;; 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"))

930
    (let ((command
931 932 933 934
	   (format "cd %s; %s"
		   (tramp-shell-quote-argument localname)
		   (mapconcat 'tramp-shell-quote-argument
			      (cons program args) " ")))
935 936
	  (tramp-process-connection-type
	   (or (null program) tramp-process-connection-type))
937
	  (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
938 939
	  (name1 name)
	  (i 0))
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994

      (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))
		  ;; 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))
	      (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))))))
995 996 997 998 999

;; Helper functions.

(defun tramp-adb-execute-adb-command (vec &rest args)
  "Returns nil on success error-output on failure."
1000 1001
  (when (> (length (tramp-file-name-host vec)) 0)
    (setq args (append (list "-s" (tramp-file-name-host vec)) args)))
1002 1003
  (with-temp-buffer
    (prog1
1004 1005
	(unless
	    (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args))
1006
	  (buffer-string))
1007
      (tramp-message vec 6 "%s" (buffer-string)))))
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040

(defun tramp-adb-find-test-command (vec)
  "Checks, whether the ash has a builtin \"test\" command.
This happens for Android >= 4.0."
  (with-tramp-connection-property vec "test"
    (zerop (tramp-adb-command-exit-status vec "type test"))))

;; Connection functions

(defun tramp-adb-send-command (vec command)
  "Send the COMMAND to connection VEC."
  (tramp-adb-maybe-open-connection vec)
  (tramp-message vec 6 "%s" command)
  (tramp-send-string vec command)
  ;; fixme: Race condition
  (tramp-adb-wait-for-output (tramp-get-connection-process vec))
  (with-current-buffer (tramp-get-connection-buffer vec)
    (save-excursion
      (goto-char (point-min))
      ;; We can't use stty to disable echo of command.
      (delete-matching-lines (regexp-quote command))
      ;; When the local machine is W32, there are still trailing ^M.
      ;; There must be a better solution by setting the correct coding
      ;; system, but this requires changes in core Tramp.
      (goto-char (point-min))
      (while (re-search-forward "\r+$" nil t)
	(replace-match "" nil nil)))))

(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."
1041 1042 1043 1044
  (tramp-adb-send-command
   vec (if command
	   (format "%s; echo tramp_exit_status $?" command)
	 "echo tramp_exit_status $?"))
1045 1046 1047 1048 1049 1050
  (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 "^ ")
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
    (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)))
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070

(defun tramp-adb-wait-for-output (proc &optional timeout)
  "Wait for output from remote command."
  (unless (buffer-live-p (process-buffer proc))
    (delete-process proc)
    (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
  (with-current-buffer (process-buffer proc)
    (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt)
	(let (buffer-read-only)
	  (goto-char (point-min))
1071 1072
	  ;; ADB terminal sends "^H" sequences.
	  (when (re-search-forward "<\b+" (point-at-eol) t)
1073 1074 1075
	    (forward-line 1)
	    (delete-region (point-min) (point)))
	  ;; Delete the prompt.
1076 1077 1078 1079
         (goto-char (point-min))
         (when (re-search-forward tramp-adb-prompt (point-at-eol) t)
           (forward-line 1)
           (delete-region (point-min) (point)))
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	  (goto-char (point-max))
	  (re-search-backward tramp-adb-prompt nil t)
	  (delete-region (point) (point-max)))
      (if timeout
	  (tramp-error
	   proc 'file-error
	   "[[Remote adb prompt `%s' not found in %d secs]]"
	   tramp-adb-prompt timeout)
	(tramp-error
	 proc 'file-error
	 "[[Remote prompt `%s' not found]]" tramp-adb-prompt)))))

(defun tramp-adb-maybe-open-connection (vec)
  "Maybe open a connection VEC.
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))
1097
	 (p (get-buffer-process buf))
1098
	 (host (tramp-file-name-host vec))
1099
	 (user (tramp-file-name-user vec))
1100
	 (devices (mapcar 'cadr (tramp-adb-parse-device-names nil))))
1101 1102 1103 1104 1105 1106 1107

    ;; 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))

1108 1109 1110 1111
    (unless
	(and p (processp p) (memq (process-status p) '(run open)))
      (save-match-data
	(when (and p (processp p)) (delete-process p))
1112 1113
	(if (not devices)
	    (tramp-error vec 'file-error "No device connected"))
1114 1115 1116
	(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)))
1117 1118 1119
	    (tramp-error
	     vec 'file-error
	     "Multiple Devices connected: No Host/Device specified"))
1120 1121 1122
	(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)
1123 1124
		 (args (if (> (length host) 0)
			   (list "-s" host "shell")
1125 1126 1127 1128
			 (list "shell")))
		 (p (let ((default-directory
			    (tramp-compat-temporary-file-directory)))
		      (apply 'start-process (tramp-get-connection-name vec) buf
1129
			     tramp-adb-program args))))
1130 1131 1132
	    (tramp-message
	     vec 6 "%s" (mapconcat 'identity (process-command p) " "))
	    ;; Wait for initial prompt.
1133
	    (tramp-adb-wait-for-output p 30)
1134 1135
	    (unless (eq 'run (process-status p))
	      (tramp-error  vec 'file-error "Terminated!"))
1136
	    (tramp-compat-set-process-query-on-exit-flag p nil)
1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159

	    ;; Check whether the properties have been changed.  If
	    ;; yes, this is a strong indication that we must expire all
	    ;; connection properties.  We start again.
	    (tramp-message vec 5 "Checking system information")
	    (tramp-adb-send-command
	     vec "echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
	    (let ((old-getprop
		   (tramp-get-connection-property vec "getprop" nil))
		  (new-getprop
		   (tramp-set-connection-property
		    vec "getprop"
		    (with-current-buffer (tramp-get-connection-buffer vec)
		      ;; Read the expression.
		      (goto-char (point-min))
		      (read (current-buffer))))))
	      (when (and (stringp old-getprop)
			 (not (string-equal old-getprop new-getprop)))
		(tramp-cleanup vec)
		(tramp-message
		 vec 3
		 "Connection reset, because remote host changed from `%s' to `%s'"
		 old-getprop new-getprop)
1160 1161
		(tramp-adb-maybe-open-connection vec)))

1162 1163 1164 1165 1166
	    ;; 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)
1167 1168 1169
		(tramp-set-file-property vec "" "su-command-p" nil)
		(tramp-error
		 vec 'file-error "Cannot switch to user `%s'" user)))
1170

1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	    ;; Set "remote-path" connection property.  This is needed
	    ;; for eshell.
	    (tramp-adb-send-command vec "echo \\\"$PATH\\\"")
	    (tramp-set-connection-property
	     vec "remote-path"
	     (split-string
	      (with-current-buffer (tramp-get-connection-buffer vec)
		;; Read the expression.
		(goto-char (point-min))
		(read (current-buffer)))
	      ":" 'omit-nulls))))))))
1182 1183 1184

(provide 'tramp-adb)
;;; tramp-adb.el ends here