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 (file-notify-add-watch . ignore
)
112 (file-notify-rm-watch . ignore
)
113 (file-notify-supported-p . ignore
)
114 (expand-file-name . tramp-adb-handle-expand-file-name
)
115 (find-backup-file-name . tramp-handle-find-backup-file-name
)
116 (directory-files . tramp-handle-directory-files
)
117 (directory-files-and-attributes
118 . tramp-adb-handle-directory-files-and-attributes
)
119 (make-directory . tramp-adb-handle-make-directory
)
120 (delete-directory . tramp-adb-handle-delete-directory
)
121 (delete-file . tramp-adb-handle-delete-file
)
122 (load . tramp-handle-load
)
123 (insert-directory . tramp-adb-handle-insert-directory
)
124 (insert-file-contents . tramp-handle-insert-file-contents
)
125 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
126 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
127 (vc-registered . ignore
) ;no vc control files on Android devices
128 (write-region . tramp-adb-handle-write-region
)
129 (set-file-modes . tramp-adb-handle-set-file-modes
)
130 (set-file-times . tramp-adb-handle-set-file-times
)
131 (copy-file . tramp-adb-handle-copy-file
)
132 (rename-file . tramp-adb-handle-rename-file
)
133 (process-file . tramp-adb-handle-process-file
)
134 (shell-command . tramp-adb-handle-shell-command
)
135 (start-file-process . tramp-adb-handle-start-file-process
))
136 "Alist of handler functions for Tramp ADB method.")
138 ;; It must be a `defsubst' in order to push the whole code into
139 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
141 (defsubst tramp-adb-file-name-p
(filename)
142 "Check if it's a filename for ADB."
143 (let ((v (tramp-dissect-file-name filename
)))
144 (string= (tramp-file-name-method v
) tramp-adb-method
)))
147 (defun tramp-adb-file-name-handler (operation &rest args
)
148 "Invoke the ADB handler for OPERATION.
149 First arg specifies the OPERATION, second arg is a list of arguments to
150 pass to the OPERATION."
151 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
)))
153 (save-match-data (apply (cdr fn
) args
))
154 (tramp-run-real-handler operation args
))))
157 (defun tramp-adb-parse-device-names (ignore)
158 "Return a list of (nil host) tuples allowed to access."
161 ;; `call-process' does not react on timer under MS Windows.
162 ;; That's why we use `start-process'.
163 (let ((p (start-process
164 tramp-adb-program
(current-buffer) tramp-adb-program
"devices"))
166 (tramp-compat-set-process-query-on-exit-flag p nil
)
167 (while (eq 'run
(process-status p
))
169 (goto-char (point-min))
170 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
171 (add-to-list 'result
(list nil
(match-string 1))))
174 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
175 "Like `expand-file-name' for Tramp files."
176 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
177 (setq dir
(or dir default-directory
"/"))
178 ;; Unless NAME is absolute, concat DIR and NAME.
179 (unless (file-name-absolute-p name
)
180 (setq name
(concat (file-name-as-directory dir
) name
)))
181 ;; If NAME is not a Tramp file, run the real handler.
182 (if (not (tramp-tramp-file-p name
))
183 (tramp-run-real-handler 'expand-file-name
(list name nil
))
185 (with-parsed-tramp-file-name name nil
186 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
187 (setq localname
(concat "/" localname
)))
188 ;; Do normal `expand-file-name' (this does "/./" and "/../").
189 ;; We bind `directory-sep-char' here for XEmacs on Windows,
190 ;; which would otherwise use backslash. `default-directory' is
191 ;; bound, because on Windows there would be problems with UNC
192 ;; shares or Cygwin mounts.
193 (let ((directory-sep-char ?
/)
194 (default-directory (tramp-compat-temporary-file-directory)))
195 (tramp-make-tramp-file-name
197 (tramp-drop-volume-letter
198 (tramp-run-real-handler
199 'expand-file-name
(list localname
))))))))
201 (defun tramp-adb-handle-file-directory-p (filename)
202 "Like `file-directory-p' for Tramp files."
203 (car (file-attributes (file-truename filename
))))
205 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
206 ;; code could be shared?
207 (defun tramp-adb-handle-file-truename (filename &optional counter prev-dirs
)
208 "Like `file-truename' for Tramp files."
209 (with-parsed-tramp-file-name (expand-file-name filename
) nil
210 (with-tramp-file-property v localname
"file-truename"
211 (let ((result nil
)) ; result steps in reverse order
212 (tramp-message v
4 "Finding true name for `%s'" filename
)
213 (let* ((directory-sep-char ?
/)
214 (steps (tramp-compat-split-string localname
"/"))
215 (localnamedir (tramp-run-real-handler
216 'file-name-as-directory
(list localname
)))
217 (is-dir (string= localname localnamedir
))
220 ;; Don't make the following value larger than
221 ;; necessary. People expect an error message in a
222 ;; timely fashion when something is wrong; otherwise
223 ;; they might think that Emacs is hung. Of course,
224 ;; correctness has to come first.
227 (while (and steps
(< numchase numchase-limit
))
228 (setq thisstep
(pop steps
))
232 (append '("") (reverse result
) (list thisstep
))
235 (nth 0 (file-attributes
236 (tramp-make-tramp-file-name
243 (cond ((string= "." thisstep
)
244 (tramp-message v
5 "Ignoring step `.'"))
245 ((string= ".." thisstep
)
246 (tramp-message v
5 "Processing step `..'")
248 ((stringp symlink-target
)
249 ;; It's a symlink, follow it.
250 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
251 (setq numchase
(1+ numchase
))
252 (when (file-name-absolute-p symlink-target
)
254 ;; If the symlink was absolute, we'll get a string
255 ;; like "/user@host:/some/target"; extract the
256 ;; "/some/target" part from it.
257 (when (tramp-tramp-file-p symlink-target
)
258 (unless (tramp-equal-remote filename symlink-target
)
261 "Symlink target `%s' on wrong host" symlink-target
))
262 (setq symlink-target localname
))
264 (append (tramp-compat-split-string
269 (setq result
(cons thisstep result
)))))
270 (when (>= numchase numchase-limit
)
273 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
274 (setq result
(reverse result
))
275 ;; Combine list to form string.
278 (mapconcat 'identity
(cons "" result
) "/")
280 (when (and is-dir
(or (string= "" result
)
281 (not (string= (substring result -
1) "/"))))
282 (setq result
(concat result
"/"))))
284 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
285 (tramp-make-tramp-file-name method user host result
)))))
287 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
288 "Like `file-attributes' for Tramp files."
289 (unless id-format
(setq id-format
'integer
))
291 (with-parsed-tramp-file-name filename nil
292 (with-tramp-file-property
293 v localname
(format "file-attributes-%s" id-format
)
294 (tramp-adb-barf-unless-okay
295 v
(format "%s -d -l %s"
296 (tramp-adb-get-ls-command v
)
297 (tramp-shell-quote-argument localname
)) "")
298 (with-current-buffer (tramp-get-buffer v
)
299 (tramp-adb-sh-fix-ls-output)
300 (cdar (tramp-do-parse-file-attributes-with-ls v id-format
)))))))
302 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format
)
303 "Parse `file-attributes' for Tramp files using the ls(1) command."
304 (with-current-buffer (tramp-get-buffer vec
)
305 (goto-char (point-min))
306 (let ((file-properties nil
))
307 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t
)
308 (let* ((mod-string (match-string 1))
309 (is-dir (eq ?d
(aref mod-string
0)))
310 (is-symlink (eq ?l
(aref mod-string
0)))
311 (uid (match-string 2))
312 (gid (match-string 3))
313 (size (string-to-number (match-string 4)))
314 (date (match-string 5))
315 (name (match-string 6))
318 (cadr (split-string name
"\\( -> \\|\n\\)")))))
321 (car (split-string name
"\\( -> \\|\n\\)"))
323 (or is-dir symlink-target
)
325 ;; no way to handle numeric ids in Androids ash
326 (if (eq id-format
'integer
) 0 uid
)
327 (if (eq id-format
'integer
) 0 gid
)
329 (date-to-time date
) ; mtime
335 (tramp-get-device vec
))
339 (defun tramp-adb-handle-directory-files-and-attributes
340 (directory &optional full match nosort id-format
)
341 "Like `directory-files-and-attributes' for Tramp files."
342 (when (file-directory-p directory
)
343 (with-parsed-tramp-file-name (expand-file-name directory
) nil
344 (with-tramp-file-property
345 v localname
(format "directory-files-attributes-%s-%s-%s-%s"
346 full match id-format nosort
)
347 (tramp-adb-barf-unless-okay
348 v
(format "%s -a -l %s"
349 (tramp-adb-get-ls-command v
)
350 (tramp-shell-quote-argument localname
)) "")
351 (with-current-buffer (tramp-get-buffer v
)
352 (tramp-adb-sh-fix-ls-output)
353 (let ((result (tramp-do-parse-file-attributes-with-ls
354 v
(or id-format
'integer
))))
359 (cons (expand-file-name (car x
) directory
) (cdr x
)))
363 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))
366 (if (or (not match
) (string-match match
(car x
)))
370 (defun tramp-adb-get-ls-command (vec)
371 (with-tramp-connection-property vec
"ls"
372 (tramp-message vec
5 "Finding a suitable `ls' command")
373 (if (zerop (tramp-adb-command-exit-status
374 vec
"ls --color=never -al /dev/null"))
375 ;; On CyanogenMod based system BusyBox is used and "ls" output
376 ;; coloring is enabled by default. So we try to disable it
381 (defun tramp-adb--gnu-switches-to-ash
383 "Almquist shell can't handle multiple arguments.
384 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
388 (replace-regexp-in-string
390 (replace-regexp-in-string "^-" "" s
)))
391 ;; FIXME: Warning about removed switches (long and non-dash).
395 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
398 (defun tramp-adb-handle-insert-directory
399 (filename switches
&optional wildcard full-directory-p
)
400 "Like `insert-directory' for Tramp files."
401 (when (stringp switches
)
402 (setq switches
(tramp-adb--gnu-switches-to-ash (split-string switches
))))
403 (with-parsed-tramp-file-name (file-truename filename
) nil
404 (with-current-buffer (tramp-get-buffer v
)
405 (let ((name (tramp-shell-quote-argument (directory-file-name localname
)))
406 (switch-d (member "-d" switches
))
407 (switch-t (member "-t" switches
))
408 (switches (mapconcat 'identity
(remove "-t" switches
) " ")))
409 (tramp-adb-barf-unless-okay
410 v
(format "%s %s %s" (tramp-adb-get-ls-command v
) switches name
)
411 "Cannot insert directory listing: %s" filename
)
413 ;; We insert also filename/. and filename/.., because "ls" doesn't.
414 (narrow-to-region (point) (point))
416 (tramp-adb-barf-unless-okay
417 v
(format "%s -d %s %s %s"
418 (tramp-adb-get-ls-command v
)
420 (concat (file-name-as-directory name
) ".")
421 (concat (file-name-as-directory name
) ".."))
422 "Cannot insert directory listing: %s" filename
))
424 (tramp-adb-sh-fix-ls-output switch-t
)))
425 (insert-buffer-substring (tramp-get-buffer v
))))
427 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
428 "Insert dummy 0 in empty size columns.
429 Androids \"ls\" command doesn't insert size column for directories:
430 Emacs dired can't find files."
432 ;; Insert missing size.
433 (goto-char (point-min))
435 (search-forward-regexp
436 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
437 (replace-match "0\\1" "\\1" nil
)
438 ;; Insert missing "/".
439 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
443 (let* ((lines (split-string (buffer-string) "\n" t
))
448 'tramp-adb-ls-output-time-less-p
449 'tramp-adb-ls-output-name-less-p
))))
450 (delete-region (point-min) (point-max))
451 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
452 ;; Add final newline.
453 (goto-char (point-max))
454 (unless (= (point) (line-beginning-position))
458 (defun tramp-adb-ls-output-time-less-p (a b
)
459 "Sort \"ls\" output by time, descending."
461 (string-match tramp-adb-ls-date-regexp a
)
462 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
463 (string-match tramp-adb-ls-date-regexp b
)
464 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
465 (tramp-time-less-p time-b time-a
)))
467 (defun tramp-adb-ls-output-name-less-p (a b
)
468 "Sort \"ls\" output by name, ascending."
470 (string-match dired-move-to-filename-regexp a
)
471 (setq posa
(match-end 0))
472 (string-match dired-move-to-filename-regexp b
)
473 (setq posb
(match-end 0))
474 (string-lessp (substring a posa
) (substring b posb
))))
476 (defun tramp-adb-handle-make-directory (dir &optional parents
)
477 "Like `make-directory' for Tramp files."
478 (setq dir
(expand-file-name dir
))
479 (with-parsed-tramp-file-name dir nil
481 (let ((par (expand-file-name ".." dir
)))
482 (unless (file-directory-p par
)
483 (make-directory par parents
))))
484 (tramp-adb-barf-unless-okay
485 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
486 "Couldn't make directory %s" dir
)
487 (tramp-flush-directory-property v
(file-name-directory localname
))))
489 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
490 "Like `delete-directory' for Tramp files."
491 (setq directory
(expand-file-name directory
))
492 (with-parsed-tramp-file-name directory nil
493 (tramp-flush-file-property v
(file-name-directory localname
))
494 (tramp-flush-directory-property v localname
)
495 (tramp-adb-barf-unless-okay
497 (if recursive
"rm -r" "rmdir")
498 (tramp-shell-quote-argument localname
))
499 "Couldn't delete %s" directory
)))
501 (defun tramp-adb-handle-delete-file (filename &optional trash
)
502 "Like `delete-file' for Tramp files."
503 (setq filename
(expand-file-name filename
))
504 (with-parsed-tramp-file-name filename nil
505 (tramp-flush-file-property v
(file-name-directory localname
))
506 (tramp-flush-file-property v localname
)
507 (tramp-adb-barf-unless-okay
508 v
(format "rm %s" (tramp-shell-quote-argument localname
))
509 "Couldn't delete %s" filename
)))
511 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
512 "Like `file-name-all-completions' for Tramp files."
515 (with-parsed-tramp-file-name directory nil
516 (with-tramp-file-property v localname
"file-name-all-completions"
518 (tramp-adb-send-command
520 (tramp-adb-get-ls-command v
)
521 (tramp-shell-quote-argument localname
)))
524 (if (file-directory-p f
)
525 (file-name-as-directory f
)
527 (with-current-buffer (tramp-get-buffer v
)
531 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
532 (split-string (buffer-string) "\n"))))))))))
534 (defun tramp-adb-handle-file-local-copy (filename)
535 "Like `file-local-copy' for Tramp files."
536 (with-parsed-tramp-file-name filename nil
537 (unless (file-exists-p (file-truename filename
))
540 "Cannot make local copy of non-existing file `%s'" filename
))
541 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
542 (with-tramp-progress-reporter
543 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
544 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
545 (delete-file tmpfile
)
547 v
'file-error
"Cannot make local copy of file `%s'" filename
))
548 (set-file-modes tmpfile
(file-modes filename
)))
551 (defun tramp-adb-handle-file-writable-p (filename)
552 "Like `tramp-sh-handle-file-writable-p'.
553 But handle the case, if the \"test\" command is not available."
554 (with-parsed-tramp-file-name filename nil
555 (with-tramp-file-property v localname
"file-writable-p"
556 (if (tramp-adb-find-test-command v
)
557 (if (file-exists-p filename
)
559 (tramp-adb-command-exit-status
560 v
(format "test -w %s" (tramp-shell-quote-argument localname
))))
562 (file-directory-p (file-name-directory filename
))
563 (file-writable-p (file-name-directory filename
))))
565 ;; Missing "test" command on Android < 4.
566 (let ((rw-path "/data/data"))
569 "Not implemented yet (assuming \"/data/data\" is writable): %s"
571 (and (>= (length localname
) (length rw-path
))
572 (string= (substring localname
0 (length rw-path
))
575 (defun tramp-adb-handle-write-region
576 (start end filename
&optional append visit lockname confirm
)
577 "Like `write-region' for Tramp files."
578 (setq filename
(expand-file-name filename
))
579 (with-parsed-tramp-file-name filename nil
582 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
583 (when (and confirm
(file-exists-p filename
))
584 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
586 (tramp-error v
'file-error
"File not overwritten")))
587 ;; We must also flush the cache of the directory, because
588 ;; `file-attributes' reads the values from there.
589 (tramp-flush-file-property v
(file-name-directory localname
))
590 (tramp-flush-file-property v localname
)
591 (let* ((curbuf (current-buffer))
592 (tmpfile (tramp-compat-make-temp-file filename
)))
593 (tramp-run-real-handler
595 (list start end tmpfile append
'no-message lockname confirm
))
596 (with-tramp-progress-reporter
597 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
599 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
600 (tramp-error v
'file-error
"Cannot write: `%s' filename"))
601 (delete-file tmpfile
)))
603 (unless (equal curbuf
(current-buffer))
606 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
608 (defun tramp-adb-handle-set-file-modes (filename mode
)
609 "Like `set-file-modes' for Tramp files."
610 (with-parsed-tramp-file-name filename nil
611 (tramp-flush-file-property v localname
)
612 (tramp-adb-barf-unless-okay
613 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
)
614 "Error while changing file's mode %s" filename
)))
616 (defun tramp-adb-handle-set-file-times (filename &optional time
)
617 "Like `set-file-times' for Tramp files."
618 (with-parsed-tramp-file-name filename nil
619 (tramp-flush-file-property v localname
)
620 (let ((time (if (or (null time
) (equal time
'(0 0)))
623 (tramp-adb-command-exit-status
624 ;; use shell arithmetic because of Emacs integer size limit
625 v
(format "touch -t $(( %d * 65536 + %d )) %s"
626 (car time
) (cadr time
)
627 (tramp-shell-quote-argument localname
))))))
629 (defun tramp-adb-handle-copy-file
630 (filename newname
&optional ok-if-already-exists keep-date
631 preserve-uid-gid preserve-extended-attributes
)
632 "Like `copy-file' for Tramp files.
633 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
634 (setq filename
(expand-file-name filename
)
635 newname
(expand-file-name newname
))
637 (if (file-directory-p filename
)
638 (tramp-file-name-handler 'copy-directory filename newname keep-date t
)
639 (with-tramp-progress-reporter
640 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
641 0 (format "Copying %s to %s" filename newname
)
643 (let ((tmpfile (file-local-copy filename
)))
648 (rename-file tmpfile newname ok-if-already-exists
)
650 (delete-file tmpfile
)
651 (signal (car err
) (cdr err
))))
654 (when (file-directory-p newname
)
656 (expand-file-name (file-name-nondirectory filename
) newname
)))
658 (with-parsed-tramp-file-name newname nil
659 (when (and (not ok-if-already-exists
)
660 (file-exists-p newname
))
661 (tramp-error v
'file-already-exists newname
))
663 ;; We must also flush the cache of the directory, because
664 ;; `file-attributes' reads the values from there.
665 (tramp-flush-file-property v
(file-name-directory localname
))
666 (tramp-flush-file-property v localname
)
667 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
669 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
671 ;; KEEP-DATE handling.
673 (set-file-times newname
(nth 5 (file-attributes filename
))))))
675 (defun tramp-adb-handle-rename-file
676 (filename newname
&optional ok-if-already-exists
)
677 "Like `rename-file' for Tramp files."
678 (setq filename
(expand-file-name filename
)
679 newname
(expand-file-name newname
))
681 (with-parsed-tramp-file-name
682 (if (file-remote-p filename
) filename newname
) nil
683 (with-tramp-progress-reporter
684 v
0 (format "Renaming %s to %s" newname filename
)
686 (if (and (tramp-equal-remote filename newname
)
687 (not (file-directory-p filename
)))
689 (when (and (not ok-if-already-exists
)
690 (file-exists-p newname
))
691 (tramp-error v
'file-already-exists newname
))
692 ;; We must also flush the cache of the directory, because
693 ;; `file-attributes' reads the values from there.
694 (tramp-flush-file-property v
(file-name-directory localname
))
695 (tramp-flush-file-property v localname
)
697 (tramp-adb-barf-unless-okay
700 (tramp-file-name-handler 'file-remote-p filename
'localname
)
702 "Error renaming %s to %s" filename newname
))
705 (copy-file filename newname ok-if-already-exists t t
)
706 (delete-file filename
)))))
708 (defun tramp-adb-handle-process-file
709 (program &optional infile destination display
&rest args
)
710 "Like `process-file' for Tramp files."
711 ;; The implementation is not complete yet.
712 (when (and (numberp destination
) (zerop destination
))
713 (error "Implementation does not handle immediate return"))
715 (with-parsed-tramp-file-name default-directory nil
716 (let (command input tmpinput stderr tmpstderr outbuf ret
)
718 (setq command
(mapconcat 'tramp-shell-quote-argument
719 (cons program args
) " "))
722 (setq input
"/dev/null")
723 (setq infile
(expand-file-name infile
))
724 (if (tramp-equal-remote default-directory infile
)
725 ;; INFILE is on the same remote host.
726 (setq input
(with-parsed-tramp-file-name infile nil localname
))
727 ;; INFILE must be copied to remote host.
728 (setq input
(tramp-make-tramp-temp-file v
)
729 tmpinput
(tramp-make-tramp-file-name method user host input
))
730 (copy-file infile tmpinput t
)))
731 (when input
(setq command
(format "%s <%s" command input
)))
736 ((bufferp destination
)
737 (setq outbuf destination
))
739 ((stringp destination
)
740 (setq outbuf
(get-buffer-create destination
)))
741 ;; (REAL-DESTINATION ERROR-DESTINATION)
745 ((bufferp (car destination
))
746 (setq outbuf
(car destination
)))
747 ((stringp (car destination
))
748 (setq outbuf
(get-buffer-create (car destination
))))
750 (setq outbuf
(current-buffer))))
753 ((stringp (cadr destination
))
754 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
755 (if (tramp-equal-remote default-directory
(cadr destination
))
756 ;; stderr is on the same remote host.
757 (setq stderr
(with-parsed-tramp-file-name
758 (cadr destination
) nil localname
))
759 ;; stderr must be copied to remote host. The temporary
760 ;; file must be deleted after execution.
761 (setq stderr
(tramp-make-tramp-temp-file v
)
762 tmpstderr
(tramp-make-tramp-file-name
763 method user host stderr
))))
764 ;; stderr to be discarded.
765 ((null (cadr destination
))
766 (setq stderr
"/dev/null"))))
769 (setq outbuf
(current-buffer))))
770 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
772 ;; Send the command. It might not return in time, so we protect
773 ;; it. Call it in a subshell, in order to preserve working
778 (tramp-adb-barf-unless-okay
779 v
(format "(cd %s; %s)"
780 (tramp-shell-quote-argument localname
) command
)
782 ;; We should show the output anyway.
784 (with-current-buffer outbuf
785 (insert-buffer-substring (tramp-get-connection-buffer v
)))
786 (when display
(display-buffer outbuf
))))
787 ;; When the user did interrupt, we should do it also. We use
788 ;; return code -1 as marker.
790 (kill-buffer (tramp-get-connection-buffer v
))
794 (kill-buffer (tramp-get-connection-buffer v
))
797 ;; Provide error file.
798 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
800 ;; Cleanup. We remove all file cache values for the connection,
801 ;; because the remote process could have changed them.
802 (when tmpinput
(delete-file tmpinput
))
804 ;; `process-file-side-effects' has been introduced with GNU
805 ;; Emacs 23.2. If set to `nil', no remote file will be changed
806 ;; by `program'. If it doesn't exist, we assume its default
808 (unless (and (boundp 'process-file-side-effects
)
809 (not (symbol-value 'process-file-side-effects
)))
810 (tramp-flush-directory-property v
""))
812 ;; Return exit status.
817 (defun tramp-adb-handle-shell-command
818 (command &optional output-buffer error-buffer
)
819 "Like `shell-command' for Tramp files."
820 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
821 ;; We cannot use `shell-file-name' and `shell-command-switch',
822 ;; they are variables of the local host.
823 (args (list "sh" "-c" (substring command
0 asynchronous
)))
827 ((bufferp output-buffer
) output-buffer
)
828 ((stringp output-buffer
) (get-buffer-create output-buffer
))
830 (setq current-buffer-p t
)
832 (t (get-buffer-create
834 "*Async Shell Command*"
835 "*Shell Command Output*")))))
838 ((bufferp error-buffer
) error-buffer
)
839 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
841 (if (and (not asynchronous
) error-buffer
)
842 (with-parsed-tramp-file-name default-directory nil
843 (list output-buffer
(tramp-make-tramp-temp-file v
)))
845 (p (get-buffer-process output-buffer
)))
847 ;; Check whether there is another process running. Tramp does not
848 ;; support 2 (asynchronous) processes in parallel.
850 (if (yes-or-no-p "A command is running. Kill it? ")
851 (ignore-errors (kill-process p
))
852 (tramp-compat-user-error "Shell command in progress")))
856 (barf-if-buffer-read-only)
858 (with-current-buffer output-buffer
859 (setq buffer-read-only nil
)
862 (if (and (not current-buffer-p
) (integerp asynchronous
))
865 (apply 'start-file-process
"*Async Shell*" buffer args
)
867 (pop-to-buffer output-buffer
)
868 (setq mode-line-process
'(":%s"))
873 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
874 ;; Insert error messages if they were separated.
876 (with-current-buffer error-buffer
877 (insert-file-contents (cadr buffer
)))
878 (delete-file (cadr buffer
)))
880 ;; This is like exchange-point-and-mark, but doesn't
881 ;; activate the mark. It is cleaner to avoid activation,
882 ;; even though the command loop would deactivate the mark
883 ;; because we inserted text.
884 (goto-char (prog1 (mark t
)
885 (set-marker (mark-marker) (point)
887 ;; There's some output, display it.
888 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
889 (if (functionp 'display-message-or-buffer
)
890 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
891 (pop-to-buffer output-buffer
))))))))
893 ;; We use BUFFER also as connection buffer during setup. Because of
894 ;; this, its original contents must be saved, and restored once
895 ;; connection has been setup.
896 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
897 "Like `start-file-process' for Tramp files."
898 (with-parsed-tramp-file-name default-directory nil
899 ;; When PROGRAM is nil, we should provide a tty. This is not
901 (unless (stringp program
)
902 (tramp-error v
'file-error
"PROGRAM must be a string"))
906 (tramp-shell-quote-argument localname
)
907 (mapconcat 'tramp-shell-quote-argument
908 (cons program args
) " ")))
909 (tramp-process-connection-type
910 (or (null program
) tramp-process-connection-type
))
911 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
916 ;; BUFFER can be nil. We use a temporary buffer.
917 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
918 (while (get-process name1
)
919 ;; NAME must be unique as process name.
921 name1
(format "%s<%d>" name i
)))
923 ;; Set the new process properties.
924 (tramp-set-connection-property v
"process-name" name
)
925 (tramp-set-connection-property v
"process-buffer" buffer
)
927 (with-current-buffer (tramp-get-connection-buffer v
)
929 ;; We catch this event. Otherwise, `start-process' could
930 ;; be called on the local host.
933 ;; Activate narrowing in order to save BUFFER
934 ;; contents. Clear also the modification time;
935 ;; otherwise we might be interrupted by
936 ;; `verify-visited-file-modtime'.
937 (let ((buffer-undo-list t
)
938 (buffer-read-only nil
)
940 (clear-visited-file-modtime)
941 (narrow-to-region (point-max) (point-max))
942 ;; We call `tramp-adb-maybe-open-connection', in
943 ;; order to cleanup the prompt afterwards.
944 (tramp-adb-maybe-open-connection v
)
946 (delete-region mark
(point))
947 (narrow-to-region (point-max) (point-max))
949 (let ((tramp-adb-prompt (regexp-quote command
)))
950 (tramp-adb-send-command v command
))
951 (let ((p (tramp-get-connection-process v
)))
952 ;; Set query flag and process marker for this
953 ;; process. We ignore errors, because the process
954 ;; could have finished already.
956 (tramp-compat-set-process-query-on-exit-flag p t
)
957 (set-marker (process-mark p
) (point)))
962 (if (string-match tramp-temp-buffer-name
(buffer-name))
964 (set-process-buffer (tramp-get-connection-process v
) nil
)
965 (kill-buffer (current-buffer)))
966 (set-buffer-modified-p bmp
))
967 (tramp-set-connection-property v
"process-name" nil
)
968 (tramp-set-connection-property v
"process-buffer" nil
))))))
972 (defun tramp-adb-execute-adb-command (vec &rest args
)
973 "Returns nil on success error-output on failure."
974 (when (> (length (tramp-file-name-host vec
)) 0)
975 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
979 (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args
))
981 (tramp-message vec
6 "%s" (buffer-string)))))
983 (defun tramp-adb-find-test-command (vec)
984 "Checks, whether the ash has a builtin \"test\" command.
985 This happens for Android >= 4.0."
986 (with-tramp-connection-property vec
"test"
987 (zerop (tramp-adb-command-exit-status vec
"type test"))))
989 ;; Connection functions
991 (defun tramp-adb-send-command (vec command
)
992 "Send the COMMAND to connection VEC."
993 (tramp-adb-maybe-open-connection vec
)
994 (tramp-message vec
6 "%s" command
)
995 (tramp-send-string vec command
)
996 ;; fixme: Race condition
997 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
998 (with-current-buffer (tramp-get-connection-buffer vec
)
1000 (goto-char (point-min))
1001 ;; We can't use stty to disable echo of command.
1002 (delete-matching-lines (regexp-quote command
))
1003 ;; When the local machine is W32, there are still trailing ^M.
1004 ;; There must be a better solution by setting the correct coding
1005 ;; system, but this requires changes in core Tramp.
1006 (goto-char (point-min))
1007 (while (re-search-forward "\r+$" nil t
)
1008 (replace-match "" nil nil
)))))
1010 (defun tramp-adb-command-exit-status
1012 "Run COMMAND and return its exit status.
1013 Sends `echo $?' along with the COMMAND for checking the exit status. If
1014 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
1015 (tramp-adb-send-command
1017 (format "%s; echo tramp_exit_status $?" command
)
1018 "echo tramp_exit_status $?"))
1019 (with-current-buffer (tramp-get-connection-buffer vec
)
1020 (goto-char (point-max))
1021 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1023 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1024 (skip-chars-forward "^ ")
1026 (read (current-buffer))
1027 (let (buffer-read-only)
1028 (delete-region (match-beginning 0) (point-max))))))
1030 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
1031 "Run COMMAND, check exit status, throw error if exit status not okay.
1032 FMT and ARGS are passed to `error'."
1033 (unless (zerop (tramp-adb-command-exit-status vec command
))
1034 (apply 'tramp-error vec
'file-error fmt args
)))
1036 (defun tramp-adb-wait-for-output (proc &optional timeout
)
1037 "Wait for output from remote command."
1038 (unless (buffer-live-p (process-buffer proc
))
1039 (delete-process proc
)
1040 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
1041 (with-current-buffer (process-buffer proc
)
1042 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
1043 (let (buffer-read-only)
1044 (goto-char (point-min))
1045 ;; ADB terminal sends "^H" sequences.
1046 (when (re-search-forward "<\b+" (point-at-eol) t
)
1048 (delete-region (point-min) (point)))
1049 ;; Delete the prompt.
1050 (goto-char (point-min))
1051 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
1053 (delete-region (point-min) (point)))
1054 (goto-char (point-max))
1055 (re-search-backward tramp-adb-prompt nil t
)
1056 (delete-region (point) (point-max)))
1060 "[[Remote adb prompt `%s' not found in %d secs]]"
1061 tramp-adb-prompt timeout
)
1064 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
1066 (defun tramp-adb-maybe-open-connection (vec)
1067 "Maybe open a connection VEC.
1068 Does not do anything if a connection is already open, but re-opens the
1069 connection if a previous connection has died for some reason."
1070 (let* ((buf (tramp-get-connection-buffer vec
))
1071 (p (get-buffer-process buf
))
1072 (host (tramp-file-name-host vec
))
1073 (user (tramp-file-name-user vec
))
1074 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1076 ;; Maybe we know already that "su" is not supported. We cannot
1077 ;; use a connection property, because we have not checked yet
1078 ;; whether it is still the same device.
1079 (when (and user
(not (tramp-get-file-property vec
"" "su-command-p" t
)))
1080 (tramp-error vec
'file-error
"Cannot switch to user `%s'" user
))
1083 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1085 (when (and p
(processp p
)) (delete-process p
))
1087 (tramp-error vec
'file-error
"No device connected"))
1088 (if (and (> (length host
) 0) (not (member host devices
)))
1089 (tramp-error vec
'file-error
"Device %s not connected" host
))
1090 (if (and (> (length devices
) 1) (zerop (length host
)))
1093 "Multiple Devices connected: No Host/Device specified"))
1094 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1095 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1096 (process-connection-type tramp-process-connection-type
)
1097 (args (if (> (length host
) 0)
1098 (list "-s" host
"shell")
1100 (p (let ((default-directory
1101 (tramp-compat-temporary-file-directory)))
1102 (apply 'start-process
(tramp-get-connection-name vec
) buf
1103 tramp-adb-program args
))))
1105 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1106 ;; Wait for initial prompt.
1107 (tramp-adb-wait-for-output p
30)
1108 (unless (eq 'run
(process-status p
))
1109 (tramp-error vec
'file-error
"Terminated!"))
1110 (tramp-compat-set-process-query-on-exit-flag p nil
)
1112 ;; Check whether the properties have been changed. If
1113 ;; yes, this is a strong indication that we must expire all
1114 ;; connection properties. We start again.
1115 (tramp-message vec
5 "Checking system information")
1116 (tramp-adb-send-command
1117 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1119 (tramp-get-connection-property vec
"getprop" nil
))
1121 (tramp-set-connection-property
1123 (with-current-buffer (tramp-get-connection-buffer vec
)
1124 ;; Read the expression.
1125 (goto-char (point-min))
1126 (read (current-buffer))))))
1127 (when (and (stringp old-getprop
)
1128 (not (string-equal old-getprop new-getprop
)))
1132 "Connection reset, because remote host changed from `%s' to `%s'"
1133 old-getprop new-getprop
)
1134 (tramp-adb-maybe-open-connection vec
)))
1136 ;; Change user if indicated.
1138 (tramp-adb-send-command vec
(format "su %s" user
))
1139 (unless (zerop (tramp-adb-command-exit-status vec nil
))
1141 (tramp-set-file-property vec
"" "su-command-p" nil
)
1143 vec
'file-error
"Cannot switch to user `%s'" user
)))
1145 ;; Set "remote-path" connection property. This is needed
1147 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1148 (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 ":" 'omit-nulls
))))))))
1157 (provide 'tramp-adb
)
1158 ;;; tramp-adb.el ends here