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 (defvar dired-move-to-filename-regexp
)
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 '((directory-file-name . tramp-handle-directory-file-name
)
89 (dired-uncache . tramp-handle-dired-uncache
)
90 (file-name-as-directory . tramp-handle-file-name-as-directory
)
91 (file-name-completion . tramp-handle-file-name-completion
)
92 (file-name-all-completions . tramp-adb-handle-file-name-all-completions
)
93 (file-attributes . tramp-adb-handle-file-attributes
)
94 (file-name-directory . tramp-handle-file-name-directory
)
95 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
96 (file-truename . tramp-adb-handle-file-truename
)
97 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
98 (file-name-as-directory . tramp-handle-file-name-as-directory
)
99 (file-regular-p . tramp-handle-file-regular-p
)
100 (file-remote-p . tramp-handle-file-remote-p
)
101 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
102 (file-directory-p . tramp-adb-handle-file-directory-p
)
103 (file-symlink-p . tramp-handle-file-symlink-p
)
104 ;; FIXME: This is too sloppy.
105 (file-executable-p . tramp-handle-file-exists-p
)
106 (file-exists-p . tramp-handle-file-exists-p
)
107 (file-readable-p . tramp-handle-file-exists-p
)
108 (file-writable-p . tramp-adb-handle-file-writable-p
)
109 (file-local-copy . tramp-adb-handle-file-local-copy
)
110 (file-modes . tramp-handle-file-modes
)
111 (expand-file-name . tramp-adb-handle-expand-file-name
)
112 (find-backup-file-name . tramp-handle-find-backup-file-name
)
113 (directory-files . tramp-handle-directory-files
)
114 (directory-files-and-attributes
115 . tramp-adb-handle-directory-files-and-attributes
)
116 (make-directory . tramp-adb-handle-make-directory
)
117 (delete-directory . tramp-adb-handle-delete-directory
)
118 (delete-file . tramp-adb-handle-delete-file
)
119 (load . tramp-handle-load
)
120 (insert-directory . tramp-adb-handle-insert-directory
)
121 (insert-file-contents . tramp-handle-insert-file-contents
)
122 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
123 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
124 (vc-registered . ignore
) ;no vc control files on Android devices
125 (write-region . tramp-adb-handle-write-region
)
126 (set-file-modes . tramp-adb-handle-set-file-modes
)
127 (set-file-times . tramp-adb-handle-set-file-times
)
128 (copy-file . tramp-adb-handle-copy-file
)
129 (rename-file . tramp-adb-handle-rename-file
)
130 (process-file . tramp-adb-handle-process-file
)
131 (shell-command . tramp-adb-handle-shell-command
)
132 (start-file-process . tramp-adb-handle-start-file-process
))
133 "Alist of handler functions for Tramp ADB method.")
135 ;; It must be a `defsubst' in order to push the whole code into
136 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
138 (defsubst tramp-adb-file-name-p
(filename)
139 "Check if it's a filename for ADB."
140 (let ((v (tramp-dissect-file-name filename
)))
141 (string= (tramp-file-name-method v
) tramp-adb-method
)))
144 (defun tramp-adb-file-name-handler (operation &rest args
)
145 "Invoke the ADB handler for OPERATION.
146 First arg specifies the OPERATION, second arg is a list of arguments to
147 pass to the OPERATION."
148 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
)))
150 (save-match-data (apply (cdr fn
) args
))
151 (tramp-run-real-handler operation args
))))
154 (defun tramp-adb-parse-device-names (ignore)
155 "Return a list of (nil host) tuples allowed to access."
158 ;; `call-process' does not react on timer under MS Windows.
159 ;; That's why we use `start-process'.
160 (let ((p (start-process
161 tramp-adb-program
(current-buffer) tramp-adb-program
"devices"))
163 (tramp-compat-set-process-query-on-exit-flag p nil
)
164 (while (eq 'run
(process-status p
))
166 (goto-char (point-min))
167 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
168 (add-to-list 'result
(list nil
(match-string 1))))
171 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
172 "Like `expand-file-name' for Tramp files."
173 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
174 (setq dir
(or dir default-directory
"/"))
175 ;; Unless NAME is absolute, concat DIR and NAME.
176 (unless (file-name-absolute-p name
)
177 (setq name
(concat (file-name-as-directory dir
) name
)))
178 ;; If NAME is not a Tramp file, run the real handler.
179 (if (not (tramp-tramp-file-p name
))
180 (tramp-run-real-handler 'expand-file-name
(list name nil
))
182 (with-parsed-tramp-file-name name nil
183 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
184 (setq localname
(concat "/" localname
)))
185 ;; Do normal `expand-file-name' (this does "/./" and "/../").
186 ;; We bind `directory-sep-char' here for XEmacs on Windows,
187 ;; which would otherwise use backslash. `default-directory' is
188 ;; bound, because on Windows there would be problems with UNC
189 ;; shares or Cygwin mounts.
190 (let ((directory-sep-char ?
/)
191 (default-directory (tramp-compat-temporary-file-directory)))
192 (tramp-make-tramp-file-name
194 (tramp-drop-volume-letter
195 (tramp-run-real-handler
196 'expand-file-name
(list localname
))))))))
198 (defun tramp-adb-handle-file-directory-p (filename)
199 "Like `file-directory-p' for Tramp files."
200 (car (file-attributes (file-truename filename
))))
202 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
203 ;; code could be shared?
204 (defun tramp-adb-handle-file-truename (filename &optional counter prev-dirs
)
205 "Like `file-truename' for Tramp files."
206 (with-parsed-tramp-file-name (expand-file-name filename
) nil
207 (with-tramp-file-property v localname
"file-truename"
208 (let ((result nil
)) ; result steps in reverse order
209 (tramp-message v
4 "Finding true name for `%s'" filename
)
210 (let* ((directory-sep-char ?
/)
211 (steps (tramp-compat-split-string localname
"/"))
212 (localnamedir (tramp-run-real-handler
213 'file-name-as-directory
(list localname
)))
214 (is-dir (string= localname localnamedir
))
217 ;; Don't make the following value larger than
218 ;; necessary. People expect an error message in a
219 ;; timely fashion when something is wrong; otherwise
220 ;; they might think that Emacs is hung. Of course,
221 ;; correctness has to come first.
224 (while (and steps
(< numchase numchase-limit
))
225 (setq thisstep
(pop steps
))
229 (append '("") (reverse result
) (list thisstep
))
232 (nth 0 (file-attributes
233 (tramp-make-tramp-file-name
240 (cond ((string= "." thisstep
)
241 (tramp-message v
5 "Ignoring step `.'"))
242 ((string= ".." thisstep
)
243 (tramp-message v
5 "Processing step `..'")
245 ((stringp symlink-target
)
246 ;; It's a symlink, follow it.
247 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
248 (setq numchase
(1+ numchase
))
249 (when (file-name-absolute-p symlink-target
)
251 ;; If the symlink was absolute, we'll get a string
252 ;; like "/user@host:/some/target"; extract the
253 ;; "/some/target" part from it.
254 (when (tramp-tramp-file-p symlink-target
)
255 (unless (tramp-equal-remote filename symlink-target
)
258 "Symlink target `%s' on wrong host" symlink-target
))
259 (setq symlink-target localname
))
261 (append (tramp-compat-split-string
266 (setq result
(cons thisstep result
)))))
267 (when (>= numchase numchase-limit
)
270 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
271 (setq result
(reverse result
))
272 ;; Combine list to form string.
275 (mapconcat 'identity
(cons "" result
) "/")
277 (when (and is-dir
(or (string= "" result
)
278 (not (string= (substring result -
1) "/"))))
279 (setq result
(concat result
"/"))))
281 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
282 (tramp-make-tramp-file-name method user host result
)))))
284 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
285 "Like `file-attributes' for Tramp files."
286 (unless id-format
(setq id-format
'integer
))
288 (with-parsed-tramp-file-name filename nil
289 (with-tramp-file-property
290 v localname
(format "file-attributes-%s" id-format
)
291 (tramp-adb-barf-unless-okay
292 v
(format "%s -d -l %s"
293 (tramp-adb-get-ls-command v
)
294 (tramp-shell-quote-argument localname
)) "")
295 (with-current-buffer (tramp-get-buffer v
)
296 (tramp-adb-sh-fix-ls-output)
297 (cdar (tramp-do-parse-file-attributes-with-ls v id-format
)))))))
299 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format
)
300 "Parse `file-attributes' for Tramp files using the ls(1) command."
301 (with-current-buffer (tramp-get-buffer vec
)
302 (goto-char (point-min))
303 (let ((file-properties nil
))
304 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t
)
305 (let* ((mod-string (match-string 1))
306 (is-dir (eq ?d
(aref mod-string
0)))
307 (is-symlink (eq ?l
(aref mod-string
0)))
308 (uid (match-string 2))
309 (gid (match-string 3))
310 (size (string-to-number (match-string 4)))
311 (date (match-string 5))
312 (name (match-string 6))
315 (cadr (split-string name
"\\( -> \\|\n\\)")))))
318 (car (split-string name
"\\( -> \\|\n\\)"))
320 (or is-dir symlink-target
)
322 ;; no way to handle numeric ids in Androids ash
323 (if (eq id-format
'integer
) 0 uid
)
324 (if (eq id-format
'integer
) 0 gid
)
326 (date-to-time date
) ; mtime
332 (tramp-get-device vec
))
336 (defun tramp-adb-handle-directory-files-and-attributes
337 (directory &optional full match nosort id-format
)
338 "Like `directory-files-and-attributes' for Tramp files."
339 (when (file-directory-p directory
)
340 (with-parsed-tramp-file-name (expand-file-name directory
) nil
341 (with-tramp-file-property
342 v localname
(format "directory-files-attributes-%s-%s-%s-%s"
343 full match id-format nosort
)
344 (tramp-adb-barf-unless-okay
345 v
(format "%s -a -l %s"
346 (tramp-adb-get-ls-command v
)
347 (tramp-shell-quote-argument localname
)) "")
348 (with-current-buffer (tramp-get-buffer v
)
349 (tramp-adb-sh-fix-ls-output)
350 (let ((result (tramp-do-parse-file-attributes-with-ls
351 v
(or id-format
'integer
))))
356 (cons (expand-file-name (car x
) directory
) (cdr x
)))
360 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))
363 (if (or (not match
) (string-match match
(car x
)))
367 (defun tramp-adb-get-ls-command (vec)
368 (with-tramp-connection-property vec
"ls"
369 (tramp-message vec
5 "Finding a suitable `ls' command")
370 (if (zerop (tramp-adb-command-exit-status
371 vec
"ls --color=never -al /dev/null"))
372 ;; On CyanogenMod based system BusyBox is used and "ls" output
373 ;; coloring is enabled by default. So we try to disable it
378 (defun tramp-adb-get-toolbox (vec)
379 "Get shell toolbox implementation: `toolbox' for original distributions
380 or `busybox' for CyanogenMod based distributions"
381 (with-tramp-connection-property vec
"toolbox"
382 (tramp-message vec
5 "Checking shell toolbox implementation")
384 ((zerop (tramp-adb-command-exit-status vec
"busybox")) 'busybox
)
385 ((zerop (tramp-adb-command-exit-status vec
"toolbox")) 'toolbox
)
388 (defun tramp-adb--gnu-switches-to-ash
390 "Almquist shell can't handle multiple arguments.
391 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
395 (replace-regexp-in-string
397 (replace-regexp-in-string "^-" "" s
)))
398 ;; FIXME: Warning about removed switches (long and non-dash).
402 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
405 (defun tramp-adb-handle-insert-directory
406 (filename switches
&optional wildcard full-directory-p
)
407 "Like `insert-directory' for Tramp files."
408 (when (stringp switches
)
409 (setq switches
(tramp-adb--gnu-switches-to-ash (split-string switches
))))
410 (with-parsed-tramp-file-name (file-truename filename
) nil
411 (with-current-buffer (tramp-get-buffer v
)
412 (let ((name (tramp-shell-quote-argument (directory-file-name localname
)))
413 (switch-d (member "-d" switches
))
414 (switch-t (member "-t" switches
))
415 (switches (mapconcat 'identity
(remove "-t" switches
) " ")))
416 (tramp-adb-barf-unless-okay
417 v
(format "%s %s %s" (tramp-adb-get-ls-command v
) switches name
)
418 "Cannot insert directory listing: %s" filename
)
420 ;; We insert also filename/. and filename/.., because "ls" doesn't.
421 (narrow-to-region (point) (point))
423 (tramp-adb-barf-unless-okay
424 v
(format "%s -d %s %s %s"
425 (tramp-adb-get-ls-command v
)
427 (concat (file-name-as-directory name
) ".")
428 (concat (file-name-as-directory name
) ".."))
429 "Cannot insert directory listing: %s" filename
))
431 (tramp-adb-sh-fix-ls-output switch-t
)))
432 (insert-buffer-substring (tramp-get-buffer v
))))
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 (= (point) (line-beginning-position))
465 (defun tramp-adb-ls-output-time-less-p (a b
)
466 "Sort \"ls\" output by time, descending."
468 (string-match tramp-adb-ls-date-regexp a
)
469 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
470 (string-match tramp-adb-ls-date-regexp b
)
471 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
472 (tramp-time-less-p time-b time-a
)))
474 (defun tramp-adb-ls-output-name-less-p (a b
)
475 "Sort \"ls\" output by name, ascending."
477 (string-match dired-move-to-filename-regexp a
)
478 (setq posa
(match-end 0))
479 (string-match dired-move-to-filename-regexp b
)
480 (setq posb
(match-end 0))
481 (string-lessp (substring a posa
) (substring b posb
))))
483 (defun tramp-adb-handle-make-directory (dir &optional parents
)
484 "Like `make-directory' for Tramp files."
485 (setq dir
(expand-file-name dir
))
486 (with-parsed-tramp-file-name dir nil
488 (let ((par (expand-file-name ".." dir
)))
489 (unless (file-directory-p par
)
490 (make-directory par parents
))))
491 (tramp-adb-barf-unless-okay
492 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
493 "Couldn't make directory %s" dir
)
494 (tramp-flush-directory-property v
(file-name-directory localname
))))
496 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
497 "Like `delete-directory' for Tramp files."
498 (setq directory
(expand-file-name directory
))
499 (with-parsed-tramp-file-name directory nil
500 (tramp-flush-file-property v
(file-name-directory localname
))
501 (tramp-flush-directory-property v localname
)
502 (tramp-adb-barf-unless-okay
504 (if recursive
"rm -r" "rmdir")
505 (tramp-shell-quote-argument localname
))
506 "Couldn't delete %s" directory
)))
508 (defun tramp-adb-handle-delete-file (filename &optional trash
)
509 "Like `delete-file' for Tramp files."
510 (setq filename
(expand-file-name filename
))
511 (with-parsed-tramp-file-name filename nil
512 (tramp-flush-file-property v
(file-name-directory localname
))
513 (tramp-flush-file-property v localname
)
514 (tramp-adb-barf-unless-okay
515 v
(format "rm %s" (tramp-shell-quote-argument localname
))
516 "Couldn't delete %s" filename
)))
518 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
519 "Like `file-name-all-completions' for Tramp files."
522 (with-parsed-tramp-file-name directory nil
523 (with-tramp-file-property v localname
"file-name-all-completions"
525 (tramp-adb-send-command
527 (tramp-adb-get-ls-command v
)
528 (tramp-shell-quote-argument localname
)))
531 (if (file-directory-p f
)
532 (file-name-as-directory f
)
534 (with-current-buffer (tramp-get-buffer v
)
538 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
539 (split-string (buffer-string) "\n"))))))))))
541 (defun tramp-adb-handle-file-local-copy (filename)
542 "Like `file-local-copy' for Tramp files."
543 (with-parsed-tramp-file-name filename nil
544 (unless (file-exists-p (file-truename filename
))
547 "Cannot make local copy of non-existing file `%s'" filename
))
548 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
549 (with-tramp-progress-reporter
550 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
551 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
552 (delete-file tmpfile
)
554 v
'file-error
"Cannot make local copy of file `%s'" filename
))
555 (set-file-modes tmpfile
(file-modes filename
)))
558 (defun tramp-adb-handle-file-writable-p (filename)
559 "Like `tramp-sh-handle-file-writable-p'.
560 But handle the case, if the \"test\" command is not available."
561 (with-parsed-tramp-file-name filename nil
562 (with-tramp-file-property v localname
"file-writable-p"
563 (if (tramp-adb-find-test-command v
)
564 (if (file-exists-p filename
)
566 (tramp-adb-command-exit-status
567 v
(format "test -w %s" (tramp-shell-quote-argument localname
))))
569 (file-directory-p (file-name-directory filename
))
570 (file-writable-p (file-name-directory filename
))))
572 ;; Missing "test" command on Android < 4.
573 (let ((rw-path "/data/data"))
576 "Not implemented yet (assuming \"/data/data\" is writable): %s"
578 (and (>= (length localname
) (length rw-path
))
579 (string= (substring localname
0 (length rw-path
))
582 (defun tramp-adb-handle-write-region
583 (start end filename
&optional append visit lockname confirm
)
584 "Like `write-region' for Tramp files."
585 (setq filename
(expand-file-name filename
))
586 (with-parsed-tramp-file-name filename nil
589 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
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 (tramp-run-real-handler
602 (list start end tmpfile append
'no-message lockname confirm
))
603 (with-tramp-progress-reporter
604 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
606 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
607 (tramp-error v
'file-error
"Cannot write: `%s' filename"))
608 (delete-file tmpfile
)))
610 (unless (equal curbuf
(current-buffer))
613 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
615 (defun tramp-adb-handle-set-file-modes (filename mode
)
616 "Like `set-file-modes' for Tramp files."
617 (with-parsed-tramp-file-name filename nil
618 (tramp-flush-file-property v localname
)
619 (tramp-adb-barf-unless-okay
620 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
)
621 "Error while changing file's mode %s" filename
)))
623 (defun tramp-adb-handle-set-file-times (filename &optional time
)
624 "Like `set-file-times' for Tramp files."
625 (with-parsed-tramp-file-name filename nil
626 (tramp-flush-file-property v localname
)
627 (let ((time (if (or (null time
) (equal time
'(0 0)))
630 (tramp-adb-command-exit-status
631 ;; use shell arithmetic because of Emacs integer size limit
632 v
(format "touch -t $(( %d * 65536 + %d )) %s"
633 (car time
) (cadr time
)
634 (tramp-shell-quote-argument localname
))))))
636 (defun tramp-adb-handle-copy-file
637 (filename newname
&optional ok-if-already-exists keep-date
638 preserve-uid-gid preserve-extended-attributes
)
639 "Like `copy-file' for Tramp files.
640 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
641 (setq filename
(expand-file-name filename
)
642 newname
(expand-file-name newname
))
644 (if (file-directory-p filename
)
645 (tramp-file-name-handler 'copy-directory filename newname keep-date t
)
646 (with-tramp-progress-reporter
647 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
648 0 (format "Copying %s to %s" filename newname
)
650 (let ((tmpfile (file-local-copy filename
)))
655 (rename-file tmpfile newname ok-if-already-exists
)
657 (delete-file tmpfile
)
658 (signal (car err
) (cdr err
))))
661 (when (file-directory-p newname
)
663 (expand-file-name (file-name-nondirectory filename
) newname
)))
665 (with-parsed-tramp-file-name newname nil
666 (when (and (not ok-if-already-exists
)
667 (file-exists-p newname
))
668 (tramp-error v
'file-already-exists newname
))
670 ;; We must also flush the cache of the directory, because
671 ;; `file-attributes' reads the values from there.
672 (tramp-flush-file-property v
(file-name-directory localname
))
673 (tramp-flush-file-property v localname
)
674 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
676 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
678 ;; KEEP-DATE handling.
680 (set-file-times newname
(nth 5 (file-attributes filename
))))))
682 (defun tramp-adb-handle-rename-file
683 (filename newname
&optional ok-if-already-exists
)
684 "Like `rename-file' for Tramp files."
685 (setq filename
(expand-file-name filename
)
686 newname
(expand-file-name newname
))
688 (with-parsed-tramp-file-name
689 (if (file-remote-p filename
) filename newname
) nil
690 (with-tramp-progress-reporter
691 v
0 (format "Renaming %s to %s" newname filename
)
693 (if (and (tramp-equal-remote filename newname
)
694 (not (file-directory-p filename
)))
696 (when (and (not ok-if-already-exists
)
697 (file-exists-p newname
))
698 (tramp-error v
'file-already-exists newname
))
699 ;; We must also flush the cache of the directory, because
700 ;; `file-attributes' reads the values from there.
701 (tramp-flush-file-property v
(file-name-directory localname
))
702 (tramp-flush-file-property v localname
)
704 (tramp-adb-barf-unless-okay
707 (tramp-file-name-handler 'file-remote-p filename
'localname
)
709 "Error renaming %s to %s" filename newname
))
712 (copy-file filename newname ok-if-already-exists t t
)
713 (delete-file filename
)))))
715 (defun tramp-adb-handle-process-file
716 (program &optional infile destination display
&rest args
)
717 "Like `process-file' for Tramp files."
718 ;; The implementation is not complete yet.
719 (when (and (numberp destination
) (zerop destination
))
720 (error "Implementation does not handle immediate return"))
722 (with-parsed-tramp-file-name default-directory nil
723 (let (command input tmpinput stderr tmpstderr outbuf ret
)
725 (setq command
(mapconcat 'tramp-shell-quote-argument
726 (cons program args
) " "))
729 (setq input
"/dev/null")
730 (setq infile
(expand-file-name infile
))
731 (if (tramp-equal-remote default-directory infile
)
732 ;; INFILE is on the same remote host.
733 (setq input
(with-parsed-tramp-file-name infile nil localname
))
734 ;; INFILE must be copied to remote host.
735 (setq input
(tramp-make-tramp-temp-file v
)
736 tmpinput
(tramp-make-tramp-file-name method user host input
))
737 (copy-file infile tmpinput t
)))
738 (when input
(setq command
(format "%s <%s" command input
)))
743 ((bufferp destination
)
744 (setq outbuf destination
))
746 ((stringp destination
)
747 (setq outbuf
(get-buffer-create destination
)))
748 ;; (REAL-DESTINATION ERROR-DESTINATION)
752 ((bufferp (car destination
))
753 (setq outbuf
(car destination
)))
754 ((stringp (car destination
))
755 (setq outbuf
(get-buffer-create (car destination
))))
757 (setq outbuf
(current-buffer))))
760 ((stringp (cadr destination
))
761 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
762 (if (tramp-equal-remote default-directory
(cadr destination
))
763 ;; stderr is on the same remote host.
764 (setq stderr
(with-parsed-tramp-file-name
765 (cadr destination
) nil localname
))
766 ;; stderr must be copied to remote host. The temporary
767 ;; file must be deleted after execution.
768 (setq stderr
(tramp-make-tramp-temp-file v
)
769 tmpstderr
(tramp-make-tramp-file-name
770 method user host stderr
))))
771 ;; stderr to be discarded.
772 ((null (cadr destination
))
773 (setq stderr
"/dev/null"))))
776 (setq outbuf
(current-buffer))))
777 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
779 ;; Send the command. It might not return in time, so we protect
780 ;; it. Call it in a subshell, in order to preserve working
785 (tramp-adb-barf-unless-okay
786 v
(format "(cd %s; %s)"
787 (tramp-shell-quote-argument localname
) command
)
789 ;; We should show the output anyway.
791 (with-current-buffer outbuf
792 (insert-buffer-substring (tramp-get-connection-buffer v
)))
793 (when display
(display-buffer outbuf
))))
794 ;; When the user did interrupt, we should do it also. We use
795 ;; return code -1 as marker.
797 (kill-buffer (tramp-get-connection-buffer v
))
801 (kill-buffer (tramp-get-connection-buffer v
))
804 ;; Provide error file.
805 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
807 ;; Cleanup. We remove all file cache values for the connection,
808 ;; because the remote process could have changed them.
809 (when tmpinput
(delete-file tmpinput
))
811 ;; `process-file-side-effects' has been introduced with GNU
812 ;; Emacs 23.2. If set to `nil', no remote file will be changed
813 ;; by `program'. If it doesn't exist, we assume its default
815 (unless (and (boundp 'process-file-side-effects
)
816 (not (symbol-value 'process-file-side-effects
)))
817 (tramp-flush-directory-property v
""))
819 ;; Return exit status.
824 (defun tramp-adb-handle-shell-command
825 (command &optional output-buffer error-buffer
)
826 "Like `shell-command' for Tramp files."
827 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
828 ;; We cannot use `shell-file-name' and `shell-command-switch',
829 ;; they are variables of the local host.
830 (args (list "sh" "-c" (substring command
0 asynchronous
)))
834 ((bufferp output-buffer
) output-buffer
)
835 ((stringp output-buffer
) (get-buffer-create output-buffer
))
837 (setq current-buffer-p t
)
839 (t (get-buffer-create
841 "*Async Shell Command*"
842 "*Shell Command Output*")))))
845 ((bufferp error-buffer
) error-buffer
)
846 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
848 (if (and (not asynchronous
) error-buffer
)
849 (with-parsed-tramp-file-name default-directory nil
850 (list output-buffer
(tramp-make-tramp-temp-file v
)))
852 (p (get-buffer-process output-buffer
)))
854 ;; Check whether there is another process running. Tramp does not
855 ;; support 2 (asynchronous) processes in parallel.
857 (if (yes-or-no-p "A command is running. Kill it? ")
858 (ignore-errors (kill-process p
))
859 (tramp-compat-user-error "Shell command in progress")))
863 (barf-if-buffer-read-only)
865 (with-current-buffer output-buffer
866 (setq buffer-read-only nil
)
869 (if (and (not current-buffer-p
) (integerp asynchronous
))
872 (apply 'start-file-process
"*Async Shell*" buffer args
)
874 (pop-to-buffer output-buffer
)
875 (setq mode-line-process
'(":%s"))
880 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
881 ;; Insert error messages if they were separated.
883 (with-current-buffer error-buffer
884 (insert-file-contents (cadr buffer
)))
885 (delete-file (cadr buffer
)))
887 ;; This is like exchange-point-and-mark, but doesn't
888 ;; activate the mark. It is cleaner to avoid activation,
889 ;; even though the command loop would deactivate the mark
890 ;; because we inserted text.
891 (goto-char (prog1 (mark t
)
892 (set-marker (mark-marker) (point)
894 ;; There's some output, display it.
895 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
896 (if (functionp 'display-message-or-buffer
)
897 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
898 (pop-to-buffer output-buffer
))))))))
900 ;; We use BUFFER also as connection buffer during setup. Because of
901 ;; this, its original contents must be saved, and restored once
902 ;; connection has been setup.
903 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
904 "Like `start-file-process' for Tramp files."
905 (with-parsed-tramp-file-name default-directory nil
906 ;; When PROGRAM is nil, we should provide a tty. This is not
908 (unless (stringp program
)
909 (tramp-error v
'file-error
"PROGRAM must be a string"))
913 (tramp-shell-quote-argument localname
)
914 (mapconcat 'tramp-shell-quote-argument
915 (cons program args
) " ")))
916 (tramp-process-connection-type
917 (or (null program
) tramp-process-connection-type
))
918 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
923 ;; BUFFER can be nil. We use a temporary buffer.
924 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
925 (while (get-process name1
)
926 ;; NAME must be unique as process name.
928 name1
(format "%s<%d>" name i
)))
930 ;; Set the new process properties.
931 (tramp-set-connection-property v
"process-name" name
)
932 (tramp-set-connection-property v
"process-buffer" buffer
)
934 (with-current-buffer (tramp-get-connection-buffer v
)
936 ;; We catch this event. Otherwise, `start-process' could
937 ;; be called on the local host.
940 ;; Activate narrowing in order to save BUFFER
941 ;; contents. Clear also the modification time;
942 ;; otherwise we might be interrupted by
943 ;; `verify-visited-file-modtime'.
944 (let ((buffer-undo-list t
)
945 (buffer-read-only nil
)
947 (clear-visited-file-modtime)
948 (narrow-to-region (point-max) (point-max))
949 ;; We call `tramp-adb-maybe-open-connection', in
950 ;; order to cleanup the prompt afterwards.
951 (tramp-adb-maybe-open-connection v
)
953 (delete-region mark
(point))
954 (narrow-to-region (point-max) (point-max))
956 (let ((tramp-adb-prompt (regexp-quote command
)))
957 (tramp-adb-send-command v command
))
958 (let ((p (tramp-get-connection-process v
)))
959 ;; Set query flag and process marker for this
960 ;; process. We ignore errors, because the process
961 ;; could have finished already.
963 (tramp-compat-set-process-query-on-exit-flag p t
)
964 (set-marker (process-mark p
) (point)))
969 (if (string-match tramp-temp-buffer-name
(buffer-name))
971 (set-process-buffer (tramp-get-connection-process v
) nil
)
972 (kill-buffer (current-buffer)))
973 (set-buffer-modified-p bmp
))
974 (tramp-set-connection-property v
"process-name" nil
)
975 (tramp-set-connection-property v
"process-buffer" nil
))))))
979 (defun tramp-adb-execute-adb-command (vec &rest args
)
980 "Returns nil on success error-output on failure."
981 (when (> (length (tramp-file-name-host vec
)) 0)
982 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
986 (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args
))
988 (tramp-message vec
6 "%s" (buffer-string)))))
990 (defun tramp-adb-find-test-command (vec)
991 "Checks, whether the ash has a builtin \"test\" command.
992 This happens for Android >= 4.0."
993 (with-tramp-connection-property vec
"test"
994 (zerop (tramp-adb-command-exit-status vec
"type test"))))
996 ;; Connection functions
998 (defun tramp-adb-send-command (vec command
)
999 "Send the COMMAND to connection VEC."
1000 (tramp-adb-maybe-open-connection vec
)
1001 (tramp-message vec
6 "%s" command
)
1002 (tramp-send-string vec command
)
1003 ;; fixme: Race condition
1004 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
1005 (with-current-buffer (tramp-get-connection-buffer vec
)
1007 (goto-char (point-min))
1008 ;; We can't use stty to disable echo of command.
1009 (delete-matching-lines (regexp-quote command
))
1010 ;; When the local machine is W32, there are still trailing ^M.
1011 ;; There must be a better solution by setting the correct coding
1012 ;; system, but this requires changes in core Tramp.
1013 (goto-char (point-min))
1014 (while (re-search-forward "\r+$" nil t
)
1015 (replace-match "" nil nil
)))))
1017 (defun tramp-adb-command-exit-status
1019 "Run COMMAND and return its exit status.
1020 Sends `echo $?' along with the COMMAND for checking the exit status. If
1021 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
1022 (tramp-adb-send-command
1024 (format "%s; echo tramp_exit_status $?" command
)
1025 "echo tramp_exit_status $?"))
1026 (with-current-buffer (tramp-get-connection-buffer vec
)
1027 (goto-char (point-max))
1028 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1030 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1031 (skip-chars-forward "^ ")
1033 (read (current-buffer))
1034 (let (buffer-read-only)
1035 (delete-region (match-beginning 0) (point-max))))))
1037 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
1038 "Run COMMAND, check exit status, throw error if exit status not okay.
1039 FMT and ARGS are passed to `error'."
1040 (unless (zerop (tramp-adb-command-exit-status vec command
))
1041 (apply 'tramp-error vec
'file-error fmt args
)))
1043 (defun tramp-adb-wait-for-output (proc &optional timeout
)
1044 "Wait for output from remote command."
1045 (unless (buffer-live-p (process-buffer proc
))
1046 (delete-process proc
)
1047 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
1048 (with-current-buffer (process-buffer proc
)
1049 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
1050 (let (buffer-read-only)
1051 (goto-char (point-min))
1052 ;; ADB terminal sends "^H" sequences.
1053 (when (re-search-forward "<\b+" (point-at-eol) t
)
1055 (delete-region (point-min) (point)))
1056 ;; Delete the prompt.
1057 (goto-char (point-min))
1058 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
1060 (delete-region (point-min) (point)))
1061 (goto-char (point-max))
1062 (re-search-backward tramp-adb-prompt nil t
)
1063 (delete-region (point) (point-max)))
1067 "[[Remote adb prompt `%s' not found in %d secs]]"
1068 tramp-adb-prompt timeout
)
1071 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
1073 (defun tramp-adb-maybe-open-connection (vec)
1074 "Maybe open a connection VEC.
1075 Does not do anything if a connection is already open, but re-opens the
1076 connection if a previous connection has died for some reason."
1077 (let* ((buf (tramp-get-connection-buffer vec
))
1078 (p (get-buffer-process buf
))
1079 (host (tramp-file-name-host vec
))
1080 (user (tramp-file-name-user vec
))
1081 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1083 ;; Maybe we know already that "su" is not supported. We cannot
1084 ;; use a connection property, because we have not checked yet
1085 ;; whether it is still the same device.
1086 (when (and user
(not (tramp-get-file-property vec
"" "su-command-p" t
)))
1087 (tramp-error vec
'file-error
"Cannot switch to user `%s'" user
))
1090 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1092 (when (and p
(processp p
)) (delete-process p
))
1094 (tramp-error vec
'file-error
"No device connected"))
1095 (if (and (> (length host
) 0) (not (member host devices
)))
1096 (tramp-error vec
'file-error
"Device %s not connected" host
))
1097 (if (and (> (length devices
) 1) (zerop (length host
)))
1100 "Multiple Devices connected: No Host/Device specified"))
1101 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1102 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1103 (process-connection-type tramp-process-connection-type
)
1104 (args (if (> (length host
) 0)
1105 (list "-s" host
"shell")
1107 (p (let ((default-directory
1108 (tramp-compat-temporary-file-directory)))
1109 (apply 'start-process
(tramp-get-connection-name vec
) buf
1110 tramp-adb-program args
))))
1112 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1113 ;; Wait for initial prompt.
1114 (tramp-adb-wait-for-output p
30)
1115 (unless (eq 'run
(process-status p
))
1116 (tramp-error vec
'file-error
"Terminated!"))
1117 (tramp-compat-set-process-query-on-exit-flag p nil
)
1119 ;; Check whether the properties have been changed. If
1120 ;; yes, this is a strong indication that we must expire all
1121 ;; connection properties. We start again.
1122 (tramp-message vec
5 "Checking system information")
1123 (tramp-adb-send-command
1124 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1126 (tramp-get-connection-property vec
"getprop" nil
))
1128 (tramp-set-connection-property
1130 (with-current-buffer (tramp-get-connection-buffer vec
)
1131 ;; Read the expression.
1132 (goto-char (point-min))
1133 (read (current-buffer))))))
1134 (when (and (stringp old-getprop
)
1135 (not (string-equal old-getprop new-getprop
)))
1139 "Connection reset, because remote host changed from `%s' to `%s'"
1140 old-getprop new-getprop
)
1141 (tramp-adb-maybe-open-connection vec
)))
1143 ;; Change user if indicated.
1145 (tramp-adb-send-command vec
(format "su %s" user
))
1146 (unless (zerop (tramp-adb-command-exit-status vec nil
))
1148 (tramp-set-file-property vec
"" "su-command-p" nil
)
1150 vec
'file-error
"Cannot switch to user `%s'" user
)))
1152 ;; Set "remote-path" connection property. This is needed
1154 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1155 (tramp-set-connection-property
1158 (with-current-buffer (tramp-get-connection-buffer vec
)
1159 ;; Read the expression.
1160 (goto-char (point-min))
1161 (read (current-buffer)))
1162 ":" 'omit-nulls
))))))))
1164 (provide 'tramp-adb
)
1165 ;;; tramp-adb.el ends here