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