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