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