1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
3 ;; Copyright (C) 2011-2013 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' performed by default handler.
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 (replace-regexp-in-string
412 (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 (if (file-remote-p filename
) filename newname
))
666 0 (format "Copying %s to %s" filename newname
)
668 (let ((tmpfile (file-local-copy filename
)))
673 (rename-file tmpfile newname ok-if-already-exists
)
675 (delete-file tmpfile
)
676 (signal (car err
) (cdr err
))))
679 (when (file-directory-p newname
)
681 (expand-file-name (file-name-nondirectory filename
) newname
)))
683 (with-parsed-tramp-file-name newname nil
684 (when (and (not ok-if-already-exists
)
685 (file-exists-p newname
))
686 (tramp-error v
'file-already-exists newname
))
688 ;; We must also flush the cache of the directory, because
689 ;; `file-attributes' reads the values from there.
690 (tramp-flush-file-property v
(file-name-directory localname
))
691 (tramp-flush-file-property v localname
)
692 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
694 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
696 ;; KEEP-DATE handling.
698 (set-file-times newname
(nth 5 (file-attributes filename
))))))
700 (defun tramp-adb-handle-rename-file
701 (filename newname
&optional ok-if-already-exists
)
702 "Like `rename-file' for Tramp files."
703 (setq filename
(expand-file-name filename
)
704 newname
(expand-file-name newname
))
706 (with-parsed-tramp-file-name
707 (if (file-remote-p filename
) filename newname
) nil
708 (with-tramp-progress-reporter
709 v
0 (format "Renaming %s to %s" newname filename
)
711 (if (and (tramp-equal-remote filename newname
)
712 (not (file-directory-p filename
)))
714 (when (and (not ok-if-already-exists
)
715 (file-exists-p newname
))
716 (tramp-error v
'file-already-exists newname
))
717 ;; We must also flush the cache of the directory, because
718 ;; `file-attributes' reads the values from there.
719 (tramp-flush-file-property v
(file-name-directory localname
))
720 (tramp-flush-file-property v localname
)
722 (tramp-adb-barf-unless-okay
725 (tramp-file-name-handler 'file-remote-p filename
'localname
)
727 "Error renaming %s to %s" filename newname
))
730 (copy-file filename newname ok-if-already-exists t t
)
731 (delete-file filename
)))))
733 (defun tramp-adb-handle-process-file
734 (program &optional infile destination display
&rest args
)
735 "Like `process-file' for Tramp files."
736 ;; The implementation is not complete yet.
737 (when (and (numberp destination
) (zerop destination
))
738 (error "Implementation does not handle immediate return"))
740 (with-parsed-tramp-file-name default-directory nil
741 (let (command input tmpinput stderr tmpstderr outbuf ret
)
743 (setq command
(mapconcat 'tramp-shell-quote-argument
744 (cons program args
) " "))
747 (setq input
"/dev/null")
748 (setq infile
(expand-file-name infile
))
749 (if (tramp-equal-remote default-directory infile
)
750 ;; INFILE is on the same remote host.
751 (setq input
(with-parsed-tramp-file-name infile nil localname
))
752 ;; INFILE must be copied to remote host.
753 (setq input
(tramp-make-tramp-temp-file v
)
754 tmpinput
(tramp-make-tramp-file-name method user host input
))
755 (copy-file infile tmpinput t
)))
756 (when input
(setq command
(format "%s <%s" command input
)))
761 ((bufferp destination
)
762 (setq outbuf destination
))
764 ((stringp destination
)
765 (setq outbuf
(get-buffer-create destination
)))
766 ;; (REAL-DESTINATION ERROR-DESTINATION)
770 ((bufferp (car destination
))
771 (setq outbuf
(car destination
)))
772 ((stringp (car destination
))
773 (setq outbuf
(get-buffer-create (car destination
))))
775 (setq outbuf
(current-buffer))))
778 ((stringp (cadr destination
))
779 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
780 (if (tramp-equal-remote default-directory
(cadr destination
))
781 ;; stderr is on the same remote host.
782 (setq stderr
(with-parsed-tramp-file-name
783 (cadr destination
) nil localname
))
784 ;; stderr must be copied to remote host. The temporary
785 ;; file must be deleted after execution.
786 (setq stderr
(tramp-make-tramp-temp-file v
)
787 tmpstderr
(tramp-make-tramp-file-name
788 method user host stderr
))))
789 ;; stderr to be discarded.
790 ((null (cadr destination
))
791 (setq stderr
"/dev/null"))))
794 (setq outbuf
(current-buffer))))
795 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
797 ;; Send the command. It might not return in time, so we protect
798 ;; it. Call it in a subshell, in order to preserve working
803 (tramp-adb-barf-unless-okay
804 v
(format "(cd %s; %s)"
805 (tramp-shell-quote-argument localname
) command
)
807 ;; We should show the output anyway.
809 (with-current-buffer outbuf
810 (insert-buffer-substring (tramp-get-connection-buffer v
)))
811 (when display
(display-buffer outbuf
))))
812 ;; When the user did interrupt, we should do it also. We use
813 ;; return code -1 as marker.
815 (kill-buffer (tramp-get-connection-buffer v
))
819 (kill-buffer (tramp-get-connection-buffer v
))
822 ;; Provide error file.
823 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
825 ;; Cleanup. We remove all file cache values for the connection,
826 ;; because the remote process could have changed them.
827 (when tmpinput
(delete-file tmpinput
))
829 ;; `process-file-side-effects' has been introduced with GNU
830 ;; Emacs 23.2. If set to `nil', no remote file will be changed
831 ;; by `program'. If it doesn't exist, we assume its default
833 (unless (and (boundp 'process-file-side-effects
)
834 (not (symbol-value 'process-file-side-effects
)))
835 (tramp-flush-directory-property v
""))
837 ;; Return exit status.
842 (defun tramp-adb-handle-shell-command
843 (command &optional output-buffer error-buffer
)
844 "Like `shell-command' for Tramp files."
845 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
846 ;; We cannot use `shell-file-name' and `shell-command-switch',
847 ;; they are variables of the local host.
848 (args (list "sh" "-c" (substring command
0 asynchronous
)))
852 ((bufferp output-buffer
) output-buffer
)
853 ((stringp output-buffer
) (get-buffer-create output-buffer
))
855 (setq current-buffer-p t
)
857 (t (get-buffer-create
859 "*Async Shell Command*"
860 "*Shell Command Output*")))))
863 ((bufferp error-buffer
) error-buffer
)
864 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
866 (if (and (not asynchronous
) error-buffer
)
867 (with-parsed-tramp-file-name default-directory nil
868 (list output-buffer
(tramp-make-tramp-temp-file v
)))
870 (p (get-buffer-process output-buffer
)))
872 ;; Check whether there is another process running. Tramp does not
873 ;; support 2 (asynchronous) processes in parallel.
875 (if (yes-or-no-p "A command is running. Kill it? ")
876 (ignore-errors (kill-process p
))
877 (tramp-compat-user-error "Shell command in progress")))
881 (barf-if-buffer-read-only)
883 (with-current-buffer output-buffer
884 (setq buffer-read-only nil
)
887 (if (and (not current-buffer-p
) (integerp asynchronous
))
890 (apply 'start-file-process
"*Async Shell*" buffer args
)
892 (pop-to-buffer output-buffer
)
893 (setq mode-line-process
'(":%s"))
898 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
899 ;; Insert error messages if they were separated.
901 (with-current-buffer error-buffer
902 (insert-file-contents (cadr buffer
)))
903 (delete-file (cadr buffer
)))
905 ;; This is like exchange-point-and-mark, but doesn't
906 ;; activate the mark. It is cleaner to avoid activation,
907 ;; even though the command loop would deactivate the mark
908 ;; because we inserted text.
909 (goto-char (prog1 (mark t
)
910 (set-marker (mark-marker) (point)
912 ;; There's some output, display it.
913 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
914 (if (functionp 'display-message-or-buffer
)
915 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
916 (pop-to-buffer output-buffer
))))))))
918 ;; We use BUFFER also as connection buffer during setup. Because of
919 ;; this, its original contents must be saved, and restored once
920 ;; connection has been setup.
921 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
922 "Like `start-file-process' for Tramp files."
923 (with-parsed-tramp-file-name default-directory nil
924 ;; When PROGRAM is nil, we should provide a tty. This is not
926 (unless (stringp program
)
927 (tramp-error v
'file-error
"PROGRAM must be a string"))
931 (tramp-shell-quote-argument localname
)
932 (mapconcat 'tramp-shell-quote-argument
933 (cons program args
) " ")))
934 (tramp-process-connection-type
935 (or (null program
) tramp-process-connection-type
))
936 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
941 ;; BUFFER can be nil. We use a temporary buffer.
942 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
943 (while (get-process name1
)
944 ;; NAME must be unique as process name.
946 name1
(format "%s<%d>" name i
)))
948 ;; Set the new process properties.
949 (tramp-set-connection-property v
"process-name" name
)
950 (tramp-set-connection-property v
"process-buffer" buffer
)
952 (with-current-buffer (tramp-get-connection-buffer v
)
954 ;; We catch this event. Otherwise, `start-process' could
955 ;; be called on the local host.
958 ;; Activate narrowing in order to save BUFFER
959 ;; contents. Clear also the modification time;
960 ;; otherwise we might be interrupted by
961 ;; `verify-visited-file-modtime'.
962 (let ((buffer-undo-list t
)
963 (buffer-read-only nil
)
965 (clear-visited-file-modtime)
966 (narrow-to-region (point-max) (point-max))
967 ;; We call `tramp-adb-maybe-open-connection', in
968 ;; order to cleanup the prompt afterwards.
969 (tramp-adb-maybe-open-connection v
)
971 (delete-region mark
(point))
972 (narrow-to-region (point-max) (point-max))
974 (let ((tramp-adb-prompt (regexp-quote command
)))
975 (tramp-adb-send-command v command
))
976 (let ((p (tramp-get-connection-process v
)))
977 ;; Set query flag and process marker for this
978 ;; process. We ignore errors, because the process
979 ;; could have finished already.
981 (tramp-compat-set-process-query-on-exit-flag p t
)
982 (set-marker (process-mark p
) (point)))
987 (if (string-match tramp-temp-buffer-name
(buffer-name))
989 (set-process-buffer (tramp-get-connection-process v
) nil
)
990 (kill-buffer (current-buffer)))
991 (set-buffer-modified-p bmp
))
992 (tramp-set-connection-property v
"process-name" nil
)
993 (tramp-set-connection-property v
"process-buffer" nil
))))))
997 (defun tramp-adb-execute-adb-command (vec &rest args
)
998 "Returns nil on success error-output on failure."
999 (when (> (length (tramp-file-name-host vec
)) 0)
1000 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
1004 (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args
))
1006 (tramp-message vec
6 "%s" (buffer-string)))))
1008 (defun tramp-adb-find-test-command (vec)
1009 "Checks, whether the ash has a builtin \"test\" command.
1010 This happens for Android >= 4.0."
1011 (with-tramp-connection-property vec
"test"
1012 (zerop (tramp-adb-command-exit-status vec
"type test"))))
1014 ;; Connection functions
1016 (defun tramp-adb-send-command (vec command
)
1017 "Send the COMMAND to connection VEC."
1018 (tramp-adb-maybe-open-connection vec
)
1019 (tramp-message vec
6 "%s" command
)
1020 (tramp-send-string vec command
)
1021 ;; fixme: Race condition
1022 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
1023 (with-current-buffer (tramp-get-connection-buffer vec
)
1025 (goto-char (point-min))
1026 ;; We can't use stty to disable echo of command.
1027 (delete-matching-lines (regexp-quote command
))
1028 ;; When the local machine is W32, there are still trailing ^M.
1029 ;; There must be a better solution by setting the correct coding
1030 ;; system, but this requires changes in core Tramp.
1031 (goto-char (point-min))
1032 (while (re-search-forward "\r+$" nil t
)
1033 (replace-match "" nil nil
)))))
1035 (defun tramp-adb-command-exit-status
1037 "Run COMMAND and return its exit status.
1038 Sends `echo $?' along with the COMMAND for checking the exit status. If
1039 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
1040 (tramp-adb-send-command
1042 (format "%s; echo tramp_exit_status $?" command
)
1043 "echo tramp_exit_status $?"))
1044 (with-current-buffer (tramp-get-connection-buffer vec
)
1045 (goto-char (point-max))
1046 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1048 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1049 (skip-chars-forward "^ ")
1051 (read (current-buffer))
1052 (let (buffer-read-only)
1053 (delete-region (match-beginning 0) (point-max))))))
1055 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
1056 "Run COMMAND, check exit status, throw error if exit status not okay.
1057 FMT and ARGS are passed to `error'."
1058 (unless (zerop (tramp-adb-command-exit-status vec command
))
1059 (apply 'tramp-error vec
'file-error fmt args
)))
1061 (defun tramp-adb-wait-for-output (proc &optional timeout
)
1062 "Wait for output from remote command."
1063 (unless (buffer-live-p (process-buffer proc
))
1064 (delete-process proc
)
1065 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
1066 (with-current-buffer (process-buffer proc
)
1067 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
1068 (let (buffer-read-only)
1069 (goto-char (point-min))
1070 ;; ADB terminal sends "^H" sequences.
1071 (when (re-search-forward "<\b+" (point-at-eol) t
)
1073 (delete-region (point-min) (point)))
1074 ;; Delete the prompt.
1075 (goto-char (point-min))
1076 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
1078 (delete-region (point-min) (point)))
1079 (goto-char (point-max))
1080 (re-search-backward tramp-adb-prompt nil t
)
1081 (delete-region (point) (point-max)))
1085 "[[Remote adb prompt `%s' not found in %d secs]]"
1086 tramp-adb-prompt timeout
)
1089 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
1091 (defun tramp-adb-maybe-open-connection (vec)
1092 "Maybe open a connection VEC.
1093 Does not do anything if a connection is already open, but re-opens the
1094 connection if a previous connection has died for some reason."
1095 (let* ((buf (tramp-get-connection-buffer vec
))
1096 (p (get-buffer-process buf
))
1097 (host (tramp-file-name-host vec
))
1098 (user (tramp-file-name-user vec
))
1099 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1101 ;; Maybe we know already that "su" is not supported. We cannot
1102 ;; use a connection property, because we have not checked yet
1103 ;; whether it is still the same device.
1104 (when (and user
(not (tramp-get-file-property vec
"" "su-command-p" t
)))
1105 (tramp-error vec
'file-error
"Cannot switch to user `%s'" user
))
1108 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1110 (when (and p
(processp p
)) (delete-process p
))
1112 (tramp-error vec
'file-error
"No device connected"))
1113 (if (and (> (length host
) 0) (not (member host devices
)))
1114 (tramp-error vec
'file-error
"Device %s not connected" host
))
1115 (if (and (> (length devices
) 1) (zerop (length host
)))
1118 "Multiple Devices connected: No Host/Device specified"))
1119 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1120 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1121 (process-connection-type tramp-process-connection-type
)
1122 (args (if (> (length host
) 0)
1123 (list "-s" host
"shell")
1125 (p (let ((default-directory
1126 (tramp-compat-temporary-file-directory)))
1127 (apply 'start-process
(tramp-get-connection-name vec
) buf
1128 tramp-adb-program args
))))
1130 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1131 ;; Wait for initial prompt.
1132 (tramp-adb-wait-for-output p
30)
1133 (unless (eq 'run
(process-status p
))
1134 (tramp-error vec
'file-error
"Terminated!"))
1135 (tramp-compat-set-process-query-on-exit-flag p nil
)
1137 ;; Check whether the properties have been changed. If
1138 ;; yes, this is a strong indication that we must expire all
1139 ;; connection properties. We start again.
1140 (tramp-message vec
5 "Checking system information")
1141 (tramp-adb-send-command
1142 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1144 (tramp-get-connection-property vec
"getprop" nil
))
1146 (tramp-set-connection-property
1148 (with-current-buffer (tramp-get-connection-buffer vec
)
1149 ;; Read the expression.
1150 (goto-char (point-min))
1151 (read (current-buffer))))))
1152 (when (and (stringp old-getprop
)
1153 (not (string-equal old-getprop new-getprop
)))
1157 "Connection reset, because remote host changed from `%s' to `%s'"
1158 old-getprop new-getprop
)
1159 (tramp-adb-maybe-open-connection vec
)))
1161 ;; Change user if indicated.
1163 (tramp-adb-send-command vec
(format "su %s" user
))
1164 (unless (zerop (tramp-adb-command-exit-status vec nil
))
1166 (tramp-set-file-property vec
"" "su-command-p" nil
)
1168 vec
'file-error
"Cannot switch to user `%s'" user
)))
1170 ;; Set "remote-path" connection property. This is needed
1172 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1173 (tramp-set-connection-property
1176 (with-current-buffer (tramp-get-connection-buffer vec
)
1177 ;; Read the expression.
1178 (goto-char (point-min))
1179 (read (current-buffer)))
1180 ":" 'omit-nulls
))))))))
1182 (provide 'tramp-adb
)
1183 ;;; tramp-adb.el ends here