Improve handling of defcustoms in tramp
[emacs.git] / lisp / net / tramp-adb.el
blob24b732255d422d446bc91997c041f77886946b6a
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 (eq 'run (process-status 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 (car (file-attributes (file-truename filename))))
251 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
252 ;; code could be shared?
253 (defun tramp-adb-handle-file-truename (filename)
254 "Like `file-truename' for Tramp files."
255 (format
256 "%s%s"
257 (with-parsed-tramp-file-name (expand-file-name filename) nil
258 (tramp-make-tramp-file-name
259 method user host
260 (with-tramp-file-property v localname "file-truename"
261 (let ((result nil)) ; result steps in reverse order
262 (tramp-message v 4 "Finding true name for `%s'" filename)
263 (let* ((steps (split-string localname "/" 'omit))
264 (localnamedir (tramp-run-real-handler
265 'file-name-as-directory (list localname)))
266 (is-dir (string= localname localnamedir))
267 (thisstep nil)
268 (numchase 0)
269 ;; Don't make the following value larger than
270 ;; necessary. People expect an error message in a
271 ;; timely fashion when something is wrong; otherwise
272 ;; they might think that Emacs is hung. Of course,
273 ;; correctness has to come first.
274 (numchase-limit 20)
275 symlink-target)
276 (while (and steps (< numchase numchase-limit))
277 (setq thisstep (pop steps))
278 (tramp-message
279 v 5 "Check %s"
280 (mapconcat 'identity
281 (append '("") (reverse result) (list thisstep))
282 "/"))
283 (setq symlink-target
284 (nth 0 (file-attributes
285 (tramp-make-tramp-file-name
286 method user host
287 (mapconcat 'identity
288 (append '("")
289 (reverse result)
290 (list thisstep))
291 "/")))))
292 (cond ((string= "." thisstep)
293 (tramp-message v 5 "Ignoring step `.'"))
294 ((string= ".." thisstep)
295 (tramp-message v 5 "Processing step `..'")
296 (pop result))
297 ((stringp symlink-target)
298 ;; It's a symlink, follow it.
299 (tramp-message v 5 "Follow symlink to %s" symlink-target)
300 (setq numchase (1+ numchase))
301 (when (file-name-absolute-p symlink-target)
302 (setq result nil))
303 ;; If the symlink was absolute, we'll get a string
304 ;; like "/user@host:/some/target"; extract the
305 ;; "/some/target" part from it.
306 (when (tramp-tramp-file-p symlink-target)
307 (unless (tramp-equal-remote filename symlink-target)
308 (tramp-error
309 v 'file-error
310 "Symlink target `%s' on wrong host" symlink-target))
311 (setq symlink-target localname))
312 (setq steps
313 (append (split-string symlink-target "/" 'omit)
314 steps)))
316 ;; It's a file.
317 (setq result (cons thisstep result)))))
318 (when (>= numchase numchase-limit)
319 (tramp-error
320 v 'file-error
321 "Maximum number (%d) of symlinks exceeded" numchase-limit))
322 (setq result (reverse result))
323 ;; Combine list to form string.
324 (setq result
325 (if result
326 (mapconcat 'identity (cons "" result) "/")
327 "/"))
328 (when (and is-dir (or (string= "" result)
329 (not (string= (substring result -1) "/"))))
330 (setq result (concat result "/"))))
332 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
333 result))))
335 ;; Preserve trailing "/".
336 (if (string-equal (file-name-nondirectory filename) "") "/" "")))
338 (defun tramp-adb-handle-file-attributes (filename &optional id-format)
339 "Like `file-attributes' for Tramp files."
340 (unless id-format (setq id-format 'integer))
341 (ignore-errors
342 (with-parsed-tramp-file-name filename nil
343 (with-tramp-file-property
344 v localname (format "file-attributes-%s" id-format)
345 (and
346 (tramp-adb-send-command-and-check
347 v (format "%s -d -l %s"
348 (tramp-adb-get-ls-command v)
349 (tramp-shell-quote-argument localname)))
350 (with-current-buffer (tramp-get-buffer v)
351 (tramp-adb-sh-fix-ls-output)
352 (cdar (tramp-do-parse-file-attributes-with-ls v id-format))))))))
354 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format)
355 "Parse `file-attributes' for Tramp files using the ls(1) command."
356 (with-current-buffer (tramp-get-buffer vec)
357 (goto-char (point-min))
358 (let ((file-properties nil))
359 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t)
360 (let* ((mod-string (match-string 1))
361 (is-dir (eq ?d (aref mod-string 0)))
362 (is-symlink (eq ?l (aref mod-string 0)))
363 (uid (match-string 2))
364 (gid (match-string 3))
365 (size (string-to-number (match-string 4)))
366 (date (match-string 5))
367 (name (match-string 6))
368 (symlink-target
369 (and is-symlink
370 (cadr (split-string name "\\( -> \\|\n\\)")))))
371 (push (list
372 (if is-symlink
373 (car (split-string name "\\( -> \\|\n\\)"))
374 name)
375 (or is-dir symlink-target)
376 1 ;link-count
377 ;; no way to handle numeric ids in Androids ash
378 (if (eq id-format 'integer) 0 uid)
379 (if (eq id-format 'integer) 0 gid)
380 '(0 0) ; atime
381 (date-to-time date) ; mtime
382 '(0 0) ; ctime
383 size
384 mod-string
385 ;; fake
387 (tramp-get-device vec))
388 file-properties)))
389 file-properties)))
391 (defun tramp-adb-handle-directory-files-and-attributes
392 (directory &optional full match nosort id-format)
393 "Like `directory-files-and-attributes' for Tramp files."
394 (when (file-directory-p directory)
395 (with-parsed-tramp-file-name (expand-file-name directory) nil
396 (copy-tree
397 (with-tramp-file-property
398 v localname (format "directory-files-and-attributes-%s-%s-%s-%s"
399 full match id-format nosort)
400 (with-current-buffer (tramp-get-buffer v)
401 (when (tramp-adb-send-command-and-check
402 v (format "%s -a -l %s"
403 (tramp-adb-get-ls-command v)
404 (tramp-shell-quote-argument localname)))
405 ;; We insert also filename/. and filename/.., because "ls" doesn't.
406 (narrow-to-region (point) (point))
407 (tramp-adb-send-command
408 v (format "%s -d -a -l %s %s"
409 (tramp-adb-get-ls-command v)
410 (tramp-shell-quote-argument
411 (concat (file-name-as-directory localname) "."))
412 (tramp-shell-quote-argument
413 (concat (file-name-as-directory localname) ".."))))
414 (widen))
415 (tramp-adb-sh-fix-ls-output)
416 (let ((result (tramp-do-parse-file-attributes-with-ls
417 v (or id-format 'integer))))
418 (when full
419 (setq result
420 (mapcar
421 (lambda (x)
422 (cons (expand-file-name (car x) directory) (cdr x)))
423 result)))
424 (unless nosort
425 (setq result
426 (sort result (lambda (x y) (string< (car x) (car y))))))
427 (delq nil
428 (mapcar (lambda (x)
429 (if (or (not match) (string-match match (car x)))
431 result)))))))))
433 (defun tramp-adb-get-ls-command (vec)
434 "Determine `ls' command at its arguments."
435 (with-tramp-connection-property vec "ls"
436 (tramp-message vec 5 "Finding a suitable `ls' command")
437 (if (tramp-adb-send-command-and-check vec "ls --color=never -al /dev/null")
438 ;; On CyanogenMod based system BusyBox is used and "ls" output
439 ;; coloring is enabled by default. So we try to disable it
440 ;; when possible.
441 "ls --color=never"
442 "ls")))
444 (defun tramp-adb--gnu-switches-to-ash (switches)
445 "Almquist shell can't handle multiple arguments.
446 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
447 (split-string
448 (apply 'concat
449 (mapcar (lambda (s)
450 (replace-regexp-in-string
451 "\\(.\\)" " -\\1" (replace-regexp-in-string "^-" "" s)))
452 ;; FIXME: Warning about removed switches (long and non-dash).
453 (delq nil
454 (mapcar
455 (lambda (s)
456 (and (not (string-match "\\(^--\\|^[^-]\\)" s)) s))
457 switches))))))
459 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time)
460 "Insert dummy 0 in empty size columns.
461 Androids \"ls\" command doesn't insert size column for directories:
462 Emacs dired can't find files."
463 (save-excursion
464 ;; Insert missing size.
465 (goto-char (point-min))
466 (while
467 (search-forward-regexp
468 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t)
469 (replace-match "0\\1" "\\1" nil)
470 ;; Insert missing "/".
471 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
472 (end-of-line)
473 (insert "/")))
474 ;; Sort entries.
475 (let* ((lines (split-string (buffer-string) "\n" t))
476 (sorted-lines
477 (sort
478 lines
479 (if sort-by-time
480 'tramp-adb-ls-output-time-less-p
481 'tramp-adb-ls-output-name-less-p))))
482 (delete-region (point-min) (point-max))
483 (insert " " (mapconcat 'identity sorted-lines "\n ")))
484 ;; Add final newline.
485 (goto-char (point-max))
486 (unless (bolp) (insert "\n"))))
488 (defun tramp-adb-ls-output-time-less-p (a b)
489 "Sort \"ls\" output by time, descending."
490 (let (time-a time-b)
491 (string-match tramp-adb-ls-date-regexp a)
492 (setq time-a (apply 'encode-time (parse-time-string (match-string 0 a))))
493 (string-match tramp-adb-ls-date-regexp b)
494 (setq time-b (apply 'encode-time (parse-time-string (match-string 0 b))))
495 (time-less-p time-b time-a)))
497 (defun tramp-adb-ls-output-name-less-p (a b)
498 "Sort \"ls\" output by name, ascending."
499 (if (string-match directory-listing-before-filename-regexp a)
500 (let ((posa (match-end 0)))
501 (if (string-match directory-listing-before-filename-regexp b)
502 (let ((posb (match-end 0)))
503 (string-lessp (substring a posa) (substring b posb)))))))
505 (defun tramp-adb-handle-make-directory (dir &optional parents)
506 "Like `make-directory' for Tramp files."
507 (setq dir (expand-file-name dir))
508 (with-parsed-tramp-file-name dir nil
509 (when parents
510 (let ((par (expand-file-name ".." dir)))
511 (unless (file-directory-p par)
512 (make-directory par parents))))
513 (tramp-adb-barf-unless-okay
514 v (format "mkdir %s" (tramp-shell-quote-argument localname))
515 "Couldn't make directory %s" dir)
516 (tramp-flush-file-property v (file-name-directory localname))
517 (tramp-flush-directory-property v localname)))
519 (defun tramp-adb-handle-delete-directory (directory &optional recursive)
520 "Like `delete-directory' for Tramp files."
521 (setq directory (expand-file-name directory))
522 (with-parsed-tramp-file-name directory nil
523 (tramp-flush-file-property v (file-name-directory localname))
524 (tramp-flush-directory-property v localname)
525 (tramp-adb-barf-unless-okay
526 v (format "%s %s"
527 (if recursive "rm -r" "rmdir")
528 (tramp-shell-quote-argument localname))
529 "Couldn't delete %s" directory)))
531 (defun tramp-adb-handle-delete-file (filename &optional _trash)
532 "Like `delete-file' for Tramp files."
533 (setq filename (expand-file-name filename))
534 (with-parsed-tramp-file-name filename nil
535 (tramp-flush-file-property v (file-name-directory localname))
536 (tramp-flush-file-property v localname)
537 (tramp-adb-barf-unless-okay
538 v (format "rm %s" (tramp-shell-quote-argument localname))
539 "Couldn't delete %s" filename)))
541 (defun tramp-adb-handle-file-name-all-completions (filename directory)
542 "Like `file-name-all-completions' for Tramp files."
543 (all-completions
544 filename
545 (with-parsed-tramp-file-name (expand-file-name directory) nil
546 (with-tramp-file-property v localname "file-name-all-completions"
547 (save-match-data
548 (tramp-adb-send-command
549 v (format "%s -a %s"
550 (tramp-adb-get-ls-command v)
551 (tramp-shell-quote-argument localname)))
552 (mapcar
553 (lambda (f)
554 (if (file-directory-p (expand-file-name f directory))
555 (file-name-as-directory f)
557 (with-current-buffer (tramp-get-buffer v)
558 (append
559 '("." "..")
560 (delq
562 (mapcar
563 (lambda (l) (and (not (string-match "^[[:space:]]*$" l)) l))
564 (split-string (buffer-string) "\n")))))))))))
566 (defun tramp-adb-handle-file-local-copy (filename)
567 "Like `file-local-copy' for Tramp files."
568 (with-parsed-tramp-file-name filename nil
569 (unless (file-exists-p (file-truename filename))
570 (tramp-error
571 v 'file-error
572 "Cannot make local copy of non-existing file `%s'" filename))
573 (let ((tmpfile (tramp-compat-make-temp-file filename)))
574 (with-tramp-progress-reporter
575 v 3 (format "Fetching %s to tmp file %s" filename tmpfile)
576 ;; "adb pull ..." does not always return an error code.
577 (when (or (tramp-adb-execute-adb-command v "pull" localname tmpfile)
578 (not (file-exists-p tmpfile)))
579 (ignore-errors (delete-file tmpfile))
580 (tramp-error
581 v 'file-error "Cannot make local copy of file `%s'" filename))
582 (set-file-modes
583 tmpfile
584 (logior (or (file-modes filename) 0) (string-to-number "0400" 8))))
585 tmpfile)))
587 (defun tramp-adb-handle-file-writable-p (filename)
588 "Like `tramp-sh-handle-file-writable-p'.
589 But handle the case, if the \"test\" command is not available."
590 (with-parsed-tramp-file-name filename nil
591 (with-tramp-file-property v localname "file-writable-p"
592 (if (tramp-adb-find-test-command v)
593 (if (file-exists-p filename)
594 (tramp-adb-send-command-and-check
595 v (format "test -w %s" (tramp-shell-quote-argument localname)))
596 (and
597 (file-directory-p (file-name-directory filename))
598 (file-writable-p (file-name-directory filename))))
600 ;; Missing "test" command on Android < 4.
601 (let ((rw-path "/data/data"))
602 (tramp-message
604 "Not implemented yet (assuming \"/data/data\" is writable): %s"
605 localname)
606 (and (>= (length localname) (length rw-path))
607 (string= (substring localname 0 (length rw-path))
608 rw-path)))))))
610 (defun tramp-adb-handle-write-region
611 (start end filename &optional append visit lockname confirm)
612 "Like `write-region' for Tramp files."
613 (setq filename (expand-file-name filename))
614 (with-parsed-tramp-file-name filename nil
615 (when (and confirm (file-exists-p filename))
616 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
617 filename))
618 (tramp-error v 'file-error "File not overwritten")))
619 ;; We must also flush the cache of the directory, because
620 ;; `file-attributes' reads the values from there.
621 (tramp-flush-file-property v (file-name-directory localname))
622 (tramp-flush-file-property v localname)
623 (let* ((curbuf (current-buffer))
624 (tmpfile (tramp-compat-make-temp-file filename)))
625 (when (and append (file-exists-p filename))
626 (copy-file filename tmpfile 'ok)
627 (set-file-modes
628 tmpfile
629 (logior (or (file-modes tmpfile) 0) (string-to-number "0600" 8))))
630 (tramp-run-real-handler
631 'write-region
632 (list start end tmpfile append 'no-message lockname confirm))
633 (with-tramp-progress-reporter
634 v 3 (format-message
635 "Moving tmp file `%s' to `%s'" tmpfile filename)
636 (unwind-protect
637 (when (tramp-adb-execute-adb-command v "push" tmpfile localname)
638 (tramp-error v 'file-error "Cannot write: `%s'" filename))
639 (delete-file tmpfile)))
641 (when (or (eq visit t) (stringp visit))
642 (set-visited-file-modtime))
644 (unless (equal curbuf (current-buffer))
645 (tramp-error
646 v 'file-error
647 "Buffer has changed from `%s' to `%s'" curbuf (current-buffer))))))
649 (defun tramp-adb-handle-set-file-modes (filename mode)
650 "Like `set-file-modes' 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 (tramp-adb-send-command-and-check v (format "chmod %o %s" mode localname))))
656 (defun tramp-adb-handle-set-file-times (filename &optional time)
657 "Like `set-file-times' for Tramp files."
658 (with-parsed-tramp-file-name filename nil
659 (tramp-flush-file-property v (file-name-directory localname))
660 (tramp-flush-file-property v localname)
661 (let ((time (if (or (null time) (equal time '(0 0)))
662 (current-time)
663 time)))
664 (tramp-adb-send-command-and-check
665 ;; Use shell arithmetic because of Emacs integer size limit.
666 v (format "touch -t $(( %d * 65536 + %d )) %s"
667 (car time) (cadr time)
668 (tramp-shell-quote-argument localname))))))
670 (defun tramp-adb-handle-copy-file
671 (filename newname &optional ok-if-already-exists keep-date
672 _preserve-uid-gid _preserve-extended-attributes)
673 "Like `copy-file' for Tramp files.
674 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
675 (setq filename (expand-file-name filename)
676 newname (expand-file-name newname))
678 (if (file-directory-p filename)
679 (tramp-file-name-handler 'copy-directory filename newname keep-date t)
680 (with-tramp-progress-reporter
681 (tramp-dissect-file-name
682 (if (tramp-tramp-file-p filename) filename newname))
683 0 (format "Copying %s to %s" filename newname)
685 (let ((tmpfile (file-local-copy filename)))
687 (if tmpfile
688 ;; Remote filename.
689 (condition-case err
690 (rename-file tmpfile newname ok-if-already-exists)
691 ((error quit)
692 (delete-file tmpfile)
693 (signal (car err) (cdr err))))
695 ;; Remote newname.
696 (when (file-directory-p newname)
697 (setq newname
698 (expand-file-name (file-name-nondirectory filename) newname)))
700 (with-parsed-tramp-file-name newname nil
701 (when (and (not ok-if-already-exists)
702 (file-exists-p newname))
703 (tramp-error v 'file-already-exists newname))
705 ;; We must also flush the cache of the directory, because
706 ;; `file-attributes' reads the values from there.
707 (tramp-flush-file-property v (file-name-directory localname))
708 (tramp-flush-file-property v localname)
709 (when (tramp-adb-execute-adb-command v "push" filename localname)
710 (tramp-error
711 v 'file-error "Cannot copy `%s' `%s'" filename newname))))))
713 ;; KEEP-DATE handling.
714 (when keep-date
715 (set-file-times newname (nth 5 (file-attributes filename))))))
717 (defun tramp-adb-handle-rename-file
718 (filename newname &optional ok-if-already-exists)
719 "Like `rename-file' for Tramp files."
720 (setq filename (expand-file-name filename)
721 newname (expand-file-name newname))
723 (let ((t1 (tramp-tramp-file-p filename))
724 (t2 (tramp-tramp-file-p newname)))
725 (with-parsed-tramp-file-name (if t1 filename newname) nil
726 (with-tramp-progress-reporter
727 v 0 (format "Renaming %s to %s" filename newname)
729 (if (and t1 t2
730 (tramp-equal-remote filename newname)
731 (not (file-directory-p filename)))
732 (let ((l1 (file-remote-p filename 'localname))
733 (l2 (file-remote-p newname 'localname)))
734 (when (and (not ok-if-already-exists)
735 (file-exists-p newname))
736 (tramp-error v 'file-already-exists newname))
737 ;; We must also flush the cache of the directory, because
738 ;; `file-attributes' reads the values from there.
739 (tramp-flush-file-property v (file-name-directory l1))
740 (tramp-flush-file-property v l1)
741 (tramp-flush-file-property v (file-name-directory l2))
742 (tramp-flush-file-property v l2)
743 ;; Short track.
744 (tramp-adb-barf-unless-okay
745 v (format "mv %s %s" l1 l2)
746 "Error renaming %s to %s" filename newname))
748 ;; Rename by copy.
749 (copy-file
750 filename newname ok-if-already-exists 'keep-time 'preserve-uid-gid)
751 (delete-file filename))))))
753 (defun tramp-adb-handle-process-file
754 (program &optional infile destination display &rest args)
755 "Like `process-file' for Tramp files."
756 ;; The implementation is not complete yet.
757 (when (and (numberp destination) (zerop destination))
758 (error "Implementation does not handle immediate return"))
760 (with-parsed-tramp-file-name default-directory nil
761 (let (command input tmpinput stderr tmpstderr outbuf ret)
762 ;; Compute command.
763 (setq command (mapconcat 'tramp-shell-quote-argument
764 (cons program args) " "))
765 ;; Determine input.
766 (if (null infile)
767 (setq input "/dev/null")
768 (setq infile (expand-file-name infile))
769 (if (tramp-equal-remote default-directory infile)
770 ;; INFILE is on the same remote host.
771 (setq input (with-parsed-tramp-file-name infile nil localname))
772 ;; INFILE must be copied to remote host.
773 (setq input (tramp-make-tramp-temp-file v)
774 tmpinput (tramp-make-tramp-file-name method user host input))
775 (copy-file infile tmpinput t)))
776 (when input (setq command (format "%s <%s" command input)))
778 ;; Determine output.
779 (cond
780 ;; Just a buffer.
781 ((bufferp destination)
782 (setq outbuf destination))
783 ;; A buffer name.
784 ((stringp destination)
785 (setq outbuf (get-buffer-create destination)))
786 ;; (REAL-DESTINATION ERROR-DESTINATION)
787 ((consp destination)
788 ;; output.
789 (cond
790 ((bufferp (car destination))
791 (setq outbuf (car destination)))
792 ((stringp (car destination))
793 (setq outbuf (get-buffer-create (car destination))))
794 ((car destination)
795 (setq outbuf (current-buffer))))
796 ;; stderr.
797 (cond
798 ((stringp (cadr destination))
799 (setcar (cdr destination) (expand-file-name (cadr destination)))
800 (if (tramp-equal-remote default-directory (cadr destination))
801 ;; stderr is on the same remote host.
802 (setq stderr (with-parsed-tramp-file-name
803 (cadr destination) nil localname))
804 ;; stderr must be copied to remote host. The temporary
805 ;; file must be deleted after execution.
806 (setq stderr (tramp-make-tramp-temp-file v)
807 tmpstderr (tramp-make-tramp-file-name
808 method user host stderr))))
809 ;; stderr to be discarded.
810 ((null (cadr destination))
811 (setq stderr "/dev/null"))))
812 ;; 't
813 (destination
814 (setq outbuf (current-buffer))))
815 (when stderr (setq command (format "%s 2>%s" command stderr)))
817 ;; Send the command. It might not return in time, so we protect
818 ;; it. Call it in a subshell, in order to preserve working
819 ;; directory.
820 (condition-case nil
821 (progn
822 (setq ret
823 (if (tramp-adb-send-command-and-check
825 (format "(cd %s; %s)"
826 (tramp-shell-quote-argument localname) command))
827 ;; Set return status accordingly.
828 0 1))
829 ;; We should add the output anyway.
830 (when outbuf
831 (with-current-buffer outbuf
832 (insert-buffer-substring (tramp-get-connection-buffer v)))
833 (when (and display (get-buffer-window outbuf t)) (redisplay))))
834 ;; When the user did interrupt, we should do it also. We use
835 ;; return code -1 as marker.
836 (quit
837 (kill-buffer (tramp-get-connection-buffer v))
838 (setq ret -1))
839 ;; Handle errors.
840 (error
841 (kill-buffer (tramp-get-connection-buffer v))
842 (setq ret 1)))
844 ;; Provide error file.
845 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
847 ;; Cleanup. We remove all file cache values for the connection,
848 ;; because the remote process could have changed them.
849 (when tmpinput (delete-file tmpinput))
851 (unless process-file-side-effects
852 (tramp-flush-directory-property v ""))
854 ;; Return exit status.
855 (if (equal ret -1)
856 (keyboard-quit)
857 ret))))
859 (defun tramp-adb-handle-shell-command
860 (command &optional output-buffer error-buffer)
861 "Like `shell-command' for Tramp files."
862 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
863 ;; We cannot use `shell-file-name' and `shell-command-switch',
864 ;; they are variables of the local host.
865 (args (list "sh" "-c" (substring command 0 asynchronous)))
866 current-buffer-p
867 (output-buffer
868 (cond
869 ((bufferp output-buffer) output-buffer)
870 ((stringp output-buffer) (get-buffer-create output-buffer))
871 (output-buffer
872 (setq current-buffer-p t)
873 (current-buffer))
874 (t (get-buffer-create
875 (if asynchronous
876 "*Async Shell Command*"
877 "*Shell Command Output*")))))
878 (error-buffer
879 (cond
880 ((bufferp error-buffer) error-buffer)
881 ((stringp error-buffer) (get-buffer-create error-buffer))))
882 (buffer
883 (if (and (not asynchronous) error-buffer)
884 (with-parsed-tramp-file-name default-directory nil
885 (list output-buffer (tramp-make-tramp-temp-file v)))
886 output-buffer))
887 (p (get-buffer-process output-buffer)))
889 ;; Check whether there is another process running. Tramp does not
890 ;; support 2 (asynchronous) processes in parallel.
891 (when p
892 (if (yes-or-no-p "A command is running. Kill it? ")
893 (ignore-errors (kill-process p))
894 (tramp-user-error p "Shell command in progress")))
896 (if current-buffer-p
897 (progn
898 (barf-if-buffer-read-only)
899 (push-mark nil t))
900 (with-current-buffer output-buffer
901 (setq buffer-read-only nil)
902 (erase-buffer)))
904 (if (and (not current-buffer-p) (integerp asynchronous))
905 (prog1
906 ;; Run the process.
907 (apply 'start-file-process "*Async Shell*" buffer args)
908 ;; Display output.
909 (pop-to-buffer output-buffer)
910 (setq mode-line-process '(":%s"))
911 (shell-mode))
913 (prog1
914 ;; Run the process.
915 (apply 'process-file (car args) nil buffer nil (cdr args))
916 ;; Insert error messages if they were separated.
917 (when (listp buffer)
918 (with-current-buffer error-buffer
919 (insert-file-contents (cadr buffer)))
920 (delete-file (cadr buffer)))
921 (if current-buffer-p
922 ;; This is like exchange-point-and-mark, but doesn't
923 ;; activate the mark. It is cleaner to avoid activation,
924 ;; even though the command loop would deactivate the mark
925 ;; because we inserted text.
926 (goto-char (prog1 (mark t)
927 (set-marker (mark-marker) (point)
928 (current-buffer))))
929 ;; There's some output, display it.
930 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
931 (display-message-or-buffer output-buffer)))))))
933 ;; We use BUFFER also as connection buffer during setup. Because of
934 ;; this, its original contents must be saved, and restored once
935 ;; connection has been setup.
936 (defun tramp-adb-handle-start-file-process (name buffer program &rest args)
937 "Like `start-file-process' for Tramp files."
938 (with-parsed-tramp-file-name default-directory nil
939 ;; When PROGRAM is nil, we should provide a tty. This is not
940 ;; possible here.
941 (unless (stringp program)
942 (tramp-error v 'file-error "PROGRAM must be a string"))
944 (let* ((buffer
945 (if buffer
946 (get-buffer-create buffer)
947 ;; BUFFER can be nil. We use a temporary buffer.
948 (generate-new-buffer tramp-temp-buffer-name)))
949 (command
950 (format "cd %s; %s"
951 (tramp-shell-quote-argument localname)
952 (mapconcat 'tramp-shell-quote-argument
953 (cons program args) " ")))
954 (tramp-process-connection-type
955 (or (null program) tramp-process-connection-type))
956 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
957 (name1 name)
958 (i 0))
960 (while (get-process name1)
961 ;; NAME must be unique as process name.
962 (setq i (1+ i)
963 name1 (format "%s<%d>" name i)))
964 (setq name name1)
965 ;; Set the new process properties.
966 (tramp-set-connection-property v "process-name" name)
967 (tramp-set-connection-property v "process-buffer" buffer)
969 (with-current-buffer (tramp-get-connection-buffer v)
970 (unwind-protect
971 ;; We catch this event. Otherwise, `start-process' could
972 ;; be called on the local host.
973 (save-excursion
974 (save-restriction
975 ;; Activate narrowing in order to save BUFFER
976 ;; contents. Clear also the modification time;
977 ;; otherwise we might be interrupted by
978 ;; `verify-visited-file-modtime'.
979 (let ((buffer-undo-list t)
980 (buffer-read-only nil)
981 (mark (point)))
982 (clear-visited-file-modtime)
983 (narrow-to-region (point-max) (point-max))
984 ;; We call `tramp-adb-maybe-open-connection', in
985 ;; order to cleanup the prompt afterwards.
986 (tramp-adb-maybe-open-connection v)
987 (widen)
988 (delete-region mark (point))
989 (narrow-to-region (point-max) (point-max))
990 ;; Send the command.
991 (let ((tramp-adb-prompt (regexp-quote command)))
992 (tramp-adb-send-command v command))
993 (let ((p (tramp-get-connection-process v)))
994 ;; Set query flag and process marker for this
995 ;; process. We ignore errors, because the process
996 ;; could have finished already.
997 (ignore-errors
998 (set-process-query-on-exit-flag p t)
999 (set-marker (process-mark p) (point)))
1000 ;; Return process.
1001 p))))
1003 ;; Save exit.
1004 (if (string-match tramp-temp-buffer-name (buffer-name))
1005 (ignore-errors
1006 (set-process-buffer (tramp-get-connection-process v) nil)
1007 (kill-buffer (current-buffer)))
1008 (set-buffer-modified-p bmp))
1009 (tramp-set-connection-property v "process-name" nil)
1010 (tramp-set-connection-property v "process-buffer" nil))))))
1012 (defun tramp-adb-get-device (vec)
1013 "Return full host name from VEC to be used in shell execution.
1014 E.g. a host name \"192.168.1.1#5555\" returns \"192.168.1.1:5555\"
1015 a host name \"R38273882DE\" returns \"R38273882DE\"."
1016 ;; Sometimes this is called before there is a connection process
1017 ;; yet. In order to work with the connection cache, we flush all
1018 ;; unwanted entries first.
1019 (tramp-flush-connection-property nil)
1020 (with-tramp-connection-property (tramp-get-connection-process vec) "device"
1021 (let* ((method (tramp-file-name-method vec))
1022 (host (tramp-file-name-host vec))
1023 (port (tramp-file-name-port vec))
1024 (devices (mapcar 'cadr (tramp-adb-parse-device-names nil))))
1025 (replace-regexp-in-string
1026 tramp-prefix-port-format ":"
1027 (cond ((member host devices) host)
1028 ;; This is the case when the host is connected to the default port.
1029 ((member (format "%s%s%d" host tramp-prefix-port-format port)
1030 devices)
1031 (format "%s:%d" host port))
1032 ;; An empty host name shall be mapped as well, when there
1033 ;; is exactly one entry in `devices'.
1034 ((and (zerop (length host)) (= (length devices) 1))
1035 (car devices))
1036 ;; Try to connect device.
1037 ((and tramp-adb-connect-if-not-connected
1038 (not (zerop (length host)))
1039 (not (tramp-adb-execute-adb-command
1040 vec "connect"
1041 (replace-regexp-in-string
1042 tramp-prefix-port-format ":" host))))
1043 ;; When new device connected, running other adb command (e.g.
1044 ;; adb shell) immediately will fail. To get around this
1045 ;; problem, add sleep 0.1 second here.
1046 (sleep-for 0.1)
1047 host)
1048 (t (tramp-error
1049 vec 'file-error "Could not find device %s" host)))))))
1051 (defun tramp-adb-execute-adb-command (vec &rest args)
1052 "Returns nil on success error-output on failure."
1053 (when (and (> (length (tramp-file-name-host vec)) 0)
1054 ;; The -s switch is only available for ADB device commands.
1055 (not (member (car args) '("connect" "disconnect"))))
1056 (setq args (append (list "-s" (tramp-adb-get-device vec)) args)))
1057 (with-temp-buffer
1058 (prog1
1059 (unless
1060 (zerop
1061 (apply 'tramp-call-process vec tramp-adb-program nil t nil args))
1062 (buffer-string))
1063 (tramp-message vec 6 "%s" (buffer-string)))))
1065 (defun tramp-adb-find-test-command (vec)
1066 "Checks, whether the ash has a builtin \"test\" command.
1067 This happens for Android >= 4.0."
1068 (with-tramp-connection-property vec "test"
1069 (tramp-adb-send-command-and-check vec "type test")))
1071 ;; Connection functions
1073 (defun tramp-adb-send-command (vec command)
1074 "Send the COMMAND to connection VEC."
1075 (tramp-adb-maybe-open-connection vec)
1076 (tramp-message vec 6 "%s" command)
1077 (tramp-send-string vec command)
1078 ;; fixme: Race condition
1079 (tramp-adb-wait-for-output (tramp-get-connection-process vec))
1080 (with-current-buffer (tramp-get-connection-buffer vec)
1081 (save-excursion
1082 (goto-char (point-min))
1083 ;; We can't use stty to disable echo of command.
1084 (delete-matching-lines (regexp-quote command))
1085 ;; When the local machine is W32, there are still trailing ^M.
1086 ;; There must be a better solution by setting the correct coding
1087 ;; system, but this requires changes in core Tramp.
1088 (goto-char (point-min))
1089 (while (re-search-forward "\r+$" nil t)
1090 (replace-match "" nil nil)))))
1092 (defun tramp-adb-send-command-and-check (vec command)
1093 "Run COMMAND and check its exit status.
1094 Sends `echo $?' along with the COMMAND for checking the exit
1095 status. If COMMAND is nil, just sends `echo $?'. Returns nil if
1096 the exit status is not equal 0, and t otherwise."
1097 (tramp-adb-send-command
1098 vec (if command
1099 (format "%s; echo tramp_exit_status $?" command)
1100 "echo tramp_exit_status $?"))
1101 (with-current-buffer (tramp-get-connection-buffer vec)
1102 (goto-char (point-max))
1103 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
1104 (tramp-error
1105 vec 'file-error "Couldn't find exit status of `%s'" command))
1106 (skip-chars-forward "^ ")
1107 (prog1
1108 (zerop (read (current-buffer)))
1109 (let (buffer-read-only)
1110 (delete-region (match-beginning 0) (point-max))))))
1112 (defun tramp-adb-barf-unless-okay (vec command fmt &rest args)
1113 "Run COMMAND, check exit status, throw error if exit status not okay.
1114 FMT and ARGS are passed to `error'."
1115 (unless (tramp-adb-send-command-and-check vec command)
1116 (apply 'tramp-error vec 'file-error fmt args)))
1118 (defun tramp-adb-wait-for-output (proc &optional timeout)
1119 "Wait for output from remote command."
1120 (unless (buffer-live-p (process-buffer proc))
1121 (delete-process proc)
1122 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
1123 (with-current-buffer (process-buffer proc)
1124 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt)
1125 (let (buffer-read-only)
1126 (goto-char (point-min))
1127 ;; ADB terminal sends "^H" sequences.
1128 (when (re-search-forward "<\b+" (point-at-eol) t)
1129 (forward-line 1)
1130 (delete-region (point-min) (point)))
1131 ;; Delete the prompt.
1132 (goto-char (point-min))
1133 (when (re-search-forward tramp-adb-prompt (point-at-eol) t)
1134 (forward-line 1)
1135 (delete-region (point-min) (point)))
1136 (goto-char (point-max))
1137 (re-search-backward tramp-adb-prompt nil t)
1138 (delete-region (point) (point-max)))
1139 (if timeout
1140 (tramp-error
1141 proc 'file-error
1142 "[[Remote adb prompt `%s' not found in %d secs]]"
1143 tramp-adb-prompt timeout)
1144 (tramp-error
1145 proc 'file-error
1146 "[[Remote prompt `%s' not found]]" tramp-adb-prompt)))))
1148 (defun tramp-adb-maybe-open-connection (vec)
1149 "Maybe open a connection VEC.
1150 Does not do anything if a connection is already open, but re-opens the
1151 connection if a previous connection has died for some reason."
1152 (tramp-check-proper-method-and-host vec)
1154 (let* ((buf (tramp-get-connection-buffer vec))
1155 (p (get-buffer-process buf))
1156 (host (tramp-file-name-host vec))
1157 (user (tramp-file-name-user vec))
1158 (device (tramp-adb-get-device vec)))
1160 ;; Set variables for proper tracing in `tramp-adb-parse-device-names'.
1161 (setq tramp-current-method (tramp-file-name-method vec)
1162 tramp-current-user (tramp-file-name-user vec)
1163 tramp-current-host (tramp-file-name-host vec))
1165 ;; Maybe we know already that "su" is not supported. We cannot
1166 ;; use a connection property, because we have not checked yet
1167 ;; whether it is still the same device.
1168 (when (and user (not (tramp-get-file-property vec "" "su-command-p" t)))
1169 (tramp-error vec 'file-error "Cannot switch to user `%s'" user))
1171 (unless
1172 (and p (processp p) (memq (process-status p) '(run open)))
1173 (save-match-data
1174 (when (and p (processp p)) (delete-process p))
1175 (if (zerop (length device))
1176 (tramp-error vec 'file-error "Device %s not connected" host))
1177 (with-tramp-progress-reporter vec 3 "Opening adb shell connection"
1178 (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
1179 (process-connection-type tramp-process-connection-type)
1180 (args (if (> (length host) 0)
1181 (list "-s" device "shell")
1182 (list "shell")))
1183 (p (let ((default-directory
1184 (tramp-compat-temporary-file-directory)))
1185 (apply 'start-process (tramp-get-connection-name vec) buf
1186 tramp-adb-program args))))
1187 (tramp-message
1188 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
1189 ;; Wait for initial prompt.
1190 (tramp-adb-wait-for-output p 30)
1191 (unless (eq 'run (process-status p))
1192 (tramp-error vec 'file-error "Terminated!"))
1193 (tramp-set-connection-property p "vector" vec)
1194 (set-process-query-on-exit-flag p nil)
1196 ;; Check whether the properties have been changed. If
1197 ;; yes, this is a strong indication that we must expire all
1198 ;; connection properties. We start again.
1199 (tramp-message vec 5 "Checking system information")
1200 (tramp-adb-send-command
1201 vec "echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1202 (let ((old-getprop
1203 (tramp-get-connection-property vec "getprop" nil))
1204 (new-getprop
1205 (tramp-set-connection-property
1206 vec "getprop"
1207 (with-current-buffer (tramp-get-connection-buffer vec)
1208 ;; Read the expression.
1209 (goto-char (point-min))
1210 (read (current-buffer))))))
1211 (when (and (stringp old-getprop)
1212 (not (string-equal old-getprop new-getprop)))
1213 (tramp-message
1214 vec 3
1215 "Connection reset, because remote host changed from `%s' to `%s'"
1216 old-getprop new-getprop)
1217 (tramp-cleanup-connection vec t)
1218 (tramp-adb-maybe-open-connection vec)))
1220 ;; Change user if indicated.
1221 (when user
1222 (tramp-adb-send-command vec (format "su %s" user))
1223 (unless (tramp-adb-send-command-and-check vec nil)
1224 (delete-process p)
1225 (tramp-set-file-property vec "" "su-command-p" nil)
1226 (tramp-error
1227 vec 'file-error "Cannot switch to user `%s'" user)))
1229 ;; Set "remote-path" connection property. This is needed
1230 ;; for eshell.
1231 (tramp-adb-send-command vec "echo \\\"$PATH\\\"")
1232 (tramp-set-connection-property
1233 vec "remote-path"
1234 (split-string
1235 (with-current-buffer (tramp-get-connection-buffer vec)
1236 ;; Read the expression.
1237 (goto-char (point-min))
1238 (read (current-buffer)))
1239 ":" 'omit))
1241 ;; Mark it as connected.
1242 (tramp-set-connection-property p "connected" t)))))))
1244 (add-hook 'tramp-unload-hook
1245 (lambda ()
1246 (unload-feature 'tramp-adb 'force)))
1248 (provide 'tramp-adb)
1250 ;;; tramp-adb.el ends here