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