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