]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-adb.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / net / tramp-adb.el
1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
2
3 ;; Copyright (C) 2011-2016 Free Software Foundation, Inc.
4
5 ;; Author: Jürgen Hötzel <juergen@archlinux.org>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; The Android Debug Bridge "adb" must be installed on your local
27 ;; machine. If it is not in your $PATH, add the following form into
28 ;; your .emacs:
29 ;;
30 ;; (setq tramp-adb-program "/path/to/adb")
31 ;;
32 ;; Due to security it is not possible to access non-root devices.
33
34 ;;; Code:
35
36 (require 'tramp)
37
38 ;;;###tramp-autoload
39 (defcustom tramp-adb-program "adb"
40 "Name of the Android Debug Bridge program."
41 :group 'tramp
42 :version "24.4"
43 :type 'string)
44
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)
52
53 ;;;###tramp-autoload
54 (defconst tramp-adb-method "adb"
55 "*When this method name is used, forward all calls to Android Debug Bridge.")
56
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)
64
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:]]")
67
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
76
77 ;;;###tramp-autoload
78 (add-to-list 'tramp-methods
79 `(,tramp-adb-method
80 (tramp-tmpdir "/data/local/tmp")
81 (tramp-default-port 5555)))
82
83 ;;;###tramp-autoload
84 (add-to-list 'tramp-default-host-alist `(,tramp-adb-method nil ""))
85
86 ;;;###tramp-autoload
87 (eval-after-load 'tramp
88 '(tramp-set-completion-function
89 tramp-adb-method '((tramp-adb-parse-device-names ""))))
90
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))
94
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.")
165
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)))
173
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))))
183
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))))
205
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))))
215
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))))))))
239
240 (defun tramp-adb-handle-file-directory-p (filename)
241 "Like `file-directory-p' for Tramp files."
242 (car (file-attributes (file-truename filename))))
243
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)))
308 (t
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 "/"))))
324
325 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
326 result))))
327
328 ;; Preserve trailing "/".
329 (if (string-equal (file-name-nondirectory filename) "") "/" "")))
330
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))))))))
346
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
379 t 1
380 (tramp-get-device vec))
381 file-properties)))
382 file-properties)))
383
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)))
423 x))
424 result)))))))))
425
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")))
435
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))))))
451
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"))))
480
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)))
489
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)))))))
497
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)))
511
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)))
523
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)))
533
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 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)
549 f))
550 (with-current-buffer (tramp-get-buffer v)
551 (append
552 '("." "..")
553 (delq
554 nil
555 (mapcar
556 (lambda (l) (and (not (string-match "^[[:space:]]*$" l)) l))
557 (split-string (buffer-string) "\n")))))))))))
558
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)))
579
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))))
592
593 ;; Missing "test" command on Android < 4.
594 (let ((rw-path "/data/data"))
595 (tramp-message
596 v 5
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)))))))
602
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)))
633
634 (when (or (eq visit t) (stringp visit))
635 (set-visited-file-modtime))
636
637 (unless (equal curbuf (current-buffer))
638 (tramp-error
639 v 'file-error
640 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer))))))
641
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))))
648
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))))))
662
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))
670
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)
677
678 (let ((tmpfile (file-local-copy filename)))
679
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))))
687
688 ;; Remote newname.
689 (when (file-directory-p newname)
690 (setq newname
691 (expand-file-name (file-name-nondirectory filename) newname)))
692
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))
697
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))))))
705
706 ;; KEEP-DATE handling.
707 (when keep-date
708 (set-file-times newname (nth 5 (file-attributes filename))))))
709
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))
715
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)
721
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))
740
741 ;; Rename by copy.
742 (copy-file
743 filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
744 (delete-file filename))))))
745
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"))
752
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)))
770
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)))
809
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
817 v
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)))
836
837 ;; Provide error file.
838 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
839
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))
843
844 (unless process-file-side-effects
845 (tramp-flush-directory-property v ""))
846
847 ;; Return exit status.
848 (if (equal ret -1)
849 (keyboard-quit)
850 ret))))
851
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)))
881
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")))
888
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)))
896
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))
905
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)))))))
925
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"))
936
937 (let ((command
938 (format "cd %s; %s"
939 (tramp-shell-quote-argument localname)
940 (mapconcat 'tramp-shell-quote-argument
941 (cons program args) " ")))
942 (tramp-process-connection-type
943 (or (null program) tramp-process-connection-type))
944 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
945 (name1 name)
946 (i 0))
947
948 (unless buffer
949 ;; BUFFER can be nil. We use a temporary buffer.
950 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
951 (while (get-process name1)
952 ;; NAME must be unique as process name.
953 (setq i (1+ i)
954 name1 (format "%s<%d>" name i)))
955 (setq name name1)
956 ;; Set the new process properties.
957 (tramp-set-connection-property v "process-name" name)
958 (tramp-set-connection-property v "process-buffer" buffer)
959
960 (with-current-buffer (tramp-get-connection-buffer v)
961 (unwind-protect
962 ;; We catch this event. Otherwise, `start-process' could
963 ;; be called on the local host.
964 (save-excursion
965 (save-restriction
966 ;; Activate narrowing in order to save BUFFER
967 ;; contents. Clear also the modification time;
968 ;; otherwise we might be interrupted by
969 ;; `verify-visited-file-modtime'.
970 (let ((buffer-undo-list t)
971 (buffer-read-only nil)
972 (mark (point)))
973 (clear-visited-file-modtime)
974 (narrow-to-region (point-max) (point-max))
975 ;; We call `tramp-adb-maybe-open-connection', in
976 ;; order to cleanup the prompt afterwards.
977 (tramp-adb-maybe-open-connection v)
978 (widen)
979 (delete-region mark (point))
980 (narrow-to-region (point-max) (point-max))
981 ;; Send the command.
982 (let ((tramp-adb-prompt (regexp-quote command)))
983 (tramp-adb-send-command v command))
984 (let ((p (tramp-get-connection-process v)))
985 ;; Set query flag and process marker for this
986 ;; process. We ignore errors, because the process
987 ;; could have finished already.
988 (ignore-errors
989 (set-process-query-on-exit-flag p t)
990 (set-marker (process-mark p) (point)))
991 ;; Return process.
992 p))))
993
994 ;; Save exit.
995 (if (string-match tramp-temp-buffer-name (buffer-name))
996 (ignore-errors
997 (set-process-buffer (tramp-get-connection-process v) nil)
998 (kill-buffer (current-buffer)))
999 (set-buffer-modified-p bmp))
1000 (tramp-set-connection-property v "process-name" nil)
1001 (tramp-set-connection-property v "process-buffer" nil))))))
1002
1003 (defun tramp-adb-get-device (vec)
1004 "Return full host name from VEC to be used in shell execution.
1005 E.g. a host name \"192.168.1.1#5555\" returns \"192.168.1.1:5555\"
1006 a host name \"R38273882DE\" returns \"R38273882DE\"."
1007 ;; Sometimes this is called before there is a connection process
1008 ;; yet. In order to work with the connection cache, we flush all
1009 ;; unwanted entries first.
1010 (tramp-flush-connection-property nil)
1011 (with-tramp-connection-property (tramp-get-connection-process vec) "device"
1012 (let* ((method (tramp-file-name-method vec))
1013 (host (tramp-file-name-host vec))
1014 (port (tramp-file-name-port vec))
1015 (devices (mapcar 'cadr (tramp-adb-parse-device-names nil))))
1016 (replace-regexp-in-string
1017 tramp-prefix-port-format ":"
1018 (cond ((member host devices) host)
1019 ;; This is the case when the host is connected to the default port.
1020 ((member (format "%s%s%d" host tramp-prefix-port-format port)
1021 devices)
1022 (format "%s:%d" host port))
1023 ;; An empty host name shall be mapped as well, when there
1024 ;; is exactly one entry in `devices'.
1025 ((and (zerop (length host)) (= (length devices) 1))
1026 (car devices))
1027 ;; Try to connect device.
1028 ((and tramp-adb-connect-if-not-connected
1029 (not (zerop (length host)))
1030 (not (tramp-adb-execute-adb-command
1031 vec "connect"
1032 (replace-regexp-in-string
1033 tramp-prefix-port-format ":" host))))
1034 ;; When new device connected, running other adb command (e.g.
1035 ;; adb shell) immediately will fail. To get around this
1036 ;; problem, add sleep 0.1 second here.
1037 (sleep-for 0.1)
1038 host)
1039 (t (tramp-error
1040 vec 'file-error "Could not find device %s" host)))))))
1041
1042 (defun tramp-adb-execute-adb-command (vec &rest args)
1043 "Returns nil on success error-output on failure."
1044 (when (and (> (length (tramp-file-name-host vec)) 0)
1045 ;; The -s switch is only available for ADB device commands.
1046 (not (member (car args) (list "connect" "disconnect"))))
1047 (setq args (append (list "-s" (tramp-adb-get-device vec)) args)))
1048 (with-temp-buffer
1049 (prog1
1050 (unless
1051 (zerop
1052 (apply 'tramp-call-process vec tramp-adb-program nil t nil args))
1053 (buffer-string))
1054 (tramp-message vec 6 "%s" (buffer-string)))))
1055
1056 (defun tramp-adb-find-test-command (vec)
1057 "Checks, whether the ash has a builtin \"test\" command.
1058 This happens for Android >= 4.0."
1059 (with-tramp-connection-property vec "test"
1060 (tramp-adb-send-command-and-check vec "type test")))
1061
1062 ;; Connection functions
1063
1064 (defun tramp-adb-send-command (vec command)
1065 "Send the COMMAND to connection VEC."
1066 (tramp-adb-maybe-open-connection vec)
1067 (tramp-message vec 6 "%s" command)
1068 (tramp-send-string vec command)
1069 ;; fixme: Race condition
1070 (tramp-adb-wait-for-output (tramp-get-connection-process vec))
1071 (with-current-buffer (tramp-get-connection-buffer vec)
1072 (save-excursion
1073 (goto-char (point-min))
1074 ;; We can't use stty to disable echo of command.
1075 (delete-matching-lines (regexp-quote command))
1076 ;; When the local machine is W32, there are still trailing ^M.
1077 ;; There must be a better solution by setting the correct coding
1078 ;; system, but this requires changes in core Tramp.
1079 (goto-char (point-min))
1080 (while (re-search-forward "\r+$" nil t)
1081 (replace-match "" nil nil)))))
1082
1083 (defun tramp-adb-send-command-and-check
1084 (vec command)
1085 "Run COMMAND and check its exit status.
1086 Sends `echo $?' along with the COMMAND for checking the exit
1087 status. If COMMAND is nil, just sends `echo $?'. Returns nil if
1088 the exit status is not equal 0, and t otherwise."
1089 (tramp-adb-send-command
1090 vec (if command
1091 (format "%s; echo tramp_exit_status $?" command)
1092 "echo tramp_exit_status $?"))
1093 (with-current-buffer (tramp-get-connection-buffer vec)
1094 (goto-char (point-max))
1095 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1096 (tramp-error
1097 vec 'file-error "Couldn't find exit status of `%s'" command))
1098 (skip-chars-forward "^ ")
1099 (prog1
1100 (zerop (read (current-buffer)))
1101 (let (buffer-read-only)
1102 (delete-region (match-beginning 0) (point-max))))))
1103
1104 (defun tramp-adb-barf-unless-okay (vec command fmt &rest args)
1105 "Run COMMAND, check exit status, throw error if exit status not okay.
1106 FMT and ARGS are passed to `error'."
1107 (unless (tramp-adb-send-command-and-check vec command)
1108 (apply 'tramp-error vec 'file-error fmt args)))
1109
1110 (defun tramp-adb-wait-for-output (proc &optional timeout)
1111 "Wait for output from remote command."
1112 (unless (buffer-live-p (process-buffer proc))
1113 (delete-process proc)
1114 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
1115 (with-current-buffer (process-buffer proc)
1116 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt)
1117 (let (buffer-read-only)
1118 (goto-char (point-min))
1119 ;; ADB terminal sends "^H" sequences.
1120 (when (re-search-forward "<\b+" (point-at-eol) t)
1121 (forward-line 1)
1122 (delete-region (point-min) (point)))
1123 ;; Delete the prompt.
1124 (goto-char (point-min))
1125 (when (re-search-forward tramp-adb-prompt (point-at-eol) t)
1126 (forward-line 1)
1127 (delete-region (point-min) (point)))
1128 (goto-char (point-max))
1129 (re-search-backward tramp-adb-prompt nil t)
1130 (delete-region (point) (point-max)))
1131 (if timeout
1132 (tramp-error
1133 proc 'file-error
1134 "[[Remote adb prompt `%s' not found in %d secs]]"
1135 tramp-adb-prompt timeout)
1136 (tramp-error
1137 proc 'file-error
1138 "[[Remote prompt `%s' not found]]" tramp-adb-prompt)))))
1139
1140 (defun tramp-adb-maybe-open-connection (vec)
1141 "Maybe open a connection VEC.
1142 Does not do anything if a connection is already open, but re-opens the
1143 connection if a previous connection has died for some reason."
1144 (tramp-check-proper-method-and-host vec)
1145
1146 (let* ((buf (tramp-get-connection-buffer vec))
1147 (p (get-buffer-process buf))
1148 (host (tramp-file-name-host vec))
1149 (user (tramp-file-name-user vec))
1150 (device (tramp-adb-get-device vec)))
1151
1152 ;; Set variables for proper tracing in `tramp-adb-parse-device-names'.
1153 (setq tramp-current-method (tramp-file-name-method vec)
1154 tramp-current-user (tramp-file-name-user vec)
1155 tramp-current-host (tramp-file-name-host vec))
1156
1157 ;; Maybe we know already that "su" is not supported. We cannot
1158 ;; use a connection property, because we have not checked yet
1159 ;; whether it is still the same device.
1160 (when (and user (not (tramp-get-file-property vec "" "su-command-p" t)))
1161 (tramp-error vec 'file-error "Cannot switch to user `%s'" user))
1162
1163 (unless
1164 (and p (processp p) (memq (process-status p) '(run open)))
1165 (save-match-data
1166 (when (and p (processp p)) (delete-process p))
1167 (if (zerop (length device))
1168 (tramp-error vec 'file-error "Device %s not connected" host))
1169 (with-tramp-progress-reporter vec 3 "Opening adb shell connection"
1170 (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
1171 (process-connection-type tramp-process-connection-type)
1172 (args (if (> (length host) 0)
1173 (list "-s" device "shell")
1174 (list "shell")))
1175 (p (let ((default-directory
1176 (tramp-compat-temporary-file-directory)))
1177 (apply 'start-process (tramp-get-connection-name vec) buf
1178 tramp-adb-program args))))
1179 (tramp-message
1180 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1181 ;; Wait for initial prompt.
1182 (tramp-adb-wait-for-output p 30)
1183 (unless (eq 'run (process-status p))
1184 (tramp-error vec 'file-error "Terminated!"))
1185 (tramp-set-connection-property p "vector" vec)
1186 (set-process-query-on-exit-flag p nil)
1187
1188 ;; Check whether the properties have been changed. If
1189 ;; yes, this is a strong indication that we must expire all
1190 ;; connection properties. We start again.
1191 (tramp-message vec 5 "Checking system information")
1192 (tramp-adb-send-command
1193 vec "echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1194 (let ((old-getprop
1195 (tramp-get-connection-property vec "getprop" nil))
1196 (new-getprop
1197 (tramp-set-connection-property
1198 vec "getprop"
1199 (with-current-buffer (tramp-get-connection-buffer vec)
1200 ;; Read the expression.
1201 (goto-char (point-min))
1202 (read (current-buffer))))))
1203 (when (and (stringp old-getprop)
1204 (not (string-equal old-getprop new-getprop)))
1205 (tramp-message
1206 vec 3
1207 "Connection reset, because remote host changed from `%s' to `%s'"
1208 old-getprop new-getprop)
1209 (tramp-cleanup-connection vec t)
1210 (tramp-adb-maybe-open-connection vec)))
1211
1212 ;; Change user if indicated.
1213 (when user
1214 (tramp-adb-send-command vec (format "su %s" user))
1215 (unless (tramp-adb-send-command-and-check vec nil)
1216 (delete-process p)
1217 (tramp-set-file-property vec "" "su-command-p" nil)
1218 (tramp-error
1219 vec 'file-error "Cannot switch to user `%s'" user)))
1220
1221 ;; Set "remote-path" connection property. This is needed
1222 ;; for eshell.
1223 (tramp-adb-send-command vec "echo \\\"$PATH\\\"")
1224 (tramp-set-connection-property
1225 vec "remote-path"
1226 (split-string
1227 (with-current-buffer (tramp-get-connection-buffer vec)
1228 ;; Read the expression.
1229 (goto-char (point-min))
1230 (read (current-buffer)))
1231 ":" 'omit))
1232
1233 ;; Mark it as connected.
1234 (tramp-set-connection-property p "connected" t)))))))
1235
1236 (add-hook 'tramp-unload-hook
1237 (lambda ()
1238 (unload-feature 'tramp-adb 'force)))
1239
1240 (provide 'tramp-adb)
1241
1242 ;;; tramp-adb.el ends here