1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
3 ;; Copyright (C) 2011-2014 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
)
41 (defcustom tramp-adb-program
"adb"
42 "Name of the Android Debug Bridge program."
48 (defconst tramp-adb-method
"adb"
49 "*When this method name is used, forward all calls to Android Debug Bridge.")
51 (defcustom tramp-adb-prompt
52 "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
53 "Regexp used as prompt in almquist shell."
58 (defconst tramp-adb-ls-date-regexp
59 "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]")
61 (defconst tramp-adb-ls-toolbox-regexp
63 "^[[:space:]]*\\([-[:alpha:]]+\\)" ; \1 permissions
64 "[[:space:]]*\\([^[:space:]]+\\)" ; \2 username
65 "[[:space:]]+\\([^[:space:]]+\\)" ; \3 group
66 "[[:space:]]+\\([[:digit:]]+\\)" ; \4 size
67 "[[:space:]]+\\([-[:digit:]]+[[:space:]][:[:digit:]]+\\)" ; \5 date
68 "[[:space:]]+\\(.*\\)$")) ; \6 filename
71 (add-to-list 'tramp-methods
73 (tramp-tmpdir "/data/local/tmp")))
76 (add-to-list 'tramp-default-host-alist
`(,tramp-adb-method nil
""))
79 (eval-after-load 'tramp
80 '(tramp-set-completion-function
81 tramp-adb-method
'((tramp-adb-parse-device-names ""))))
84 (add-to-list 'tramp-foreign-file-name-handler-alist
85 (cons 'tramp-adb-file-name-p
'tramp-adb-file-name-handler
))
87 (defconst tramp-adb-file-name-handler-alist
88 '((access-file . ignore
)
89 (add-name-to-file . tramp-adb-handle-copy-file
)
90 ;; `byte-compiler-base-file-name' performed by default handler.
91 ;; `copy-directory' performed by default handler.
92 (copy-file . tramp-adb-handle-copy-file
)
93 (delete-directory . tramp-adb-handle-delete-directory
)
94 (delete-file . tramp-adb-handle-delete-file
)
95 ;; `diff-latest-backup-file' performed by default handler.
96 (directory-file-name . tramp-handle-directory-file-name
)
97 (directory-files . tramp-handle-directory-files
)
98 (directory-files-and-attributes
99 . tramp-adb-handle-directory-files-and-attributes
)
100 (dired-call-process . ignore
)
101 (dired-compress-file . ignore
)
102 (dired-uncache . tramp-handle-dired-uncache
)
103 (expand-file-name . tramp-adb-handle-expand-file-name
)
104 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
106 (file-attributes . tramp-adb-handle-file-attributes
)
107 (file-directory-p . tramp-adb-handle-file-directory-p
)
108 ;; `file-equal-p' performed by default handler.
109 ;; FIXME: This is too sloppy.
110 (file-executable-p . tramp-handle-file-exists-p
)
111 (file-exists-p . tramp-handle-file-exists-p
)
112 ;; `file-in-directory-p' performed by default handler.
113 (file-local-copy . tramp-adb-handle-file-local-copy
)
114 (file-modes . tramp-handle-file-modes
)
115 (file-name-all-completions . tramp-adb-handle-file-name-all-completions
)
116 (file-name-as-directory . tramp-handle-file-name-as-directory
)
117 (file-name-completion . tramp-handle-file-name-completion
)
118 (file-name-directory . tramp-handle-file-name-directory
)
119 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
120 ;; `file-name-sans-versions' performed by default handler.
121 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
122 (file-notify-add-watch . tramp-handle-file-notify-add-watch
)
123 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch
)
124 (file-ownership-preserved-p . ignore
)
125 (file-readable-p . tramp-handle-file-exists-p
)
126 (file-regular-p . tramp-handle-file-regular-p
)
127 (file-remote-p . tramp-handle-file-remote-p
)
128 (file-selinux-context . ignore
)
129 (file-symlink-p . tramp-handle-file-symlink-p
)
130 (file-truename . tramp-adb-handle-file-truename
)
131 (file-writable-p . tramp-adb-handle-file-writable-p
)
132 (find-backup-file-name . tramp-handle-find-backup-file-name
)
133 ;; `find-file-noselect' performed by default handler.
134 ;; `get-file-buffer' performed by default handler.
135 (insert-directory . tramp-handle-insert-directory
)
136 (insert-file-contents . tramp-handle-insert-file-contents
)
137 (load . tramp-handle-load
)
138 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
139 (make-directory . tramp-adb-handle-make-directory
)
140 (make-directory-internal . ignore
)
141 (make-symbolic-link . tramp-handle-make-symbolic-link
)
142 (process-file . tramp-adb-handle-process-file
)
143 (rename-file . tramp-adb-handle-rename-file
)
144 (set-file-acl . ignore
)
145 (set-file-modes . tramp-adb-handle-set-file-modes
)
146 (set-file-selinux-context . ignore
)
147 (set-file-times . tramp-adb-handle-set-file-times
)
148 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
149 (shell-command . tramp-adb-handle-shell-command
)
150 (start-file-process . tramp-adb-handle-start-file-process
)
151 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
152 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
153 (vc-registered . ignore
)
154 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
)
155 (write-region . tramp-adb-handle-write-region
))
156 "Alist of handler functions for Tramp ADB method.")
158 ;; It must be a `defsubst' in order to push the whole code into
159 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
161 (defsubst tramp-adb-file-name-p
(filename)
162 "Check if it's a filename for ADB."
163 (let ((v (tramp-dissect-file-name filename
)))
164 (string= (tramp-file-name-method v
) tramp-adb-method
)))
167 (defun tramp-adb-file-name-handler (operation &rest args
)
168 "Invoke the ADB handler for OPERATION.
169 First arg specifies the OPERATION, second arg is a list of arguments to
170 pass to the OPERATION."
171 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
)))
173 (save-match-data (apply (cdr fn
) args
))
174 (tramp-run-real-handler operation args
))))
177 (defun tramp-adb-parse-device-names (_ignore)
178 "Return a list of (nil host) tuples allowed to access."
181 ;; `call-process' does not react on timer under MS Windows.
182 ;; That's why we use `start-process'.
183 (let ((p (start-process
184 tramp-adb-program
(current-buffer) tramp-adb-program
"devices"))
186 (tramp-compat-set-process-query-on-exit-flag p nil
)
187 (while (eq 'run
(process-status p
))
188 (accept-process-output p
0.1))
189 (accept-process-output p
0.1)
190 (goto-char (point-min))
191 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
192 (add-to-list 'result
(list nil
(match-string 1))))
195 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
196 "Like `expand-file-name' for Tramp files."
197 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
198 (setq dir
(or dir default-directory
"/"))
199 ;; Unless NAME is absolute, concat DIR and NAME.
200 (unless (file-name-absolute-p name
)
201 (setq name
(concat (file-name-as-directory dir
) name
)))
202 ;; If NAME is not a Tramp file, run the real handler.
203 (if (not (tramp-tramp-file-p name
))
204 (tramp-run-real-handler 'expand-file-name
(list name nil
))
206 (with-parsed-tramp-file-name name nil
207 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
208 (setq localname
(concat "/" localname
)))
209 ;; Do normal `expand-file-name' (this does "/./" and "/../").
210 ;; We bind `directory-sep-char' here for XEmacs on Windows,
211 ;; which would otherwise use backslash. `default-directory' is
212 ;; bound, because on Windows there would be problems with UNC
213 ;; shares or Cygwin mounts.
214 (let ((directory-sep-char ?
/)
215 (default-directory (tramp-compat-temporary-file-directory)))
216 (tramp-make-tramp-file-name
218 (tramp-drop-volume-letter
219 (tramp-run-real-handler
220 'expand-file-name
(list localname
))))))))
222 (defun tramp-adb-handle-file-directory-p (filename)
223 "Like `file-directory-p' for Tramp files."
224 (car (file-attributes (file-truename filename
))))
226 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
227 ;; code could be shared?
228 (defun tramp-adb-handle-file-truename (filename)
229 "Like `file-truename' for Tramp files."
232 (with-parsed-tramp-file-name (expand-file-name filename
) nil
233 (tramp-make-tramp-file-name
235 (with-tramp-file-property v localname
"file-truename"
236 (let ((result nil
)) ; result steps in reverse order
237 (tramp-message v
4 "Finding true name for `%s'" filename
)
238 (let* ((directory-sep-char ?
/)
239 (steps (tramp-compat-split-string localname
"/"))
240 (localnamedir (tramp-run-real-handler
241 'file-name-as-directory
(list localname
)))
242 (is-dir (string= localname localnamedir
))
245 ;; Don't make the following value larger than
246 ;; necessary. People expect an error message in a
247 ;; timely fashion when something is wrong; otherwise
248 ;; they might think that Emacs is hung. Of course,
249 ;; correctness has to come first.
252 (while (and steps
(< numchase numchase-limit
))
253 (setq thisstep
(pop steps
))
257 (append '("") (reverse result
) (list thisstep
))
260 (nth 0 (file-attributes
261 (tramp-make-tramp-file-name
268 (cond ((string= "." thisstep
)
269 (tramp-message v
5 "Ignoring step `.'"))
270 ((string= ".." thisstep
)
271 (tramp-message v
5 "Processing step `..'")
273 ((stringp symlink-target
)
274 ;; It's a symlink, follow it.
275 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
276 (setq numchase
(1+ numchase
))
277 (when (file-name-absolute-p symlink-target
)
279 ;; If the symlink was absolute, we'll get a string
280 ;; like "/user@host:/some/target"; extract the
281 ;; "/some/target" part from it.
282 (when (tramp-tramp-file-p symlink-target
)
283 (unless (tramp-equal-remote filename symlink-target
)
286 "Symlink target `%s' on wrong host" symlink-target
))
287 (setq symlink-target localname
))
289 (append (tramp-compat-split-string
294 (setq result
(cons thisstep result
)))))
295 (when (>= numchase numchase-limit
)
298 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
299 (setq result
(reverse result
))
300 ;; Combine list to form string.
303 (mapconcat 'identity
(cons "" result
) "/")
305 (when (and is-dir
(or (string= "" result
)
306 (not (string= (substring result -
1) "/"))))
307 (setq result
(concat result
"/"))))
309 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
312 ;; Preserve trailing "/".
313 (if (string-equal (file-name-nondirectory filename
) "") "/" "")))
315 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
316 "Like `file-attributes' for Tramp files."
317 (unless id-format
(setq id-format
'integer
))
319 (with-parsed-tramp-file-name filename nil
320 (with-tramp-file-property
321 v localname
(format "file-attributes-%s" id-format
)
323 (tramp-adb-send-command-and-check
324 v
(format "%s -d -l %s"
325 (tramp-adb-get-ls-command v
)
326 (tramp-shell-quote-argument localname
)))
327 (with-current-buffer (tramp-get-buffer v
)
328 (tramp-adb-sh-fix-ls-output)
329 (cdar (tramp-do-parse-file-attributes-with-ls v id-format
))))))))
331 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format
)
332 "Parse `file-attributes' for Tramp files using the ls(1) command."
333 (with-current-buffer (tramp-get-buffer vec
)
334 (goto-char (point-min))
335 (let ((file-properties nil
))
336 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t
)
337 (let* ((mod-string (match-string 1))
338 (is-dir (eq ?d
(aref mod-string
0)))
339 (is-symlink (eq ?l
(aref mod-string
0)))
340 (uid (match-string 2))
341 (gid (match-string 3))
342 (size (string-to-number (match-string 4)))
343 (date (match-string 5))
344 (name (match-string 6))
347 (cadr (split-string name
"\\( -> \\|\n\\)")))))
350 (car (split-string name
"\\( -> \\|\n\\)"))
352 (or is-dir symlink-target
)
354 ;; no way to handle numeric ids in Androids ash
355 (if (eq id-format
'integer
) 0 uid
)
356 (if (eq id-format
'integer
) 0 gid
)
358 (date-to-time date
) ; mtime
364 (tramp-get-device vec
))
368 (defun tramp-adb-handle-directory-files-and-attributes
369 (directory &optional full match nosort id-format
)
370 "Like `directory-files-and-attributes' for Tramp files."
371 (when (file-directory-p directory
)
372 (with-parsed-tramp-file-name (expand-file-name directory
) nil
373 (with-tramp-file-property
374 v localname
(format "directory-files-attributes-%s-%s-%s-%s"
375 full match id-format nosort
)
376 (with-current-buffer (tramp-get-buffer v
)
377 (when (tramp-adb-send-command-and-check
378 v
(format "%s -a -l %s"
379 (tramp-adb-get-ls-command v
)
380 (tramp-shell-quote-argument localname
)))
381 ;; We insert also filename/. and filename/.., because "ls" doesn't.
382 (narrow-to-region (point) (point))
383 (tramp-adb-send-command
384 v
(format "%s -d -a -l %s %s"
385 (tramp-adb-get-ls-command v
)
386 (concat (file-name-as-directory localname
) ".")
387 (concat (file-name-as-directory localname
) "..")))
389 (tramp-adb-sh-fix-ls-output)
390 (let ((result (tramp-do-parse-file-attributes-with-ls
391 v
(or id-format
'integer
))))
396 (cons (expand-file-name (car x
) directory
) (cdr x
)))
400 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))
403 (if (or (not match
) (string-match match
(car x
)))
407 (defun tramp-adb-get-ls-command (vec)
408 (with-tramp-connection-property vec
"ls"
409 (tramp-message vec
5 "Finding a suitable `ls' command")
410 (if (tramp-adb-send-command-and-check vec
"ls --color=never -al /dev/null")
411 ;; On CyanogenMod based system BusyBox is used and "ls" output
412 ;; coloring is enabled by default. So we try to disable it
417 (defun tramp-adb--gnu-switches-to-ash
419 "Almquist shell can't handle multiple arguments.
420 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
424 (tramp-compat-replace-regexp-in-string
426 (tramp-compat-replace-regexp-in-string "^-" "" s
)))
427 ;; FIXME: Warning about removed switches (long and non-dash).
431 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
434 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
435 "Insert dummy 0 in empty size columns.
436 Androids \"ls\" command doesn't insert size column for directories:
437 Emacs dired can't find files."
439 ;; Insert missing size.
440 (goto-char (point-min))
442 (search-forward-regexp
443 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
444 (replace-match "0\\1" "\\1" nil
)
445 ;; Insert missing "/".
446 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
450 (let* ((lines (split-string (buffer-string) "\n" t
))
455 'tramp-adb-ls-output-time-less-p
456 'tramp-adb-ls-output-name-less-p
))))
457 (delete-region (point-min) (point-max))
458 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
459 ;; Add final newline.
460 (goto-char (point-max))
461 (unless (bolp) (insert "\n"))))
463 (defun tramp-adb-ls-output-time-less-p (a b
)
464 "Sort \"ls\" output by time, descending."
466 (string-match tramp-adb-ls-date-regexp a
)
467 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
468 (string-match tramp-adb-ls-date-regexp b
)
469 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
470 (time-less-p time-b time-a
)))
472 (defun tramp-adb-ls-output-name-less-p (a b
)
473 "Sort \"ls\" output by name, ascending."
475 (string-match directory-listing-before-filename-regexp a
)
476 (setq posa
(match-end 0))
477 (string-match directory-listing-before-filename-regexp b
)
478 (setq posb
(match-end 0))
479 (string-lessp (substring a posa
) (substring b posb
))))
481 (defun tramp-adb-handle-make-directory (dir &optional parents
)
482 "Like `make-directory' for Tramp files."
483 (setq dir
(expand-file-name dir
))
484 (with-parsed-tramp-file-name dir nil
486 (let ((par (expand-file-name ".." dir
)))
487 (unless (file-directory-p par
)
488 (make-directory par parents
))))
489 (tramp-adb-barf-unless-okay
490 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
491 "Couldn't make directory %s" dir
)
492 (tramp-flush-file-property v
(file-name-directory localname
))
493 (tramp-flush-directory-property v localname
)))
495 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
496 "Like `delete-directory' for Tramp files."
497 (setq directory
(expand-file-name directory
))
498 (with-parsed-tramp-file-name directory nil
499 (tramp-flush-file-property v
(file-name-directory localname
))
500 (tramp-flush-directory-property v localname
)
501 (tramp-adb-barf-unless-okay
503 (if recursive
"rm -r" "rmdir")
504 (tramp-shell-quote-argument localname
))
505 "Couldn't delete %s" directory
)))
507 (defun tramp-adb-handle-delete-file (filename &optional _trash
)
508 "Like `delete-file' for Tramp files."
509 (setq filename
(expand-file-name filename
))
510 (with-parsed-tramp-file-name filename nil
511 (tramp-flush-file-property v
(file-name-directory localname
))
512 (tramp-flush-file-property v localname
)
513 (tramp-adb-barf-unless-okay
514 v
(format "rm %s" (tramp-shell-quote-argument localname
))
515 "Couldn't delete %s" filename
)))
517 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
518 "Like `file-name-all-completions' for Tramp files."
521 (with-parsed-tramp-file-name directory nil
522 (with-tramp-file-property v localname
"file-name-all-completions"
524 (tramp-adb-send-command
526 (tramp-adb-get-ls-command v
)
527 (tramp-shell-quote-argument localname
)))
530 (if (file-directory-p (expand-file-name f directory
))
531 (file-name-as-directory f
)
533 (with-current-buffer (tramp-get-buffer v
)
539 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
540 (split-string (buffer-string) "\n")))))))))))
542 (defun tramp-adb-handle-file-local-copy (filename)
543 "Like `file-local-copy' for Tramp files."
544 (with-parsed-tramp-file-name filename nil
545 (unless (file-exists-p (file-truename filename
))
548 "Cannot make local copy of non-existing file `%s'" filename
))
549 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
550 (with-tramp-progress-reporter
551 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
552 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
553 (delete-file tmpfile
)
555 v
'file-error
"Cannot make local copy of file `%s'" filename
))
558 (logior (or (file-modes filename
) 0)
559 (tramp-compat-octal-to-decimal "0400"))))
562 (defun tramp-adb-handle-file-writable-p (filename)
563 "Like `tramp-sh-handle-file-writable-p'.
564 But handle the case, if the \"test\" command is not available."
565 (with-parsed-tramp-file-name filename nil
566 (with-tramp-file-property v localname
"file-writable-p"
567 (if (tramp-adb-find-test-command v
)
568 (if (file-exists-p filename
)
569 (tramp-adb-send-command-and-check
570 v
(format "test -w %s" (tramp-shell-quote-argument localname
)))
572 (file-directory-p (file-name-directory filename
))
573 (file-writable-p (file-name-directory filename
))))
575 ;; Missing "test" command on Android < 4.
576 (let ((rw-path "/data/data"))
579 "Not implemented yet (assuming \"/data/data\" is writable): %s"
581 (and (>= (length localname
) (length rw-path
))
582 (string= (substring localname
0 (length rw-path
))
585 (defun tramp-adb-handle-write-region
586 (start end filename
&optional append visit lockname confirm
)
587 "Like `write-region' for Tramp files."
588 (setq filename
(expand-file-name filename
))
589 (with-parsed-tramp-file-name filename nil
590 (when (and confirm
(file-exists-p filename
))
591 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
593 (tramp-error v
'file-error
"File not overwritten")))
594 ;; We must also flush the cache of the directory, because
595 ;; `file-attributes' reads the values from there.
596 (tramp-flush-file-property v
(file-name-directory localname
))
597 (tramp-flush-file-property v localname
)
598 (let* ((curbuf (current-buffer))
599 (tmpfile (tramp-compat-make-temp-file filename
)))
600 (when (and append
(file-exists-p filename
))
601 (copy-file filename tmpfile
'ok
)
604 (logior (or (file-modes tmpfile
) 0)
605 (tramp-compat-octal-to-decimal "0600"))))
606 (tramp-run-real-handler
608 (list start end tmpfile append
'no-message lockname confirm
))
609 (with-tramp-progress-reporter
610 v
3 (format "Moving tmp file `%s' to `%s'" tmpfile filename
)
612 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
613 (tramp-error v
'file-error
"Cannot write: `%s'" filename
))
614 (delete-file tmpfile
)))
616 (when (or (eq visit t
) (stringp visit
))
617 (set-visited-file-modtime))
619 (unless (equal curbuf
(current-buffer))
622 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
624 (defun tramp-adb-handle-set-file-modes (filename mode
)
625 "Like `set-file-modes' for Tramp files."
626 (with-parsed-tramp-file-name filename nil
627 (tramp-flush-file-property v localname
)
628 (tramp-adb-send-command-and-check
629 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
))))
631 (defun tramp-adb-handle-set-file-times (filename &optional time
)
632 "Like `set-file-times' for Tramp files."
633 (with-parsed-tramp-file-name filename nil
634 (tramp-flush-file-property v localname
)
635 (let ((time (if (or (null time
) (equal time
'(0 0)))
638 (tramp-adb-send-command-and-check
639 ;; Use shell arithmetic because of Emacs integer size limit.
640 v
(format "touch -t $(( %d * 65536 + %d )) %s"
641 (car time
) (cadr time
)
642 (tramp-shell-quote-argument localname
))))))
644 (defun tramp-adb-handle-copy-file
645 (filename newname
&optional ok-if-already-exists keep-date
646 _preserve-uid-gid _preserve-extended-attributes
)
647 "Like `copy-file' for Tramp files.
648 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
649 (setq filename
(expand-file-name filename
)
650 newname
(expand-file-name newname
))
652 (if (file-directory-p filename
)
653 (tramp-file-name-handler 'copy-directory filename newname keep-date t
)
654 (with-tramp-progress-reporter
655 (tramp-dissect-file-name
656 (if (tramp-tramp-file-p filename
) filename newname
))
657 0 (format "Copying %s to %s" filename newname
)
659 (let ((tmpfile (file-local-copy filename
)))
664 (rename-file tmpfile newname ok-if-already-exists
)
666 (delete-file tmpfile
)
667 (signal (car err
) (cdr err
))))
670 (when (file-directory-p newname
)
672 (expand-file-name (file-name-nondirectory filename
) newname
)))
674 (with-parsed-tramp-file-name newname nil
675 (when (and (not ok-if-already-exists
)
676 (file-exists-p newname
))
677 (tramp-error v
'file-already-exists newname
))
679 ;; We must also flush the cache of the directory, because
680 ;; `file-attributes' reads the values from there.
681 (tramp-flush-file-property v
(file-name-directory localname
))
682 (tramp-flush-file-property v localname
)
683 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
685 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
687 ;; KEEP-DATE handling.
689 (set-file-times newname
(nth 5 (file-attributes filename
))))))
691 (defun tramp-adb-handle-rename-file
692 (filename newname
&optional ok-if-already-exists
)
693 "Like `rename-file' for Tramp files."
694 (setq filename
(expand-file-name filename
)
695 newname
(expand-file-name newname
))
697 (let ((t1 (tramp-tramp-file-p filename
))
698 (t2 (tramp-tramp-file-p newname
)))
699 (with-parsed-tramp-file-name (if t1 filename newname
) nil
700 (with-tramp-progress-reporter
701 v
0 (format "Renaming %s to %s" filename newname
)
704 (tramp-equal-remote filename newname
)
705 (not (file-directory-p filename
)))
706 (let ((l1 (tramp-file-name-handler
707 'file-remote-p filename
'localname
))
708 (l2 (tramp-file-name-handler
709 'file-remote-p newname
'localname
)))
710 (when (and (not ok-if-already-exists
)
711 (file-exists-p newname
))
712 (tramp-error v
'file-already-exists newname
))
713 ;; We must also flush the cache of the directory, because
714 ;; `file-attributes' reads the values from there.
715 (tramp-flush-file-property v
(file-name-directory l1
))
716 (tramp-flush-file-property v l1
)
717 (tramp-flush-file-property v
(file-name-directory l2
))
718 (tramp-flush-file-property v l2
)
720 (tramp-adb-barf-unless-okay
721 v
(format "mv %s %s" l1 l2
)
722 "Error renaming %s to %s" filename newname
))
725 (copy-file filename newname ok-if-already-exists t t
)
726 (delete-file filename
))))))
728 (defun tramp-adb-handle-process-file
729 (program &optional infile destination display
&rest args
)
730 "Like `process-file' for Tramp files."
731 ;; The implementation is not complete yet.
732 (when (and (numberp destination
) (zerop destination
))
733 (error "Implementation does not handle immediate return"))
735 (with-parsed-tramp-file-name default-directory nil
736 (let (command input tmpinput stderr tmpstderr outbuf ret
)
738 (setq command
(mapconcat 'tramp-shell-quote-argument
739 (cons program args
) " "))
742 (setq input
"/dev/null")
743 (setq infile
(expand-file-name infile
))
744 (if (tramp-equal-remote default-directory infile
)
745 ;; INFILE is on the same remote host.
746 (setq input
(with-parsed-tramp-file-name infile nil localname
))
747 ;; INFILE must be copied to remote host.
748 (setq input
(tramp-make-tramp-temp-file v
)
749 tmpinput
(tramp-make-tramp-file-name method user host input
))
750 (copy-file infile tmpinput t
)))
751 (when input
(setq command
(format "%s <%s" command input
)))
756 ((bufferp destination
)
757 (setq outbuf destination
))
759 ((stringp destination
)
760 (setq outbuf
(get-buffer-create destination
)))
761 ;; (REAL-DESTINATION ERROR-DESTINATION)
765 ((bufferp (car destination
))
766 (setq outbuf
(car destination
)))
767 ((stringp (car destination
))
768 (setq outbuf
(get-buffer-create (car destination
))))
770 (setq outbuf
(current-buffer))))
773 ((stringp (cadr destination
))
774 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
775 (if (tramp-equal-remote default-directory
(cadr destination
))
776 ;; stderr is on the same remote host.
777 (setq stderr
(with-parsed-tramp-file-name
778 (cadr destination
) nil localname
))
779 ;; stderr must be copied to remote host. The temporary
780 ;; file must be deleted after execution.
781 (setq stderr
(tramp-make-tramp-temp-file v
)
782 tmpstderr
(tramp-make-tramp-file-name
783 method user host stderr
))))
784 ;; stderr to be discarded.
785 ((null (cadr destination
))
786 (setq stderr
"/dev/null"))))
789 (setq outbuf
(current-buffer))))
790 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
792 ;; Send the command. It might not return in time, so we protect
793 ;; it. Call it in a subshell, in order to preserve working
798 (if (tramp-adb-send-command-and-check
800 (format "(cd %s; %s)"
801 (tramp-shell-quote-argument localname
) command
))
802 ;; Set return status accordingly.
804 ;; We should add the output anyway.
806 (with-current-buffer outbuf
807 (insert-buffer-substring (tramp-get-connection-buffer v
)))
808 (when (and display
(get-buffer-window outbuf t
)) (redisplay))))
809 ;; When the user did interrupt, we should do it also. We use
810 ;; return code -1 as marker.
812 (kill-buffer (tramp-get-connection-buffer v
))
816 (kill-buffer (tramp-get-connection-buffer v
))
819 ;; Provide error file.
820 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
822 ;; Cleanup. We remove all file cache values for the connection,
823 ;; because the remote process could have changed them.
824 (when tmpinput
(delete-file tmpinput
))
826 ;; `process-file-side-effects' has been introduced with GNU
827 ;; Emacs 23.2. If set to `nil', no remote file will be changed
828 ;; by `program'. If it doesn't exist, we assume its default
830 (unless (and (boundp 'process-file-side-effects
)
831 (not (symbol-value 'process-file-side-effects
)))
832 (tramp-flush-directory-property v
""))
834 ;; Return exit status.
839 (defun tramp-adb-handle-shell-command
840 (command &optional output-buffer error-buffer
)
841 "Like `shell-command' for Tramp files."
842 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
843 ;; We cannot use `shell-file-name' and `shell-command-switch',
844 ;; they are variables of the local host.
845 (args (list "sh" "-c" (substring command
0 asynchronous
)))
849 ((bufferp output-buffer
) output-buffer
)
850 ((stringp output-buffer
) (get-buffer-create output-buffer
))
852 (setq current-buffer-p t
)
854 (t (get-buffer-create
856 "*Async Shell Command*"
857 "*Shell Command Output*")))))
860 ((bufferp error-buffer
) error-buffer
)
861 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
863 (if (and (not asynchronous
) error-buffer
)
864 (with-parsed-tramp-file-name default-directory nil
865 (list output-buffer
(tramp-make-tramp-temp-file v
)))
867 (p (get-buffer-process output-buffer
)))
869 ;; Check whether there is another process running. Tramp does not
870 ;; support 2 (asynchronous) processes in parallel.
872 (if (yes-or-no-p "A command is running. Kill it? ")
873 (ignore-errors (kill-process p
))
874 (tramp-user-error p
"Shell command in progress")))
878 (barf-if-buffer-read-only)
880 (with-current-buffer output-buffer
881 (setq buffer-read-only nil
)
884 (if (and (not current-buffer-p
) (integerp asynchronous
))
887 (apply 'start-file-process
"*Async Shell*" buffer args
)
889 (pop-to-buffer output-buffer
)
890 (setq mode-line-process
'(":%s"))
895 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
896 ;; Insert error messages if they were separated.
898 (with-current-buffer error-buffer
899 (insert-file-contents (cadr buffer
)))
900 (delete-file (cadr buffer
)))
902 ;; This is like exchange-point-and-mark, but doesn't
903 ;; activate the mark. It is cleaner to avoid activation,
904 ;; even though the command loop would deactivate the mark
905 ;; because we inserted text.
906 (goto-char (prog1 (mark t
)
907 (set-marker (mark-marker) (point)
909 ;; There's some output, display it.
910 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
911 (if (functionp 'display-message-or-buffer
)
912 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
913 (pop-to-buffer output-buffer
))))))))
915 ;; We use BUFFER also as connection buffer during setup. Because of
916 ;; this, its original contents must be saved, and restored once
917 ;; connection has been setup.
918 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
919 "Like `start-file-process' for Tramp files."
920 (with-parsed-tramp-file-name default-directory nil
921 ;; When PROGRAM is nil, we should provide a tty. This is not
923 (unless (stringp program
)
924 (tramp-error v
'file-error
"PROGRAM must be a string"))
928 (tramp-shell-quote-argument localname
)
929 (mapconcat 'tramp-shell-quote-argument
930 (cons program args
) " ")))
931 (tramp-process-connection-type
932 (or (null program
) tramp-process-connection-type
))
933 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
938 ;; BUFFER can be nil. We use a temporary buffer.
939 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
940 (while (get-process name1
)
941 ;; NAME must be unique as process name.
943 name1
(format "%s<%d>" name i
)))
945 ;; Set the new process properties.
946 (tramp-set-connection-property v
"process-name" name
)
947 (tramp-set-connection-property v
"process-buffer" buffer
)
949 (with-current-buffer (tramp-get-connection-buffer v
)
951 ;; We catch this event. Otherwise, `start-process' could
952 ;; be called on the local host.
955 ;; Activate narrowing in order to save BUFFER
956 ;; contents. Clear also the modification time;
957 ;; otherwise we might be interrupted by
958 ;; `verify-visited-file-modtime'.
959 (let ((buffer-undo-list t
)
960 (buffer-read-only nil
)
962 (clear-visited-file-modtime)
963 (narrow-to-region (point-max) (point-max))
964 ;; We call `tramp-adb-maybe-open-connection', in
965 ;; order to cleanup the prompt afterwards.
966 (tramp-adb-maybe-open-connection v
)
968 (delete-region mark
(point))
969 (narrow-to-region (point-max) (point-max))
971 (let ((tramp-adb-prompt (regexp-quote command
)))
972 (tramp-adb-send-command v command
))
973 (let ((p (tramp-get-connection-process v
)))
974 ;; Set query flag and process marker for this
975 ;; process. We ignore errors, because the process
976 ;; could have finished already.
978 (tramp-compat-set-process-query-on-exit-flag p t
)
979 (set-marker (process-mark p
) (point)))
984 (if (string-match tramp-temp-buffer-name
(buffer-name))
986 (set-process-buffer (tramp-get-connection-process v
) nil
)
987 (kill-buffer (current-buffer)))
988 (set-buffer-modified-p bmp
))
989 (tramp-set-connection-property v
"process-name" nil
)
990 (tramp-set-connection-property v
"process-buffer" nil
))))))
994 (defun tramp-adb-execute-adb-command (vec &rest args
)
995 "Returns nil on success error-output on failure."
996 (when (> (length (tramp-file-name-host vec
)) 0)
997 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
1002 (apply 'tramp-call-process vec tramp-adb-program nil t nil args
))
1004 (tramp-message vec
6 "%s" (buffer-string)))))
1006 (defun tramp-adb-find-test-command (vec)
1007 "Checks, whether the ash has a builtin \"test\" command.
1008 This happens for Android >= 4.0."
1009 (with-tramp-connection-property vec
"test"
1010 (tramp-adb-send-command-and-check vec
"type test")))
1012 ;; Connection functions
1014 (defun tramp-adb-send-command (vec command
)
1015 "Send the COMMAND to connection VEC."
1016 (tramp-adb-maybe-open-connection vec
)
1017 (tramp-message vec
6 "%s" command
)
1018 (tramp-send-string vec command
)
1019 ;; fixme: Race condition
1020 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
1021 (with-current-buffer (tramp-get-connection-buffer vec
)
1023 (goto-char (point-min))
1024 ;; We can't use stty to disable echo of command.
1025 (delete-matching-lines (regexp-quote command
))
1026 ;; When the local machine is W32, there are still trailing ^M.
1027 ;; There must be a better solution by setting the correct coding
1028 ;; system, but this requires changes in core Tramp.
1029 (goto-char (point-min))
1030 (while (re-search-forward "\r+$" nil t
)
1031 (replace-match "" nil nil
)))))
1033 (defun tramp-adb-send-command-and-check
1035 "Run COMMAND and check its exit status.
1036 Sends `echo $?' along with the COMMAND for checking the exit
1037 status. If COMMAND is nil, just sends `echo $?'. Returns nil if
1038 the exit status is not equal 0, and t otherwise."
1039 (tramp-adb-send-command
1041 (format "%s; echo tramp_exit_status $?" command
)
1042 "echo tramp_exit_status $?"))
1043 (with-current-buffer (tramp-get-connection-buffer vec
)
1044 (goto-char (point-max))
1045 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1047 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1048 (skip-chars-forward "^ ")
1050 (zerop (read (current-buffer)))
1051 (let (buffer-read-only)
1052 (delete-region (match-beginning 0) (point-max))))))
1054 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
1055 "Run COMMAND, check exit status, throw error if exit status not okay.
1056 FMT and ARGS are passed to `error'."
1057 (unless (tramp-adb-send-command-and-check vec command
)
1058 (apply 'tramp-error vec
'file-error fmt args
)))
1060 (defun tramp-adb-wait-for-output (proc &optional timeout
)
1061 "Wait for output from remote command."
1062 (unless (buffer-live-p (process-buffer proc
))
1063 (delete-process proc
)
1064 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
1065 (with-current-buffer (process-buffer proc
)
1066 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
1067 (let (buffer-read-only)
1068 (goto-char (point-min))
1069 ;; ADB terminal sends "^H" sequences.
1070 (when (re-search-forward "<\b+" (point-at-eol) t
)
1072 (delete-region (point-min) (point)))
1073 ;; Delete the prompt.
1074 (goto-char (point-min))
1075 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
1077 (delete-region (point-min) (point)))
1078 (goto-char (point-max))
1079 (re-search-backward tramp-adb-prompt nil t
)
1080 (delete-region (point) (point-max)))
1084 "[[Remote adb prompt `%s' not found in %d secs]]"
1085 tramp-adb-prompt timeout
)
1088 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
1090 (defun tramp-adb-maybe-open-connection (vec)
1091 "Maybe open a connection VEC.
1092 Does not do anything if a connection is already open, but re-opens the
1093 connection if a previous connection has died for some reason."
1094 (tramp-check-proper-method-and-host vec
)
1096 (let* ((buf (tramp-get-connection-buffer vec
))
1097 (p (get-buffer-process buf
))
1098 (host (tramp-file-name-host vec
))
1099 (user (tramp-file-name-user vec
))
1102 ;; Maybe we know already that "su" is not supported. We cannot
1103 ;; use a connection property, because we have not checked yet
1104 ;; whether it is still the same device.
1105 (when (and user
(not (tramp-get-file-property vec
"" "su-command-p" t
)))
1106 (tramp-error vec
'file-error
"Cannot switch to user `%s'" user
))
1109 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1111 (when (and p
(processp p
)) (delete-process p
))
1112 (setq devices
(mapcar 'cadr
(tramp-adb-parse-device-names nil
)))
1114 (tramp-error vec
'file-error
"No device connected"))
1115 (if (and (> (length host
) 0) (not (member host devices
)))
1116 (tramp-error vec
'file-error
"Device %s not connected" host
))
1117 (if (and (> (length devices
) 1) (zerop (length host
)))
1120 "Multiple Devices connected: No Host/Device specified"))
1121 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1122 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1123 (process-connection-type tramp-process-connection-type
)
1124 (args (if (> (length host
) 0)
1125 (list "-s" host
"shell")
1127 (p (let ((default-directory
1128 (tramp-compat-temporary-file-directory)))
1129 (apply 'start-process
(tramp-get-connection-name vec
) buf
1130 tramp-adb-program args
))))
1132 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1133 ;; Wait for initial prompt.
1134 (tramp-adb-wait-for-output p
30)
1135 (unless (eq 'run
(process-status p
))
1136 (tramp-error vec
'file-error
"Terminated!"))
1137 (tramp-set-connection-property p
"vector" vec
)
1138 (tramp-compat-set-process-query-on-exit-flag p nil
)
1140 ;; Check whether the properties have been changed. If
1141 ;; yes, this is a strong indication that we must expire all
1142 ;; connection properties. We start again.
1143 (tramp-message vec
5 "Checking system information")
1144 (tramp-adb-send-command
1145 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1147 (tramp-get-connection-property vec
"getprop" nil
))
1149 (tramp-set-connection-property
1151 (with-current-buffer (tramp-get-connection-buffer vec
)
1152 ;; Read the expression.
1153 (goto-char (point-min))
1154 (read (current-buffer))))))
1155 (when (and (stringp old-getprop
)
1156 (not (string-equal old-getprop new-getprop
)))
1159 "Connection reset, because remote host changed from `%s' to `%s'"
1160 old-getprop new-getprop
)
1161 (tramp-cleanup-connection vec t
)
1162 (tramp-adb-maybe-open-connection vec
)))
1164 ;; Change user if indicated.
1166 (tramp-adb-send-command vec
(format "su %s" user
))
1167 (unless (tramp-adb-send-command-and-check vec nil
)
1169 (tramp-set-file-property vec
"" "su-command-p" nil
)
1171 vec
'file-error
"Cannot switch to user `%s'" user
)))
1173 ;; Set "remote-path" connection property. This is needed
1175 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1176 (tramp-set-connection-property
1179 (with-current-buffer (tramp-get-connection-buffer vec
)
1180 ;; Read the expression.
1181 (goto-char (point-min))
1182 (read (current-buffer)))
1183 ":" 'omit-nulls
))))))))
1185 (add-hook 'tramp-unload-hook
1187 (unload-feature 'tramp-adb
'force
)))
1189 (provide 'tramp-adb
)
1190 ;;; tramp-adb.el ends here