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