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