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