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