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