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 must be installed on your local machine.
27 ;; Add the following form into your .emacs:
29 ;; (setq tramp-adb-sdk-dir "/path/to/android/sdk")
31 ;; Due to security it is not possible to access non-root devices.
37 (defvar dired-move-to-filename-regexp
)
39 (defcustom tramp-adb-sdk-dir
"~/Android/sdk"
40 "Set to the directory containing the Android SDK."
46 (defconst tramp-adb-method
"adb"
47 "*When this method name is used, forward all calls to Android Debug Bridge.")
49 (defcustom tramp-adb-prompt
50 "^\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
51 "Regexp used as prompt in almquist shell."
56 (defconst tramp-adb-ls-date-regexp
57 "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]")
60 (add-to-list 'tramp-methods
`(,tramp-adb-method
))
63 (eval-after-load 'tramp
64 '(tramp-set-completion-function
65 tramp-adb-method
'((tramp-adb-parse-device-names ""))))
68 (add-to-list 'tramp-foreign-file-name-handler-alist
69 (cons 'tramp-adb-file-name-p
'tramp-adb-file-name-handler
))
71 (defconst tramp-adb-file-name-handler-alist
72 '((directory-file-name . tramp-handle-directory-file-name
)
73 (dired-uncache . tramp-handle-dired-uncache
)
74 (file-name-as-directory . tramp-handle-file-name-as-directory
)
75 (file-name-completion . tramp-handle-file-name-completion
)
76 (file-name-all-completions . tramp-adb-handle-file-name-all-completions
)
77 (file-attributes . tramp-adb-handle-file-attributes
)
78 (file-name-directory . tramp-handle-file-name-directory
)
79 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
80 (file-truename . tramp-adb-handle-file-truename
)
81 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
82 (file-name-as-directory . tramp-handle-file-name-as-directory
)
83 (file-regular-p . tramp-handle-file-regular-p
)
84 (file-remote-p . tramp-handle-file-remote-p
)
85 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
86 (file-directory-p . tramp-adb-handle-file-directory-p
)
87 (file-symlink-p . tramp-handle-file-symlink-p
)
88 ;; FIXME: This is too sloppy.
89 (file-executable-p . file-exists-p
)
90 (file-exists-p . tramp-adb-handle-file-exists-p
)
91 (file-readable-p . tramp-handle-file-exists-p
)
92 (file-writable-p . tramp-adb-handle-file-writable-p
)
93 (file-local-copy . tramp-adb-handle-file-local-copy
)
94 (file-modes . tramp-handle-file-modes
)
95 (expand-file-name . tramp-adb-handle-expand-file-name
)
96 (find-backup-file-name . tramp-handle-find-backup-file-name
)
97 (directory-files . tramp-handle-directory-files
)
98 (make-directory . tramp-adb-handle-make-directory
)
99 (delete-directory . tramp-adb-handle-delete-directory
)
100 (delete-file . tramp-adb-handle-delete-file
)
101 (load . tramp-handle-load
)
102 (insert-directory . tramp-adb-handle-insert-directory
)
103 (insert-file-contents . tramp-handle-insert-file-contents
)
104 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
105 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
106 (vc-registered . ignore
) ;no vc control files on Android devices
107 (write-region . tramp-adb-handle-write-region
)
108 (set-file-modes . tramp-adb-handle-set-file-modes
)
109 (set-file-times . ignore
)
110 (copy-file . tramp-adb-handle-copy-file
)
111 (rename-file . tramp-adb-handle-rename-file
)
112 (process-file . tramp-adb-handle-process-file
)
113 (shell-command . tramp-adb-handle-shell-command
)
114 (start-file-process . tramp-adb-handle-start-file-process
))
115 "Alist of handler functions for Tramp ADB method.")
117 ;; It must be a `defsubst' in order to push the whole code into
118 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
120 (defsubst tramp-adb-file-name-p
(filename)
121 "Check if it's a filename for ADB."
122 (let ((v (tramp-dissect-file-name filename
)))
123 (string= (tramp-file-name-method v
) tramp-adb-method
)))
126 (defun tramp-adb-file-name-handler (operation &rest args
)
127 "Invoke the ADB handler for OPERATION.
128 First arg specifies the OPERATION, second arg is a list of arguments to
129 pass to the OPERATION."
130 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
))
131 ;; `tramp-default-host's default value is (system-name). Not
134 (unless (equal (eval (car (get 'tramp-default-host
'standard-value
)))
136 tramp-default-host
)))
138 (save-match-data (apply (cdr fn
) args
))
139 (tramp-run-real-handler operation args
))))
141 ;; This cannot be a constant, because `tramp-adb-sdk-dir' is customizable.
142 (defun tramp-adb-program ()
143 "The Android Debug Bridge."
144 (expand-file-name "platform-tools/adb" tramp-adb-sdk-dir
))
147 (defun tramp-adb-parse-device-names (ignore)
148 "Return a list of (nil host) tuples allowed to access."
150 (when (zerop (call-process (tramp-adb-program) nil t nil
"devices"))
152 (goto-char (point-min))
153 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
154 (add-to-list 'result
(list nil
(match-string 1))))
157 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
158 "Like `expand-file-name' for Tramp files."
159 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
160 (setq dir
(or dir default-directory
"/"))
161 ;; Unless NAME is absolute, concat DIR and NAME.
162 (unless (file-name-absolute-p name
)
163 (setq name
(concat (file-name-as-directory dir
) name
)))
164 ;; If NAME is not a Tramp file, run the real handler.
165 (if (not (tramp-tramp-file-p name
))
166 (tramp-run-real-handler 'expand-file-name
(list name nil
))
168 (with-parsed-tramp-file-name name nil
169 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
170 (setq localname
(concat "/" localname
)))
171 ;; Do normal `expand-file-name' (this does "/./" and "/../").
172 ;; We bind `directory-sep-char' here for XEmacs on Windows,
173 ;; which would otherwise use backslash. `default-directory' is
174 ;; bound, because on Windows there would be problems with UNC
175 ;; shares or Cygwin mounts.
176 (let ((directory-sep-char ?
/)
177 (default-directory (tramp-compat-temporary-file-directory)))
178 (tramp-make-tramp-file-name
180 (tramp-drop-volume-letter
181 (tramp-run-real-handler
182 'expand-file-name
(list localname
))))))))
184 (defun tramp-adb-handle-file-directory-p (filename)
185 "Like `file-directory-p' for Tramp files."
186 (car (file-attributes (file-truename filename
))))
188 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
189 ;; code could be shared?
190 (defun tramp-adb-handle-file-truename (filename &optional counter prev-dirs
)
191 "Like `file-truename' for Tramp files."
192 (with-parsed-tramp-file-name (expand-file-name filename
) nil
193 (with-tramp-file-property v localname
"file-truename"
194 (let ((result nil
)) ; result steps in reverse order
195 (tramp-message v
4 "Finding true name for `%s'" filename
)
196 (let* ((directory-sep-char ?
/)
197 (steps (tramp-compat-split-string localname
"/"))
198 (localnamedir (tramp-run-real-handler
199 'file-name-as-directory
(list localname
)))
200 (is-dir (string= localname localnamedir
))
203 ;; Don't make the following value larger than
204 ;; necessary. People expect an error message in a
205 ;; timely fashion when something is wrong; otherwise
206 ;; they might think that Emacs is hung. Of course,
207 ;; correctness has to come first.
210 (while (and steps
(< numchase numchase-limit
))
211 (setq thisstep
(pop steps
))
215 (append '("") (reverse result
) (list thisstep
))
218 (nth 0 (file-attributes
219 (tramp-make-tramp-file-name
226 (cond ((string= "." thisstep
)
227 (tramp-message v
5 "Ignoring step `.'"))
228 ((string= ".." thisstep
)
229 (tramp-message v
5 "Processing step `..'")
231 ((stringp symlink-target
)
232 ;; It's a symlink, follow it.
233 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
234 (setq numchase
(1+ numchase
))
235 (when (file-name-absolute-p symlink-target
)
237 ;; If the symlink was absolute, we'll get a string
238 ;; like "/user@host:/some/target"; extract the
239 ;; "/some/target" part from it.
240 (when (tramp-tramp-file-p symlink-target
)
241 (unless (tramp-equal-remote filename symlink-target
)
244 "Symlink target `%s' on wrong host" symlink-target
))
245 (setq symlink-target localname
))
247 (append (tramp-compat-split-string
252 (setq result
(cons thisstep result
)))))
253 (when (>= numchase numchase-limit
)
256 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
257 (setq result
(reverse result
))
258 ;; Combine list to form string.
261 (mapconcat 'identity
(cons "" result
) "/")
263 (when (and is-dir
(or (string= "" result
)
264 (not (string= (substring result -
1) "/"))))
265 (setq result
(concat result
"/"))))
267 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
268 (tramp-make-tramp-file-name method user host result
)))))
270 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
271 "Like `file-attributes' for Tramp files."
272 (unless id-format
(setq id-format
'integer
))
274 (with-parsed-tramp-file-name filename nil
275 (with-tramp-file-property
276 v localname
(format "file-attributes-%s" id-format
)
277 (tramp-adb-barf-unless-okay
278 v
(format "%s -d -l %s"
279 (tramp-adb-get-ls-command v
)
280 (tramp-shell-quote-argument localname
)) "")
281 (with-current-buffer (tramp-get-buffer v
)
282 (tramp-adb-sh-fix-ls-output)
283 (let* ((columns (split-string (buffer-string)))
284 (mod-string (nth 0 columns
))
285 (is-dir (eq ?d
(aref mod-string
0)))
286 (is-symlink (eq ?l
(aref mod-string
0)))
289 (cadr (split-string (buffer-string) "\\( -> \\|\n\\)"))))
290 (uid (nth 1 columns
))
291 (gid (nth 2 columns
))
292 (date (format "%s %s" (nth 4 columns
) (nth 5 columns
)))
293 (size (string-to-number (nth 3 columns
))))
295 (or is-dir symlink-target
)
297 ;; no way to handle numeric ids in Androids ash
298 (if (eq id-format
'integer
) 0 uid
)
299 (if (eq id-format
'integer
) 0 gid
)
301 (date-to-time date
) ; mtime
308 (defun tramp-adb-get-ls-command (vec)
309 (with-tramp-connection-property vec
"ls"
310 (tramp-message vec
5 "Finding a suitable `ls' command")
311 (if (zerop (tramp-adb-command-exit-status
312 vec
"ls --color=never -al /dev/null"))
313 ;; On CyanogenMod based system BusyBox is used and "ls" output
314 ;; coloring is enabled by default. So we try to disable it
319 (defun tramp-adb-get-toolbox (vec)
320 "Get shell toolbox implementation: `toolbox' for orginal distributions
321 or `busybox' for CynagenMode based distributions"
322 (with-tramp-connection-property vec
"toolbox"
323 (tramp-message vec
5 "Checking shell toolbox implementation")
325 ((zerop (tramp-adb-command-exit-status vec
"busybox")) 'busybox
)
326 ((zerop (tramp-adb-command-exit-status vec
"toolbox")) 'toolbox
)
329 (defun tramp-adb--gnu-switches-to-ash
331 "Almquist shell can't handle multiple arguments.
332 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
336 (replace-regexp-in-string
338 (replace-regexp-in-string "^-" "" s
)))
339 ;; FIXME: Warning about removed switches (long and non-dash).
343 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
346 (defun tramp-adb-handle-insert-directory
347 (filename switches
&optional wildcard full-directory-p
)
348 "Like `insert-directory' for Tramp files."
349 (when (stringp switches
)
350 (setq switches
(tramp-adb--gnu-switches-to-ash (split-string switches
))))
351 (with-parsed-tramp-file-name (file-truename filename
) nil
352 (with-current-buffer (tramp-get-buffer v
)
353 (let ((name (tramp-shell-quote-argument (directory-file-name localname
)))
354 (switch-d (member "-d" switches
))
355 (switch-t (member "-t" switches
))
356 (switches (mapconcat 'identity
(remove "-t" switches
) " ")))
357 (tramp-adb-barf-unless-okay
358 v
(format "%s %s %s" (tramp-adb-get-ls-command v
) switches name
)
359 "Cannot insert directory listing: %s" filename
)
361 ;; We insert also filename/. and filename/.., because "ls" doesn't.
362 (narrow-to-region (point) (point))
364 (tramp-adb-barf-unless-okay
365 v
(format "%s -d %s %s %s"
366 (tramp-adb-get-ls-command v
)
368 (concat (file-name-as-directory name
) ".")
369 (concat (file-name-as-directory name
) ".."))
370 "Cannot insert directory listing: %s" filename
))
372 (tramp-adb-sh-fix-ls-output switch-t
)))
373 (insert-buffer-substring (tramp-get-buffer v
))))
375 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
376 "Insert dummy 0 in empty size columns.
377 Androids \"ls\" command doesn't insert size column for directories:
378 Emacs dired can't find files."
380 ;; Insert missing size.
381 (goto-char (point-min))
383 (search-forward-regexp
384 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
385 (replace-match "0\\1" "\\1" nil
)
386 ;; Insert missing "/".
387 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
391 (let* ((lines (split-string (buffer-string) "\n" t
))
396 'tramp-adb-ls-output-time-less-p
397 'tramp-adb-ls-output-name-less-p
))))
398 (delete-region (point-min) (point-max))
399 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
400 ;; Add final newline.
401 (goto-char (point-max))
402 (unless (= (point) (line-beginning-position))
406 (defun tramp-adb-ls-output-time-less-p (a b
)
407 "Sort \"ls\" output by time, descending."
409 (string-match tramp-adb-ls-date-regexp a
)
410 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
411 (string-match tramp-adb-ls-date-regexp b
)
412 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
413 (time-less-p time-b time-a
)))
415 (defun tramp-adb-ls-output-name-less-p (a b
)
416 "Sort \"ls\" output by name, ascending."
418 (string-match dired-move-to-filename-regexp a
)
419 (setq posa
(match-end 0))
420 (string-match dired-move-to-filename-regexp b
)
421 (setq posb
(match-end 0))
422 (string-lessp (substring a posa
) (substring b posb
))))
424 (defun tramp-adb-handle-make-directory (dir &optional parents
)
425 "Like `make-directory' for Tramp files."
426 (setq dir
(expand-file-name dir
))
427 (with-parsed-tramp-file-name dir nil
429 (let ((par (expand-file-name ".." dir
)))
430 (unless (file-directory-p par
)
431 (make-directory par parents
))))
432 (tramp-adb-barf-unless-okay
433 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
434 "Couldn't make directory %s" dir
)
435 (tramp-flush-directory-property v
(file-name-directory localname
))))
437 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
438 "Like `delete-directory' for Tramp files."
439 (setq directory
(expand-file-name directory
))
440 (with-parsed-tramp-file-name directory nil
441 (tramp-flush-file-property v
(file-name-directory localname
))
442 (tramp-flush-directory-property v localname
)
443 (tramp-adb-barf-unless-okay
445 (if recursive
"rm -r" "rmdir")
446 (tramp-shell-quote-argument localname
))
447 "Couldn't delete %s" directory
)))
449 (defun tramp-adb-handle-delete-file (filename &optional trash
)
450 "Like `delete-file' for Tramp files."
451 (setq filename
(expand-file-name filename
))
452 (with-parsed-tramp-file-name filename nil
453 (tramp-flush-file-property v
(file-name-directory localname
))
454 (tramp-flush-file-property v localname
)
455 (tramp-adb-barf-unless-okay
456 v
(format "rm %s" (tramp-shell-quote-argument localname
))
457 "Couldn't delete %s" filename
)))
459 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
460 "Like `file-name-all-completions' for Tramp files."
463 (with-parsed-tramp-file-name directory nil
464 (with-tramp-file-property v localname
"file-name-all-completions"
466 (tramp-adb-send-command
468 (tramp-adb-get-ls-command v
)
469 (tramp-shell-quote-argument localname
)))
472 (if (file-directory-p f
)
473 (file-name-as-directory f
)
475 (with-current-buffer (tramp-get-buffer v
)
479 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
480 (split-string (buffer-string) "\n"))))))))))
482 (defun tramp-adb-handle-file-local-copy (filename)
483 "Like `file-local-copy' for Tramp files."
484 (with-parsed-tramp-file-name filename nil
485 (unless (file-exists-p (file-truename filename
))
488 "Cannot make local copy of non-existing file `%s'" filename
))
489 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
490 (with-tramp-progress-reporter
491 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
492 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
493 (delete-file tmpfile
)
495 v
'file-error
"Cannot make local copy of file `%s'" filename
))
496 (set-file-modes tmpfile
(file-modes filename
)))
499 (defun tramp-adb-handle-file-writable-p (filename)
500 "Like `tramp-sh-handle-file-writable-p'.
501 But handle the case, if the \"test\" command is not available."
502 (with-parsed-tramp-file-name filename nil
503 (with-tramp-file-property v localname
"file-writable-p"
504 (if (tramp-adb-find-test-command v
)
505 (if (file-exists-p filename
)
507 (tramp-adb-command-exit-status
508 v
(format "test -w %s" (tramp-shell-quote-argument localname
))))
510 (file-directory-p (file-name-directory filename
))
511 (file-writable-p (file-name-directory filename
))))
513 ;; Missing "test" command on Android < 4.
514 (let ((rw-path "/data/data"))
517 "Not implemented yet (assuming \"/data/data\" is writable): %s"
519 (and (>= (length localname
) (length rw-path
))
520 (string= (substring localname
0 (length rw-path
))
523 (defun tramp-adb-handle-write-region
524 (start end filename
&optional append visit lockname confirm
)
525 "Like `write-region' for Tramp files."
526 (setq filename
(expand-file-name filename
))
527 (with-parsed-tramp-file-name filename nil
530 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
531 (when (and confirm
(file-exists-p filename
))
532 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
534 (tramp-error v
'file-error
"File not overwritten")))
535 ;; We must also flush the cache of the directory, because
536 ;; `file-attributes' reads the values from there.
537 (tramp-flush-file-property v
(file-name-directory localname
))
538 (tramp-flush-file-property v localname
)
539 (let* ((curbuf (current-buffer))
540 (tmpfile (tramp-compat-make-temp-file filename
)))
541 (tramp-run-real-handler
543 (list start end tmpfile append
'no-message lockname confirm
))
544 (with-tramp-progress-reporter
545 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
547 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
548 (tramp-error v
'file-error
"Cannot write: `%s' filename"))
549 (delete-file tmpfile
)))
551 (unless (equal curbuf
(current-buffer))
554 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
556 (defun tramp-adb-handle-set-file-modes (filename mode
)
557 "Like `set-file-modes' for Tramp files."
558 (with-parsed-tramp-file-name filename nil
559 (tramp-flush-file-property v localname
)
560 (tramp-adb-barf-unless-okay
561 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
)
562 "Error while changing file's mode %s" filename
)))
564 (defun tramp-adb-handle-copy-file
565 (filename newname
&optional ok-if-already-exists keep-date
566 preserve-uid-gid preserve-extended-attributes
)
567 "Like `copy-file' for Tramp files.
568 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
569 (setq filename
(expand-file-name filename
)
570 newname
(expand-file-name newname
))
572 (if (file-directory-p filename
)
573 (copy-directory filename newname keep-date t
)
574 (with-tramp-progress-reporter
575 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
576 0 (format "Copying %s to %s" filename newname
)
578 (let ((tmpfile (file-local-copy filename
)))
583 (rename-file tmpfile newname ok-if-already-exists
)
585 (delete-file tmpfile
)
586 (signal (car err
) (cdr err
))))
589 (when (file-directory-p newname
)
591 (expand-file-name (file-name-nondirectory filename
) newname
)))
593 (with-parsed-tramp-file-name newname nil
594 (when (and (not ok-if-already-exists
)
595 (file-exists-p newname
))
596 (tramp-error v
'file-already-exists newname
))
598 ;; We must also flush the cache of the directory, because
599 ;; `file-attributes' reads the values from there.
600 (tramp-flush-file-property v
(file-name-directory localname
))
601 (tramp-flush-file-property v localname
)
602 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
604 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
606 ;; KEEP-DATE handling.
608 (set-file-times newname
(nth 5 (file-attributes filename
))))))
610 (defun tramp-adb-handle-rename-file
611 (filename newname
&optional ok-if-already-exists
)
612 "Like `rename-file' for Tramp files."
613 (setq filename
(expand-file-name filename
)
614 newname
(expand-file-name newname
))
616 (with-parsed-tramp-file-name
617 (if (file-remote-p filename
) filename newname
) nil
618 (with-tramp-progress-reporter
619 v
0 (format "Renaming %s to %s" newname filename
)
621 (if (and (tramp-equal-remote filename newname
)
622 (not (file-directory-p filename
)))
624 (when (and (not ok-if-already-exists
)
625 (file-exists-p newname
))
626 (tramp-error v
'file-already-exists newname
))
627 ;; We must also flush the cache of the directory, because
628 ;; `file-attributes' reads the values from there.
629 (tramp-flush-file-property v
(file-name-directory localname
))
630 (tramp-flush-file-property v localname
)
632 (tramp-adb-barf-unless-okay
633 v
(format "mv %s %s" (file-remote-p filename
'localname
) localname
)
634 "Error renaming %s to %s" filename newname
))
637 (copy-file filename newname ok-if-already-exists t t
)
638 (delete-file filename
)))))
640 (defun tramp-adb-handle-process-file
641 (program &optional infile destination display
&rest args
)
642 "Like `process-file' for Tramp files."
643 ;; The implementation is not complete yet.
644 (when (and (numberp destination
) (zerop destination
))
645 (error "Implementation does not handle immediate return"))
647 (with-parsed-tramp-file-name default-directory nil
648 (let (command input tmpinput stderr tmpstderr outbuf ret
)
650 (setq command
(mapconcat 'tramp-shell-quote-argument
651 (cons program args
) " "))
654 (setq input
"/dev/null")
655 (setq infile
(expand-file-name infile
))
656 (if (tramp-equal-remote default-directory infile
)
657 ;; INFILE is on the same remote host.
658 (setq input
(with-parsed-tramp-file-name infile nil localname
))
659 ;; INFILE must be copied to remote host.
660 (setq input
(tramp-make-tramp-temp-file v
)
661 tmpinput
(tramp-make-tramp-file-name method user host input
))
662 (copy-file infile tmpinput t
)))
663 (when input
(setq command
(format "%s <%s" command input
)))
668 ((bufferp destination
)
669 (setq outbuf destination
))
671 ((stringp destination
)
672 (setq outbuf
(get-buffer-create destination
)))
673 ;; (REAL-DESTINATION ERROR-DESTINATION)
677 ((bufferp (car destination
))
678 (setq outbuf
(car destination
)))
679 ((stringp (car destination
))
680 (setq outbuf
(get-buffer-create (car destination
))))
682 (setq outbuf
(current-buffer))))
685 ((stringp (cadr destination
))
686 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
687 (if (tramp-equal-remote default-directory
(cadr destination
))
688 ;; stderr is on the same remote host.
689 (setq stderr
(with-parsed-tramp-file-name
690 (cadr destination
) nil localname
))
691 ;; stderr must be copied to remote host. The temporary
692 ;; file must be deleted after execution.
693 (setq stderr
(tramp-make-tramp-temp-file v
)
694 tmpstderr
(tramp-make-tramp-file-name
695 method user host stderr
))))
696 ;; stderr to be discarded.
697 ((null (cadr destination
))
698 (setq stderr
"/dev/null"))))
701 (setq outbuf
(current-buffer))))
702 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
704 ;; Send the command. It might not return in time, so we protect
705 ;; it. Call it in a subshell, in order to preserve working
711 (tramp-adb-barf-unless-okay
712 v
(format "(cd %s; %s)"
713 (tramp-shell-quote-argument localname
)
716 ;; We should show the output anyway.
718 (with-current-buffer outbuf
719 (insert-buffer-substring (tramp-get-connection-buffer v
)))
720 (when display
(display-buffer outbuf
))))
721 ;; When the user did interrupt, we should do it also. We use
722 ;; return code -1 as marker.
724 (kill-buffer (tramp-get-connection-buffer v
))
728 (kill-buffer (tramp-get-connection-buffer v
))
731 ;; Provide error file.
732 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
734 ;; Cleanup. We remove all file cache values for the connection,
735 ;; because the remote process could have changed them.
736 (when tmpinput
(delete-file tmpinput
))
738 ;; `process-file-side-effects' has been introduced with GNU
739 ;; Emacs 23.2. If set to `nil', no remote file will be changed
740 ;; by `program'. If it doesn't exist, we assume its default
742 (unless (and (boundp 'process-file-side-effects
)
743 (not (symbol-value 'process-file-side-effects
)))
744 (tramp-flush-directory-property v
""))
746 ;; Return exit status.
751 (defun tramp-adb-handle-shell-command
752 (command &optional output-buffer error-buffer
)
753 "Like `shell-command' for Tramp files."
754 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
755 ;; We cannot use `shell-file-name' and `shell-command-switch',
756 ;; they are variables of the local host.
757 (args (list "sh" "-c" (substring command
0 asynchronous
)))
761 ((bufferp output-buffer
) output-buffer
)
762 ((stringp output-buffer
) (get-buffer-create output-buffer
))
764 (setq current-buffer-p t
)
766 (t (get-buffer-create
768 "*Async Shell Command*"
769 "*Shell Command Output*")))))
772 ((bufferp error-buffer
) error-buffer
)
773 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
775 (if (and (not asynchronous
) error-buffer
)
776 (with-parsed-tramp-file-name default-directory nil
777 (list output-buffer
(tramp-make-tramp-temp-file v
)))
779 (p (get-buffer-process output-buffer
)))
781 ;; Check whether there is another process running. Tramp does not
782 ;; support 2 (asynchronous) processes in parallel.
784 (if (yes-or-no-p "A command is running. Kill it? ")
785 (ignore-errors (kill-process p
))
786 (error "Shell command in progress")))
790 (barf-if-buffer-read-only)
792 (with-current-buffer output-buffer
793 (setq buffer-read-only nil
)
796 (if (and (not current-buffer-p
) (integerp asynchronous
))
799 (apply 'start-file-process
"*Async Shell*" buffer args
)
801 (pop-to-buffer output-buffer
)
802 (setq mode-line-process
'(":%s"))
807 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
808 ;; Insert error messages if they were separated.
810 (with-current-buffer error-buffer
811 (insert-file-contents (cadr buffer
)))
812 (delete-file (cadr buffer
)))
814 ;; This is like exchange-point-and-mark, but doesn't
815 ;; activate the mark. It is cleaner to avoid activation,
816 ;; even though the command loop would deactivate the mark
817 ;; because we inserted text.
818 (goto-char (prog1 (mark t
)
819 (set-marker (mark-marker) (point)
821 ;; There's some output, display it.
822 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
823 (if (functionp 'display-message-or-buffer
)
824 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
825 (pop-to-buffer output-buffer
))))))))
827 ;; We use BUFFER also as connection buffer during setup. Because of
828 ;; this, its original contents must be saved, and restored once
829 ;; connection has been setup.
830 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
831 "Like `start-file-process' for Tramp files."
832 (with-parsed-tramp-file-name default-directory nil
833 ;; When PROGRAM is nil, we just provide a tty.
835 (when (stringp program
)
837 (tramp-shell-quote-argument localname
)
838 (mapconcat 'tramp-shell-quote-argument
839 (cons program args
) " "))))
840 (tramp-process-connection-type
841 (or (null program
) tramp-process-connection-type
))
842 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
849 ;; BUFFER can be nil. We use a temporary buffer.
850 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
851 (while (get-process name1
)
852 ;; NAME must be unique as process name.
854 name1
(format "%s<%d>" name i
)))
856 ;; Set the new process properties.
857 (tramp-set-connection-property v
"process-name" name
)
858 (tramp-set-connection-property v
"process-buffer" buffer
)
859 ;; Activate narrowing in order to save BUFFER contents.
860 ;; Clear also the modification time; otherwise we might
861 ;; be interrupted by `verify-visited-file-modtime'.
862 (with-current-buffer (tramp-get-connection-buffer v
)
863 (let ((buffer-undo-list t
))
864 (clear-visited-file-modtime)
865 (narrow-to-region (point-max) (point-max))
868 (tramp-adb-send-command v command
)
869 ;; Open the connection.
870 (tramp-adb-maybe-open-connection v
))))
871 (let ((p (tramp-get-connection-process v
)))
872 ;; Set sentinel and query flag for this process.
873 (tramp-set-connection-property p
"vector" v
)
874 (set-process-sentinel p
'tramp-process-sentinel
)
875 (tramp-compat-set-process-query-on-exit-flag p t
)
879 (with-current-buffer (tramp-get-connection-buffer v
)
880 (if (string-match tramp-temp-buffer-name
(buffer-name))
882 (set-process-buffer (tramp-get-connection-process v
) nil
)
883 (kill-buffer (current-buffer)))
884 (set-buffer-modified-p bmp
)))
885 (tramp-set-connection-property v
"process-name" nil
)
886 (tramp-set-connection-property v
"process-buffer" nil
)))))
888 ;; Android < 4 doesn't provide test command.
890 (defun tramp-adb-handle-file-exists-p (filename)
891 "Like `file-exists-p' for Tramp files."
892 (with-parsed-tramp-file-name filename nil
893 (with-tramp-file-property v localname
"file-exists-p"
894 (file-attributes filename
))))
898 (defun tramp-adb-execute-adb-command (vec &rest args
)
899 "Returns nil on success error-output on failure."
900 (when (tramp-file-name-host vec
)
901 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
904 (unless (zerop (apply 'call-process
(tramp-adb-program) nil t nil args
))
908 (tramp-adb-program) (mapconcat 'identity args
" ") (buffer-string)))))
910 (defun tramp-adb-find-test-command (vec)
911 "Checks, whether the ash has a builtin \"test\" command.
912 This happens for Android >= 4.0."
913 (with-tramp-connection-property vec
"test"
914 (zerop (tramp-adb-command-exit-status vec
"type test"))))
916 ;; Connection functions
918 (defun tramp-adb-send-command (vec command
)
919 "Send the COMMAND to connection VEC."
920 (tramp-adb-maybe-open-connection vec
)
921 (tramp-message vec
6 "%s" command
)
922 (tramp-send-string vec command
)
923 ;; fixme: Race condition
924 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
925 (with-current-buffer (tramp-get-connection-buffer vec
)
927 (goto-char (point-min))
928 ;; We can't use stty to disable echo of command.
929 (delete-matching-lines (regexp-quote command
))
930 ;; When the local machine is W32, there are still trailing ^M.
931 ;; There must be a better solution by setting the correct coding
932 ;; system, but this requires changes in core Tramp.
933 (goto-char (point-min))
934 (while (re-search-forward "\r+$" nil t
)
935 (replace-match "" nil nil
)))))
937 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
938 "Run COMMAND, check exit status, throw error if exit status not okay.
939 FMT and ARGS are passed to `error'."
940 (tramp-adb-send-command vec
(format "%s; echo tramp_exit_status $?" command
))
941 (with-current-buffer (tramp-get-connection-buffer vec
)
942 (goto-char (point-max))
943 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
945 vec
'file-error
"Couldn't find exit status of `%s'" command
))
946 (skip-chars-forward "^ ")
947 (unless (zerop (read (current-buffer)))
948 (apply 'tramp-error vec
'file-error fmt args
))
949 (let (buffer-read-only)
950 (delete-region (match-beginning 0) (point-max)))))
952 (defun tramp-adb-command-exit-status
954 "Run COMMAND and return its exit status.
955 Sends `echo $?' along with the COMMAND for checking the exit status. If
956 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
957 (tramp-adb-send-command vec
(format "%s; echo tramp_exit_status $?" command
))
958 (with-current-buffer (tramp-get-connection-buffer vec
)
959 (goto-char (point-max))
960 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
962 vec
'file-error
"Couldn't find exit status of `%s'" command
))
963 (skip-chars-forward "^ ")
964 (read (current-buffer))))
966 (defun tramp-adb-wait-for-output (proc &optional timeout
)
967 "Wait for output from remote command."
968 (unless (buffer-live-p (process-buffer proc
))
969 (delete-process proc
)
970 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
971 (with-current-buffer (process-buffer proc
)
972 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
973 (let (buffer-read-only)
974 (goto-char (point-min))
975 ;; ADB terminal sends "^H" sequences.
976 (when (re-search-forward "<\b+" (point-at-eol) t
)
978 (delete-region (point-min) (point)))
979 ;; Delete the prompt.
980 (goto-char (point-min))
981 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
983 (delete-region (point-min) (point)))
984 (goto-char (point-max))
985 (re-search-backward tramp-adb-prompt nil t
)
986 (delete-region (point) (point-max)))
990 "[[Remote adb prompt `%s' not found in %d secs]]"
991 tramp-adb-prompt timeout
)
994 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
996 (defun tramp-adb-maybe-open-connection (vec)
997 "Maybe open a connection VEC.
998 Does not do anything if a connection is already open, but re-opens the
999 connection if a previous connection has died for some reason."
1000 (let* ((buf (tramp-get-connection-buffer vec
))
1001 (p (get-buffer-process buf
))
1002 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1004 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1006 (when (and p
(processp p
)) (delete-process p
))
1008 (tramp-error vec
'file-error
"No device connected"))
1009 (if (and (tramp-file-name-host vec
)
1010 (not (member (tramp-file-name-host vec
) devices
)))
1013 "Device %s not connected" (tramp-file-name-host vec
)))
1014 (if (and (not (eq (length devices
) 1))
1015 (not (tramp-file-name-host vec
)))
1018 "Multiple Devices connected: No Host/Device specified"))
1019 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1020 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1021 (process-connection-type tramp-process-connection-type
)
1022 (args (if (tramp-file-name-host vec
)
1023 (list "-s" (tramp-file-name-host vec
) "shell")
1025 (p (let ((default-directory
1026 (tramp-compat-temporary-file-directory)))
1027 (apply 'start-process
(tramp-get-connection-name vec
) buf
1028 (tramp-adb-program) args
))))
1030 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1031 ;; Wait for initial prompt.
1032 (tramp-adb-wait-for-output p
)
1033 (unless (eq 'run
(process-status p
))
1034 (tramp-error vec
'file-error
"Terminated!"))
1035 (set-process-query-on-exit-flag p nil
)
1037 ;; Check whether the properties have been changed. If
1038 ;; yes, this is a strong indication that we must expire all
1039 ;; connection properties. We start again.
1040 (tramp-message vec
5 "Checking system information")
1041 (tramp-adb-send-command
1042 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1044 (tramp-get-connection-property vec
"getprop" nil
))
1046 (tramp-set-connection-property
1048 (with-current-buffer (tramp-get-connection-buffer vec
)
1049 ;; Read the expression.
1050 (goto-char (point-min))
1051 (read (current-buffer))))))
1052 (when (and (stringp old-getprop
)
1053 (not (string-equal old-getprop new-getprop
)))
1057 "Connection reset, because remote host changed from `%s' to `%s'"
1058 old-getprop new-getprop
)
1059 (tramp-adb-maybe-open-connection vec
)))))))))
1061 (provide 'tramp-adb
)
1062 ;;; tramp-adb.el ends here