1 ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp
3 ;; Copyright (C) 2011-2013 Free Software Foundation, Inc.
5 ;; Author: Juergen Hoetzel <juergen@archlinux.org>
6 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; The Android Debug Bridge "adb" must be installed on your local
27 ;; machine. If it is not in your $PATH, add the following form into
30 ;; (setq tramp-adb-program "/path/to/adb")
32 ;; Due to security it is not possible to access non-root devices.
39 (defvar dired-move-to-filename-regexp
)
41 (defcustom tramp-adb-program
"adb"
42 "Name of the Android Debug Bridge program."
48 (defconst tramp-adb-method
"adb"
49 "*When this method name is used, forward all calls to Android Debug Bridge.")
51 (defcustom tramp-adb-prompt
52 "^\\(?:[[:digit:]]*|?\\)?\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]"
53 "Regexp used as prompt in almquist shell."
58 (defconst tramp-adb-ls-date-regexp
59 "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]")
61 (defconst tramp-adb-ls-toolbox-regexp
63 "^[[:space:]]*\\([-[:alpha:]]+\\)" ; \1 permissions
64 "[[:space:]]*\\([^[:space:]]+\\)" ; \2 username
65 "[[:space:]]+\\([^[:space:]]+\\)" ; \3 group
66 "[[:space:]]+\\([[:digit:]]+\\)" ; \4 size
67 "[[:space:]]+\\([-[:digit:]]+[[:space:]][:[:digit:]]+\\)" ; \5 date
68 "[[:space:]]+\\(.*\\)$")) ; \6 filename
71 (add-to-list 'tramp-methods
73 (tramp-tmpdir "/data/local/tmp")))
76 (add-to-list 'tramp-default-host-alist
`(,tramp-adb-method nil
""))
79 (eval-after-load 'tramp
80 '(tramp-set-completion-function
81 tramp-adb-method
'((tramp-adb-parse-device-names ""))))
84 (add-to-list 'tramp-foreign-file-name-handler-alist
85 (cons 'tramp-adb-file-name-p
'tramp-adb-file-name-handler
))
87 (defconst tramp-adb-file-name-handler-alist
88 '((directory-file-name . tramp-handle-directory-file-name
)
89 (dired-uncache . tramp-handle-dired-uncache
)
90 (file-name-as-directory . tramp-handle-file-name-as-directory
)
91 (file-name-completion . tramp-handle-file-name-completion
)
92 (file-name-all-completions . tramp-adb-handle-file-name-all-completions
)
93 (file-attributes . tramp-adb-handle-file-attributes
)
94 (file-name-directory . tramp-handle-file-name-directory
)
95 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
96 (file-truename . tramp-adb-handle-file-truename
)
97 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
98 (file-name-as-directory . tramp-handle-file-name-as-directory
)
99 (file-regular-p . tramp-handle-file-regular-p
)
100 (file-remote-p . tramp-handle-file-remote-p
)
101 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
102 (file-directory-p . tramp-adb-handle-file-directory-p
)
103 (file-symlink-p . tramp-handle-file-symlink-p
)
104 ;; FIXME: This is too sloppy.
105 (file-executable-p . tramp-handle-file-exists-p
)
106 (file-exists-p . tramp-handle-file-exists-p
)
107 (file-readable-p . tramp-handle-file-exists-p
)
108 (file-writable-p . tramp-adb-handle-file-writable-p
)
109 (file-local-copy . tramp-adb-handle-file-local-copy
)
110 (file-modes . tramp-handle-file-modes
)
111 (expand-file-name . tramp-adb-handle-expand-file-name
)
112 (find-backup-file-name . tramp-handle-find-backup-file-name
)
113 (directory-files . tramp-handle-directory-files
)
114 (directory-files-and-attributes
115 . tramp-adb-handle-directory-files-and-attributes
)
116 (make-directory . tramp-adb-handle-make-directory
)
117 (delete-directory . tramp-adb-handle-delete-directory
)
118 (delete-file . tramp-adb-handle-delete-file
)
119 (load . tramp-handle-load
)
120 (insert-directory . tramp-adb-handle-insert-directory
)
121 (insert-file-contents . tramp-handle-insert-file-contents
)
122 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
123 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
124 (vc-registered . ignore
) ;no vc control files on Android devices
125 (write-region . tramp-adb-handle-write-region
)
126 (set-file-modes . tramp-adb-handle-set-file-modes
)
127 (set-file-times . tramp-adb-handle-set-file-times
)
128 (copy-file . tramp-adb-handle-copy-file
)
129 (rename-file . tramp-adb-handle-rename-file
)
130 (process-file . tramp-adb-handle-process-file
)
131 (shell-command . tramp-adb-handle-shell-command
)
132 (start-file-process . tramp-adb-handle-start-file-process
))
133 "Alist of handler functions for Tramp ADB method.")
135 ;; It must be a `defsubst' in order to push the whole code into
136 ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading.
138 (defsubst tramp-adb-file-name-p
(filename)
139 "Check if it's a filename for ADB."
140 (let ((v (tramp-dissect-file-name filename
)))
141 (string= (tramp-file-name-method v
) tramp-adb-method
)))
144 (defun tramp-adb-file-name-handler (operation &rest args
)
145 "Invoke the ADB handler for OPERATION.
146 First arg specifies the OPERATION, second arg is a list of arguments to
147 pass to the OPERATION."
148 (let ((fn (assoc operation tramp-adb-file-name-handler-alist
)))
150 (save-match-data (apply (cdr fn
) args
))
151 (tramp-run-real-handler operation args
))))
154 (defun tramp-adb-parse-device-names (ignore)
155 "Return a list of (nil host) tuples allowed to access."
158 ;; `call-process' does not react on timer under MS Windows.
159 ;; That's why we use `start-process'.
160 (let ((p (start-process
161 tramp-adb-program
(current-buffer) tramp-adb-program
"devices"))
163 (tramp-compat-set-process-query-on-exit-flag p nil
)
164 (while (eq 'run
(process-status p
))
166 (goto-char (point-min))
167 (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t
)
168 (add-to-list 'result
(list nil
(match-string 1))))
171 (defun tramp-adb-handle-expand-file-name (name &optional dir
)
172 "Like `expand-file-name' for Tramp files."
173 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
174 (setq dir
(or dir default-directory
"/"))
175 ;; Unless NAME is absolute, concat DIR and NAME.
176 (unless (file-name-absolute-p name
)
177 (setq name
(concat (file-name-as-directory dir
) name
)))
178 ;; If NAME is not a Tramp file, run the real handler.
179 (if (not (tramp-tramp-file-p name
))
180 (tramp-run-real-handler 'expand-file-name
(list name nil
))
182 (with-parsed-tramp-file-name name nil
183 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
184 (setq localname
(concat "/" localname
)))
185 ;; Do normal `expand-file-name' (this does "/./" and "/../").
186 ;; We bind `directory-sep-char' here for XEmacs on Windows,
187 ;; which would otherwise use backslash. `default-directory' is
188 ;; bound, because on Windows there would be problems with UNC
189 ;; shares or Cygwin mounts.
190 (let ((directory-sep-char ?
/)
191 (default-directory (tramp-compat-temporary-file-directory)))
192 (tramp-make-tramp-file-name
194 (tramp-drop-volume-letter
195 (tramp-run-real-handler
196 'expand-file-name
(list localname
))))))))
198 (defun tramp-adb-handle-file-directory-p (filename)
199 "Like `file-directory-p' for Tramp files."
200 (car (file-attributes (file-truename filename
))))
202 ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the
203 ;; code could be shared?
204 (defun tramp-adb-handle-file-truename (filename &optional counter prev-dirs
)
205 "Like `file-truename' for Tramp files."
206 (with-parsed-tramp-file-name (expand-file-name filename
) nil
207 (with-tramp-file-property v localname
"file-truename"
208 (let ((result nil
)) ; result steps in reverse order
209 (tramp-message v
4 "Finding true name for `%s'" filename
)
210 (let* ((directory-sep-char ?
/)
211 (steps (tramp-compat-split-string localname
"/"))
212 (localnamedir (tramp-run-real-handler
213 'file-name-as-directory
(list localname
)))
214 (is-dir (string= localname localnamedir
))
217 ;; Don't make the following value larger than
218 ;; necessary. People expect an error message in a
219 ;; timely fashion when something is wrong; otherwise
220 ;; they might think that Emacs is hung. Of course,
221 ;; correctness has to come first.
224 (while (and steps
(< numchase numchase-limit
))
225 (setq thisstep
(pop steps
))
229 (append '("") (reverse result
) (list thisstep
))
232 (nth 0 (file-attributes
233 (tramp-make-tramp-file-name
240 (cond ((string= "." thisstep
)
241 (tramp-message v
5 "Ignoring step `.'"))
242 ((string= ".." thisstep
)
243 (tramp-message v
5 "Processing step `..'")
245 ((stringp symlink-target
)
246 ;; It's a symlink, follow it.
247 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
248 (setq numchase
(1+ numchase
))
249 (when (file-name-absolute-p symlink-target
)
251 ;; If the symlink was absolute, we'll get a string
252 ;; like "/user@host:/some/target"; extract the
253 ;; "/some/target" part from it.
254 (when (tramp-tramp-file-p symlink-target
)
255 (unless (tramp-equal-remote filename symlink-target
)
258 "Symlink target `%s' on wrong host" symlink-target
))
259 (setq symlink-target localname
))
261 (append (tramp-compat-split-string
266 (setq result
(cons thisstep result
)))))
267 (when (>= numchase numchase-limit
)
270 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
271 (setq result
(reverse result
))
272 ;; Combine list to form string.
275 (mapconcat 'identity
(cons "" result
) "/")
277 (when (and is-dir
(or (string= "" result
)
278 (not (string= (substring result -
1) "/"))))
279 (setq result
(concat result
"/"))))
281 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
282 (tramp-make-tramp-file-name method user host result
)))))
284 (defun tramp-adb-handle-file-attributes (filename &optional id-format
)
285 "Like `file-attributes' for Tramp files."
286 (unless id-format
(setq id-format
'integer
))
288 (with-parsed-tramp-file-name filename nil
289 (with-tramp-file-property
290 v localname
(format "file-attributes-%s" id-format
)
291 (tramp-adb-barf-unless-okay
292 v
(format "%s -d -l %s"
293 (tramp-adb-get-ls-command v
)
294 (tramp-shell-quote-argument localname
)) "")
295 (with-current-buffer (tramp-get-buffer v
)
296 (tramp-adb-sh-fix-ls-output)
297 (cdar (tramp-do-parse-file-attributes-with-ls v id-format
)))))))
299 (defun tramp-do-parse-file-attributes-with-ls (vec &optional id-format
)
300 "Parse `file-attributes' for Tramp files using the ls(1) command."
301 (with-current-buffer (tramp-get-buffer vec
)
302 (goto-char (point-min))
303 (let ((file-properties nil
))
304 (while (re-search-forward tramp-adb-ls-toolbox-regexp nil t
)
305 (let* ((mod-string (match-string 1))
306 (is-dir (eq ?d
(aref mod-string
0)))
307 (is-symlink (eq ?l
(aref mod-string
0)))
308 (uid (match-string 2))
309 (gid (match-string 3))
310 (size (string-to-number (match-string 4)))
311 (date (match-string 5))
312 (name (match-string 6))
315 (cadr (split-string name
"\\( -> \\|\n\\)")))))
318 (car (split-string name
"\\( -> \\|\n\\)"))
320 (or is-dir symlink-target
)
322 ;; no way to handle numeric ids in Androids ash
323 (if (eq id-format
'integer
) 0 uid
)
324 (if (eq id-format
'integer
) 0 gid
)
326 (date-to-time date
) ; mtime
332 (tramp-get-device vec
))
336 (defun tramp-adb-handle-directory-files-and-attributes
337 (directory &optional full match nosort id-format
)
338 "Like `directory-files-and-attributes' for Tramp files."
339 (when (file-directory-p directory
)
340 (with-parsed-tramp-file-name (expand-file-name directory
) nil
341 (with-tramp-file-property
342 v localname
(format "directory-files-attributes-%s-%s-%s-%s"
343 full match id-format nosort
)
344 (tramp-adb-barf-unless-okay
345 v
(format "%s -a -l %s"
346 (tramp-adb-get-ls-command v
)
347 (tramp-shell-quote-argument localname
)) "")
348 (with-current-buffer (tramp-get-buffer v
)
349 (tramp-adb-sh-fix-ls-output)
350 (let ((result (tramp-do-parse-file-attributes-with-ls
351 v
(or id-format
'integer
))))
356 (cons (expand-file-name (car x
) directory
) (cdr x
)))
360 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))
363 (if (or (not match
) (string-match match
(car x
)))
367 (defun tramp-adb-get-ls-command (vec)
368 (with-tramp-connection-property vec
"ls"
369 (tramp-message vec
5 "Finding a suitable `ls' command")
370 (if (zerop (tramp-adb-command-exit-status
371 vec
"ls --color=never -al /dev/null"))
372 ;; On CyanogenMod based system BusyBox is used and "ls" output
373 ;; coloring is enabled by default. So we try to disable it
378 (defun tramp-adb--gnu-switches-to-ash
380 "Almquist shell can't handle multiple arguments.
381 Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"."
385 (replace-regexp-in-string
387 (replace-regexp-in-string "^-" "" s
)))
388 ;; FIXME: Warning about removed switches (long and non-dash).
392 (and (not (string-match "\\(^--\\|^[^-]\\)" s
)) s
))
395 (defun tramp-adb-handle-insert-directory
396 (filename switches
&optional wildcard full-directory-p
)
397 "Like `insert-directory' for Tramp files."
398 (when (stringp switches
)
399 (setq switches
(tramp-adb--gnu-switches-to-ash (split-string switches
))))
400 (with-parsed-tramp-file-name (file-truename filename
) nil
401 (with-current-buffer (tramp-get-buffer v
)
402 (let ((name (tramp-shell-quote-argument (directory-file-name localname
)))
403 (switch-d (member "-d" switches
))
404 (switch-t (member "-t" switches
))
405 (switches (mapconcat 'identity
(remove "-t" switches
) " ")))
406 (tramp-adb-barf-unless-okay
407 v
(format "%s %s %s" (tramp-adb-get-ls-command v
) switches name
)
408 "Cannot insert directory listing: %s" filename
)
410 ;; We insert also filename/. and filename/.., because "ls" doesn't.
411 (narrow-to-region (point) (point))
413 (tramp-adb-barf-unless-okay
414 v
(format "%s -d %s %s %s"
415 (tramp-adb-get-ls-command v
)
417 (concat (file-name-as-directory name
) ".")
418 (concat (file-name-as-directory name
) ".."))
419 "Cannot insert directory listing: %s" filename
))
421 (tramp-adb-sh-fix-ls-output switch-t
)))
422 (insert-buffer-substring (tramp-get-buffer v
))))
424 (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time
)
425 "Insert dummy 0 in empty size columns.
426 Androids \"ls\" command doesn't insert size column for directories:
427 Emacs dired can't find files."
429 ;; Insert missing size.
430 (goto-char (point-min))
432 (search-forward-regexp
433 "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t
)
434 (replace-match "0\\1" "\\1" nil
)
435 ;; Insert missing "/".
436 (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$")
440 (let* ((lines (split-string (buffer-string) "\n" t
))
445 'tramp-adb-ls-output-time-less-p
446 'tramp-adb-ls-output-name-less-p
))))
447 (delete-region (point-min) (point-max))
448 (insert " " (mapconcat 'identity sorted-lines
"\n ")))
449 ;; Add final newline.
450 (goto-char (point-max))
451 (unless (= (point) (line-beginning-position))
455 (defun tramp-adb-ls-output-time-less-p (a b
)
456 "Sort \"ls\" output by time, descending."
458 (string-match tramp-adb-ls-date-regexp a
)
459 (setq time-a
(apply 'encode-time
(parse-time-string (match-string 0 a
))))
460 (string-match tramp-adb-ls-date-regexp b
)
461 (setq time-b
(apply 'encode-time
(parse-time-string (match-string 0 b
))))
462 (tramp-time-less-p time-b time-a
)))
464 (defun tramp-adb-ls-output-name-less-p (a b
)
465 "Sort \"ls\" output by name, ascending."
467 (string-match dired-move-to-filename-regexp a
)
468 (setq posa
(match-end 0))
469 (string-match dired-move-to-filename-regexp b
)
470 (setq posb
(match-end 0))
471 (string-lessp (substring a posa
) (substring b posb
))))
473 (defun tramp-adb-handle-make-directory (dir &optional parents
)
474 "Like `make-directory' for Tramp files."
475 (setq dir
(expand-file-name dir
))
476 (with-parsed-tramp-file-name dir nil
478 (let ((par (expand-file-name ".." dir
)))
479 (unless (file-directory-p par
)
480 (make-directory par parents
))))
481 (tramp-adb-barf-unless-okay
482 v
(format "mkdir %s" (tramp-shell-quote-argument localname
))
483 "Couldn't make directory %s" dir
)
484 (tramp-flush-directory-property v
(file-name-directory localname
))))
486 (defun tramp-adb-handle-delete-directory (directory &optional recursive
)
487 "Like `delete-directory' for Tramp files."
488 (setq directory
(expand-file-name directory
))
489 (with-parsed-tramp-file-name directory nil
490 (tramp-flush-file-property v
(file-name-directory localname
))
491 (tramp-flush-directory-property v localname
)
492 (tramp-adb-barf-unless-okay
494 (if recursive
"rm -r" "rmdir")
495 (tramp-shell-quote-argument localname
))
496 "Couldn't delete %s" directory
)))
498 (defun tramp-adb-handle-delete-file (filename &optional trash
)
499 "Like `delete-file' for Tramp files."
500 (setq filename
(expand-file-name filename
))
501 (with-parsed-tramp-file-name filename nil
502 (tramp-flush-file-property v
(file-name-directory localname
))
503 (tramp-flush-file-property v localname
)
504 (tramp-adb-barf-unless-okay
505 v
(format "rm %s" (tramp-shell-quote-argument localname
))
506 "Couldn't delete %s" filename
)))
508 (defun tramp-adb-handle-file-name-all-completions (filename directory
)
509 "Like `file-name-all-completions' for Tramp files."
512 (with-parsed-tramp-file-name directory nil
513 (with-tramp-file-property v localname
"file-name-all-completions"
515 (tramp-adb-send-command
517 (tramp-adb-get-ls-command v
)
518 (tramp-shell-quote-argument localname
)))
521 (if (file-directory-p f
)
522 (file-name-as-directory f
)
524 (with-current-buffer (tramp-get-buffer v
)
528 (lambda (l) (and (not (string-match "^[[:space:]]*$" l
)) l
))
529 (split-string (buffer-string) "\n"))))))))))
531 (defun tramp-adb-handle-file-local-copy (filename)
532 "Like `file-local-copy' for Tramp files."
533 (with-parsed-tramp-file-name filename nil
534 (unless (file-exists-p (file-truename filename
))
537 "Cannot make local copy of non-existing file `%s'" filename
))
538 (let ((tmpfile (tramp-compat-make-temp-file filename
)))
539 (with-tramp-progress-reporter
540 v
3 (format "Fetching %s to tmp file %s" filename tmpfile
)
541 (when (tramp-adb-execute-adb-command v
"pull" localname tmpfile
)
542 (delete-file tmpfile
)
544 v
'file-error
"Cannot make local copy of file `%s'" filename
))
545 (set-file-modes tmpfile
(file-modes filename
)))
548 (defun tramp-adb-handle-file-writable-p (filename)
549 "Like `tramp-sh-handle-file-writable-p'.
550 But handle the case, if the \"test\" command is not available."
551 (with-parsed-tramp-file-name filename nil
552 (with-tramp-file-property v localname
"file-writable-p"
553 (if (tramp-adb-find-test-command v
)
554 (if (file-exists-p filename
)
556 (tramp-adb-command-exit-status
557 v
(format "test -w %s" (tramp-shell-quote-argument localname
))))
559 (file-directory-p (file-name-directory filename
))
560 (file-writable-p (file-name-directory filename
))))
562 ;; Missing "test" command on Android < 4.
563 (let ((rw-path "/data/data"))
566 "Not implemented yet (assuming \"/data/data\" is writable): %s"
568 (and (>= (length localname
) (length rw-path
))
569 (string= (substring localname
0 (length rw-path
))
572 (defun tramp-adb-handle-write-region
573 (start end filename
&optional append visit lockname confirm
)
574 "Like `write-region' for Tramp files."
575 (setq filename
(expand-file-name filename
))
576 (with-parsed-tramp-file-name filename nil
579 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
580 (when (and confirm
(file-exists-p filename
))
581 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
583 (tramp-error v
'file-error
"File not overwritten")))
584 ;; We must also flush the cache of the directory, because
585 ;; `file-attributes' reads the values from there.
586 (tramp-flush-file-property v
(file-name-directory localname
))
587 (tramp-flush-file-property v localname
)
588 (let* ((curbuf (current-buffer))
589 (tmpfile (tramp-compat-make-temp-file filename
)))
590 (tramp-run-real-handler
592 (list start end tmpfile append
'no-message lockname confirm
))
593 (with-tramp-progress-reporter
594 v
3 (format "Moving tmp file %s to %s" tmpfile filename
)
596 (when (tramp-adb-execute-adb-command v
"push" tmpfile localname
)
597 (tramp-error v
'file-error
"Cannot write: `%s' filename"))
598 (delete-file tmpfile
)))
600 (unless (equal curbuf
(current-buffer))
603 "Buffer has changed from `%s' to `%s'" curbuf
(current-buffer))))))
605 (defun tramp-adb-handle-set-file-modes (filename mode
)
606 "Like `set-file-modes' for Tramp files."
607 (with-parsed-tramp-file-name filename nil
608 (tramp-flush-file-property v localname
)
609 (tramp-adb-barf-unless-okay
610 v
(format "chmod %s %s" (tramp-compat-decimal-to-octal mode
) localname
)
611 "Error while changing file's mode %s" filename
)))
613 (defun tramp-adb-handle-set-file-times (filename &optional time
)
614 "Like `set-file-times' for Tramp files."
615 (with-parsed-tramp-file-name filename nil
616 (tramp-flush-file-property v localname
)
617 (let ((time (if (or (null time
) (equal time
'(0 0)))
620 (tramp-adb-command-exit-status
621 ;; use shell arithmetic because of Emacs integer size limit
622 v
(format "touch -t $(( %d * 65536 + %d )) %s"
623 (car time
) (cadr time
)
624 (tramp-shell-quote-argument localname
))))))
626 (defun tramp-adb-handle-copy-file
627 (filename newname
&optional ok-if-already-exists keep-date
628 preserve-uid-gid preserve-extended-attributes
)
629 "Like `copy-file' for Tramp files.
630 PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
631 (setq filename
(expand-file-name filename
)
632 newname
(expand-file-name newname
))
634 (if (file-directory-p filename
)
635 (tramp-file-name-handler 'copy-directory filename newname keep-date t
)
636 (with-tramp-progress-reporter
637 (tramp-dissect-file-name (if (file-remote-p filename
) filename newname
))
638 0 (format "Copying %s to %s" filename newname
)
640 (let ((tmpfile (file-local-copy filename
)))
645 (rename-file tmpfile newname ok-if-already-exists
)
647 (delete-file tmpfile
)
648 (signal (car err
) (cdr err
))))
651 (when (file-directory-p newname
)
653 (expand-file-name (file-name-nondirectory filename
) newname
)))
655 (with-parsed-tramp-file-name newname nil
656 (when (and (not ok-if-already-exists
)
657 (file-exists-p newname
))
658 (tramp-error v
'file-already-exists newname
))
660 ;; We must also flush the cache of the directory, because
661 ;; `file-attributes' reads the values from there.
662 (tramp-flush-file-property v
(file-name-directory localname
))
663 (tramp-flush-file-property v localname
)
664 (when (tramp-adb-execute-adb-command v
"push" filename localname
)
666 v
'file-error
"Cannot copy `%s' `%s'" filename newname
))))))
668 ;; KEEP-DATE handling.
670 (set-file-times newname
(nth 5 (file-attributes filename
))))))
672 (defun tramp-adb-handle-rename-file
673 (filename newname
&optional ok-if-already-exists
)
674 "Like `rename-file' for Tramp files."
675 (setq filename
(expand-file-name filename
)
676 newname
(expand-file-name newname
))
678 (with-parsed-tramp-file-name
679 (if (file-remote-p filename
) filename newname
) nil
680 (with-tramp-progress-reporter
681 v
0 (format "Renaming %s to %s" newname filename
)
683 (if (and (tramp-equal-remote filename newname
)
684 (not (file-directory-p filename
)))
686 (when (and (not ok-if-already-exists
)
687 (file-exists-p newname
))
688 (tramp-error v
'file-already-exists newname
))
689 ;; We must also flush the cache of the directory, because
690 ;; `file-attributes' reads the values from there.
691 (tramp-flush-file-property v
(file-name-directory localname
))
692 (tramp-flush-file-property v localname
)
694 (tramp-adb-barf-unless-okay
697 (tramp-file-name-handler 'file-remote-p filename
'localname
)
699 "Error renaming %s to %s" filename newname
))
702 (copy-file filename newname ok-if-already-exists t t
)
703 (delete-file filename
)))))
705 (defun tramp-adb-handle-process-file
706 (program &optional infile destination display
&rest args
)
707 "Like `process-file' for Tramp files."
708 ;; The implementation is not complete yet.
709 (when (and (numberp destination
) (zerop destination
))
710 (error "Implementation does not handle immediate return"))
712 (with-parsed-tramp-file-name default-directory nil
713 (let (command input tmpinput stderr tmpstderr outbuf ret
)
715 (setq command
(mapconcat 'tramp-shell-quote-argument
716 (cons program args
) " "))
719 (setq input
"/dev/null")
720 (setq infile
(expand-file-name infile
))
721 (if (tramp-equal-remote default-directory infile
)
722 ;; INFILE is on the same remote host.
723 (setq input
(with-parsed-tramp-file-name infile nil localname
))
724 ;; INFILE must be copied to remote host.
725 (setq input
(tramp-make-tramp-temp-file v
)
726 tmpinput
(tramp-make-tramp-file-name method user host input
))
727 (copy-file infile tmpinput t
)))
728 (when input
(setq command
(format "%s <%s" command input
)))
733 ((bufferp destination
)
734 (setq outbuf destination
))
736 ((stringp destination
)
737 (setq outbuf
(get-buffer-create destination
)))
738 ;; (REAL-DESTINATION ERROR-DESTINATION)
742 ((bufferp (car destination
))
743 (setq outbuf
(car destination
)))
744 ((stringp (car destination
))
745 (setq outbuf
(get-buffer-create (car destination
))))
747 (setq outbuf
(current-buffer))))
750 ((stringp (cadr destination
))
751 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
752 (if (tramp-equal-remote default-directory
(cadr destination
))
753 ;; stderr is on the same remote host.
754 (setq stderr
(with-parsed-tramp-file-name
755 (cadr destination
) nil localname
))
756 ;; stderr must be copied to remote host. The temporary
757 ;; file must be deleted after execution.
758 (setq stderr
(tramp-make-tramp-temp-file v
)
759 tmpstderr
(tramp-make-tramp-file-name
760 method user host stderr
))))
761 ;; stderr to be discarded.
762 ((null (cadr destination
))
763 (setq stderr
"/dev/null"))))
766 (setq outbuf
(current-buffer))))
767 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
769 ;; Send the command. It might not return in time, so we protect
770 ;; it. Call it in a subshell, in order to preserve working
775 (tramp-adb-barf-unless-okay
776 v
(format "(cd %s; %s)"
777 (tramp-shell-quote-argument localname
) command
)
779 ;; We should show the output anyway.
781 (with-current-buffer outbuf
782 (insert-buffer-substring (tramp-get-connection-buffer v
)))
783 (when display
(display-buffer outbuf
))))
784 ;; When the user did interrupt, we should do it also. We use
785 ;; return code -1 as marker.
787 (kill-buffer (tramp-get-connection-buffer v
))
791 (kill-buffer (tramp-get-connection-buffer v
))
794 ;; Provide error file.
795 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
797 ;; Cleanup. We remove all file cache values for the connection,
798 ;; because the remote process could have changed them.
799 (when tmpinput
(delete-file tmpinput
))
801 ;; `process-file-side-effects' has been introduced with GNU
802 ;; Emacs 23.2. If set to `nil', no remote file will be changed
803 ;; by `program'. If it doesn't exist, we assume its default
805 (unless (and (boundp 'process-file-side-effects
)
806 (not (symbol-value 'process-file-side-effects
)))
807 (tramp-flush-directory-property v
""))
809 ;; Return exit status.
814 (defun tramp-adb-handle-shell-command
815 (command &optional output-buffer error-buffer
)
816 "Like `shell-command' for Tramp files."
817 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
818 ;; We cannot use `shell-file-name' and `shell-command-switch',
819 ;; they are variables of the local host.
820 (args (list "sh" "-c" (substring command
0 asynchronous
)))
824 ((bufferp output-buffer
) output-buffer
)
825 ((stringp output-buffer
) (get-buffer-create output-buffer
))
827 (setq current-buffer-p t
)
829 (t (get-buffer-create
831 "*Async Shell Command*"
832 "*Shell Command Output*")))))
835 ((bufferp error-buffer
) error-buffer
)
836 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
838 (if (and (not asynchronous
) error-buffer
)
839 (with-parsed-tramp-file-name default-directory nil
840 (list output-buffer
(tramp-make-tramp-temp-file v
)))
842 (p (get-buffer-process output-buffer
)))
844 ;; Check whether there is another process running. Tramp does not
845 ;; support 2 (asynchronous) processes in parallel.
847 (if (yes-or-no-p "A command is running. Kill it? ")
848 (ignore-errors (kill-process p
))
849 (tramp-compat-user-error "Shell command in progress")))
853 (barf-if-buffer-read-only)
855 (with-current-buffer output-buffer
856 (setq buffer-read-only nil
)
859 (if (and (not current-buffer-p
) (integerp asynchronous
))
862 (apply 'start-file-process
"*Async Shell*" buffer args
)
864 (pop-to-buffer output-buffer
)
865 (setq mode-line-process
'(":%s"))
870 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
871 ;; Insert error messages if they were separated.
873 (with-current-buffer error-buffer
874 (insert-file-contents (cadr buffer
)))
875 (delete-file (cadr buffer
)))
877 ;; This is like exchange-point-and-mark, but doesn't
878 ;; activate the mark. It is cleaner to avoid activation,
879 ;; even though the command loop would deactivate the mark
880 ;; because we inserted text.
881 (goto-char (prog1 (mark t
)
882 (set-marker (mark-marker) (point)
884 ;; There's some output, display it.
885 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
886 (if (functionp 'display-message-or-buffer
)
887 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
888 (pop-to-buffer output-buffer
))))))))
890 ;; We use BUFFER also as connection buffer during setup. Because of
891 ;; this, its original contents must be saved, and restored once
892 ;; connection has been setup.
893 (defun tramp-adb-handle-start-file-process (name buffer program
&rest args
)
894 "Like `start-file-process' for Tramp files."
895 (with-parsed-tramp-file-name default-directory nil
896 ;; When PROGRAM is nil, we should provide a tty. This is not
898 (unless (stringp program
)
899 (tramp-error v
'file-error
"PROGRAM must be a string"))
903 (tramp-shell-quote-argument localname
)
904 (mapconcat 'tramp-shell-quote-argument
905 (cons program args
) " ")))
906 (tramp-process-connection-type
907 (or (null program
) tramp-process-connection-type
))
908 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
913 ;; BUFFER can be nil. We use a temporary buffer.
914 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
915 (while (get-process name1
)
916 ;; NAME must be unique as process name.
918 name1
(format "%s<%d>" name i
)))
920 ;; Set the new process properties.
921 (tramp-set-connection-property v
"process-name" name
)
922 (tramp-set-connection-property v
"process-buffer" buffer
)
924 (with-current-buffer (tramp-get-connection-buffer v
)
926 ;; We catch this event. Otherwise, `start-process' could
927 ;; be called on the local host.
930 ;; Activate narrowing in order to save BUFFER
931 ;; contents. Clear also the modification time;
932 ;; otherwise we might be interrupted by
933 ;; `verify-visited-file-modtime'.
934 (let ((buffer-undo-list t
)
935 (buffer-read-only nil
)
937 (clear-visited-file-modtime)
938 (narrow-to-region (point-max) (point-max))
939 ;; We call `tramp-adb-maybe-open-connection', in
940 ;; order to cleanup the prompt afterwards.
941 (tramp-adb-maybe-open-connection v
)
943 (delete-region mark
(point))
944 (narrow-to-region (point-max) (point-max))
946 (let ((tramp-adb-prompt (regexp-quote command
)))
947 (tramp-adb-send-command v command
))
948 (let ((p (tramp-get-connection-process v
)))
949 ;; Set query flag and process marker for this
950 ;; process. We ignore errors, because the process
951 ;; could have finished already.
953 (tramp-compat-set-process-query-on-exit-flag p t
)
954 (set-marker (process-mark p
) (point)))
959 (if (string-match tramp-temp-buffer-name
(buffer-name))
961 (set-process-buffer (tramp-get-connection-process v
) nil
)
962 (kill-buffer (current-buffer)))
963 (set-buffer-modified-p bmp
))
964 (tramp-set-connection-property v
"process-name" nil
)
965 (tramp-set-connection-property v
"process-buffer" nil
))))))
969 (defun tramp-adb-execute-adb-command (vec &rest args
)
970 "Returns nil on success error-output on failure."
971 (when (> (length (tramp-file-name-host vec
)) 0)
972 (setq args
(append (list "-s" (tramp-file-name-host vec
)) args
)))
976 (zerop (apply 'tramp-call-process tramp-adb-program nil t nil args
))
978 (tramp-message vec
6 "%s" (buffer-string)))))
980 (defun tramp-adb-find-test-command (vec)
981 "Checks, whether the ash has a builtin \"test\" command.
982 This happens for Android >= 4.0."
983 (with-tramp-connection-property vec
"test"
984 (zerop (tramp-adb-command-exit-status vec
"type test"))))
986 ;; Connection functions
988 (defun tramp-adb-send-command (vec command
)
989 "Send the COMMAND to connection VEC."
990 (tramp-adb-maybe-open-connection vec
)
991 (tramp-message vec
6 "%s" command
)
992 (tramp-send-string vec command
)
993 ;; fixme: Race condition
994 (tramp-adb-wait-for-output (tramp-get-connection-process vec
))
995 (with-current-buffer (tramp-get-connection-buffer vec
)
997 (goto-char (point-min))
998 ;; We can't use stty to disable echo of command.
999 (delete-matching-lines (regexp-quote command
))
1000 ;; When the local machine is W32, there are still trailing ^M.
1001 ;; There must be a better solution by setting the correct coding
1002 ;; system, but this requires changes in core Tramp.
1003 (goto-char (point-min))
1004 (while (re-search-forward "\r+$" nil t
)
1005 (replace-match "" nil nil
)))))
1007 (defun tramp-adb-command-exit-status
1009 "Run COMMAND and return its exit status.
1010 Sends `echo $?' along with the COMMAND for checking the exit status. If
1011 COMMAND is nil, just sends `echo $?'. Returns the exit status found."
1012 (tramp-adb-send-command
1014 (format "%s; echo tramp_exit_status $?" command
)
1015 "echo tramp_exit_status $?"))
1016 (with-current-buffer (tramp-get-connection-buffer vec
)
1017 (goto-char (point-max))
1018 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
1020 vec
'file-error
"Couldn't find exit status of `%s'" command
))
1021 (skip-chars-forward "^ ")
1023 (read (current-buffer))
1024 (let (buffer-read-only)
1025 (delete-region (match-beginning 0) (point-max))))))
1027 (defun tramp-adb-barf-unless-okay (vec command fmt
&rest args
)
1028 "Run COMMAND, check exit status, throw error if exit status not okay.
1029 FMT and ARGS are passed to `error'."
1030 (unless (zerop (tramp-adb-command-exit-status vec command
))
1031 (apply 'tramp-error vec
'file-error fmt args
)))
1033 (defun tramp-adb-wait-for-output (proc &optional timeout
)
1034 "Wait for output from remote command."
1035 (unless (buffer-live-p (process-buffer proc
))
1036 (delete-process proc
)
1037 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
1038 (with-current-buffer (process-buffer proc
)
1039 (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt
)
1040 (let (buffer-read-only)
1041 (goto-char (point-min))
1042 ;; ADB terminal sends "^H" sequences.
1043 (when (re-search-forward "<\b+" (point-at-eol) t
)
1045 (delete-region (point-min) (point)))
1046 ;; Delete the prompt.
1047 (goto-char (point-min))
1048 (when (re-search-forward tramp-adb-prompt
(point-at-eol) t
)
1050 (delete-region (point-min) (point)))
1051 (goto-char (point-max))
1052 (re-search-backward tramp-adb-prompt nil t
)
1053 (delete-region (point) (point-max)))
1057 "[[Remote adb prompt `%s' not found in %d secs]]"
1058 tramp-adb-prompt timeout
)
1061 "[[Remote prompt `%s' not found]]" tramp-adb-prompt
)))))
1063 (defun tramp-adb-maybe-open-connection (vec)
1064 "Maybe open a connection VEC.
1065 Does not do anything if a connection is already open, but re-opens the
1066 connection if a previous connection has died for some reason."
1067 (let* ((buf (tramp-get-connection-buffer vec
))
1068 (p (get-buffer-process buf
))
1069 (host (tramp-file-name-host vec
))
1070 (user (tramp-file-name-user vec
))
1071 (devices (mapcar 'cadr
(tramp-adb-parse-device-names nil
))))
1073 ;; Maybe we know already that "su" is not supported. We cannot
1074 ;; use a connection property, because we have not checked yet
1075 ;; whether it is still the same device.
1076 (when (and user
(not (tramp-get-file-property vec
"" "su-command-p" t
)))
1077 (tramp-error vec
'file-error
"Cannot switch to user `%s'" user
))
1080 (and p
(processp p
) (memq (process-status p
) '(run open
)))
1082 (when (and p
(processp p
)) (delete-process p
))
1084 (tramp-error vec
'file-error
"No device connected"))
1085 (if (and (> (length host
) 0) (not (member host devices
)))
1086 (tramp-error vec
'file-error
"Device %s not connected" host
))
1087 (if (and (> (length devices
) 1) (zerop (length host
)))
1090 "Multiple Devices connected: No Host/Device specified"))
1091 (with-tramp-progress-reporter vec
3 "Opening adb shell connection"
1092 (let* ((coding-system-for-read 'utf-8-dos
) ;is this correct?
1093 (process-connection-type tramp-process-connection-type
)
1094 (args (if (> (length host
) 0)
1095 (list "-s" host
"shell")
1097 (p (let ((default-directory
1098 (tramp-compat-temporary-file-directory)))
1099 (apply 'start-process
(tramp-get-connection-name vec
) buf
1100 tramp-adb-program args
))))
1102 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
1103 ;; Wait for initial prompt.
1104 (tramp-adb-wait-for-output p
30)
1105 (unless (eq 'run
(process-status p
))
1106 (tramp-error vec
'file-error
"Terminated!"))
1107 (tramp-compat-set-process-query-on-exit-flag p nil
)
1109 ;; Check whether the properties have been changed. If
1110 ;; yes, this is a strong indication that we must expire all
1111 ;; connection properties. We start again.
1112 (tramp-message vec
5 "Checking system information")
1113 (tramp-adb-send-command
1114 vec
"echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
1116 (tramp-get-connection-property vec
"getprop" nil
))
1118 (tramp-set-connection-property
1120 (with-current-buffer (tramp-get-connection-buffer vec
)
1121 ;; Read the expression.
1122 (goto-char (point-min))
1123 (read (current-buffer))))))
1124 (when (and (stringp old-getprop
)
1125 (not (string-equal old-getprop new-getprop
)))
1129 "Connection reset, because remote host changed from `%s' to `%s'"
1130 old-getprop new-getprop
)
1131 (tramp-adb-maybe-open-connection vec
)))
1133 ;; Change user if indicated.
1135 (tramp-adb-send-command vec
(format "su %s" user
))
1136 (unless (zerop (tramp-adb-command-exit-status vec nil
))
1138 (tramp-set-file-property vec
"" "su-command-p" nil
)
1140 vec
'file-error
"Cannot switch to user `%s'" user
)))
1142 ;; Set "remote-path" connection property. This is needed
1144 (tramp-adb-send-command vec
"echo \\\"$PATH\\\"")
1145 (tramp-set-connection-property
1148 (with-current-buffer (tramp-get-connection-buffer vec
)
1149 ;; Read the expression.
1150 (goto-char (point-min))
1151 (read (current-buffer)))
1152 ":" 'omit-nulls
))))))))
1154 (provide 'tramp-adb
)
1155 ;;; tramp-adb.el ends here