1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
3 ;; Copyright (C) 2011-2012 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-directory-p . tramp-adb-handle-file-directory-p
)
86 (file-symlink-p . tramp-handle-file-symlink-p
)
87 ;; FIXME: This is too sloppy.
88 (file-executable-p . file-exists-p
)
89 (file-exists-p . tramp-adb-handle-file-exists-p
)
90 (file-readable-p . tramp-handle-file-exists-p
)
91 (file-writable-p . tramp-adb-handle-file-writable-p
)
92 (file-local-copy . tramp-adb-handle-file-local-copy
)
93 (file-modes . tramp-handle-file-modes
)
94 (expand-file-name . tramp-adb-handle-expand-file-name
)
95 (find-backup-file-name . tramp-handle-find-backup-file-name
)
96 (directory-files . tramp-handle-directory-files
)
97 (make-directory . tramp-adb-handle-make-directory
)
98 (delete-directory . tramp-adb-handle-delete-directory
)
99 (delete-file . tramp-adb-handle-delete-file
)
100 (load . tramp-handle-load
)
101 (insert-directory . tramp-adb-handle-insert-directory
)
102 (insert-file-contents . tramp-handle-insert-file-contents
)
103 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
104 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
105 (vc-registered . ignore
) ;no vc control files on Android devices
106 (write-region . tramp-adb-handle-write-region
)
107 (set-file-modes . tramp-adb-handle-set-file-modes
)
108 (set-file-times . ignore
)
109 (copy-file . tramp-adb-handle-copy-file
)
110 (rename-file . tramp-adb-handle-rename-file
)
111 (process-file . tramp-adb-handle-process-file
)
112 (shell-command . tramp-adb-handle-shell-command
)
113 (start-file-process . tramp-adb-handle-start-file-process
))
114 "Alist of handler functions for Tramp ADB method.")
116 ;; It must be a `defsubst' in order to push the whole code into
117 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
119 (defsubst tramp-adb-file-name-p
(filename)
120 "Check if it's a filename for ADB."
121 (let ((v (tramp-dissect-file-name filename
)))
122 (string= (tramp-file-name-method v
) tramp-adb-method
)))
125 (defun tramp-adb-file-name-handler (operation &rest args
)
126 "Invoke the ADB handler for OPERATION.
127 First arg specifies the OPERATION, second arg is a list of arguments to
128 pass to the OPERATION."
129 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
))
130 ;; `tramp-default-host's default value is (system-name). Not
133 (unless (equal (eval (car (get 'tramp-default-host
'standard-value
)))
135 tramp-default-host
)))
137 (save-match-data (apply (cdr fn
) args
))
138 (tramp-run-real-handler operation args
))))
140 ;; This cannot be a constant, because `tramp-adb-sdk-dir' is customizable.
141 (defun tramp-adb-program ()
142 "The Android Debug Bridge."
143 (expand-file-name "platform-tools/adb" tramp-adb-sdk-dir
))
146 (defun tramp-adb-parse-device-names (ignore)
147 "Return a list of (nil host) tuples allowed to access."
149 (when (zerop (call-process (tramp-adb-program) nil t nil
"devices"))
151 (goto-char (point-min))
152 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
153 (add-to-list 'result
(list nil
(match-string 1))))
156 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
157 "Like `expand-file-name' for Tramp files."
158 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
159 (setq dir
(or dir default-directory
"/"))
160 ;; Unless NAME is absolute, concat DIR and NAME.
161 (unless (file-name-absolute-p name
)
162 (setq name
(concat (file-name-as-directory dir
) name
)))
163 ;; If NAME is not a Tramp file, run the real handler.
164 (if (not (tramp-tramp-file-p name
))
165 (tramp-run-real-handler 'expand-file-name
(list name nil
))
167 (with-parsed-tramp-file-name name nil
168 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
169 (setq localname
(concat "/" localname
)))
170 ;; Do normal `expand-file-name' (this does "/./" and "/../").
171 ;; We bind `directory-sep-char' here for XEmacs on Windows,
172 ;; which would otherwise use backslash. `default-directory' is
173 ;; bound, because on Windows there would be problems with UNC
174 ;; shares or Cygwin mounts.
175 (let ((directory-sep-char ?
/)
176 (default-directory (tramp-compat-temporary-file-directory)))
177 (tramp-make-tramp-file-name
179 (tramp-drop-volume-letter
180 (tramp-run-real-handler
181 'expand-file-name
(list localname
))))))))
183 (defun tramp-adb-handle-file-directory-p (filename)
184 "Like `file-directory-p' for Tramp files."
185 (car (file-attributes (file-truename filename
))))
187 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
188 ;; code could be shared?
189 (defun tramp-adb-handle-file-truename (filename &optional counter prev-dirs
)
190 "Like `file-truename' for Tramp files."
191 (with-parsed-tramp-file-name (expand-file-name filename
) nil
192 (with-tramp-file-property v localname
"file-truename"
193 (let ((result nil
)) ; result steps in reverse order
194 (tramp-message v
4 "Finding true name for `%s'" filename
)
195 (let* ((directory-sep-char ?
/)
196 (steps (tramp-compat-split-string localname
"/"))
197 (localnamedir (tramp-run-real-handler
198 'file-name-as-directory
(list localname
)))
199 (is-dir (string= localname localnamedir
))
202 ;; Don't make the following value larger than
203 ;; necessary. People expect an error message in a
204 ;; timely fashion when something is wrong; otherwise
205 ;; they might think that Emacs is hung. Of course,
206 ;; correctness has to come first.
209 (while (and steps
(< numchase numchase-limit
))
210 (setq thisstep
(pop steps
))
214 (append '("") (reverse result
) (list thisstep
))
217 (nth 0 (file-attributes
218 (tramp-make-tramp-file-name
225 (cond ((string= "." thisstep
)
226 (tramp-message v
5 "Ignoring step `.'"))
227 ((string= ".." thisstep
)
228 (tramp-message v
5 "Processing step `..'")
230 ((stringp symlink-target
)
231 ;; It's a symlink, follow it.
232 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
233 (setq numchase
(1+ numchase
))
234 (when (file-name-absolute-p symlink-target
)
236 ;; If the symlink was absolute, we'll get a string
237 ;; like "/user@host:/some/target"; extract the
238 ;; "/some/target" part from it.
239 (when (tramp-tramp-file-p symlink-target
)
240 (unless (tramp-equal-remote filename symlink-target
)
243 "Symlink target `%s' on wrong host" symlink-target
))
244 (setq symlink-target localname
))
246 (append (tramp-compat-split-string
251 (setq result
(cons thisstep result
)))))
252 (when (>= numchase numchase-limit
)
255 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
256 (setq result
(reverse result
))
257 ;; Combine list to form string.
260 (mapconcat 'identity
(cons "" result
) "/")
262 (when (and is-dir
(or (string= "" result
)
263 (not (string= (substring result -
1) "/"))))
264 (setq result
(concat result
"/"))))
266 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
267 (tramp-make-tramp-file-name method user host result
)))))
269 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
270 "Like `file-attributes' for Tramp files."
271 (unless id-format
(setq id-format
'integer
))
273 (with-parsed-tramp-file-name filename nil
274 (with-tramp-file-property
275 v localname
(format "file-attributes-%s" id-format
)
276 (tramp-adb-barf-unless-okay
277 v
(format "%s -d -l %s"
278 (tramp-adb-get-ls-command v
)
279 (tramp-shell-quote-argument localname
)) "")
280 (with-current-buffer (tramp-get-buffer v
)
281 (tramp-adb-sh-fix-ls-output)
282 (let* ((columns (split-string (buffer-string)))
283 (mod-string (nth 0 columns
))
284 (is-dir (eq ?d
(aref mod-string
0)))
285 (is-symlink (eq ?l
(aref mod-string
0)))
288 (cadr (split-string (buffer-string) "\\( -> \\|\n\\)"))))
289 (uid (nth 1 columns
))
290 (gid (nth 2 columns
))
291 (date (format "%s %s" (nth 4 columns
) (nth 5 columns
)))
292 (size (string-to-number (nth 3 columns
))))
294 (or is-dir symlink-target
)
296 ;; no way to handle numeric ids in Androids ash
297 (if (eq id-format
'integer
) 0 uid
)
298 (if (eq id-format
'integer
) 0 gid
)
300 (date-to-time date
) ; mtime
307 (defun tramp-adb-get-ls-command (vec)
308 (with-tramp-connection-property vec
"ls"
309 (tramp-message vec
5 "Finding a suitable `ls' command")
310 (if (zerop (tramp-adb-command-exit-status
311 vec
"ls --color=never -al /dev/null"))
312 ;; On CyanogenMod based system BusyBox is used and "ls" output
313 ;; coloring is enabled by default. So we try to disable it
318 (defun tramp-adb-get-toolbox (vec)
319 "Get shell toolbox implementation: `toolbox' for orginal distributions
320 or `busybox' for CynagenMode based distributions"
321 (with-tramp-connection-property vec
"toolbox"
322 (tramp-message vec
5 "Checking shell toolbox implementation")
324 ((zerop (tramp-adb-command-exit-status vec
"busybox")) 'busybox
)
325 ((zerop (tramp-adb-command-exit-status vec
"toolbox")) 'toolbox
)
328 (defun tramp-adb--gnu-switches-to-ash
330 "Almquist shell can't handle multiple arguments.
331 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
335 (replace-regexp-in-string
337 (replace-regexp-in-string "^-" "" s
)))
338 ;; FIXME: Warning about removed switches (long and non-dash).
342 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
345 (defun tramp-adb-handle-insert-directory
346 (filename switches
&optional wildcard full-directory-p
)
347 "Like `insert-directory' for Tramp files."
348 (when (stringp switches
)
349 (setq switches
(tramp-adb--gnu-switches-to-ash (split-string switches
))))
350 (with-parsed-tramp-file-name (file-truename filename
) nil
351 (with-current-buffer (tramp-get-buffer v
)
352 (let ((name (tramp-shell-quote-argument (directory-file-name localname
)))
353 (switch-d (member "-d" switches
))
354 (switch-t (member "-t" switches
))
355 (switches (mapconcat 'identity
(remove "-t" switches
) " ")))
356 (tramp-adb-barf-unless-okay
357 v
(format "%s %s %s" (tramp-adb-get-ls-command v
) switches name
)
358 "Cannot insert directory listing: %s" filename
)
360 ;; We insert also filename/. and filename/.., because "ls" doesn't.
361 (narrow-to-region (point) (point))
363 (tramp-adb-barf-unless-okay
364 v
(format "%s -d %s %s %s"
365 (tramp-adb-get-ls-command v
)
367 (concat (file-name-as-directory name
) ".")
368 (concat (file-name-as-directory name
) ".."))
369 "Cannot insert directory listing: %s" filename
))
371 (tramp-adb-sh-fix-ls-output switch-t
)))
372 (insert-buffer-substring (tramp-get-buffer v
))))
374 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
375 "Insert dummy 0 in empty size columns.
376 Androids \"ls\" command doesn't insert size column for directories:
377 Emacs dired can't find files."
379 ;; Insert missing size.
380 (goto-char (point-min))
382 (search-forward-regexp
383 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
384 (replace-match "0\\1" "\\1" nil
)
385 ;; Insert missing "/".
386 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
390 (let* ((lines (split-string (buffer-string) "\n" t
))
395 'tramp-adb-ls-output-time-less-p
396 'tramp-adb-ls-output-name-less-p
))))
397 (delete-region (point-min) (point-max))
398 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
399 ;; Add final newline.
400 (goto-char (point-max))
401 (unless (= (point) (line-beginning-position))
405 (defun tramp-adb-ls-output-time-less-p (a b
)
406 "Sort \"ls\" output by time, descending."
408 (string-match tramp-adb-ls-date-regexp a
)
409 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
410 (string-match tramp-adb-ls-date-regexp b
)
411 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
412 (time-less-p time-b time-a
)))
414 (defun tramp-adb-ls-output-name-less-p (a b
)
415 "Sort \"ls\" output by name, ascending."
417 (string-match dired-move-to-filename-regexp a
)
418 (setq posa
(match-end 0))
419 (string-match dired-move-to-filename-regexp b
)
420 (setq posb
(match-end 0))
421 (string-lessp (substring a posa
) (substring b posb
))))
423 (defun tramp-adb-handle-make-directory (dir &optional parents
)
424 "Like `make-directory' for Tramp files."
425 (setq dir
(expand-file-name dir
))
426 (with-parsed-tramp-file-name dir nil
428 (let ((par (expand-file-name ".." dir
)))
429 (unless (file-directory-p par
)
430 (make-directory par parents
))))
431 (tramp-adb-barf-unless-okay
432 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
433 "Couldn't make directory %s" dir
)
434 (tramp-flush-directory-property v
(file-name-directory localname
))))
436 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
437 "Like `delete-directory' for Tramp files."
438 (setq directory
(expand-file-name directory
))
439 (with-parsed-tramp-file-name directory nil
440 (tramp-flush-file-property v
(file-name-directory localname
))
441 (tramp-flush-directory-property v localname
)
442 (tramp-adb-barf-unless-okay
444 (if recursive
"rm -r" "rmdir")
445 (tramp-shell-quote-argument localname
))
446 "Couldn't delete %s" directory
)))
448 (defun tramp-adb-handle-delete-file (filename &optional trash
)
449 "Like `delete-file' for Tramp files."
450 (setq filename
(expand-file-name filename
))
451 (with-parsed-tramp-file-name filename nil
452 (tramp-flush-file-property v
(file-name-directory localname
))
453 (tramp-flush-file-property v localname
)
454 (tramp-adb-barf-unless-okay
455 v
(format "rm %s" (tramp-shell-quote-argument localname
))
456 "Couldn't delete %s" filename
)))
458 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
459 "Like `file-name-all-completions' for Tramp files."
462 (with-parsed-tramp-file-name directory nil
463 (with-tramp-file-property v localname
"file-name-all-completions"
465 (tramp-adb-send-command
467 (tramp-adb-get-ls-command v
)
468 (tramp-shell-quote-argument localname
)))
471 (if (file-directory-p f
)
472 (file-name-as-directory f
)
474 (with-current-buffer (tramp-get-buffer v
)
478 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
479 (split-string (buffer-string) "\n"))))))))))
481 (defun tramp-adb-handle-file-local-copy (filename)
482 "Like `file-local-copy' for Tramp files."
483 (with-parsed-tramp-file-name filename nil
484 (unless (file-exists-p (file-truename filename
))
487 "Cannot make local copy of non-existing file `%s'" filename
))
488 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
489 (with-tramp-progress-reporter
490 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
491 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
492 (delete-file tmpfile
)
494 v
'file-error
"Cannot make local copy of file `%s'" filename
))
495 (set-file-modes tmpfile
(file-modes filename
)))
498 (defun tramp-adb-handle-file-writable-p (filename)
499 "Like `tramp-sh-handle-file-writable-p'.
500 But handle the case, if the \"test\" command is not available."
501 (with-parsed-tramp-file-name filename nil
502 (with-tramp-file-property v localname
"file-writable-p"
503 (if (tramp-adb-find-test-command v
)
504 (if (file-exists-p filename
)
506 (tramp-adb-command-exit-status
507 v
(format "test -w %s" (tramp-shell-quote-argument localname
))))
509 (file-directory-p (file-name-directory filename
))
510 (file-writable-p (file-name-directory filename
))))
512 ;; Missing "test" command on Android < 4.
513 (let ((rw-path "/data/data"))
516 "Not implemented yet (assuming \"/data/data\" is writable): %s"
518 (and (>= (length localname
) (length rw-path
))
519 (string= (substring localname
0 (length rw-path
))
522 (defun tramp-adb-handle-write-region
523 (start end filename
&optional append visit lockname confirm
)
524 "Like `write-region' for Tramp files."
525 (setq filename
(expand-file-name filename
))
526 (with-parsed-tramp-file-name filename nil
529 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
530 (when (and confirm
(file-exists-p filename
))
531 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
533 (tramp-error v
'file-error
"File not overwritten")))
534 ;; We must also flush the cache of the directory, because
535 ;; `file-attributes' reads the values from there.
536 (tramp-flush-file-property v
(file-name-directory localname
))
537 (tramp-flush-file-property v localname
)
538 (let* ((curbuf (current-buffer))
539 (tmpfile (tramp-compat-make-temp-file filename
)))
540 (tramp-run-real-handler
542 (list start end tmpfile append
'no-message lockname confirm
))
543 (with-tramp-progress-reporter
544 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
546 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
547 (tramp-error v
'file-error
"Cannot write: `%s' filename"))
548 (delete-file tmpfile
)))
550 (unless (equal curbuf
(current-buffer))
553 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
555 (defun tramp-adb-handle-set-file-modes (filename mode
)
556 "Like `set-file-modes' for Tramp files."
557 (with-parsed-tramp-file-name filename nil
558 (tramp-flush-file-property v localname
)
559 (tramp-adb-barf-unless-okay
560 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
)
561 "Error while changing file's mode %s" filename
)))
563 (defun tramp-adb-handle-copy-file
564 (filename newname
&optional ok-if-already-exists keep-date
565 preserve-uid-gid preserve-extended-attributes
)
566 "Like `copy-file' for Tramp files.
567 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
568 (setq filename
(expand-file-name filename
)
569 newname
(expand-file-name newname
))
571 (if (file-directory-p filename
)
572 (copy-directory filename newname keep-date t
)
573 (with-tramp-progress-reporter
574 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
575 0 (format "Copying %s to %s" filename newname
)
577 (let ((tmpfile (file-local-copy filename
)))
582 (rename-file tmpfile newname ok-if-already-exists
)
584 (delete-file tmpfile
)
585 (signal (car err
) (cdr err
))))
588 (when (file-directory-p newname
)
590 (expand-file-name (file-name-nondirectory filename
) newname
)))
592 (with-parsed-tramp-file-name newname nil
593 (when (and (not ok-if-already-exists
)
594 (file-exists-p newname
))
595 (tramp-error v
'file-already-exists newname
))
597 ;; We must also flush the cache of the directory, because
598 ;; `file-attributes' reads the values from there.
599 (tramp-flush-file-property v
(file-name-directory localname
))
600 (tramp-flush-file-property v localname
)
601 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
603 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
605 ;; KEEP-DATE handling.
607 (set-file-times newname
(nth 5 (file-attributes filename
))))))
609 (defun tramp-adb-handle-rename-file
610 (filename newname
&optional ok-if-already-exists
)
611 "Like `rename-file' for Tramp files."
612 (setq filename
(expand-file-name filename
)
613 newname
(expand-file-name newname
))
615 (with-parsed-tramp-file-name
616 (if (file-remote-p filename
) filename newname
) nil
617 (with-tramp-progress-reporter
618 v
0 (format "Renaming %s to %s" newname filename
)
620 (if (and (tramp-equal-remote filename newname
)
621 (not (file-directory-p filename
)))
623 (when (and (not ok-if-already-exists
)
624 (file-exists-p newname
))
625 (tramp-error v
'file-already-exists newname
))
626 ;; We must also flush the cache of the directory, because
627 ;; `file-attributes' reads the values from there.
628 (tramp-flush-file-property v
(file-name-directory localname
))
629 (tramp-flush-file-property v localname
)
631 (tramp-adb-barf-unless-okay
632 v
(format "mv %s %s" (file-remote-p filename
'localname
) localname
)
633 "Error renaming %s to %s" filename newname
))
636 (copy-file filename newname ok-if-already-exists t t
)
637 (delete-file filename
)))))
639 (defun tramp-adb-handle-process-file
640 (program &optional infile destination display
&rest args
)
641 "Like `process-file' for Tramp files."
642 ;; The implementation is not complete yet.
643 (when (and (numberp destination
) (zerop destination
))
644 (error "Implementation does not handle immediate return"))
646 (with-parsed-tramp-file-name default-directory nil
647 (let (command input tmpinput stderr tmpstderr outbuf ret
)
649 (setq command
(mapconcat 'tramp-shell-quote-argument
650 (cons program args
) " "))
653 (setq input
"/dev/null")
654 (setq infile
(expand-file-name infile
))
655 (if (tramp-equal-remote default-directory infile
)
656 ;; INFILE is on the same remote host.
657 (setq input
(with-parsed-tramp-file-name infile nil localname
))
658 ;; INFILE must be copied to remote host.
659 (setq input
(tramp-make-tramp-temp-file v
)
660 tmpinput
(tramp-make-tramp-file-name method user host input
))
661 (copy-file infile tmpinput t
)))
662 (when input
(setq command
(format "%s <%s" command input
)))
667 ((bufferp destination
)
668 (setq outbuf destination
))
670 ((stringp destination
)
671 (setq outbuf
(get-buffer-create destination
)))
672 ;; (REAL-DESTINATION ERROR-DESTINATION)
676 ((bufferp (car destination
))
677 (setq outbuf
(car destination
)))
678 ((stringp (car destination
))
679 (setq outbuf
(get-buffer-create (car destination
))))
681 (setq outbuf
(current-buffer))))
684 ((stringp (cadr destination
))
685 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
686 (if (tramp-equal-remote default-directory
(cadr destination
))
687 ;; stderr is on the same remote host.
688 (setq stderr
(with-parsed-tramp-file-name
689 (cadr destination
) nil localname
))
690 ;; stderr must be copied to remote host. The temporary
691 ;; file must be deleted after execution.
692 (setq stderr
(tramp-make-tramp-temp-file v
)
693 tmpstderr
(tramp-make-tramp-file-name
694 method user host stderr
))))
695 ;; stderr to be discarded.
696 ((null (cadr destination
))
697 (setq stderr
"/dev/null"))))
700 (setq outbuf
(current-buffer))))
701 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
703 ;; Send the command. It might not return in time, so we protect
704 ;; it. Call it in a subshell, in order to preserve working
710 (tramp-adb-barf-unless-okay
711 v
(format "(cd %s; %s)"
712 (tramp-shell-quote-argument localname
)
715 ;; We should show the output anyway.
717 (with-current-buffer outbuf
718 (insert-buffer-substring (tramp-get-connection-buffer v
)))
719 (when display
(display-buffer outbuf
))))
720 ;; When the user did interrupt, we should do it also. We use
721 ;; return code -1 as marker.
723 (kill-buffer (tramp-get-connection-buffer v
))
727 (kill-buffer (tramp-get-connection-buffer v
))
730 ;; Provide error file.
731 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
733 ;; Cleanup. We remove all file cache values for the connection,
734 ;; because the remote process could have changed them.
735 (when tmpinput
(delete-file tmpinput
))
737 ;; `process-file-side-effects' has been introduced with GNU
738 ;; Emacs 23.2. If set to `nil', no remote file will be changed
739 ;; by `program'. If it doesn't exist, we assume its default
741 (unless (and (boundp 'process-file-side-effects
)
742 (not (symbol-value 'process-file-side-effects
)))
743 (tramp-flush-directory-property v
""))
745 ;; Return exit status.
750 (defun tramp-adb-handle-shell-command
751 (command &optional output-buffer error-buffer
)
752 "Like `shell-command' for Tramp files."
753 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
754 ;; We cannot use `shell-file-name' and `shell-command-switch',
755 ;; they are variables of the local host.
756 (args (list "sh" "-c" (substring command
0 asynchronous
)))
760 ((bufferp output-buffer
) output-buffer
)
761 ((stringp output-buffer
) (get-buffer-create output-buffer
))
763 (setq current-buffer-p t
)
765 (t (get-buffer-create
767 "*Async Shell Command*"
768 "*Shell Command Output*")))))
771 ((bufferp error-buffer
) error-buffer
)
772 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
774 (if (and (not asynchronous
) error-buffer
)
775 (with-parsed-tramp-file-name default-directory nil
776 (list output-buffer
(tramp-make-tramp-temp-file v
)))
778 (p (get-buffer-process output-buffer
)))
780 ;; Check whether there is another process running. Tramp does not
781 ;; support 2 (asynchronous) processes in parallel.
783 (if (yes-or-no-p "A command is running. Kill it? ")
784 (ignore-errors (kill-process p
))
785 (error "Shell command in progress")))
789 (barf-if-buffer-read-only)
791 (with-current-buffer output-buffer
792 (setq buffer-read-only nil
)
795 (if (and (not current-buffer-p
) (integerp asynchronous
))
798 (apply 'start-file-process
"*Async Shell*" buffer args
)
800 (pop-to-buffer output-buffer
)
801 (setq mode-line-process
'(":%s"))
806 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
807 ;; Insert error messages if they were separated.
809 (with-current-buffer error-buffer
810 (insert-file-contents (cadr buffer
)))
811 (delete-file (cadr buffer
)))
813 ;; This is like exchange-point-and-mark, but doesn't
814 ;; activate the mark. It is cleaner to avoid activation,
815 ;; even though the command loop would deactivate the mark
816 ;; because we inserted text.
817 (goto-char (prog1 (mark t
)
818 (set-marker (mark-marker) (point)
820 ;; There's some output, display it.
821 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
822 (if (functionp 'display-message-or-buffer
)
823 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
824 (pop-to-buffer output-buffer
))))))))
826 ;; We use BUFFER also as connection buffer during setup. Because of
827 ;; this, its original contents must be saved, and restored once
828 ;; connection has been setup.
829 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
830 "Like `start-file-process' for Tramp files."
831 (with-parsed-tramp-file-name default-directory nil
832 ;; When PROGRAM is nil, we just provide a tty.
834 (when (stringp program
)
836 (tramp-shell-quote-argument localname
)
837 (mapconcat 'tramp-shell-quote-argument
838 (cons program args
) " "))))
839 (tramp-process-connection-type
840 (or (null program
) tramp-process-connection-type
))
841 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
848 ;; BUFFER can be nil. We use a temporary buffer.
849 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
850 (while (get-process name1
)
851 ;; NAME must be unique as process name.
853 name1
(format "%s<%d>" name i
)))
855 ;; Set the new process properties.
856 (tramp-set-connection-property v
"process-name" name
)
857 (tramp-set-connection-property v
"process-buffer" buffer
)
858 ;; Activate narrowing in order to save BUFFER contents.
859 ;; Clear also the modification time; otherwise we might
860 ;; be interrupted by `verify-visited-file-modtime'.
861 (with-current-buffer (tramp-get-connection-buffer v
)
862 (let ((buffer-undo-list t
))
863 (clear-visited-file-modtime)
864 (narrow-to-region (point-max) (point-max))
867 (tramp-adb-send-command v command
)
868 ;; Open the connection.
869 (tramp-adb-maybe-open-connection v
))))
870 (let ((p (tramp-get-connection-process v
)))
871 ;; Set sentinel and query flag for this process.
872 (tramp-set-connection-property p
"vector" v
)
873 (set-process-sentinel p
'tramp-process-sentinel
)
874 (tramp-compat-set-process-query-on-exit-flag p t
)
878 (with-current-buffer (tramp-get-connection-buffer v
)
879 (if (string-match tramp-temp-buffer-name
(buffer-name))
881 (set-process-buffer (tramp-get-connection-process v
) nil
)
882 (kill-buffer (current-buffer)))
883 (set-buffer-modified-p bmp
)))
884 (tramp-set-connection-property v
"process-name" nil
)
885 (tramp-set-connection-property v
"process-buffer" nil
)))))
887 ;; Android < 4 doesn't provide test command.
889 (defun tramp-adb-handle-file-exists-p (filename)
890 "Like `file-exists-p' for Tramp files."
891 (with-parsed-tramp-file-name filename nil
892 (with-tramp-file-property v localname
"file-exists-p"
893 (file-attributes filename
))))
897 (defun tramp-adb-execute-adb-command (vec &rest args
)
898 "Returns nil on success error-output on failure."
899 (when (tramp-file-name-host vec
)
900 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
903 (unless (zerop (apply 'call-process
(tramp-adb-program) nil t nil args
))
907 (tramp-adb-program) (mapconcat 'identity args
" ") (buffer-string)))))
909 (defun tramp-adb-find-test-command (vec)
910 "Checks, whether the ash has a builtin \"test\" command.
911 This happens for Android >= 4.0."
912 (with-tramp-connection-property vec
"test"
913 (zerop (tramp-adb-command-exit-status vec
"type test"))))
915 ;; Connection functions
917 (defun tramp-adb-send-command (vec command
)
918 "Send the COMMAND to connection VEC."
919 (tramp-adb-maybe-open-connection vec
)
920 (tramp-message vec
6 "%s" command
)
921 (tramp-send-string vec command
)
922 ;; fixme: Race condition
923 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
924 (with-current-buffer (tramp-get-connection-buffer vec
)
926 (goto-char (point-min))
927 ;; We can't use stty to disable echo of command.
928 (delete-matching-lines (regexp-quote command
))
929 ;; When the local machine is W32, there are still trailing ^M.
930 ;; There must be a better solution by setting the correct coding
931 ;; system, but this requires changes in core Tramp.
932 (goto-char (point-min))
933 (while (re-search-forward "\r+$" nil t
)
934 (replace-match "" nil nil
)))))
936 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
937 "Run COMMAND, check exit status, throw error if exit status not okay.
938 FMT and ARGS are passed to `error'."
939 (tramp-adb-send-command vec
(format "%s; echo tramp_exit_status $?" command
))
940 (with-current-buffer (tramp-get-connection-buffer vec
)
941 (goto-char (point-max))
942 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
944 vec
'file-error
"Couldn't find exit status of `%s'" command
))
945 (skip-chars-forward "^ ")
946 (unless (zerop (read (current-buffer)))
947 (apply 'tramp-error vec
'file-error fmt args
))
948 (let (buffer-read-only)
949 (delete-region (match-beginning 0) (point-max)))))
951 (defun tramp-adb-command-exit-status
953 "Run COMMAND and return its exit status.
954 Sends `echo $?' along with the COMMAND for checking the exit status. If
955 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
956 (tramp-adb-send-command vec
(format "%s; echo tramp_exit_status $?" command
))
957 (with-current-buffer (tramp-get-connection-buffer vec
)
958 (goto-char (point-max))
959 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
961 vec
'file-error
"Couldn't find exit status of `%s'" command
))
962 (skip-chars-forward "^ ")
963 (read (current-buffer))))
965 (defun tramp-adb-wait-for-output (proc &optional timeout
)
966 "Wait for output from remote command."
967 (unless (buffer-live-p (process-buffer proc
))
968 (delete-process proc
)
969 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
970 (with-current-buffer (process-buffer proc
)
971 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
972 (let (buffer-read-only)
973 (goto-char (point-min))
974 ;; ADB terminal sends "^H" sequences.
975 (when (re-search-forward "<\b+" (point-at-eol) t
)
977 (delete-region (point-min) (point)))
978 ;; Delete the prompt.
979 (goto-char (point-min))
980 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
982 (delete-region (point-min) (point)))
983 (goto-char (point-max))
984 (re-search-backward tramp-adb-prompt nil t
)
985 (delete-region (point) (point-max)))
989 "[[Remote adb prompt `%s' not found in %d secs]]"
990 tramp-adb-prompt timeout
)
993 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
995 (defun tramp-adb-maybe-open-connection (vec)
996 "Maybe open a connection VEC.
997 Does not do anything if a connection is already open, but re-opens the
998 connection if a previous connection has died for some reason."
999 (let* ((buf (tramp-get-connection-buffer vec
))
1000 (p (get-buffer-process buf
)))
1002 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1004 (when (and p
(processp p
)) (delete-process p
))
1005 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1006 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1007 (process-connection-type tramp-process-connection-type
)
1008 (args (if (tramp-file-name-host vec
)
1009 (list "-s" (tramp-file-name-host vec
) "shell")
1011 (p (let ((default-directory
1012 (tramp-compat-temporary-file-directory)))
1013 (apply 'start-process
(tramp-get-connection-name vec
) buf
1014 (tramp-adb-program) args
))))
1016 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1017 ;; Wait for initial prompt.
1018 (tramp-adb-wait-for-output p
)
1019 (unless (eq 'run
(process-status p
))
1020 (tramp-error vec
'file-error
"Terminated!"))
1021 (set-process-query-on-exit-flag p nil
)
1023 ;; Check whether the properties have been changed. If
1024 ;; yes, this is a strong indication that we must expire all
1025 ;; connection properties. We start again.
1026 (tramp-message vec
5 "Checking system information")
1027 (tramp-adb-send-command
1028 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1030 (tramp-get-connection-property vec
"getprop" nil
))
1032 (tramp-set-connection-property
1034 (with-current-buffer (tramp-get-connection-buffer vec
)
1035 ;; Read the expression.
1036 (goto-char (point-min))
1037 (read (current-buffer))))))
1038 (when (and (stringp old-getprop
)
1039 (not (string-equal old-getprop new-getprop
)))
1043 "Connection reset, because remote host changed from `%s' to `%s'"
1044 old-getprop new-getprop
)
1045 (tramp-adb-maybe-open-connection vec
)))))))))
1047 (provide 'tramp-adb
)
1048 ;;; tramp-adb.el ends here