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