1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
3 ;; Copyright (C) 2011-2015 Free Software Foundation, Inc.
5 ;; Author: Jürgen Hötzel <juergen@archlinux.org>
6 ;; Keywords: comm, processes
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/>.
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
30 ;; (setq tramp-adb-program "/path/to/adb")
32 ;; Due to security it is not possible to access non-root devices.
38 ;; Pacify byte-compiler.
39 (defvar directory-sep-char
)
42 (defcustom tramp-adb-program
"adb"
43 "Name of the Android Debug Bridge program."
49 (defcustom tramp-adb-connect-if-not-connected nil
50 "Try to run `adb connect' if provided device is not connected currently.
51 It is used for TCP/IP devices."
57 (defconst tramp-adb-method
"adb"
58 "*When this method name is used, forward all calls to Android Debug Bridge.")
61 (defcustom tramp-adb-prompt
62 "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
63 "Regexp used as prompt in almquist shell."
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:]]")
71 (defconst tramp-adb-ls-toolbox-regexp
73 "^[[:space:]]*\\([-[:alpha:]]+\\)" ; \1 permissions
74 "[[:space:]]*\\([^[:space:]]+\\)" ; \2 username
75 "[[:space:]]+\\([^[:space:]]+\\)" ; \3 group
76 "[[:space:]]+\\([[:digit:]]+\\)" ; \4 size
77 "[[:space:]]+\\([-[:digit:]]+[[:space:]][:[:digit:]]+\\)" ; \5 date
78 "[[:space:]]\\(.*\\)$")) ; \6 filename
81 (add-to-list 'tramp-methods
83 (tramp-tmpdir "/data/local/tmp")
84 (tramp-default-port 5555)))
87 (add-to-list 'tramp-default-host-alist
`(,tramp-adb-method nil
""))
90 (eval-after-load 'tramp
91 '(tramp-set-completion-function
92 tramp-adb-method
'((tramp-adb-parse-device-names ""))))
95 (add-to-list 'tramp-foreign-file-name-handler-alist
96 (cons 'tramp-adb-file-name-p
'tramp-adb-file-name-handler
))
98 (defconst tramp-adb-file-name-handler-alist
99 '((access-file . ignore
)
100 (add-name-to-file . tramp-adb-handle-copy-file
)
101 ;; `byte-compiler-base-file-name' performed by default handler.
102 ;; `copy-directory' performed by default handler.
103 (copy-file . tramp-adb-handle-copy-file
)
104 (delete-directory . tramp-adb-handle-delete-directory
)
105 (delete-file . tramp-adb-handle-delete-file
)
106 ;; `diff-latest-backup-file' performed by default handler.
107 (directory-file-name . tramp-handle-directory-file-name
)
108 (directory-files . tramp-handle-directory-files
)
109 (directory-files-and-attributes
110 . tramp-adb-handle-directory-files-and-attributes
)
111 (dired-call-process . ignore
)
112 (dired-compress-file . ignore
)
113 (dired-uncache . tramp-handle-dired-uncache
)
114 (expand-file-name . tramp-adb-handle-expand-file-name
)
115 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
117 (file-attributes . tramp-adb-handle-file-attributes
)
118 (file-directory-p . tramp-adb-handle-file-directory-p
)
119 ;; `file-equal-p' performed by default handler.
120 ;; FIXME: This is too sloppy.
121 (file-executable-p . tramp-handle-file-exists-p
)
122 (file-exists-p . tramp-handle-file-exists-p
)
123 ;; `file-in-directory-p' performed by default handler.
124 (file-local-copy . tramp-adb-handle-file-local-copy
)
125 (file-modes . tramp-handle-file-modes
)
126 (file-name-all-completions . tramp-adb-handle-file-name-all-completions
)
127 (file-name-as-directory . tramp-handle-file-name-as-directory
)
128 (file-name-completion . tramp-handle-file-name-completion
)
129 (file-name-directory . tramp-handle-file-name-directory
)
130 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
131 ;; `file-name-sans-versions' performed by default handler.
132 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
133 (file-notify-add-watch . tramp-handle-file-notify-add-watch
)
134 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch
)
135 (file-ownership-preserved-p . ignore
)
136 (file-readable-p . tramp-handle-file-exists-p
)
137 (file-regular-p . tramp-handle-file-regular-p
)
138 (file-remote-p . tramp-handle-file-remote-p
)
139 (file-selinux-context . ignore
)
140 (file-symlink-p . tramp-handle-file-symlink-p
)
141 (file-truename . tramp-adb-handle-file-truename
)
142 (file-writable-p . tramp-adb-handle-file-writable-p
)
143 (find-backup-file-name . tramp-handle-find-backup-file-name
)
144 ;; `find-file-noselect' performed by default handler.
145 ;; `get-file-buffer' performed by default handler.
146 (insert-directory . tramp-handle-insert-directory
)
147 (insert-file-contents . tramp-handle-insert-file-contents
)
148 (load . tramp-handle-load
)
149 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
150 (make-directory . tramp-adb-handle-make-directory
)
151 (make-directory-internal . ignore
)
152 (make-symbolic-link . tramp-handle-make-symbolic-link
)
153 (process-file . tramp-adb-handle-process-file
)
154 (rename-file . tramp-adb-handle-rename-file
)
155 (set-file-acl . ignore
)
156 (set-file-modes . tramp-adb-handle-set-file-modes
)
157 (set-file-selinux-context . ignore
)
158 (set-file-times . tramp-adb-handle-set-file-times
)
159 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
160 (shell-command . tramp-adb-handle-shell-command
)
161 (start-file-process . tramp-adb-handle-start-file-process
)
162 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
163 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
164 (vc-registered . ignore
)
165 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
)
166 (write-region . tramp-adb-handle-write-region
))
167 "Alist of handler functions for Tramp ADB method.")
169 ;; It must be a `defsubst' in order to push the whole code into
170 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
172 (defsubst tramp-adb-file-name-p
(filename)
173 "Check if it's a filename for ADB."
174 (let ((v (tramp-dissect-file-name filename
)))
175 (string= (tramp-file-name-method v
) tramp-adb-method
)))
178 (defun tramp-adb-file-name-handler (operation &rest args
)
179 "Invoke the ADB handler for OPERATION.
180 First arg specifies the OPERATION, second arg is a list of arguments to
181 pass to the OPERATION."
182 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
)))
184 (save-match-data (apply (cdr fn
) args
))
185 (tramp-run-real-handler operation args
))))
188 (defun tramp-adb-parse-device-names (_ignore)
189 "Return a list of (nil host) tuples allowed to access."
192 ;; `call-process' does not react on timer under MS Windows.
193 ;; That's why we use `start-process'.
194 (let ((p (start-process
195 tramp-adb-program
(current-buffer) tramp-adb-program
"devices"))
196 (v (vector tramp-adb-method tramp-current-user
197 tramp-current-host nil nil
))
199 (tramp-message v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
200 (tramp-compat-set-process-query-on-exit-flag p nil
)
201 (while (eq 'run
(process-status p
))
202 (accept-process-output p
0.1))
203 (accept-process-output p
0.1)
204 (tramp-message v
6 "\n%s" (buffer-string))
205 (goto-char (point-min))
206 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
207 (add-to-list 'result
(list nil
(match-string 1))))
209 ;; Replace ":" by "#".
214 (replace-regexp-in-string
215 ":" tramp-prefix-port-format
(car (cdr elt
)))))
219 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
220 "Like `expand-file-name' for Tramp files."
221 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
222 (setq dir
(or dir default-directory
"/"))
223 ;; Unless NAME is absolute, concat DIR and NAME.
224 (unless (file-name-absolute-p name
)
225 (setq name
(concat (file-name-as-directory dir
) name
)))
226 ;; If NAME is not a Tramp file, run the real handler.
227 (if (not (tramp-tramp-file-p name
))
228 (tramp-run-real-handler 'expand-file-name
(list name nil
))
230 (with-parsed-tramp-file-name name nil
231 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
232 (setq localname
(concat "/" localname
)))
233 ;; Do normal `expand-file-name' (this does "/./" and "/../").
234 ;; We bind `directory-sep-char' here for XEmacs on Windows,
235 ;; which would otherwise use backslash. `default-directory' is
236 ;; bound, because on Windows there would be problems with UNC
237 ;; shares or Cygwin mounts.
238 (let ((directory-sep-char ?
/)
239 (default-directory (tramp-compat-temporary-file-directory)))
240 (tramp-make-tramp-file-name
242 (tramp-drop-volume-letter
243 (tramp-run-real-handler
244 'expand-file-name
(list localname
))))))))
246 (defun tramp-adb-handle-file-directory-p (filename)
247 "Like `file-directory-p' for Tramp files."
248 (car (file-attributes (file-truename filename
))))
250 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
251 ;; code could be shared?
252 (defun tramp-adb-handle-file-truename (filename)
253 "Like `file-truename' for Tramp files."
256 (with-parsed-tramp-file-name (expand-file-name filename
) nil
257 (tramp-make-tramp-file-name
259 (with-tramp-file-property v localname
"file-truename"
260 (let ((result nil
)) ; result steps in reverse order
261 (tramp-message v
4 "Finding true name for `%s'" filename
)
262 (let* ((directory-sep-char ?
/)
263 (steps (tramp-compat-split-string localname
"/"))
264 (localnamedir (tramp-run-real-handler
265 'file-name-as-directory
(list localname
)))
266 (is-dir (string= localname localnamedir
))
269 ;; Don't make the following value larger than
270 ;; necessary. People expect an error message in a
271 ;; timely fashion when something is wrong; otherwise
272 ;; they might think that Emacs is hung. Of course,
273 ;; correctness has to come first.
276 (while (and steps
(< numchase numchase-limit
))
277 (setq thisstep
(pop steps
))
281 (append '("") (reverse result
) (list thisstep
))
284 (nth 0 (file-attributes
285 (tramp-make-tramp-file-name
292 (cond ((string= "." thisstep
)
293 (tramp-message v
5 "Ignoring step `.'"))
294 ((string= ".." thisstep
)
295 (tramp-message v
5 "Processing step `..'")
297 ((stringp symlink-target
)
298 ;; It's a symlink, follow it.
299 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
300 (setq numchase
(1+ numchase
))
301 (when (file-name-absolute-p symlink-target
)
303 ;; If the symlink was absolute, we'll get a string
304 ;; like "/user@host:/some/target"; extract the
305 ;; "/some/target" part from it.
306 (when (tramp-tramp-file-p symlink-target
)
307 (unless (tramp-equal-remote filename symlink-target
)
310 "Symlink target `%s' on wrong host" symlink-target
))
311 (setq symlink-target localname
))
313 (append (tramp-compat-split-string
318 (setq result
(cons thisstep result
)))))
319 (when (>= numchase numchase-limit
)
322 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
323 (setq result
(reverse result
))
324 ;; Combine list to form string.
327 (mapconcat 'identity
(cons "" result
) "/")
329 (when (and is-dir
(or (string= "" result
)
330 (not (string= (substring result -
1) "/"))))
331 (setq result
(concat result
"/"))))
333 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
336 ;; Preserve trailing "/".
337 (if (string-equal (file-name-nondirectory filename
) "") "/" "")))
339 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
340 "Like `file-attributes' for Tramp files."
341 (unless id-format
(setq id-format
'integer
))
343 (with-parsed-tramp-file-name filename nil
344 (with-tramp-file-property
345 v localname
(format "file-attributes-%s" id-format
)
347 (tramp-adb-send-command-and-check
348 v
(format "%s -d -l %s"
349 (tramp-adb-get-ls-command v
)
350 (tramp-shell-quote-argument localname
)))
351 (with-current-buffer (tramp-get-buffer v
)
352 (tramp-adb-sh-fix-ls-output)
353 (cdar (tramp-do-parse-file-attributes-with-ls v id-format
))))))))
355 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format
)
356 "Parse `file-attributes' for Tramp files using the ls(1) command."
357 (with-current-buffer (tramp-get-buffer vec
)
358 (goto-char (point-min))
359 (let ((file-properties nil
))
360 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t
)
361 (let* ((mod-string (match-string 1))
362 (is-dir (eq ?d
(aref mod-string
0)))
363 (is-symlink (eq ?l
(aref mod-string
0)))
364 (uid (match-string 2))
365 (gid (match-string 3))
366 (size (string-to-number (match-string 4)))
367 (date (match-string 5))
368 (name (match-string 6))
371 (cadr (split-string name
"\\( -> \\|\n\\)")))))
374 (car (split-string name
"\\( -> \\|\n\\)"))
376 (or is-dir symlink-target
)
378 ;; no way to handle numeric ids in Androids ash
379 (if (eq id-format
'integer
) 0 uid
)
380 (if (eq id-format
'integer
) 0 gid
)
382 (date-to-time date
) ; mtime
388 (tramp-get-device vec
))
392 (defun tramp-adb-handle-directory-files-and-attributes
393 (directory &optional full match nosort id-format
)
394 "Like `directory-files-and-attributes' for Tramp files."
395 (when (file-directory-p directory
)
396 (with-parsed-tramp-file-name (expand-file-name directory
) nil
397 (with-tramp-file-property
398 v localname
(format "directory-files-attributes-%s-%s-%s-%s"
399 full match id-format nosort
)
400 (with-current-buffer (tramp-get-buffer v
)
401 (when (tramp-adb-send-command-and-check
402 v
(format "%s -a -l %s"
403 (tramp-adb-get-ls-command v
)
404 (tramp-shell-quote-argument localname
)))
405 ;; We insert also filename/. and filename/.., because "ls" doesn't.
406 (narrow-to-region (point) (point))
407 (tramp-adb-send-command
408 v
(format "%s -d -a -l %s %s"
409 (tramp-adb-get-ls-command v
)
410 (tramp-shell-quote-argument
411 (concat (file-name-as-directory localname
) "."))
412 (tramp-shell-quote-argument
413 (concat (file-name-as-directory localname
) ".."))))
415 (tramp-adb-sh-fix-ls-output)
416 (let ((result (tramp-do-parse-file-attributes-with-ls
417 v
(or id-format
'integer
))))
422 (cons (expand-file-name (car x
) directory
) (cdr x
)))
426 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))
429 (if (or (not match
) (string-match match
(car x
)))
433 (defun tramp-adb-get-ls-command (vec)
434 (with-tramp-connection-property vec
"ls"
435 (tramp-message vec
5 "Finding a suitable `ls' command")
436 (if (tramp-adb-send-command-and-check vec
"ls --color=never -al /dev/null")
437 ;; On CyanogenMod based system BusyBox is used and "ls" output
438 ;; coloring is enabled by default. So we try to disable it
443 (defun tramp-adb--gnu-switches-to-ash
445 "Almquist shell can't handle multiple arguments.
446 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
450 (tramp-compat-replace-regexp-in-string
452 (tramp-compat-replace-regexp-in-string "^-" "" s
)))
453 ;; FIXME: Warning about removed switches (long and non-dash).
457 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
460 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
461 "Insert dummy 0 in empty size columns.
462 Androids \"ls\" command doesn't insert size column for directories:
463 Emacs dired can't find files."
465 ;; Insert missing size.
466 (goto-char (point-min))
468 (search-forward-regexp
469 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
470 (replace-match "0\\1" "\\1" nil
)
471 ;; Insert missing "/".
472 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
476 (let* ((lines (split-string (buffer-string) "\n" t
))
481 'tramp-adb-ls-output-time-less-p
482 'tramp-adb-ls-output-name-less-p
))))
483 (delete-region (point-min) (point-max))
484 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
485 ;; Add final newline.
486 (goto-char (point-max))
487 (unless (bolp) (insert "\n"))))
489 (defun tramp-adb-ls-output-time-less-p (a b
)
490 "Sort \"ls\" output by time, descending."
492 (string-match tramp-adb-ls-date-regexp a
)
493 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
494 (string-match tramp-adb-ls-date-regexp b
)
495 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
496 (time-less-p time-b time-a
)))
498 (defun tramp-adb-ls-output-name-less-p (a b
)
499 "Sort \"ls\" output by name, ascending."
501 (string-match directory-listing-before-filename-regexp a
)
502 (setq posa
(match-end 0))
503 (string-match directory-listing-before-filename-regexp b
)
504 (setq posb
(match-end 0))
505 (string-lessp (substring a posa
) (substring b posb
))))
507 (defun tramp-adb-handle-make-directory (dir &optional parents
)
508 "Like `make-directory' for Tramp files."
509 (setq dir
(expand-file-name dir
))
510 (with-parsed-tramp-file-name dir nil
512 (let ((par (expand-file-name ".." dir
)))
513 (unless (file-directory-p par
)
514 (make-directory par parents
))))
515 (tramp-adb-barf-unless-okay
516 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
517 "Couldn't make directory %s" dir
)
518 (tramp-flush-file-property v
(file-name-directory localname
))
519 (tramp-flush-directory-property v localname
)))
521 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
522 "Like `delete-directory' for Tramp files."
523 (setq directory
(expand-file-name directory
))
524 (with-parsed-tramp-file-name directory nil
525 (tramp-flush-file-property v
(file-name-directory localname
))
526 (tramp-flush-directory-property v localname
)
527 (tramp-adb-barf-unless-okay
529 (if recursive
"rm -r" "rmdir")
530 (tramp-shell-quote-argument localname
))
531 "Couldn't delete %s" directory
)))
533 (defun tramp-adb-handle-delete-file (filename &optional _trash
)
534 "Like `delete-file' for Tramp files."
535 (setq filename
(expand-file-name filename
))
536 (with-parsed-tramp-file-name filename nil
537 (tramp-flush-file-property v
(file-name-directory localname
))
538 (tramp-flush-file-property v localname
)
539 (tramp-adb-barf-unless-okay
540 v
(format "rm %s" (tramp-shell-quote-argument localname
))
541 "Couldn't delete %s" filename
)))
543 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
544 "Like `file-name-all-completions' for Tramp files."
547 (with-parsed-tramp-file-name directory nil
548 (with-tramp-file-property v localname
"file-name-all-completions"
550 (tramp-adb-send-command
552 (tramp-adb-get-ls-command v
)
553 (tramp-shell-quote-argument localname
)))
556 (if (file-directory-p (expand-file-name f directory
))
557 (file-name-as-directory f
)
559 (with-current-buffer (tramp-get-buffer v
)
565 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
566 (split-string (buffer-string) "\n")))))))))))
568 (defun tramp-adb-handle-file-local-copy (filename)
569 "Like `file-local-copy' for Tramp files."
570 (with-parsed-tramp-file-name filename nil
571 (unless (file-exists-p (file-truename filename
))
574 "Cannot make local copy of non-existing file `%s'" filename
))
575 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
576 (with-tramp-progress-reporter
577 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
578 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
579 (delete-file tmpfile
)
581 v
'file-error
"Cannot make local copy of file `%s'" filename
))
584 (logior (or (file-modes filename
) 0)
585 (tramp-compat-octal-to-decimal "0400"))))
588 (defun tramp-adb-handle-file-writable-p (filename)
589 "Like `tramp-sh-handle-file-writable-p'.
590 But handle the case, if the \"test\" command is not available."
591 (with-parsed-tramp-file-name filename nil
592 (with-tramp-file-property v localname
"file-writable-p"
593 (if (tramp-adb-find-test-command v
)
594 (if (file-exists-p filename
)
595 (tramp-adb-send-command-and-check
596 v
(format "test -w %s" (tramp-shell-quote-argument localname
)))
598 (file-directory-p (file-name-directory filename
))
599 (file-writable-p (file-name-directory filename
))))
601 ;; Missing "test" command on Android < 4.
602 (let ((rw-path "/data/data"))
605 "Not implemented yet (assuming \"/data/data\" is writable): %s"
607 (and (>= (length localname
) (length rw-path
))
608 (string= (substring localname
0 (length rw-path
))
611 (defun tramp-adb-handle-write-region
612 (start end filename
&optional append visit lockname confirm
)
613 "Like `write-region' for Tramp files."
614 (setq filename
(expand-file-name filename
))
615 (with-parsed-tramp-file-name filename nil
616 (when (and confirm
(file-exists-p filename
))
617 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
619 (tramp-error v
'file-error
"File not overwritten")))
620 ;; We must also flush the cache of the directory, because
621 ;; `file-attributes' reads the values from there.
622 (tramp-flush-file-property v
(file-name-directory localname
))
623 (tramp-flush-file-property v localname
)
624 (let* ((curbuf (current-buffer))
625 (tmpfile (tramp-compat-make-temp-file filename
)))
626 (when (and append
(file-exists-p filename
))
627 (copy-file filename tmpfile
'ok
)
630 (logior (or (file-modes tmpfile
) 0)
631 (tramp-compat-octal-to-decimal "0600"))))
632 (tramp-run-real-handler
634 (list start end tmpfile append
'no-message lockname confirm
))
635 (with-tramp-progress-reporter
636 v
3 (format "Moving tmp file `%s' to `%s'" tmpfile filename
)
638 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
639 (tramp-error v
'file-error
"Cannot write: `%s'" filename
))
640 (delete-file tmpfile
)))
642 (when (or (eq visit t
) (stringp visit
))
643 (set-visited-file-modtime))
645 (unless (equal curbuf
(current-buffer))
648 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
650 (defun tramp-adb-handle-set-file-modes (filename mode
)
651 "Like `set-file-modes' for Tramp files."
652 (with-parsed-tramp-file-name filename nil
653 (tramp-flush-file-property v localname
)
654 (tramp-adb-send-command-and-check
655 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
))))
657 (defun tramp-adb-handle-set-file-times (filename &optional time
)
658 "Like `set-file-times' for Tramp files."
659 (with-parsed-tramp-file-name filename nil
660 (tramp-flush-file-property v localname
)
661 (let ((time (if (or (null time
) (equal time
'(0 0)))
664 (tramp-adb-send-command-and-check
665 ;; Use shell arithmetic because of Emacs integer size limit.
666 v
(format "touch -t $(( %d * 65536 + %d )) %s"
667 (car time
) (cadr time
)
668 (tramp-shell-quote-argument localname
))))))
670 (defun tramp-adb-handle-copy-file
671 (filename newname
&optional ok-if-already-exists keep-date
672 _preserve-uid-gid _preserve-extended-attributes
)
673 "Like `copy-file' for Tramp files.
674 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
675 (setq filename
(expand-file-name filename
)
676 newname
(expand-file-name newname
))
678 (if (file-directory-p filename
)
679 (tramp-file-name-handler 'copy-directory filename newname keep-date t
)
680 (with-tramp-progress-reporter
681 (tramp-dissect-file-name
682 (if (tramp-tramp-file-p filename
) filename newname
))
683 0 (format "Copying %s to %s" filename newname
)
685 (let ((tmpfile (file-local-copy filename
)))
690 (rename-file tmpfile newname ok-if-already-exists
)
692 (delete-file tmpfile
)
693 (signal (car err
) (cdr err
))))
696 (when (file-directory-p newname
)
698 (expand-file-name (file-name-nondirectory filename
) newname
)))
700 (with-parsed-tramp-file-name newname nil
701 (when (and (not ok-if-already-exists
)
702 (file-exists-p newname
))
703 (tramp-error v
'file-already-exists newname
))
705 ;; We must also flush the cache of the directory, because
706 ;; `file-attributes' reads the values from there.
707 (tramp-flush-file-property v
(file-name-directory localname
))
708 (tramp-flush-file-property v localname
)
709 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
711 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
713 ;; KEEP-DATE handling.
715 (set-file-times newname
(nth 5 (file-attributes filename
))))))
717 (defun tramp-adb-handle-rename-file
718 (filename newname
&optional ok-if-already-exists
)
719 "Like `rename-file' for Tramp files."
720 (setq filename
(expand-file-name filename
)
721 newname
(expand-file-name newname
))
723 (let ((t1 (tramp-tramp-file-p filename
))
724 (t2 (tramp-tramp-file-p newname
)))
725 (with-parsed-tramp-file-name (if t1 filename newname
) nil
726 (with-tramp-progress-reporter
727 v
0 (format "Renaming %s to %s" filename newname
)
730 (tramp-equal-remote filename newname
)
731 (not (file-directory-p filename
)))
732 (let ((l1 (tramp-file-name-handler
733 'file-remote-p filename
'localname
))
734 (l2 (tramp-file-name-handler
735 'file-remote-p newname
'localname
)))
736 (when (and (not ok-if-already-exists
)
737 (file-exists-p newname
))
738 (tramp-error v
'file-already-exists newname
))
739 ;; We must also flush the cache of the directory, because
740 ;; `file-attributes' reads the values from there.
741 (tramp-flush-file-property v
(file-name-directory l1
))
742 (tramp-flush-file-property v l1
)
743 (tramp-flush-file-property v
(file-name-directory l2
))
744 (tramp-flush-file-property v l2
)
746 (tramp-adb-barf-unless-okay
747 v
(format "mv %s %s" l1 l2
)
748 "Error renaming %s to %s" filename newname
))
751 (copy-file filename newname ok-if-already-exists t t
)
752 (delete-file filename
))))))
754 (defun tramp-adb-handle-process-file
755 (program &optional infile destination display
&rest args
)
756 "Like `process-file' for Tramp files."
757 ;; The implementation is not complete yet.
758 (when (and (numberp destination
) (zerop destination
))
759 (error "Implementation does not handle immediate return"))
761 (with-parsed-tramp-file-name default-directory nil
762 (let (command input tmpinput stderr tmpstderr outbuf ret
)
764 (setq command
(mapconcat 'tramp-shell-quote-argument
765 (cons program args
) " "))
768 (setq input
"/dev/null")
769 (setq infile
(expand-file-name infile
))
770 (if (tramp-equal-remote default-directory infile
)
771 ;; INFILE is on the same remote host.
772 (setq input
(with-parsed-tramp-file-name infile nil localname
))
773 ;; INFILE must be copied to remote host.
774 (setq input
(tramp-make-tramp-temp-file v
)
775 tmpinput
(tramp-make-tramp-file-name method user host input
))
776 (copy-file infile tmpinput t
)))
777 (when input
(setq command
(format "%s <%s" command input
)))
782 ((bufferp destination
)
783 (setq outbuf destination
))
785 ((stringp destination
)
786 (setq outbuf
(get-buffer-create destination
)))
787 ;; (REAL-DESTINATION ERROR-DESTINATION)
791 ((bufferp (car destination
))
792 (setq outbuf
(car destination
)))
793 ((stringp (car destination
))
794 (setq outbuf
(get-buffer-create (car destination
))))
796 (setq outbuf
(current-buffer))))
799 ((stringp (cadr destination
))
800 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
801 (if (tramp-equal-remote default-directory
(cadr destination
))
802 ;; stderr is on the same remote host.
803 (setq stderr
(with-parsed-tramp-file-name
804 (cadr destination
) nil localname
))
805 ;; stderr must be copied to remote host. The temporary
806 ;; file must be deleted after execution.
807 (setq stderr
(tramp-make-tramp-temp-file v
)
808 tmpstderr
(tramp-make-tramp-file-name
809 method user host stderr
))))
810 ;; stderr to be discarded.
811 ((null (cadr destination
))
812 (setq stderr
"/dev/null"))))
815 (setq outbuf
(current-buffer))))
816 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
818 ;; Send the command. It might not return in time, so we protect
819 ;; it. Call it in a subshell, in order to preserve working
824 (if (tramp-adb-send-command-and-check
826 (format "(cd %s; %s)"
827 (tramp-shell-quote-argument localname
) command
))
828 ;; Set return status accordingly.
830 ;; We should add the output anyway.
832 (with-current-buffer outbuf
833 (insert-buffer-substring (tramp-get-connection-buffer v
)))
834 (when (and display
(get-buffer-window outbuf t
)) (redisplay))))
835 ;; When the user did interrupt, we should do it also. We use
836 ;; return code -1 as marker.
838 (kill-buffer (tramp-get-connection-buffer v
))
842 (kill-buffer (tramp-get-connection-buffer v
))
845 ;; Provide error file.
846 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
848 ;; Cleanup. We remove all file cache values for the connection,
849 ;; because the remote process could have changed them.
850 (when tmpinput
(delete-file tmpinput
))
852 ;; `process-file-side-effects' has been introduced with GNU
853 ;; Emacs 23.2. If set to `nil', no remote file will be changed
854 ;; by `program'. If it doesn't exist, we assume its default
856 (unless (and (boundp 'process-file-side-effects
)
857 (not (symbol-value 'process-file-side-effects
)))
858 (tramp-flush-directory-property v
""))
860 ;; Return exit status.
865 (defun tramp-adb-handle-shell-command
866 (command &optional output-buffer error-buffer
)
867 "Like `shell-command' for Tramp files."
868 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
869 ;; We cannot use `shell-file-name' and `shell-command-switch',
870 ;; they are variables of the local host.
871 (args (list "sh" "-c" (substring command
0 asynchronous
)))
875 ((bufferp output-buffer
) output-buffer
)
876 ((stringp output-buffer
) (get-buffer-create output-buffer
))
878 (setq current-buffer-p t
)
880 (t (get-buffer-create
882 "*Async Shell Command*"
883 "*Shell Command Output*")))))
886 ((bufferp error-buffer
) error-buffer
)
887 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
889 (if (and (not asynchronous
) error-buffer
)
890 (with-parsed-tramp-file-name default-directory nil
891 (list output-buffer
(tramp-make-tramp-temp-file v
)))
893 (p (get-buffer-process output-buffer
)))
895 ;; Check whether there is another process running. Tramp does not
896 ;; support 2 (asynchronous) processes in parallel.
898 (if (yes-or-no-p "A command is running. Kill it? ")
899 (ignore-errors (kill-process p
))
900 (tramp-user-error p
"Shell command in progress")))
904 (barf-if-buffer-read-only)
906 (with-current-buffer output-buffer
907 (setq buffer-read-only nil
)
910 (if (and (not current-buffer-p
) (integerp asynchronous
))
913 (apply 'start-file-process
"*Async Shell*" buffer args
)
915 (pop-to-buffer output-buffer
)
916 (setq mode-line-process
'(":%s"))
921 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
922 ;; Insert error messages if they were separated.
924 (with-current-buffer error-buffer
925 (insert-file-contents (cadr buffer
)))
926 (delete-file (cadr buffer
)))
928 ;; This is like exchange-point-and-mark, but doesn't
929 ;; activate the mark. It is cleaner to avoid activation,
930 ;; even though the command loop would deactivate the mark
931 ;; because we inserted text.
932 (goto-char (prog1 (mark t
)
933 (set-marker (mark-marker) (point)
935 ;; There's some output, display it.
936 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
937 (if (functionp 'display-message-or-buffer
)
938 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
939 (pop-to-buffer output-buffer
))))))))
941 ;; We use BUFFER also as connection buffer during setup. Because of
942 ;; this, its original contents must be saved, and restored once
943 ;; connection has been setup.
944 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
945 "Like `start-file-process' for Tramp files."
946 (with-parsed-tramp-file-name default-directory nil
947 ;; When PROGRAM is nil, we should provide a tty. This is not
949 (unless (stringp program
)
950 (tramp-error v
'file-error
"PROGRAM must be a string"))
954 (tramp-shell-quote-argument localname
)
955 (mapconcat 'tramp-shell-quote-argument
956 (cons program args
) " ")))
957 (tramp-process-connection-type
958 (or (null program
) tramp-process-connection-type
))
959 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
964 ;; BUFFER can be nil. We use a temporary buffer.
965 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
966 (while (get-process name1
)
967 ;; NAME must be unique as process name.
969 name1
(format "%s<%d>" name i
)))
971 ;; Set the new process properties.
972 (tramp-set-connection-property v
"process-name" name
)
973 (tramp-set-connection-property v
"process-buffer" buffer
)
975 (with-current-buffer (tramp-get-connection-buffer v
)
977 ;; We catch this event. Otherwise, `start-process' could
978 ;; be called on the local host.
981 ;; Activate narrowing in order to save BUFFER
982 ;; contents. Clear also the modification time;
983 ;; otherwise we might be interrupted by
984 ;; `verify-visited-file-modtime'.
985 (let ((buffer-undo-list t
)
986 (buffer-read-only nil
)
988 (clear-visited-file-modtime)
989 (narrow-to-region (point-max) (point-max))
990 ;; We call `tramp-adb-maybe-open-connection', in
991 ;; order to cleanup the prompt afterwards.
992 (tramp-adb-maybe-open-connection v
)
994 (delete-region mark
(point))
995 (narrow-to-region (point-max) (point-max))
997 (let ((tramp-adb-prompt (regexp-quote command
)))
998 (tramp-adb-send-command v command
))
999 (let ((p (tramp-get-connection-process v
)))
1000 ;; Set query flag and process marker for this
1001 ;; process. We ignore errors, because the process
1002 ;; could have finished already.
1004 (tramp-compat-set-process-query-on-exit-flag p t
)
1005 (set-marker (process-mark p
) (point)))
1010 (if (string-match tramp-temp-buffer-name
(buffer-name))
1012 (set-process-buffer (tramp-get-connection-process v
) nil
)
1013 (kill-buffer (current-buffer)))
1014 (set-buffer-modified-p bmp
))
1015 (tramp-set-connection-property v
"process-name" nil
)
1016 (tramp-set-connection-property v
"process-buffer" nil
))))))
1018 (defun tramp-adb-get-device (vec)
1019 "Return full host name from VEC to be used in shell execution.
1020 E.g. a host name \"192.168.1.1#5555\" returns \"192.168.1.1:5555\"
1021 a host name \"R38273882DE\" returns \"R38273882DE\"."
1022 ;; Sometimes this is called before there is a connection process
1023 ;; yet. In order to work with the connection cache, we flush all
1024 ;; unwanted entries first.
1025 (tramp-flush-connection-property nil
)
1026 (with-tramp-connection-property (tramp-get-connection-process vec
) "device"
1027 (let* ((method (tramp-file-name-method vec
))
1028 (host (tramp-file-name-host vec
))
1029 (port (tramp-file-name-port vec
))
1030 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1031 (replace-regexp-in-string
1032 tramp-prefix-port-format
":"
1033 (cond ((member host devices
) host
)
1034 ;; This is the case when the host is connected to the default port.
1035 ((member (format "%s%s%d" host tramp-prefix-port-format port
)
1037 (format "%s:%d" host port
))
1038 ;; An empty host name shall be mapped as well, when there
1039 ;; is exactly one entry in `devices'.
1040 ((and (zerop (length host
)) (= (length devices
) 1))
1042 ;; Try to connect device.
1043 ((and tramp-adb-connect-if-not-connected
1044 (not (zerop (length host
)))
1045 (not (tramp-adb-execute-adb-command
1047 (replace-regexp-in-string
1048 tramp-prefix-port-format
":" host
))))
1049 ;; When new device connected, running other adb command (e.g.
1050 ;; adb shell) immediately will fail. To get around this
1051 ;; problem, add sleep 0.1 second here.
1055 vec
'file-error
"Could not find device %s" host
)))))))
1057 (defun tramp-adb-execute-adb-command (vec &rest args
)
1058 "Returns nil on success error-output on failure."
1059 (when (and (> (length (tramp-file-name-host vec
)) 0)
1060 ;; The -s switch is only available for ADB device commands.
1061 (not (member (car args
) (list "connect" "disconnect"))))
1062 (setq args
(append (list "-s" (tramp-adb-get-device vec
)) args
)))
1067 (apply 'tramp-call-process vec tramp-adb-program nil t nil args
))
1069 (tramp-message vec
6 "%s" (buffer-string)))))
1071 (defun tramp-adb-find-test-command (vec)
1072 "Checks, whether the ash has a builtin \"test\" command.
1073 This happens for Android >= 4.0."
1074 (with-tramp-connection-property vec
"test"
1075 (tramp-adb-send-command-and-check vec
"type test")))
1077 ;; Connection functions
1079 (defun tramp-adb-send-command (vec command
)
1080 "Send the COMMAND to connection VEC."
1081 (tramp-adb-maybe-open-connection vec
)
1082 (tramp-message vec
6 "%s" command
)
1083 (tramp-send-string vec command
)
1084 ;; fixme: Race condition
1085 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
1086 (with-current-buffer (tramp-get-connection-buffer vec
)
1088 (goto-char (point-min))
1089 ;; We can't use stty to disable echo of command.
1090 (delete-matching-lines (regexp-quote command
))
1091 ;; When the local machine is W32, there are still trailing ^M.
1092 ;; There must be a better solution by setting the correct coding
1093 ;; system, but this requires changes in core Tramp.
1094 (goto-char (point-min))
1095 (while (re-search-forward "\r+$" nil t
)
1096 (replace-match "" nil nil
)))))
1098 (defun tramp-adb-send-command-and-check
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
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
)
1112 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1113 (skip-chars-forward "^ ")
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
)
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
)
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)))
1149 "[[Remote adb prompt `%s' not found in %d secs]]"
1150 tramp-adb-prompt timeout
)
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
))
1179 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1181 (when (and p
(processp p
)) (delete-process p
))
1182 (if (zerop (length device
))
1183 (tramp-error vec
'file-error
"Device %s not connected" host
))
1184 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1185 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1186 (process-connection-type tramp-process-connection-type
)
1187 (args (if (> (length host
) 0)
1188 (list "-s" device
"shell")
1190 (p (let ((default-directory
1191 (tramp-compat-temporary-file-directory)))
1192 (apply 'start-process
(tramp-get-connection-name vec
) buf
1193 tramp-adb-program args
))))
1195 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1196 ;; Wait for initial prompt.
1197 (tramp-adb-wait-for-output p
30)
1198 (unless (eq 'run
(process-status p
))
1199 (tramp-error vec
'file-error
"Terminated!"))
1200 (tramp-set-connection-property p
"vector" vec
)
1201 (tramp-compat-set-process-query-on-exit-flag p nil
)
1203 ;; Check whether the properties have been changed. If
1204 ;; yes, this is a strong indication that we must expire all
1205 ;; connection properties. We start again.
1206 (tramp-message vec
5 "Checking system information")
1207 (tramp-adb-send-command
1208 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1210 (tramp-get-connection-property vec
"getprop" nil
))
1212 (tramp-set-connection-property
1214 (with-current-buffer (tramp-get-connection-buffer vec
)
1215 ;; Read the expression.
1216 (goto-char (point-min))
1217 (read (current-buffer))))))
1218 (when (and (stringp old-getprop
)
1219 (not (string-equal old-getprop new-getprop
)))
1222 "Connection reset, because remote host changed from `%s' to `%s'"
1223 old-getprop new-getprop
)
1224 (tramp-cleanup-connection vec t
)
1225 (tramp-adb-maybe-open-connection vec
)))
1227 ;; Change user if indicated.
1229 (tramp-adb-send-command vec
(format "su %s" user
))
1230 (unless (tramp-adb-send-command-and-check vec nil
)
1232 (tramp-set-file-property vec
"" "su-command-p" nil
)
1234 vec
'file-error
"Cannot switch to user `%s'" user
)))
1236 ;; Set "remote-path" connection property. This is needed
1238 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1239 (tramp-set-connection-property
1242 (with-current-buffer (tramp-get-connection-buffer vec
)
1243 ;; Read the expression.
1244 (goto-char (point-min))
1245 (read (current-buffer)))
1246 ":" 'omit-nulls
))))))))
1248 (add-hook 'tramp-unload-hook
1250 (unload-feature 'tramp-adb
'force
)))
1252 (provide 'tramp-adb
)
1254 ;;; tramp-adb.el ends here