1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
3 ;; Copyright (C) 2011-2014 Free Software Foundation, Inc.
5 ;; Author: Juergen Hoetzel <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.
39 ;; Pacify byte-compiler.
40 (defvar directory-sep-char
)
41 (defvar dired-move-to-filename-regexp
)
43 (defcustom tramp-adb-program
"adb"
44 "Name of the Android Debug Bridge program."
50 (defconst tramp-adb-method
"adb"
51 "*When this method name is used, forward all calls to Android Debug Bridge.")
53 (defcustom tramp-adb-prompt
54 "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
55 "Regexp used as prompt in almquist shell."
60 (defconst tramp-adb-ls-date-regexp
61 "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]")
63 (defconst tramp-adb-ls-toolbox-regexp
65 "^[[:space:]]*\\([-[:alpha:]]+\\)" ; \1 permissions
66 "[[:space:]]*\\([^[:space:]]+\\)" ; \2 username
67 "[[:space:]]+\\([^[:space:]]+\\)" ; \3 group
68 "[[:space:]]+\\([[:digit:]]+\\)" ; \4 size
69 "[[:space:]]+\\([-[:digit:]]+[[:space:]][:[:digit:]]+\\)" ; \5 date
70 "[[:space:]]+\\(.*\\)$")) ; \6 filename
73 (add-to-list 'tramp-methods
75 (tramp-tmpdir "/data/local/tmp")))
78 (add-to-list 'tramp-default-host-alist
`(,tramp-adb-method nil
""))
81 (eval-after-load 'tramp
82 '(tramp-set-completion-function
83 tramp-adb-method
'((tramp-adb-parse-device-names ""))))
86 (add-to-list 'tramp-foreign-file-name-handler-alist
87 (cons 'tramp-adb-file-name-p
'tramp-adb-file-name-handler
))
89 (defconst tramp-adb-file-name-handler-alist
90 '((access-file . ignore
)
91 (add-name-to-file . tramp-adb-handle-copy-file
)
92 ;; `byte-compiler-base-file-name' performed by default handler.
93 ;; `copy-directory' performed by default handler.
94 (copy-file . tramp-adb-handle-copy-file
)
95 (delete-directory . tramp-adb-handle-delete-directory
)
96 (delete-file . tramp-adb-handle-delete-file
)
97 ;; `diff-latest-backup-file' performed by default handler.
98 (directory-file-name . tramp-handle-directory-file-name
)
99 (directory-files . tramp-handle-directory-files
)
100 (directory-files-and-attributes
101 . tramp-adb-handle-directory-files-and-attributes
)
102 (dired-call-process . ignore
)
103 (dired-compress-file . ignore
)
104 (dired-uncache . tramp-handle-dired-uncache
)
105 (expand-file-name . tramp-adb-handle-expand-file-name
)
106 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
108 (file-attributes . tramp-adb-handle-file-attributes
)
109 (file-directory-p . tramp-adb-handle-file-directory-p
)
110 ;; `file-equal-p' performed by default handler.
111 ;; FIXME: This is too sloppy.
112 (file-executable-p . tramp-handle-file-exists-p
)
113 (file-exists-p . tramp-handle-file-exists-p
)
114 ;; `file-in-directory-p' performed by default handler.
115 (file-local-copy . tramp-adb-handle-file-local-copy
)
116 (file-modes . tramp-handle-file-modes
)
117 (file-name-all-completions . tramp-adb-handle-file-name-all-completions
)
118 (file-name-as-directory . tramp-handle-file-name-as-directory
)
119 (file-name-completion . tramp-handle-file-name-completion
)
120 (file-name-directory . tramp-handle-file-name-directory
)
121 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
122 ;; `file-name-sans-versions' performed by default handler.
123 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
124 (file-notify-add-watch . tramp-handle-file-notify-add-watch
)
125 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch
)
126 (file-ownership-preserved-p . ignore
)
127 (file-readable-p . tramp-handle-file-exists-p
)
128 (file-regular-p . tramp-handle-file-regular-p
)
129 (file-remote-p . tramp-handle-file-remote-p
)
130 (file-selinux-context . ignore
)
131 (file-symlink-p . tramp-handle-file-symlink-p
)
132 (file-truename . tramp-adb-handle-file-truename
)
133 (file-writable-p . tramp-adb-handle-file-writable-p
)
134 (find-backup-file-name . tramp-handle-find-backup-file-name
)
135 ;; `find-file-noselect' performed by default handler.
136 ;; `get-file-buffer' performed by default handler.
137 (insert-directory . tramp-adb-handle-insert-directory
)
138 (insert-file-contents . tramp-handle-insert-file-contents
)
139 (load . tramp-handle-load
)
140 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
141 (make-directory . tramp-adb-handle-make-directory
)
142 (make-directory-internal . ignore
)
143 (make-symbolic-link . ignore
)
144 (process-file . tramp-adb-handle-process-file
)
145 (rename-file . tramp-adb-handle-rename-file
)
146 (set-file-acl . ignore
)
147 (set-file-modes . tramp-adb-handle-set-file-modes
)
148 (set-file-selinux-context . ignore
)
149 (set-file-times . tramp-adb-handle-set-file-times
)
150 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
151 (shell-command . tramp-adb-handle-shell-command
)
152 (start-file-process . tramp-adb-handle-start-file-process
)
153 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
154 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
155 (vc-registered . ignore
)
156 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
)
157 (write-region . tramp-adb-handle-write-region
))
158 "Alist of handler functions for Tramp ADB method.")
160 ;; It must be a `defsubst' in order to push the whole code into
161 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
163 (defsubst tramp-adb-file-name-p
(filename)
164 "Check if it's a filename for ADB."
165 (let ((v (tramp-dissect-file-name filename
)))
166 (string= (tramp-file-name-method v
) tramp-adb-method
)))
169 (defun tramp-adb-file-name-handler (operation &rest args
)
170 "Invoke the ADB handler for OPERATION.
171 First arg specifies the OPERATION, second arg is a list of arguments to
172 pass to the OPERATION."
173 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
)))
175 (save-match-data (apply (cdr fn
) args
))
176 (tramp-run-real-handler operation args
))))
179 (defun tramp-adb-parse-device-names (_ignore)
180 "Return a list of (nil host) tuples allowed to access."
183 ;; `call-process' does not react on timer under MS Windows.
184 ;; That's why we use `start-process'.
185 (let ((p (start-process
186 tramp-adb-program
(current-buffer) tramp-adb-program
"devices"))
188 (tramp-compat-set-process-query-on-exit-flag p nil
)
189 (while (eq 'run
(process-status p
))
191 (goto-char (point-min))
192 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
193 (add-to-list 'result
(list nil
(match-string 1))))
196 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
197 "Like `expand-file-name' for Tramp files."
198 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
199 (setq dir
(or dir default-directory
"/"))
200 ;; Unless NAME is absolute, concat DIR and NAME.
201 (unless (file-name-absolute-p name
)
202 (setq name
(concat (file-name-as-directory dir
) name
)))
203 ;; If NAME is not a Tramp file, run the real handler.
204 (if (not (tramp-tramp-file-p name
))
205 (tramp-run-real-handler 'expand-file-name
(list name nil
))
207 (with-parsed-tramp-file-name name nil
208 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
209 (setq localname
(concat "/" localname
)))
210 ;; Do normal `expand-file-name' (this does "/./" and "/../").
211 ;; We bind `directory-sep-char' here for XEmacs on Windows,
212 ;; which would otherwise use backslash. `default-directory' is
213 ;; bound, because on Windows there would be problems with UNC
214 ;; shares or Cygwin mounts.
215 (let ((directory-sep-char ?
/)
216 (default-directory (tramp-compat-temporary-file-directory)))
217 (tramp-make-tramp-file-name
219 (tramp-drop-volume-letter
220 (tramp-run-real-handler
221 'expand-file-name
(list localname
))))))))
223 (defun tramp-adb-handle-file-directory-p (filename)
224 "Like `file-directory-p' for Tramp files."
225 (car (file-attributes (file-truename filename
))))
227 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
228 ;; code could be shared?
229 (defun tramp-adb-handle-file-truename (filename)
230 "Like `file-truename' for Tramp files."
231 (with-parsed-tramp-file-name (expand-file-name filename
) nil
232 (with-tramp-file-property v localname
"file-truename"
233 (let ((result nil
)) ; result steps in reverse order
234 (tramp-message v
4 "Finding true name for `%s'" filename
)
235 (let* ((directory-sep-char ?
/)
236 (steps (tramp-compat-split-string localname
"/"))
237 (localnamedir (tramp-run-real-handler
238 'file-name-as-directory
(list localname
)))
239 (is-dir (string= localname localnamedir
))
242 ;; Don't make the following value larger than
243 ;; necessary. People expect an error message in a
244 ;; timely fashion when something is wrong; otherwise
245 ;; they might think that Emacs is hung. Of course,
246 ;; correctness has to come first.
249 (while (and steps
(< numchase numchase-limit
))
250 (setq thisstep
(pop steps
))
254 (append '("") (reverse result
) (list thisstep
))
257 (nth 0 (file-attributes
258 (tramp-make-tramp-file-name
265 (cond ((string= "." thisstep
)
266 (tramp-message v
5 "Ignoring step `.'"))
267 ((string= ".." thisstep
)
268 (tramp-message v
5 "Processing step `..'")
270 ((stringp symlink-target
)
271 ;; It's a symlink, follow it.
272 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
273 (setq numchase
(1+ numchase
))
274 (when (file-name-absolute-p symlink-target
)
276 ;; If the symlink was absolute, we'll get a string
277 ;; like "/user@host:/some/target"; extract the
278 ;; "/some/target" part from it.
279 (when (tramp-tramp-file-p symlink-target
)
280 (unless (tramp-equal-remote filename symlink-target
)
283 "Symlink target `%s' on wrong host" symlink-target
))
284 (setq symlink-target localname
))
286 (append (tramp-compat-split-string
291 (setq result
(cons thisstep result
)))))
292 (when (>= numchase numchase-limit
)
295 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
296 (setq result
(reverse result
))
297 ;; Combine list to form string.
300 (mapconcat 'identity
(cons "" result
) "/")
302 (when (and is-dir
(or (string= "" result
)
303 (not (string= (substring result -
1) "/"))))
304 (setq result
(concat result
"/"))))
306 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
307 (tramp-make-tramp-file-name method user host result
)))))
309 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
310 "Like `file-attributes' for Tramp files."
311 (unless id-format
(setq id-format
'integer
))
313 (with-parsed-tramp-file-name filename nil
314 (with-tramp-file-property
315 v localname
(format "file-attributes-%s" id-format
)
316 (tramp-adb-barf-unless-okay
317 v
(format "%s -d -l %s"
318 (tramp-adb-get-ls-command v
)
319 (tramp-shell-quote-argument localname
)) "")
320 (with-current-buffer (tramp-get-buffer v
)
321 (tramp-adb-sh-fix-ls-output)
322 (cdar (tramp-do-parse-file-attributes-with-ls v id-format
)))))))
324 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format
)
325 "Parse `file-attributes' for Tramp files using the ls(1) command."
326 (with-current-buffer (tramp-get-buffer vec
)
327 (goto-char (point-min))
328 (let ((file-properties nil
))
329 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t
)
330 (let* ((mod-string (match-string 1))
331 (is-dir (eq ?d
(aref mod-string
0)))
332 (is-symlink (eq ?l
(aref mod-string
0)))
333 (uid (match-string 2))
334 (gid (match-string 3))
335 (size (string-to-number (match-string 4)))
336 (date (match-string 5))
337 (name (match-string 6))
340 (cadr (split-string name
"\\( -> \\|\n\\)")))))
343 (car (split-string name
"\\( -> \\|\n\\)"))
345 (or is-dir symlink-target
)
347 ;; no way to handle numeric ids in Androids ash
348 (if (eq id-format
'integer
) 0 uid
)
349 (if (eq id-format
'integer
) 0 gid
)
351 (date-to-time date
) ; mtime
357 (tramp-get-device vec
))
361 (defun tramp-adb-handle-directory-files-and-attributes
362 (directory &optional full match nosort id-format
)
363 "Like `directory-files-and-attributes' for Tramp files."
364 (when (file-directory-p directory
)
365 (with-parsed-tramp-file-name (expand-file-name directory
) nil
366 (with-tramp-file-property
367 v localname
(format "directory-files-attributes-%s-%s-%s-%s"
368 full match id-format nosort
)
369 (tramp-adb-barf-unless-okay
370 v
(format "%s -a -l %s"
371 (tramp-adb-get-ls-command v
)
372 (tramp-shell-quote-argument localname
)) "")
373 (with-current-buffer (tramp-get-buffer v
)
374 (tramp-adb-sh-fix-ls-output)
375 (let ((result (tramp-do-parse-file-attributes-with-ls
376 v
(or id-format
'integer
))))
381 (cons (expand-file-name (car x
) directory
) (cdr x
)))
385 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))
388 (if (or (not match
) (string-match match
(car x
)))
392 (defun tramp-adb-get-ls-command (vec)
393 (with-tramp-connection-property vec
"ls"
394 (tramp-message vec
5 "Finding a suitable `ls' command")
395 (if (zerop (tramp-adb-command-exit-status
396 vec
"ls --color=never -al /dev/null"))
397 ;; On CyanogenMod based system BusyBox is used and "ls" output
398 ;; coloring is enabled by default. So we try to disable it
403 (defun tramp-adb--gnu-switches-to-ash
405 "Almquist shell can't handle multiple arguments.
406 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
410 (tramp-compat-replace-regexp-in-string
412 (tramp-compat-replace-regexp-in-string "^-" "" s
)))
413 ;; FIXME: Warning about removed switches (long and non-dash).
417 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
420 (defun tramp-adb-handle-insert-directory
421 (filename switches
&optional _wildcard _full-directory-p
)
422 "Like `insert-directory' for Tramp files."
423 (when (stringp switches
)
424 (setq switches
(tramp-adb--gnu-switches-to-ash (split-string switches
))))
425 (with-parsed-tramp-file-name (file-truename filename
) nil
426 (with-current-buffer (tramp-get-buffer v
)
427 (let ((name (tramp-shell-quote-argument (directory-file-name localname
)))
428 (switch-d (member "-d" switches
))
429 (switch-t (member "-t" switches
))
430 (switches (mapconcat 'identity
(remove "-t" switches
) " ")))
431 (tramp-adb-barf-unless-okay
432 v
(format "%s %s %s" (tramp-adb-get-ls-command v
) switches name
)
433 "Cannot insert directory listing: %s" filename
)
435 ;; We insert also filename/. and filename/.., because "ls" doesn't.
436 (narrow-to-region (point) (point))
438 (tramp-adb-barf-unless-okay
439 v
(format "%s -d %s %s %s"
440 (tramp-adb-get-ls-command v
)
442 (concat (file-name-as-directory name
) ".")
443 (concat (file-name-as-directory name
) ".."))
444 "Cannot insert directory listing: %s" filename
))
446 (tramp-adb-sh-fix-ls-output switch-t
)))
447 (insert-buffer-substring (tramp-get-buffer v
))))
449 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
450 "Insert dummy 0 in empty size columns.
451 Androids \"ls\" command doesn't insert size column for directories:
452 Emacs dired can't find files."
454 ;; Insert missing size.
455 (goto-char (point-min))
457 (search-forward-regexp
458 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
459 (replace-match "0\\1" "\\1" nil
)
460 ;; Insert missing "/".
461 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
465 (let* ((lines (split-string (buffer-string) "\n" t
))
470 'tramp-adb-ls-output-time-less-p
471 'tramp-adb-ls-output-name-less-p
))))
472 (delete-region (point-min) (point-max))
473 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
474 ;; Add final newline.
475 (goto-char (point-max))
476 (unless (= (point) (line-beginning-position))
480 (defun tramp-adb-ls-output-time-less-p (a b
)
481 "Sort \"ls\" output by time, descending."
483 (string-match tramp-adb-ls-date-regexp a
)
484 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
485 (string-match tramp-adb-ls-date-regexp b
)
486 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
487 (tramp-time-less-p time-b time-a
)))
489 (defun tramp-adb-ls-output-name-less-p (a b
)
490 "Sort \"ls\" output by name, ascending."
492 (string-match dired-move-to-filename-regexp a
)
493 (setq posa
(match-end 0))
494 (string-match dired-move-to-filename-regexp b
)
495 (setq posb
(match-end 0))
496 (string-lessp (substring a posa
) (substring b posb
))))
498 (defun tramp-adb-handle-make-directory (dir &optional parents
)
499 "Like `make-directory' for Tramp files."
500 (setq dir
(expand-file-name dir
))
501 (with-parsed-tramp-file-name dir nil
503 (let ((par (expand-file-name ".." dir
)))
504 (unless (file-directory-p par
)
505 (make-directory par parents
))))
506 (tramp-adb-barf-unless-okay
507 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
508 "Couldn't make directory %s" dir
)
509 (tramp-flush-directory-property v
(file-name-directory localname
))))
511 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
512 "Like `delete-directory' for Tramp files."
513 (setq directory
(expand-file-name directory
))
514 (with-parsed-tramp-file-name directory nil
515 (tramp-flush-file-property v
(file-name-directory localname
))
516 (tramp-flush-directory-property v localname
)
517 (tramp-adb-barf-unless-okay
519 (if recursive
"rm -r" "rmdir")
520 (tramp-shell-quote-argument localname
))
521 "Couldn't delete %s" directory
)))
523 (defun tramp-adb-handle-delete-file (filename &optional _trash
)
524 "Like `delete-file' for Tramp files."
525 (setq filename
(expand-file-name filename
))
526 (with-parsed-tramp-file-name filename nil
527 (tramp-flush-file-property v
(file-name-directory localname
))
528 (tramp-flush-file-property v localname
)
529 (tramp-adb-barf-unless-okay
530 v
(format "rm %s" (tramp-shell-quote-argument localname
))
531 "Couldn't delete %s" filename
)))
533 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
534 "Like `file-name-all-completions' for Tramp files."
537 (with-parsed-tramp-file-name directory nil
538 (with-tramp-file-property v localname
"file-name-all-completions"
540 (tramp-adb-send-command
542 (tramp-adb-get-ls-command v
)
543 (tramp-shell-quote-argument localname
)))
546 (if (file-directory-p f
)
547 (file-name-as-directory f
)
549 (with-current-buffer (tramp-get-buffer v
)
553 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
554 (split-string (buffer-string) "\n"))))))))))
556 (defun tramp-adb-handle-file-local-copy (filename)
557 "Like `file-local-copy' for Tramp files."
558 (with-parsed-tramp-file-name filename nil
559 (unless (file-exists-p (file-truename filename
))
562 "Cannot make local copy of non-existing file `%s'" filename
))
563 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
564 (with-tramp-progress-reporter
565 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
566 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
567 (delete-file tmpfile
)
569 v
'file-error
"Cannot make local copy of file `%s'" filename
))
570 (set-file-modes tmpfile
(file-modes filename
)))
573 (defun tramp-adb-handle-file-writable-p (filename)
574 "Like `tramp-sh-handle-file-writable-p'.
575 But handle the case, if the \"test\" command is not available."
576 (with-parsed-tramp-file-name filename nil
577 (with-tramp-file-property v localname
"file-writable-p"
578 (if (tramp-adb-find-test-command v
)
579 (if (file-exists-p filename
)
581 (tramp-adb-command-exit-status
582 v
(format "test -w %s" (tramp-shell-quote-argument localname
))))
584 (file-directory-p (file-name-directory filename
))
585 (file-writable-p (file-name-directory filename
))))
587 ;; Missing "test" command on Android < 4.
588 (let ((rw-path "/data/data"))
591 "Not implemented yet (assuming \"/data/data\" is writable): %s"
593 (and (>= (length localname
) (length rw-path
))
594 (string= (substring localname
0 (length rw-path
))
597 (defun tramp-adb-handle-write-region
598 (start end filename
&optional append visit lockname confirm
)
599 "Like `write-region' for Tramp files."
600 (setq filename
(expand-file-name filename
))
601 (with-parsed-tramp-file-name filename nil
604 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
605 (when (and confirm
(file-exists-p filename
))
606 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
608 (tramp-error v
'file-error
"File not overwritten")))
609 ;; We must also flush the cache of the directory, because
610 ;; `file-attributes' reads the values from there.
611 (tramp-flush-file-property v
(file-name-directory localname
))
612 (tramp-flush-file-property v localname
)
613 (let* ((curbuf (current-buffer))
614 (tmpfile (tramp-compat-make-temp-file filename
)))
615 (tramp-run-real-handler
617 (list start end tmpfile append
'no-message lockname confirm
))
618 (with-tramp-progress-reporter
619 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
621 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
622 (tramp-error v
'file-error
"Cannot write: `%s' filename"))
623 (delete-file tmpfile
)))
625 (when (or (eq visit t
) (stringp visit
))
626 (set-visited-file-modtime))
628 (unless (equal curbuf
(current-buffer))
631 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
633 (defun tramp-adb-handle-set-file-modes (filename mode
)
634 "Like `set-file-modes' for Tramp files."
635 (with-parsed-tramp-file-name filename nil
636 (tramp-flush-file-property v localname
)
637 (tramp-adb-barf-unless-okay
638 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
)
639 "Error while changing file's mode %s" filename
)))
641 (defun tramp-adb-handle-set-file-times (filename &optional time
)
642 "Like `set-file-times' for Tramp files."
643 (with-parsed-tramp-file-name filename nil
644 (tramp-flush-file-property v localname
)
645 (let ((time (if (or (null time
) (equal time
'(0 0)))
648 (tramp-adb-command-exit-status
649 ;; use shell arithmetic because of Emacs integer size limit
650 v
(format "touch -t $(( %d * 65536 + %d )) %s"
651 (car time
) (cadr time
)
652 (tramp-shell-quote-argument localname
))))))
654 (defun tramp-adb-handle-copy-file
655 (filename newname
&optional ok-if-already-exists keep-date
656 _preserve-uid-gid _preserve-extended-attributes
)
657 "Like `copy-file' for Tramp files.
658 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
659 (setq filename
(expand-file-name filename
)
660 newname
(expand-file-name newname
))
662 (if (file-directory-p filename
)
663 (tramp-file-name-handler 'copy-directory filename newname keep-date t
)
664 (with-tramp-progress-reporter
665 (tramp-dissect-file-name
666 (if (tramp-tramp-file-p filename
) filename newname
))
667 0 (format "Copying %s to %s" filename newname
)
669 (let ((tmpfile (file-local-copy filename
)))
674 (rename-file tmpfile newname ok-if-already-exists
)
676 (delete-file tmpfile
)
677 (signal (car err
) (cdr err
))))
680 (when (file-directory-p newname
)
682 (expand-file-name (file-name-nondirectory filename
) newname
)))
684 (with-parsed-tramp-file-name newname nil
685 (when (and (not ok-if-already-exists
)
686 (file-exists-p newname
))
687 (tramp-error v
'file-already-exists newname
))
689 ;; We must also flush the cache of the directory, because
690 ;; `file-attributes' reads the values from there.
691 (tramp-flush-file-property v
(file-name-directory localname
))
692 (tramp-flush-file-property v localname
)
693 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
695 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
697 ;; KEEP-DATE handling.
699 (set-file-times newname
(nth 5 (file-attributes filename
))))))
701 (defun tramp-adb-handle-rename-file
702 (filename newname
&optional ok-if-already-exists
)
703 "Like `rename-file' for Tramp files."
704 (setq filename
(expand-file-name filename
)
705 newname
(expand-file-name newname
))
707 (with-parsed-tramp-file-name
708 (if (tramp-tramp-file-p filename
) filename newname
) nil
709 (with-tramp-progress-reporter
710 v
0 (format "Renaming %s to %s" newname filename
)
712 (if (and (tramp-equal-remote filename newname
)
713 (not (file-directory-p filename
)))
715 (when (and (not ok-if-already-exists
)
716 (file-exists-p newname
))
717 (tramp-error v
'file-already-exists newname
))
718 ;; We must also flush the cache of the directory, because
719 ;; `file-attributes' reads the values from there.
720 (tramp-flush-file-property v
(file-name-directory localname
))
721 (tramp-flush-file-property v localname
)
723 (tramp-adb-barf-unless-okay
726 (tramp-file-name-handler 'file-remote-p filename
'localname
)
728 "Error renaming %s to %s" filename newname
))
731 (copy-file filename newname ok-if-already-exists t t
)
732 (delete-file filename
)))))
734 (defun tramp-adb-handle-process-file
735 (program &optional infile destination display
&rest args
)
736 "Like `process-file' for Tramp files."
737 ;; The implementation is not complete yet.
738 (when (and (numberp destination
) (zerop destination
))
739 (error "Implementation does not handle immediate return"))
741 (with-parsed-tramp-file-name default-directory nil
742 (let (command input tmpinput stderr tmpstderr outbuf ret
)
744 (setq command
(mapconcat 'tramp-shell-quote-argument
745 (cons program args
) " "))
748 (setq input
"/dev/null")
749 (setq infile
(expand-file-name infile
))
750 (if (tramp-equal-remote default-directory infile
)
751 ;; INFILE is on the same remote host.
752 (setq input
(with-parsed-tramp-file-name infile nil localname
))
753 ;; INFILE must be copied to remote host.
754 (setq input
(tramp-make-tramp-temp-file v
)
755 tmpinput
(tramp-make-tramp-file-name method user host input
))
756 (copy-file infile tmpinput t
)))
757 (when input
(setq command
(format "%s <%s" command input
)))
762 ((bufferp destination
)
763 (setq outbuf destination
))
765 ((stringp destination
)
766 (setq outbuf
(get-buffer-create destination
)))
767 ;; (REAL-DESTINATION ERROR-DESTINATION)
771 ((bufferp (car destination
))
772 (setq outbuf
(car destination
)))
773 ((stringp (car destination
))
774 (setq outbuf
(get-buffer-create (car destination
))))
776 (setq outbuf
(current-buffer))))
779 ((stringp (cadr destination
))
780 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
781 (if (tramp-equal-remote default-directory
(cadr destination
))
782 ;; stderr is on the same remote host.
783 (setq stderr
(with-parsed-tramp-file-name
784 (cadr destination
) nil localname
))
785 ;; stderr must be copied to remote host. The temporary
786 ;; file must be deleted after execution.
787 (setq stderr
(tramp-make-tramp-temp-file v
)
788 tmpstderr
(tramp-make-tramp-file-name
789 method user host stderr
))))
790 ;; stderr to be discarded.
791 ((null (cadr destination
))
792 (setq stderr
"/dev/null"))))
795 (setq outbuf
(current-buffer))))
796 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
798 ;; Send the command. It might not return in time, so we protect
799 ;; it. Call it in a subshell, in order to preserve working
804 (tramp-adb-barf-unless-okay
805 v
(format "(cd %s; %s)"
806 (tramp-shell-quote-argument localname
) command
)
808 ;; We should show the output anyway.
810 (with-current-buffer outbuf
811 (insert-buffer-substring (tramp-get-connection-buffer v
)))
812 (when display
(display-buffer outbuf
))))
813 ;; When the user did interrupt, we should do it also. We use
814 ;; return code -1 as marker.
816 (kill-buffer (tramp-get-connection-buffer v
))
820 (kill-buffer (tramp-get-connection-buffer v
))
823 ;; Provide error file.
824 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
826 ;; Cleanup. We remove all file cache values for the connection,
827 ;; because the remote process could have changed them.
828 (when tmpinput
(delete-file tmpinput
))
830 ;; `process-file-side-effects' has been introduced with GNU
831 ;; Emacs 23.2. If set to `nil', no remote file will be changed
832 ;; by `program'. If it doesn't exist, we assume its default
834 (unless (and (boundp 'process-file-side-effects
)
835 (not (symbol-value 'process-file-side-effects
)))
836 (tramp-flush-directory-property v
""))
838 ;; Return exit status.
843 (defun tramp-adb-handle-shell-command
844 (command &optional output-buffer error-buffer
)
845 "Like `shell-command' for Tramp files."
846 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
847 ;; We cannot use `shell-file-name' and `shell-command-switch',
848 ;; they are variables of the local host.
849 (args (list "sh" "-c" (substring command
0 asynchronous
)))
853 ((bufferp output-buffer
) output-buffer
)
854 ((stringp output-buffer
) (get-buffer-create output-buffer
))
856 (setq current-buffer-p t
)
858 (t (get-buffer-create
860 "*Async Shell Command*"
861 "*Shell Command Output*")))))
864 ((bufferp error-buffer
) error-buffer
)
865 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
867 (if (and (not asynchronous
) error-buffer
)
868 (with-parsed-tramp-file-name default-directory nil
869 (list output-buffer
(tramp-make-tramp-temp-file v
)))
871 (p (get-buffer-process output-buffer
)))
873 ;; Check whether there is another process running. Tramp does not
874 ;; support 2 (asynchronous) processes in parallel.
876 (if (yes-or-no-p "A command is running. Kill it? ")
877 (ignore-errors (kill-process p
))
878 (tramp-user-error p
"Shell command in progress")))
882 (barf-if-buffer-read-only)
884 (with-current-buffer output-buffer
885 (setq buffer-read-only nil
)
888 (if (and (not current-buffer-p
) (integerp asynchronous
))
891 (apply 'start-file-process
"*Async Shell*" buffer args
)
893 (pop-to-buffer output-buffer
)
894 (setq mode-line-process
'(":%s"))
899 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
900 ;; Insert error messages if they were separated.
902 (with-current-buffer error-buffer
903 (insert-file-contents (cadr buffer
)))
904 (delete-file (cadr buffer
)))
906 ;; This is like exchange-point-and-mark, but doesn't
907 ;; activate the mark. It is cleaner to avoid activation,
908 ;; even though the command loop would deactivate the mark
909 ;; because we inserted text.
910 (goto-char (prog1 (mark t
)
911 (set-marker (mark-marker) (point)
913 ;; There's some output, display it.
914 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
915 (if (functionp 'display-message-or-buffer
)
916 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
917 (pop-to-buffer output-buffer
))))))))
919 ;; We use BUFFER also as connection buffer during setup. Because of
920 ;; this, its original contents must be saved, and restored once
921 ;; connection has been setup.
922 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
923 "Like `start-file-process' for Tramp files."
924 (with-parsed-tramp-file-name default-directory nil
925 ;; When PROGRAM is nil, we should provide a tty. This is not
927 (unless (stringp program
)
928 (tramp-error v
'file-error
"PROGRAM must be a string"))
932 (tramp-shell-quote-argument localname
)
933 (mapconcat 'tramp-shell-quote-argument
934 (cons program args
) " ")))
935 (tramp-process-connection-type
936 (or (null program
) tramp-process-connection-type
))
937 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
942 ;; BUFFER can be nil. We use a temporary buffer.
943 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
944 (while (get-process name1
)
945 ;; NAME must be unique as process name.
947 name1
(format "%s<%d>" name i
)))
949 ;; Set the new process properties.
950 (tramp-set-connection-property v
"process-name" name
)
951 (tramp-set-connection-property v
"process-buffer" buffer
)
953 (with-current-buffer (tramp-get-connection-buffer v
)
955 ;; We catch this event. Otherwise, `start-process' could
956 ;; be called on the local host.
959 ;; Activate narrowing in order to save BUFFER
960 ;; contents. Clear also the modification time;
961 ;; otherwise we might be interrupted by
962 ;; `verify-visited-file-modtime'.
963 (let ((buffer-undo-list t
)
964 (buffer-read-only nil
)
966 (clear-visited-file-modtime)
967 (narrow-to-region (point-max) (point-max))
968 ;; We call `tramp-adb-maybe-open-connection', in
969 ;; order to cleanup the prompt afterwards.
970 (tramp-adb-maybe-open-connection v
)
972 (delete-region mark
(point))
973 (narrow-to-region (point-max) (point-max))
975 (let ((tramp-adb-prompt (regexp-quote command
)))
976 (tramp-adb-send-command v command
))
977 (let ((p (tramp-get-connection-process v
)))
978 ;; Set query flag and process marker for this
979 ;; process. We ignore errors, because the process
980 ;; could have finished already.
982 (tramp-compat-set-process-query-on-exit-flag p t
)
983 (set-marker (process-mark p
) (point)))
988 (if (string-match tramp-temp-buffer-name
(buffer-name))
990 (set-process-buffer (tramp-get-connection-process v
) nil
)
991 (kill-buffer (current-buffer)))
992 (set-buffer-modified-p bmp
))
993 (tramp-set-connection-property v
"process-name" nil
)
994 (tramp-set-connection-property v
"process-buffer" nil
))))))
998 (defun tramp-adb-execute-adb-command (vec &rest args
)
999 "Returns nil on success error-output on failure."
1000 (when (> (length (tramp-file-name-host vec
)) 0)
1001 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
1005 (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args
))
1007 (tramp-message vec
6 "%s" (buffer-string)))))
1009 (defun tramp-adb-find-test-command (vec)
1010 "Checks, whether the ash has a builtin \"test\" command.
1011 This happens for Android >= 4.0."
1012 (with-tramp-connection-property vec
"test"
1013 (zerop (tramp-adb-command-exit-status vec
"type test"))))
1015 ;; Connection functions
1017 (defun tramp-adb-send-command (vec command
)
1018 "Send the COMMAND to connection VEC."
1019 (tramp-adb-maybe-open-connection vec
)
1020 (tramp-message vec
6 "%s" command
)
1021 (tramp-send-string vec command
)
1022 ;; fixme: Race condition
1023 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
1024 (with-current-buffer (tramp-get-connection-buffer vec
)
1026 (goto-char (point-min))
1027 ;; We can't use stty to disable echo of command.
1028 (delete-matching-lines (regexp-quote command
))
1029 ;; When the local machine is W32, there are still trailing ^M.
1030 ;; There must be a better solution by setting the correct coding
1031 ;; system, but this requires changes in core Tramp.
1032 (goto-char (point-min))
1033 (while (re-search-forward "\r+$" nil t
)
1034 (replace-match "" nil nil
)))))
1036 (defun tramp-adb-command-exit-status
1038 "Run COMMAND and return its exit status.
1039 Sends `echo $?' along with the COMMAND for checking the exit status. If
1040 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
1041 (tramp-adb-send-command
1043 (format "%s; echo tramp_exit_status $?" command
)
1044 "echo tramp_exit_status $?"))
1045 (with-current-buffer (tramp-get-connection-buffer vec
)
1046 (goto-char (point-max))
1047 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1049 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1050 (skip-chars-forward "^ ")
1052 (read (current-buffer))
1053 (let (buffer-read-only)
1054 (delete-region (match-beginning 0) (point-max))))))
1056 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
1057 "Run COMMAND, check exit status, throw error if exit status not okay.
1058 FMT and ARGS are passed to `error'."
1059 (unless (zerop (tramp-adb-command-exit-status vec command
))
1060 (apply 'tramp-error vec
'file-error fmt args
)))
1062 (defun tramp-adb-wait-for-output (proc &optional timeout
)
1063 "Wait for output from remote command."
1064 (unless (buffer-live-p (process-buffer proc
))
1065 (delete-process proc
)
1066 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
1067 (with-current-buffer (process-buffer proc
)
1068 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
1069 (let (buffer-read-only)
1070 (goto-char (point-min))
1071 ;; ADB terminal sends "^H" sequences.
1072 (when (re-search-forward "<\b+" (point-at-eol) t
)
1074 (delete-region (point-min) (point)))
1075 ;; Delete the prompt.
1076 (goto-char (point-min))
1077 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
1079 (delete-region (point-min) (point)))
1080 (goto-char (point-max))
1081 (re-search-backward tramp-adb-prompt nil t
)
1082 (delete-region (point) (point-max)))
1086 "[[Remote adb prompt `%s' not found in %d secs]]"
1087 tramp-adb-prompt timeout
)
1090 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
1092 (defun tramp-adb-maybe-open-connection (vec)
1093 "Maybe open a connection VEC.
1094 Does not do anything if a connection is already open, but re-opens the
1095 connection if a previous connection has died for some reason."
1096 (tramp-check-proper-method-and-host vec
)
1098 (let* ((buf (tramp-get-connection-buffer vec
))
1099 (p (get-buffer-process buf
))
1100 (host (tramp-file-name-host vec
))
1101 (user (tramp-file-name-user vec
))
1102 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1104 ;; Maybe we know already that "su" is not supported. We cannot
1105 ;; use a connection property, because we have not checked yet
1106 ;; whether it is still the same device.
1107 (when (and user
(not (tramp-get-file-property vec
"" "su-command-p" t
)))
1108 (tramp-error vec
'file-error
"Cannot switch to user `%s'" user
))
1111 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1113 (when (and p
(processp p
)) (delete-process p
))
1115 (tramp-error vec
'file-error
"No device connected"))
1116 (if (and (> (length host
) 0) (not (member host devices
)))
1117 (tramp-error vec
'file-error
"Device %s not connected" host
))
1118 (if (and (> (length devices
) 1) (zerop (length host
)))
1121 "Multiple Devices connected: No Host/Device specified"))
1122 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1123 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1124 (process-connection-type tramp-process-connection-type
)
1125 (args (if (> (length host
) 0)
1126 (list "-s" host
"shell")
1128 (p (let ((default-directory
1129 (tramp-compat-temporary-file-directory)))
1130 (apply 'start-process
(tramp-get-connection-name vec
) buf
1131 tramp-adb-program args
))))
1133 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1134 ;; Wait for initial prompt.
1135 (tramp-adb-wait-for-output p
30)
1136 (unless (eq 'run
(process-status p
))
1137 (tramp-error vec
'file-error
"Terminated!"))
1138 (tramp-set-connection-property p
"vector" vec
)
1139 (tramp-compat-set-process-query-on-exit-flag p nil
)
1141 ;; Check whether the properties have been changed. If
1142 ;; yes, this is a strong indication that we must expire all
1143 ;; connection properties. We start again.
1144 (tramp-message vec
5 "Checking system information")
1145 (tramp-adb-send-command
1146 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1148 (tramp-get-connection-property vec
"getprop" nil
))
1150 (tramp-set-connection-property
1152 (with-current-buffer (tramp-get-connection-buffer vec
)
1153 ;; Read the expression.
1154 (goto-char (point-min))
1155 (read (current-buffer))))))
1156 (when (and (stringp old-getprop
)
1157 (not (string-equal old-getprop new-getprop
)))
1160 "Connection reset, because remote host changed from `%s' to `%s'"
1161 old-getprop new-getprop
)
1162 (tramp-cleanup-connection vec t
)
1163 (tramp-adb-maybe-open-connection vec
)))
1165 ;; Change user if indicated.
1167 (tramp-adb-send-command vec
(format "su %s" user
))
1168 (unless (zerop (tramp-adb-command-exit-status vec nil
))
1170 (tramp-set-file-property vec
"" "su-command-p" nil
)
1172 vec
'file-error
"Cannot switch to user `%s'" user
)))
1174 ;; Set "remote-path" connection property. This is needed
1176 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1177 (tramp-set-connection-property
1180 (with-current-buffer (tramp-get-connection-buffer vec
)
1181 ;; Read the expression.
1182 (goto-char (point-min))
1183 (read (current-buffer)))
1184 ":" 'omit-nulls
))))))))
1186 (provide 'tramp-adb
)
1187 ;;; tramp-adb.el ends here