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