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