diff options
| -rw-r--r-- | lisp/ChangeLog | 4 | ||||
| -rw-r--r-- | lisp/net/tramp-adb.el | 979 |
2 files changed, 983 insertions, 0 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index b7b24526ebf..849e9947754 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | 2012-12-04 Juergen Hoetzel <juergen@archlinux.org> | ||
| 2 | |||
| 3 | * net/tramp-adb.el: New package. | ||
| 4 | |||
| 1 | 2012-12-04 Chong Yidong <cyd@gnu.org> | 5 | 2012-12-04 Chong Yidong <cyd@gnu.org> |
| 2 | 6 | ||
| 3 | * terminal.el: Move to obsolete/. | 7 | * terminal.el: Move to obsolete/. |
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el new file mode 100644 index 00000000000..a56ccd98e47 --- /dev/null +++ b/lisp/net/tramp-adb.el | |||
| @@ -0,0 +1,979 @@ | |||
| 1 | ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp | ||
| 2 | |||
| 3 | ;; Copyright (C) 2011, 2012 Free Software Foundation, Inc. | ||
| 4 | |||
| 5 | ;; Author: Juergen Hoetzel <juergen@archlinux.org> | ||
| 6 | ;; Keywords: comm, processes | ||
| 7 | ;; Package: tramp | ||
| 8 | |||
| 9 | ;; This file is part of GNU Emacs. | ||
| 10 | |||
| 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. | ||
| 15 | |||
| 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. | ||
| 20 | |||
| 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/>. | ||
| 23 | |||
| 24 | ;;; Commentary: | ||
| 25 | |||
| 26 | ;; The Android Debug Bridge must be installed on your local machine. | ||
| 27 | ;; Add the following form into your .emacs: | ||
| 28 | ;; | ||
| 29 | ;; (setq tramp-adb-sdk-dir "/path/to/android/sdk") | ||
| 30 | ;; | ||
| 31 | ;; Due to security it is not possible to access non-root devices. | ||
| 32 | |||
| 33 | ;;; Code: | ||
| 34 | |||
| 35 | (require 'tramp) | ||
| 36 | |||
| 37 | (defvar dired-move-to-filename-regexp) | ||
| 38 | |||
| 39 | (defcustom tramp-adb-sdk-dir "~/Android/sdk" | ||
| 40 | "Set to the directory containing the Android SDK." | ||
| 41 | :type 'string | ||
| 42 | :group 'tramp-adb) | ||
| 43 | |||
| 44 | ;;;###tramp-autoload | ||
| 45 | (defconst tramp-adb-method "adb" | ||
| 46 | "*When this method name is used, forward all calls to Android Debug Bridge.") | ||
| 47 | |||
| 48 | (defcustom tramp-adb-prompt "^\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]" | ||
| 49 | "Regexp used as prompt in ADB shell." | ||
| 50 | :type 'string | ||
| 51 | :group 'tramp-adb) | ||
| 52 | |||
| 53 | (defconst tramp-adb-ls-date-regexp "[[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]][0-9][0-9]:[0-9][0-9][[:space:]]") | ||
| 54 | |||
| 55 | ;;;###tramp-autoload | ||
| 56 | (add-to-list 'tramp-methods `(,tramp-adb-method)) | ||
| 57 | |||
| 58 | ;;;###tramp-autoload | ||
| 59 | (eval-after-load 'tramp | ||
| 60 | '(tramp-set-completion-function | ||
| 61 | tramp-adb-method '((tramp-adb-parse-device-names "")))) | ||
| 62 | |||
| 63 | ;;;###tramp-autoload | ||
| 64 | (add-to-list 'tramp-foreign-file-name-handler-alist | ||
| 65 | (cons 'tramp-adb-file-name-p 'tramp-adb-file-name-handler)) | ||
| 66 | |||
| 67 | (defconst tramp-adb-file-name-handler-alist | ||
| 68 | '((directory-file-name . tramp-handle-directory-file-name) | ||
| 69 | (dired-uncache . tramp-handle-dired-uncache) | ||
| 70 | (file-name-as-directory . tramp-handle-file-name-as-directory) | ||
| 71 | (file-name-completion . tramp-handle-file-name-completion) | ||
| 72 | (file-name-all-completions . tramp-adb-handle-file-name-all-completions) | ||
| 73 | (file-attributes . tramp-adb-handle-file-attributes) | ||
| 74 | (file-name-directory . tramp-handle-file-name-directory) | ||
| 75 | (file-name-nondirectory . tramp-handle-file-name-nondirectory) | ||
| 76 | (file-truename . tramp-adb-handle-file-truename) | ||
| 77 | (file-newer-than-file-p . tramp-handle-file-newer-than-file-p) | ||
| 78 | (file-name-as-directory . tramp-handle-file-name-as-directory) | ||
| 79 | (file-regular-p . tramp-handle-file-regular-p) | ||
| 80 | (file-remote-p . tramp-handle-file-remote-p) | ||
| 81 | (file-directory-p . tramp-adb-handle-file-directory-p) | ||
| 82 | (file-symlink-p . tramp-handle-file-symlink-p) | ||
| 83 | ;; FIXME: This is too sloppy. | ||
| 84 | (file-executable-p . file-exists-p) | ||
| 85 | (file-exists-p . tramp-adb-handle-file-exists-p) | ||
| 86 | (file-readable-p . tramp-handle-file-exists-p) | ||
| 87 | (file-writable-p . tramp-adb-handle-file-writable-p) | ||
| 88 | (file-local-copy . tramp-adb-handle-file-local-copy) | ||
| 89 | (file-modes . tramp-handle-file-modes) | ||
| 90 | (expand-file-name . tramp-adb-handle-expand-file-name) | ||
| 91 | (find-backup-file-name . tramp-handle-find-backup-file-name) | ||
| 92 | (directory-files . tramp-handle-directory-files) | ||
| 93 | (make-directory . tramp-adb-handle-make-directory) | ||
| 94 | (delete-directory . tramp-adb-handle-delete-directory) | ||
| 95 | (delete-file . tramp-adb-handle-delete-file) | ||
| 96 | (load . tramp-handle-load) | ||
| 97 | (insert-directory . tramp-adb-handle-insert-directory) | ||
| 98 | (insert-file-contents . tramp-handle-insert-file-contents) | ||
| 99 | (substitute-in-file-name . tramp-handle-substitute-in-file-name) | ||
| 100 | (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory) | ||
| 101 | (vc-registered . ignore) ;no vc control files on Android devices | ||
| 102 | (write-region . tramp-adb-handle-write-region) | ||
| 103 | (set-file-modes . tramp-adb-handle-set-file-modes) | ||
| 104 | (set-file-times . ignore) | ||
| 105 | (copy-file . tramp-adb-handle-copy-file) | ||
| 106 | (rename-file . tramp-adb-handle-rename-file) | ||
| 107 | (process-file . tramp-adb-handle-process-file) | ||
| 108 | (shell-command . tramp-adb-handle-shell-command) | ||
| 109 | (start-file-process . tramp-adb-handle-start-file-process)) | ||
| 110 | "Alist of handler functions for Tramp ADB method.") | ||
| 111 | |||
| 112 | ;;;###tramp-autoload | ||
| 113 | (defun tramp-adb-file-name-p (filename) | ||
| 114 | "Check if it's a filename for ADB." | ||
| 115 | (let ((v (tramp-dissect-file-name filename))) | ||
| 116 | (string= (tramp-file-name-method v) tramp-adb-method))) | ||
| 117 | |||
| 118 | ;;;###tramp-autoload | ||
| 119 | (defun tramp-adb-file-name-handler (operation &rest args) | ||
| 120 | "Invoke the ADB handler for OPERATION. | ||
| 121 | First arg specifies the OPERATION, second arg is a list of arguments to | ||
| 122 | pass to the OPERATION." | ||
| 123 | (let ((fn (assoc operation tramp-adb-file-name-handler-alist)) | ||
| 124 | ;; `tramp-default-host's default value is (system-name). Not | ||
| 125 | ;; useful for us. | ||
| 126 | (tramp-default-host | ||
| 127 | (unless (equal (eval (car (get 'tramp-default-host 'standard-value))) | ||
| 128 | tramp-default-host) | ||
| 129 | tramp-default-host))) | ||
| 130 | (if fn | ||
| 131 | (save-match-data (apply (cdr fn) args)) | ||
| 132 | (tramp-run-real-handler operation args)))) | ||
| 133 | |||
| 134 | ;; This cannot be a constant, because `tramp-adb-sdk-dir' is customizable. | ||
| 135 | (defun tramp-adb-program () | ||
| 136 | "The Android Debug Bridge." | ||
| 137 | (expand-file-name "platform-tools/adb" tramp-adb-sdk-dir)) | ||
| 138 | |||
| 139 | ;;;###tramp-autoload | ||
| 140 | (defun tramp-adb-parse-device-names (ignore) | ||
| 141 | "Return a list of (nil host) tuples allowed to access." | ||
| 142 | (with-temp-buffer | ||
| 143 | (when (zerop (call-process (tramp-adb-program) nil t nil "devices")) | ||
| 144 | (let (result) | ||
| 145 | (goto-char (point-min)) | ||
| 146 | (while (search-forward-regexp "^\\(\\S-+\\)[[:space:]]+device$" nil t) | ||
| 147 | (add-to-list 'result (list nil (match-string 1)))) | ||
| 148 | result)))) | ||
| 149 | |||
| 150 | (defun tramp-adb-handle-expand-file-name (name &optional dir) | ||
| 151 | "Like `expand-file-name' for Tramp files." | ||
| 152 | ;; If DIR is not given, use DEFAULT-DIRECTORY or "/". | ||
| 153 | (setq dir (or dir default-directory "/")) | ||
| 154 | ;; Unless NAME is absolute, concat DIR and NAME. | ||
| 155 | (unless (file-name-absolute-p name) | ||
| 156 | (setq name (concat (file-name-as-directory dir) name))) | ||
| 157 | ;; If NAME is not a Tramp file, run the real handler. | ||
| 158 | (if (not (tramp-tramp-file-p name)) | ||
| 159 | (tramp-run-real-handler 'expand-file-name (list name nil)) | ||
| 160 | ;; Dissect NAME. | ||
| 161 | (with-parsed-tramp-file-name name nil | ||
| 162 | (unless (tramp-run-real-handler 'file-name-absolute-p (list localname)) | ||
| 163 | (setq localname (concat "/" localname))) | ||
| 164 | ;; Do normal `expand-file-name' (this does "/./" and "/../"). | ||
| 165 | ;; We bind `directory-sep-char' here for XEmacs on Windows, | ||
| 166 | ;; which would otherwise use backslash. `default-directory' is | ||
| 167 | ;; bound, because on Windows there would be problems with UNC | ||
| 168 | ;; shares or Cygwin mounts. | ||
| 169 | (let ((directory-sep-char ?/) | ||
| 170 | (default-directory (tramp-compat-temporary-file-directory))) | ||
| 171 | (tramp-make-tramp-file-name | ||
| 172 | method user host | ||
| 173 | (tramp-drop-volume-letter | ||
| 174 | (tramp-run-real-handler | ||
| 175 | 'expand-file-name (list localname)))))))) | ||
| 176 | |||
| 177 | (defun tramp-adb-handle-file-directory-p (filename) | ||
| 178 | "Like `file-directory-p' for Tramp files." | ||
| 179 | (car (file-attributes (file-truename filename)))) | ||
| 180 | |||
| 181 | ;; This is derived from `tramp-sh-handle-file-truename'. Maybe the | ||
| 182 | ;; code could be shared? | ||
| 183 | (defun tramp-adb-handle-file-truename (filename &optional counter prev-dirs) | ||
| 184 | "Like `file-truename' for Tramp files." | ||
| 185 | (with-parsed-tramp-file-name (expand-file-name filename) nil | ||
| 186 | (with-tramp-file-property v localname "file-truename" | ||
| 187 | (let ((result nil)) ; result steps in reverse order | ||
| 188 | (tramp-message v 4 "Finding true name for `%s'" filename) | ||
| 189 | (let* ((directory-sep-char ?/) | ||
| 190 | (steps (tramp-compat-split-string localname "/")) | ||
| 191 | (localnamedir (tramp-run-real-handler | ||
| 192 | 'file-name-as-directory (list localname))) | ||
| 193 | (is-dir (string= localname localnamedir)) | ||
| 194 | (thisstep nil) | ||
| 195 | (numchase 0) | ||
| 196 | ;; Don't make the following value larger than | ||
| 197 | ;; necessary. People expect an error message in a | ||
| 198 | ;; timely fashion when something is wrong; otherwise | ||
| 199 | ;; they might think that Emacs is hung. Of course, | ||
| 200 | ;; correctness has to come first. | ||
| 201 | (numchase-limit 20) | ||
| 202 | symlink-target) | ||
| 203 | (while (and steps (< numchase numchase-limit)) | ||
| 204 | (setq thisstep (pop steps)) | ||
| 205 | (tramp-message | ||
| 206 | v 5 "Check %s" | ||
| 207 | (mapconcat 'identity | ||
| 208 | (append '("") (reverse result) (list thisstep)) | ||
| 209 | "/")) | ||
| 210 | (setq symlink-target | ||
| 211 | (nth 0 (file-attributes | ||
| 212 | (tramp-make-tramp-file-name | ||
| 213 | method user host | ||
| 214 | (mapconcat 'identity | ||
| 215 | (append '("") | ||
| 216 | (reverse result) | ||
| 217 | (list thisstep)) | ||
| 218 | "/"))))) | ||
| 219 | (cond ((string= "." thisstep) | ||
| 220 | (tramp-message v 5 "Ignoring step `.'")) | ||
| 221 | ((string= ".." thisstep) | ||
| 222 | (tramp-message v 5 "Processing step `..'") | ||
| 223 | (pop result)) | ||
| 224 | ((stringp symlink-target) | ||
| 225 | ;; It's a symlink, follow it. | ||
| 226 | (tramp-message v 5 "Follow symlink to %s" symlink-target) | ||
| 227 | (setq numchase (1+ numchase)) | ||
| 228 | (when (file-name-absolute-p symlink-target) | ||
| 229 | (setq result nil)) | ||
| 230 | ;; If the symlink was absolute, we'll get a string | ||
| 231 | ;; like "/user@host:/some/target"; extract the | ||
| 232 | ;; "/some/target" part from it. | ||
| 233 | (when (tramp-tramp-file-p symlink-target) | ||
| 234 | (unless (tramp-equal-remote filename symlink-target) | ||
| 235 | (tramp-error | ||
| 236 | v 'file-error | ||
| 237 | "Symlink target `%s' on wrong host" symlink-target)) | ||
| 238 | (setq symlink-target localname)) | ||
| 239 | (setq steps | ||
| 240 | (append (tramp-compat-split-string | ||
| 241 | symlink-target "/") | ||
| 242 | steps))) | ||
| 243 | (t | ||
| 244 | ;; It's a file. | ||
| 245 | (setq result (cons thisstep result))))) | ||
| 246 | (when (>= numchase numchase-limit) | ||
| 247 | (tramp-error | ||
| 248 | v 'file-error | ||
| 249 | "Maximum number (%d) of symlinks exceeded" numchase-limit)) | ||
| 250 | (setq result (reverse result)) | ||
| 251 | ;; Combine list to form string. | ||
| 252 | (setq result | ||
| 253 | (if result | ||
| 254 | (mapconcat 'identity (cons "" result) "/") | ||
| 255 | "/")) | ||
| 256 | (when (and is-dir (or (string= "" result) | ||
| 257 | (not (string= (substring result -1) "/")))) | ||
| 258 | (setq result (concat result "/")))) | ||
| 259 | |||
| 260 | (tramp-message v 4 "True name of `%s' is `%s'" filename result) | ||
| 261 | (tramp-make-tramp-file-name method user host result))))) | ||
| 262 | |||
| 263 | (defun tramp-adb-handle-file-attributes (filename &optional id-format) | ||
| 264 | "Like `file-attributes' for Tramp files." | ||
| 265 | (unless id-format (setq id-format 'integer)) | ||
| 266 | (ignore-errors | ||
| 267 | (with-parsed-tramp-file-name filename nil | ||
| 268 | (with-tramp-file-property v localname (format "file-attributes-%s" id-format) | ||
| 269 | (tramp-adb-barf-unless-okay | ||
| 270 | v (format "ls -d -l %s" (tramp-shell-quote-argument localname)) "") | ||
| 271 | (with-current-buffer (tramp-get-buffer v) | ||
| 272 | (tramp-adb-sh-fix-ls-output) | ||
| 273 | (let* ((columns (split-string (buffer-string))) | ||
| 274 | (mod-string (nth 0 columns)) | ||
| 275 | (is-dir (eq ?d (aref mod-string 0))) | ||
| 276 | (is-symlink (eq ?l (aref mod-string 0))) | ||
| 277 | (symlink-target (and is-symlink (cadr (split-string (buffer-string) "\\( -> \\|\n\\)")))) | ||
| 278 | (uid (nth 1 columns)) | ||
| 279 | (gid (nth 2 columns)) | ||
| 280 | (date (format "%s %s" (nth 4 columns) (nth 5 columns))) | ||
| 281 | (size (string-to-number (nth 3 columns)))) | ||
| 282 | (list | ||
| 283 | (or is-dir symlink-target) | ||
| 284 | 1 ;link-count | ||
| 285 | ;; no way to handle numeric ids in Androids ash | ||
| 286 | (if (eq id-format 'integer) 0 uid) | ||
| 287 | (if (eq id-format 'integer) 0 gid) | ||
| 288 | '(0 0) ; atime | ||
| 289 | (date-to-time date) ; mtime | ||
| 290 | '(0 0) ; ctime | ||
| 291 | size | ||
| 292 | mod-string | ||
| 293 | ;; fake | ||
| 294 | t 1 1))))))) | ||
| 295 | |||
| 296 | (defun tramp-adb--gnu-switches-to-ash | ||
| 297 | (switches) | ||
| 298 | "Almquist shell can't handle multiple arguments. | ||
| 299 | Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"." | ||
| 300 | (split-string | ||
| 301 | (apply 'concat | ||
| 302 | (mapcar (lambda (s) | ||
| 303 | (replace-regexp-in-string | ||
| 304 | "\\(.\\)" " -\\1" | ||
| 305 | (replace-regexp-in-string "^-" "" s))) | ||
| 306 | ;; FIXME: Warning about removed switches (long and non-dash). | ||
| 307 | (delq nil | ||
| 308 | (mapcar | ||
| 309 | (lambda (s) (and (not (string-match "\\(^--\\|^[^-]\\)" s)) s)) | ||
| 310 | switches)))))) | ||
| 311 | |||
| 312 | (defun tramp-adb-handle-insert-directory | ||
| 313 | (filename switches &optional wildcard full-directory-p) | ||
| 314 | "Like `insert-directory' for Tramp files." | ||
| 315 | (when (stringp switches) | ||
| 316 | (setq switches (tramp-adb--gnu-switches-to-ash (split-string switches)))) | ||
| 317 | (with-parsed-tramp-file-name (file-truename filename) nil | ||
| 318 | (with-current-buffer (tramp-get-buffer v) | ||
| 319 | (let ((name (tramp-shell-quote-argument (directory-file-name localname))) | ||
| 320 | (switch-d (member "-d" switches)) | ||
| 321 | (switch-t (member "-t" switches)) | ||
| 322 | (switches (mapconcat 'identity (remove "-t" switches) " "))) | ||
| 323 | (tramp-adb-barf-unless-okay | ||
| 324 | v (format "ls %s %s" switches name) | ||
| 325 | "Cannot insert directory listing: %s" filename) | ||
| 326 | (unless switch-d | ||
| 327 | ;; We insert also filename/. and filename/.., because "ls" doesn't. | ||
| 328 | (narrow-to-region (point) (point)) | ||
| 329 | (ignore-errors | ||
| 330 | (tramp-adb-barf-unless-okay | ||
| 331 | v (format "ls -d %s %s %s" | ||
| 332 | switches | ||
| 333 | (concat (file-name-as-directory name) ".") | ||
| 334 | (concat (file-name-as-directory name) "..")) | ||
| 335 | "Cannot insert directory listing: %s" filename)) | ||
| 336 | (widen)) | ||
| 337 | (tramp-adb-sh-fix-ls-output switch-t))) | ||
| 338 | (insert-buffer-substring (tramp-get-buffer v)))) | ||
| 339 | |||
| 340 | (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time) | ||
| 341 | "Androids ls command doesn't insert size column for directories: Emacs dired can't find files. Insert dummy 0 in empty size columns." | ||
| 342 | (save-excursion | ||
| 343 | ;; Insert missing size. | ||
| 344 | (goto-char (point-min)) | ||
| 345 | (while (search-forward-regexp "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t) | ||
| 346 | (replace-match "0\\1" "\\1" nil) | ||
| 347 | ;; Insert missing "/". | ||
| 348 | (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$") | ||
| 349 | (end-of-line) | ||
| 350 | (insert "/"))) | ||
| 351 | ;; Sort entries. | ||
| 352 | (let* ((lines (split-string (buffer-string) "\n" t)) | ||
| 353 | (sorted-lines | ||
| 354 | (sort | ||
| 355 | lines | ||
| 356 | (if sort-by-time | ||
| 357 | 'tramp-adb-ls-output-time-less-p | ||
| 358 | 'tramp-adb-ls-output-name-less-p)))) | ||
| 359 | (delete-region (point-min) (point-max)) | ||
| 360 | (insert " " (mapconcat 'identity sorted-lines "\n "))) | ||
| 361 | ;; Add final newline. | ||
| 362 | (goto-char (point-max)) | ||
| 363 | (unless (= (point) (line-beginning-position)) | ||
| 364 | (insert "\n")))) | ||
| 365 | |||
| 366 | |||
| 367 | (defun tramp-adb-ls-output-time-less-p (a b) | ||
| 368 | "Sort \"ls\" output by time, descending." | ||
| 369 | (let (time-a time-b) | ||
| 370 | (string-match tramp-adb-ls-date-regexp a) | ||
| 371 | (setq time-a (apply 'encode-time (parse-time-string (match-string 0 a)))) | ||
| 372 | (string-match tramp-adb-ls-date-regexp b) | ||
| 373 | (setq time-b (apply 'encode-time (parse-time-string (match-string 0 b)))) | ||
| 374 | (time-less-p time-b time-a))) | ||
| 375 | |||
| 376 | (defun tramp-adb-ls-output-name-less-p (a b) | ||
| 377 | "Sort \"ls\" output by name, ascending." | ||
| 378 | (let (posa posb) | ||
| 379 | (string-match dired-move-to-filename-regexp a) | ||
| 380 | (setq posa (match-end 0)) | ||
| 381 | (string-match dired-move-to-filename-regexp b) | ||
| 382 | (setq posb (match-end 0)) | ||
| 383 | (string-lessp (substring a posa) (substring b posb)))) | ||
| 384 | |||
| 385 | (defun tramp-adb-handle-make-directory (dir &optional parents) | ||
| 386 | "Like `make-directory' for Tramp files." | ||
| 387 | (setq dir (expand-file-name dir)) | ||
| 388 | (with-parsed-tramp-file-name dir nil | ||
| 389 | (when parents | ||
| 390 | (let ((par (expand-file-name ".." dir))) | ||
| 391 | (unless (file-directory-p par) | ||
| 392 | (make-directory par parents)))) | ||
| 393 | (tramp-adb-barf-unless-okay | ||
| 394 | v (format "mkdir %s" (tramp-shell-quote-argument localname)) | ||
| 395 | "Couldn't make directory %s" dir) | ||
| 396 | (tramp-flush-directory-property v (file-name-directory localname)))) | ||
| 397 | |||
| 398 | (defun tramp-adb-handle-delete-directory (directory &optional recursive) | ||
| 399 | "Like `delete-directory' for Tramp files." | ||
| 400 | (setq directory (expand-file-name directory)) | ||
| 401 | (with-parsed-tramp-file-name directory nil | ||
| 402 | (tramp-flush-file-property v (file-name-directory localname)) | ||
| 403 | (tramp-flush-directory-property v localname) | ||
| 404 | (tramp-adb-barf-unless-okay | ||
| 405 | v (format "%s %s" | ||
| 406 | (if recursive "rm -r" "rmdir") | ||
| 407 | (tramp-shell-quote-argument localname)) | ||
| 408 | "Couldn't delete %s" directory))) | ||
| 409 | |||
| 410 | (defun tramp-adb-handle-delete-file (filename &optional trash) | ||
| 411 | "Like `delete-file' for Tramp files." | ||
| 412 | (setq filename (expand-file-name filename)) | ||
| 413 | (with-parsed-tramp-file-name filename nil | ||
| 414 | (tramp-flush-file-property v (file-name-directory localname)) | ||
| 415 | (tramp-flush-file-property v localname) | ||
| 416 | (tramp-adb-barf-unless-okay | ||
| 417 | v (format "rm %s" (tramp-shell-quote-argument localname)) | ||
| 418 | "Couldn't delete %s" filename))) | ||
| 419 | |||
| 420 | (defun tramp-adb-handle-file-name-all-completions (filename directory) | ||
| 421 | "Like `file-name-all-completions' for Tramp files." | ||
| 422 | (all-completions | ||
| 423 | filename | ||
| 424 | (with-parsed-tramp-file-name directory nil | ||
| 425 | (with-tramp-file-property v localname "file-name-all-completions" | ||
| 426 | (save-match-data | ||
| 427 | (tramp-adb-send-command | ||
| 428 | v (format "ls %s" (tramp-shell-quote-argument localname))) | ||
| 429 | (mapcar | ||
| 430 | (lambda (f) | ||
| 431 | (if (file-directory-p f) | ||
| 432 | (file-name-as-directory f) | ||
| 433 | f)) | ||
| 434 | (with-current-buffer (tramp-get-buffer v) | ||
| 435 | (delq | ||
| 436 | nil | ||
| 437 | (mapcar | ||
| 438 | (lambda (l) (and (not (string-match "^[[:space:]]*$" l)) l)) | ||
| 439 | (split-string (buffer-string) "\n")))))))))) | ||
| 440 | |||
| 441 | (defun tramp-adb-handle-file-local-copy (filename) | ||
| 442 | "Like `file-local-copy' for Tramp files." | ||
| 443 | (with-parsed-tramp-file-name filename nil | ||
| 444 | (unless (file-exists-p (file-truename filename)) | ||
| 445 | (tramp-error | ||
| 446 | v 'file-error | ||
| 447 | "Cannot make local copy of non-existing file `%s'" filename)) | ||
| 448 | (let ((tmpfile (tramp-compat-make-temp-file filename))) | ||
| 449 | (with-tramp-progress-reporter | ||
| 450 | v 3 (format "Fetching %s to tmp file %s" filename tmpfile) | ||
| 451 | (when (tramp-adb-execute-adb-command v "pull" localname tmpfile) | ||
| 452 | (delete-file tmpfile) | ||
| 453 | (tramp-error | ||
| 454 | v 'file-error "Cannot make local copy of file `%s'" filename)) | ||
| 455 | (set-file-modes tmpfile (file-modes filename))) | ||
| 456 | tmpfile))) | ||
| 457 | |||
| 458 | (defun tramp-adb-handle-file-writable-p (filename) | ||
| 459 | "Like `tramp-sh-handle-file-writable-p'. | ||
| 460 | But handle the case, if the \"test\" command is not available." | ||
| 461 | (with-parsed-tramp-file-name filename nil | ||
| 462 | (with-tramp-file-property v localname "file-writable-p" | ||
| 463 | (if (tramp-adb-find-test-command v) | ||
| 464 | (if (file-exists-p filename) | ||
| 465 | (zerop | ||
| 466 | (tramp-adb-command-exit-status | ||
| 467 | v (format "test -w %s" (tramp-shell-quote-argument localname)))) | ||
| 468 | (and | ||
| 469 | (file-directory-p (file-name-directory filename)) | ||
| 470 | (file-writable-p (file-name-directory filename)))) | ||
| 471 | |||
| 472 | ;; Missing "test" command on Android < 4. | ||
| 473 | (let ((rw-path "/data/data")) | ||
| 474 | (tramp-message | ||
| 475 | v 5 | ||
| 476 | "Not implemented yet (assuming \"/data/data\" is writable): %s" | ||
| 477 | localname) | ||
| 478 | (and (>= (length localname) (length rw-path)) | ||
| 479 | (string= (substring localname 0 (length rw-path)) | ||
| 480 | rw-path))))))) | ||
| 481 | |||
| 482 | (defun tramp-adb-handle-write-region | ||
| 483 | (start end filename &optional append visit lockname confirm) | ||
| 484 | "Like `write-region' for Tramp files." | ||
| 485 | (setq filename (expand-file-name filename)) | ||
| 486 | (with-parsed-tramp-file-name filename nil | ||
| 487 | (when append | ||
| 488 | (tramp-error | ||
| 489 | v 'file-error "Cannot append to file using Tramp (`%s')" filename)) | ||
| 490 | (when (and confirm (file-exists-p filename)) | ||
| 491 | (unless (y-or-n-p (format "File %s exists; overwrite anyway? " | ||
| 492 | filename)) | ||
| 493 | (tramp-error v 'file-error "File not overwritten"))) | ||
| 494 | ;; We must also flush the cache of the directory, because | ||
| 495 | ;; `file-attributes' reads the values from there. | ||
| 496 | (tramp-flush-file-property v (file-name-directory localname)) | ||
| 497 | (tramp-flush-file-property v localname) | ||
| 498 | (let* ((curbuf (current-buffer)) | ||
| 499 | (tmpfile (tramp-compat-make-temp-file filename))) | ||
| 500 | (tramp-run-real-handler | ||
| 501 | 'write-region | ||
| 502 | (list start end tmpfile append 'no-message lockname confirm)) | ||
| 503 | (with-tramp-progress-reporter | ||
| 504 | v 3 (format "Moving tmp file %s to %s" tmpfile filename) | ||
| 505 | (unwind-protect | ||
| 506 | (when (tramp-adb-execute-adb-command v "push" tmpfile localname) | ||
| 507 | (tramp-error v 'file-error "Cannot write: `%s' filename")) | ||
| 508 | (delete-file tmpfile))) | ||
| 509 | |||
| 510 | (unless (equal curbuf (current-buffer)) | ||
| 511 | (tramp-error | ||
| 512 | v 'file-error | ||
| 513 | "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))))) | ||
| 514 | |||
| 515 | (defun tramp-adb-handle-set-file-modes (filename mode) | ||
| 516 | "Like `set-file-modes' for Tramp files." | ||
| 517 | (with-parsed-tramp-file-name filename nil | ||
| 518 | (tramp-flush-file-property v localname) | ||
| 519 | (tramp-adb-barf-unless-okay | ||
| 520 | v (format "chmod %s %s" (tramp-compat-decimal-to-octal mode) localname) | ||
| 521 | "Error while changing file's mode %s" filename))) | ||
| 522 | |||
| 523 | (defun tramp-adb-handle-copy-file | ||
| 524 | (filename newname &optional ok-if-already-exists keep-date | ||
| 525 | preserve-uid-gid preserve-selinux-context) | ||
| 526 | "Like `copy-file' for Tramp files. | ||
| 527 | PRESERVE-UID-GID and PRESERVE-SELINUX-CONTEXT are completely ignored." | ||
| 528 | (setq filename (expand-file-name filename) | ||
| 529 | newname (expand-file-name newname)) | ||
| 530 | |||
| 531 | (if (file-directory-p filename) | ||
| 532 | (copy-directory filename newname keep-date t) | ||
| 533 | (with-tramp-progress-reporter | ||
| 534 | (tramp-dissect-file-name (if (file-remote-p filename) filename newname)) | ||
| 535 | 0 (format "Copying %s to %s" filename newname) | ||
| 536 | |||
| 537 | (let ((tmpfile (file-local-copy filename))) | ||
| 538 | |||
| 539 | (if tmpfile | ||
| 540 | ;; Remote filename. | ||
| 541 | (condition-case err | ||
| 542 | (rename-file tmpfile newname ok-if-already-exists) | ||
| 543 | ((error quit) | ||
| 544 | (delete-file tmpfile) | ||
| 545 | (signal (car err) (cdr err)))) | ||
| 546 | |||
| 547 | ;; Remote newname. | ||
| 548 | (when (file-directory-p newname) | ||
| 549 | (setq newname | ||
| 550 | (expand-file-name (file-name-nondirectory filename) newname))) | ||
| 551 | |||
| 552 | (with-parsed-tramp-file-name newname nil | ||
| 553 | (when (and (not ok-if-already-exists) | ||
| 554 | (file-exists-p newname)) | ||
| 555 | (tramp-error v 'file-already-exists newname)) | ||
| 556 | |||
| 557 | ;; We must also flush the cache of the directory, because | ||
| 558 | ;; `file-attributes' reads the values from there. | ||
| 559 | (tramp-flush-file-property v (file-name-directory localname)) | ||
| 560 | (tramp-flush-file-property v localname) | ||
| 561 | (when (tramp-adb-execute-adb-command v "push" filename localname) | ||
| 562 | (tramp-error | ||
| 563 | v 'file-error "Cannot copy `%s' `%s'" filename newname)))))) | ||
| 564 | |||
| 565 | ;; KEEP-DATE handling. | ||
| 566 | (when keep-date | ||
| 567 | (set-file-times newname (nth 5 (file-attributes filename)))))) | ||
| 568 | |||
| 569 | (defun tramp-adb-handle-rename-file | ||
| 570 | (filename newname &optional ok-if-already-exists) | ||
| 571 | "Like `rename-file' for Tramp files." | ||
| 572 | (setq filename (expand-file-name filename) | ||
| 573 | newname (expand-file-name newname)) | ||
| 574 | |||
| 575 | (with-parsed-tramp-file-name | ||
| 576 | (if (file-remote-p filename) filename newname) nil | ||
| 577 | (with-tramp-progress-reporter | ||
| 578 | v 0 (format "Renaming %s to %s" newname filename) | ||
| 579 | |||
| 580 | (if (and (tramp-equal-remote filename newname) | ||
| 581 | (not (file-directory-p filename))) | ||
| 582 | (progn | ||
| 583 | (when (and (not ok-if-already-exists) | ||
| 584 | (file-exists-p newname)) | ||
| 585 | (tramp-error v 'file-already-exists newname)) | ||
| 586 | ;; We must also flush the cache of the directory, because | ||
| 587 | ;; `file-attributes' reads the values from there. | ||
| 588 | (tramp-flush-file-property v (file-name-directory localname)) | ||
| 589 | (tramp-flush-file-property v localname) | ||
| 590 | ;; Short track. | ||
| 591 | (tramp-adb-barf-unless-okay | ||
| 592 | v (format "mv %s %s" (file-remote-p filename 'localname) localname) | ||
| 593 | "Error renaming %s to %s" filename newname)) | ||
| 594 | |||
| 595 | ;; Rename by copy. | ||
| 596 | (copy-file filename newname ok-if-already-exists t t) | ||
| 597 | (delete-file filename))))) | ||
| 598 | |||
| 599 | (defun tramp-adb-handle-process-file | ||
| 600 | (program &optional infile destination display &rest args) | ||
| 601 | "Like `process-file' for Tramp files." | ||
| 602 | ;; The implementation is not complete yet. | ||
| 603 | (when (and (numberp destination) (zerop destination)) | ||
| 604 | (error "Implementation does not handle immediate return")) | ||
| 605 | |||
| 606 | (with-parsed-tramp-file-name default-directory nil | ||
| 607 | (let (command input tmpinput stderr tmpstderr outbuf ret) | ||
| 608 | ;; Compute command. | ||
| 609 | (setq command (mapconcat 'tramp-shell-quote-argument | ||
| 610 | (cons program args) " ")) | ||
| 611 | ;; Determine input. | ||
| 612 | (if (null infile) | ||
| 613 | (setq input "/dev/null") | ||
| 614 | (setq infile (expand-file-name infile)) | ||
| 615 | (if (tramp-equal-remote default-directory infile) | ||
| 616 | ;; INFILE is on the same remote host. | ||
| 617 | (setq input (with-parsed-tramp-file-name infile nil localname)) | ||
| 618 | ;; INFILE must be copied to remote host. | ||
| 619 | (setq input (tramp-make-tramp-temp-file v) | ||
| 620 | tmpinput (tramp-make-tramp-file-name method user host input)) | ||
| 621 | (copy-file infile tmpinput t))) | ||
| 622 | (when input (setq command (format "%s <%s" command input))) | ||
| 623 | |||
| 624 | ;; Determine output. | ||
| 625 | (cond | ||
| 626 | ;; Just a buffer. | ||
| 627 | ((bufferp destination) | ||
| 628 | (setq outbuf destination)) | ||
| 629 | ;; A buffer name. | ||
| 630 | ((stringp destination) | ||
| 631 | (setq outbuf (get-buffer-create destination))) | ||
| 632 | ;; (REAL-DESTINATION ERROR-DESTINATION) | ||
| 633 | ((consp destination) | ||
| 634 | ;; output. | ||
| 635 | (cond | ||
| 636 | ((bufferp (car destination)) | ||
| 637 | (setq outbuf (car destination))) | ||
| 638 | ((stringp (car destination)) | ||
| 639 | (setq outbuf (get-buffer-create (car destination)))) | ||
| 640 | ((car destination) | ||
| 641 | (setq outbuf (current-buffer)))) | ||
| 642 | ;; stderr. | ||
| 643 | (cond | ||
| 644 | ((stringp (cadr destination)) | ||
| 645 | (setcar (cdr destination) (expand-file-name (cadr destination))) | ||
| 646 | (if (tramp-equal-remote default-directory (cadr destination)) | ||
| 647 | ;; stderr is on the same remote host. | ||
| 648 | (setq stderr (with-parsed-tramp-file-name | ||
| 649 | (cadr destination) nil localname)) | ||
| 650 | ;; stderr must be copied to remote host. The temporary | ||
| 651 | ;; file must be deleted after execution. | ||
| 652 | (setq stderr (tramp-make-tramp-temp-file v) | ||
| 653 | tmpstderr (tramp-make-tramp-file-name | ||
| 654 | method user host stderr)))) | ||
| 655 | ;; stderr to be discarded. | ||
| 656 | ((null (cadr destination)) | ||
| 657 | (setq stderr "/dev/null")))) | ||
| 658 | ;; 't | ||
| 659 | (destination | ||
| 660 | (setq outbuf (current-buffer)))) | ||
| 661 | (when stderr (setq command (format "%s 2>%s" command stderr))) | ||
| 662 | |||
| 663 | ;; Send the command. It might not return in time, so we protect | ||
| 664 | ;; it. Call it in a subshell, in order to preserve working | ||
| 665 | ;; directory. | ||
| 666 | (condition-case nil | ||
| 667 | (progn | ||
| 668 | (setq ret 0 | ||
| 669 | ret | ||
| 670 | (tramp-adb-barf-unless-okay | ||
| 671 | v (format "(cd %s; %s)" | ||
| 672 | (tramp-shell-quote-argument localname) | ||
| 673 | command) | ||
| 674 | "")) | ||
| 675 | ;; We should show the output anyway. | ||
| 676 | (when outbuf | ||
| 677 | (with-current-buffer outbuf | ||
| 678 | (insert-buffer-substring (tramp-get-connection-buffer v))) | ||
| 679 | (when display (display-buffer outbuf)))) | ||
| 680 | ;; When the user did interrupt, we should do it also. We use | ||
| 681 | ;; return code -1 as marker. | ||
| 682 | (quit | ||
| 683 | (kill-buffer (tramp-get-connection-buffer v)) | ||
| 684 | (setq ret -1)) | ||
| 685 | ;; Handle errors. | ||
| 686 | (error | ||
| 687 | (kill-buffer (tramp-get-connection-buffer v)) | ||
| 688 | (setq ret 1))) | ||
| 689 | |||
| 690 | ;; Provide error file. | ||
| 691 | (when tmpstderr (rename-file tmpstderr (cadr destination) t)) | ||
| 692 | |||
| 693 | ;; Cleanup. We remove all file cache values for the connection, | ||
| 694 | ;; because the remote process could have changed them. | ||
| 695 | (when tmpinput (delete-file tmpinput)) | ||
| 696 | |||
| 697 | ;; `process-file-side-effects' has been introduced with GNU | ||
| 698 | ;; Emacs 23.2. If set to `nil', no remote file will be changed | ||
| 699 | ;; by `program'. If it doesn't exist, we assume its default | ||
| 700 | ;; value 't'. | ||
| 701 | (unless (and (boundp 'process-file-side-effects) | ||
| 702 | (not (symbol-value 'process-file-side-effects))) | ||
| 703 | (tramp-flush-directory-property v "")) | ||
| 704 | |||
| 705 | ;; Return exit status. | ||
| 706 | (if (equal ret -1) | ||
| 707 | (keyboard-quit) | ||
| 708 | ret)))) | ||
| 709 | |||
| 710 | (defun tramp-adb-handle-shell-command | ||
| 711 | (command &optional output-buffer error-buffer) | ||
| 712 | "Like `shell-command' for Tramp files." | ||
| 713 | (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command)) | ||
| 714 | ;; We cannot use `shell-file-name' and `shell-command-switch', | ||
| 715 | ;; they are variables of the local host. | ||
| 716 | (args (list "sh" "-c" (substring command 0 asynchronous))) | ||
| 717 | current-buffer-p | ||
| 718 | (output-buffer | ||
| 719 | (cond | ||
| 720 | ((bufferp output-buffer) output-buffer) | ||
| 721 | ((stringp output-buffer) (get-buffer-create output-buffer)) | ||
| 722 | (output-buffer | ||
| 723 | (setq current-buffer-p t) | ||
| 724 | (current-buffer)) | ||
| 725 | (t (get-buffer-create | ||
| 726 | (if asynchronous | ||
| 727 | "*Async Shell Command*" | ||
| 728 | "*Shell Command Output*"))))) | ||
| 729 | (error-buffer | ||
| 730 | (cond | ||
| 731 | ((bufferp error-buffer) error-buffer) | ||
| 732 | ((stringp error-buffer) (get-buffer-create error-buffer)))) | ||
| 733 | (buffer | ||
| 734 | (if (and (not asynchronous) error-buffer) | ||
| 735 | (with-parsed-tramp-file-name default-directory nil | ||
| 736 | (list output-buffer (tramp-make-tramp-temp-file v))) | ||
| 737 | output-buffer)) | ||
| 738 | (p (get-buffer-process output-buffer))) | ||
| 739 | |||
| 740 | ;; Check whether there is another process running. Tramp does not | ||
| 741 | ;; support 2 (asynchronous) processes in parallel. | ||
| 742 | (when p | ||
| 743 | (if (yes-or-no-p "A command is running. Kill it? ") | ||
| 744 | (ignore-errors (kill-process p)) | ||
| 745 | (error "Shell command in progress"))) | ||
| 746 | |||
| 747 | (if current-buffer-p | ||
| 748 | (progn | ||
| 749 | (barf-if-buffer-read-only) | ||
| 750 | (push-mark nil t)) | ||
| 751 | (with-current-buffer output-buffer | ||
| 752 | (setq buffer-read-only nil) | ||
| 753 | (erase-buffer))) | ||
| 754 | |||
| 755 | (if (and (not current-buffer-p) (integerp asynchronous)) | ||
| 756 | (prog1 | ||
| 757 | ;; Run the process. | ||
| 758 | (apply 'start-file-process "*Async Shell*" buffer args) | ||
| 759 | ;; Display output. | ||
| 760 | (pop-to-buffer output-buffer) | ||
| 761 | (setq mode-line-process '(":%s")) | ||
| 762 | (shell-mode)) | ||
| 763 | |||
| 764 | (prog1 | ||
| 765 | ;; Run the process. | ||
| 766 | (apply 'process-file (car args) nil buffer nil (cdr args)) | ||
| 767 | ;; Insert error messages if they were separated. | ||
| 768 | (when (listp buffer) | ||
| 769 | (with-current-buffer error-buffer | ||
| 770 | (insert-file-contents (cadr buffer))) | ||
| 771 | (delete-file (cadr buffer))) | ||
| 772 | (if current-buffer-p | ||
| 773 | ;; This is like exchange-point-and-mark, but doesn't | ||
| 774 | ;; activate the mark. It is cleaner to avoid activation, | ||
| 775 | ;; even though the command loop would deactivate the mark | ||
| 776 | ;; because we inserted text. | ||
| 777 | (goto-char (prog1 (mark t) | ||
| 778 | (set-marker (mark-marker) (point) | ||
| 779 | (current-buffer)))) | ||
| 780 | ;; There's some output, display it. | ||
| 781 | (when (with-current-buffer output-buffer (> (point-max) (point-min))) | ||
| 782 | (if (functionp 'display-message-or-buffer) | ||
| 783 | (tramp-compat-funcall 'display-message-or-buffer output-buffer) | ||
| 784 | (pop-to-buffer output-buffer)))))))) | ||
| 785 | |||
| 786 | ;; We use BUFFER also as connection buffer during setup. Because of | ||
| 787 | ;; this, its original contents must be saved, and restored once | ||
| 788 | ;; connection has been setup. | ||
| 789 | (defun tramp-adb-handle-start-file-process (name buffer program &rest args) | ||
| 790 | "Like `start-file-process' for Tramp files." | ||
| 791 | (with-parsed-tramp-file-name default-directory nil | ||
| 792 | ;; When PROGRAM is nil, we just provide a tty. | ||
| 793 | (let ((command | ||
| 794 | (when (stringp program) | ||
| 795 | (format "cd %s; %s" | ||
| 796 | (tramp-shell-quote-argument localname) | ||
| 797 | (mapconcat 'tramp-shell-quote-argument | ||
| 798 | (cons program args) " ")))) | ||
| 799 | (tramp-process-connection-type | ||
| 800 | (or (null program) tramp-process-connection-type)) | ||
| 801 | (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer))) | ||
| 802 | (name1 name) | ||
| 803 | (i 0)) | ||
| 804 | (unwind-protect | ||
| 805 | (save-excursion | ||
| 806 | (save-restriction | ||
| 807 | (unless buffer | ||
| 808 | ;; BUFFER can be nil. We use a temporary buffer. | ||
| 809 | (setq buffer (generate-new-buffer tramp-temp-buffer-name))) | ||
| 810 | (while (get-process name1) | ||
| 811 | ;; NAME must be unique as process name. | ||
| 812 | (setq i (1+ i) | ||
| 813 | name1 (format "%s<%d>" name i))) | ||
| 814 | (setq name name1) | ||
| 815 | ;; Set the new process properties. | ||
| 816 | (tramp-set-connection-property v "process-name" name) | ||
| 817 | (tramp-set-connection-property v "process-buffer" buffer) | ||
| 818 | ;; Activate narrowing in order to save BUFFER contents. | ||
| 819 | ;; Clear also the modification time; otherwise we might | ||
| 820 | ;; be interrupted by `verify-visited-file-modtime'. | ||
| 821 | (with-current-buffer (tramp-get-connection-buffer v) | ||
| 822 | (let ((buffer-undo-list t)) | ||
| 823 | (clear-visited-file-modtime) | ||
| 824 | (narrow-to-region (point-max) (point-max)) | ||
| 825 | (if command | ||
| 826 | ;; Send the command. | ||
| 827 | (tramp-adb-send-command v command) | ||
| 828 | ;; Open the connection. | ||
| 829 | (tramp-adb-maybe-open-connection v)))) | ||
| 830 | (let ((p (tramp-get-connection-process v))) | ||
| 831 | ;; Set sentinel and query flag for this process. | ||
| 832 | (tramp-set-connection-property p "vector" v) | ||
| 833 | (set-process-sentinel p 'tramp-process-sentinel) | ||
| 834 | (tramp-compat-set-process-query-on-exit-flag p t) | ||
| 835 | ;; Return process. | ||
| 836 | p))) | ||
| 837 | ;; Save exit. | ||
| 838 | (with-current-buffer (tramp-get-connection-buffer v) | ||
| 839 | (if (string-match tramp-temp-buffer-name (buffer-name)) | ||
| 840 | (progn | ||
| 841 | (set-process-buffer (tramp-get-connection-process v) nil) | ||
| 842 | (kill-buffer (current-buffer))) | ||
| 843 | (set-buffer-modified-p bmp))) | ||
| 844 | (tramp-set-connection-property v "process-name" nil) | ||
| 845 | (tramp-set-connection-property v "process-buffer" nil))))) | ||
| 846 | |||
| 847 | ;; Android < 4 doesn't provide test command. | ||
| 848 | |||
| 849 | (defun tramp-adb-handle-file-exists-p (filename) | ||
| 850 | "Like `file-exists-p' for Tramp files." | ||
| 851 | (with-parsed-tramp-file-name filename nil | ||
| 852 | (with-tramp-file-property v localname "file-exists-p" | ||
| 853 | (file-attributes filename)))) | ||
| 854 | |||
| 855 | ;; Helper functions. | ||
| 856 | |||
| 857 | (defun tramp-adb-execute-adb-command (vec &rest args) | ||
| 858 | "Returns nil on success error-output on failure." | ||
| 859 | (when (tramp-file-name-host vec) | ||
| 860 | (setq args (append (list "-s" (tramp-file-name-host vec)) args))) | ||
| 861 | (with-temp-buffer | ||
| 862 | (prog1 | ||
| 863 | (unless (zerop (apply 'call-process (tramp-adb-program) nil t nil args)) | ||
| 864 | (buffer-string)) | ||
| 865 | (tramp-message | ||
| 866 | vec 6 "%s %s\n%s" | ||
| 867 | (tramp-adb-program) (mapconcat 'identity args " ") (buffer-string))))) | ||
| 868 | |||
| 869 | (defun tramp-adb-find-test-command (vec) | ||
| 870 | "Checks, whether the ash has a builtin \"test\" command. | ||
| 871 | This happens for Android >= 4.0." | ||
| 872 | (with-tramp-connection-property vec "test" | ||
| 873 | (zerop (tramp-adb-command-exit-status vec "type test")))) | ||
| 874 | |||
| 875 | ;; Connection functions | ||
| 876 | |||
| 877 | (defun tramp-adb-send-command (vec command) | ||
| 878 | "Send the COMMAND to connection VEC." | ||
| 879 | (tramp-adb-maybe-open-connection vec) | ||
| 880 | (tramp-message vec 6 "%s" command) | ||
| 881 | (tramp-send-string vec command) | ||
| 882 | ;; fixme: Race condition | ||
| 883 | (tramp-adb-wait-for-output (tramp-get-connection-process vec)) | ||
| 884 | (with-current-buffer (tramp-get-connection-buffer vec) | ||
| 885 | (save-excursion | ||
| 886 | (goto-char (point-min)) | ||
| 887 | ;; We can't use stty to disable echo of command. | ||
| 888 | (delete-matching-lines (regexp-quote command)) | ||
| 889 | ;; When the local machine is W32, there are still trailing ^M. | ||
| 890 | ;; There must be a better solution by setting the correct coding | ||
| 891 | ;; system, but this requires changes in core Tramp. | ||
| 892 | (goto-char (point-min)) | ||
| 893 | (while (re-search-forward "\r+$" nil t) | ||
| 894 | (replace-match "" nil nil))))) | ||
| 895 | |||
| 896 | (defun tramp-adb-barf-unless-okay (vec command fmt &rest args) | ||
| 897 | "Run COMMAND, check exit status, throw error if exit status not okay. | ||
| 898 | FMT and ARGS are passed to `error'." | ||
| 899 | (tramp-adb-send-command vec (format "%s; echo tramp_exit_status $?" command)) | ||
| 900 | (with-current-buffer (tramp-get-connection-buffer vec) | ||
| 901 | (goto-char (point-max)) | ||
| 902 | (unless (re-search-backward "tramp_exit_status [0-9]+" nil t) | ||
| 903 | (tramp-error | ||
| 904 | vec 'file-error "Couldn't find exit status of `%s'" command)) | ||
| 905 | (skip-chars-forward "^ ") | ||
| 906 | (unless (zerop (read (current-buffer))) | ||
| 907 | (apply 'tramp-error vec 'file-error fmt args)) | ||
| 908 | (let (buffer-read-only) | ||
| 909 | (delete-region (match-beginning 0) (point-max))))) | ||
| 910 | |||
| 911 | (defun tramp-adb-command-exit-status | ||
| 912 | (vec command) | ||
| 913 | "Run COMMAND and return its exit status. | ||
| 914 | Sends `echo $?' along with the COMMAND for checking the exit status. If | ||
| 915 | COMMAND is nil, just sends `echo $?'. Returns the exit status found." | ||
| 916 | (tramp-adb-send-command vec (format "%s; echo tramp_exit_status $?" command)) | ||
| 917 | (with-current-buffer (tramp-get-connection-buffer vec) | ||
| 918 | (goto-char (point-max)) | ||
| 919 | (unless (re-search-backward "tramp_exit_status [0-9]+" nil t) | ||
| 920 | (tramp-error | ||
| 921 | vec 'file-error "Couldn't find exit status of `%s'" command)) | ||
| 922 | (skip-chars-forward "^ ") | ||
| 923 | (read (current-buffer)))) | ||
| 924 | |||
| 925 | (defun tramp-adb-wait-for-output (proc &optional timeout) | ||
| 926 | "Wait for output from remote command." | ||
| 927 | (unless (buffer-live-p (process-buffer proc)) | ||
| 928 | (delete-process proc) | ||
| 929 | (tramp-error proc 'file-error "Process `%s' not available, try again" proc)) | ||
| 930 | (with-current-buffer (process-buffer proc) | ||
| 931 | (if (tramp-wait-for-regexp proc timeout tramp-adb-prompt) | ||
| 932 | (let (buffer-read-only) | ||
| 933 | (goto-char (point-min)) | ||
| 934 | (when (re-search-forward tramp-adb-prompt (point-at-eol) t) | ||
| 935 | (forward-line 1) | ||
| 936 | (delete-region (point-min) (point))) | ||
| 937 | ;; Delete the prompt. | ||
| 938 | (goto-char (point-max)) | ||
| 939 | (re-search-backward tramp-adb-prompt nil t) | ||
| 940 | (delete-region (point) (point-max))) | ||
| 941 | (if timeout | ||
| 942 | (tramp-error | ||
| 943 | proc 'file-error | ||
| 944 | "[[Remote adb prompt `%s' not found in %d secs]]" | ||
| 945 | tramp-adb-prompt timeout) | ||
| 946 | (tramp-error | ||
| 947 | proc 'file-error | ||
| 948 | "[[Remote prompt `%s' not found]]" tramp-adb-prompt))))) | ||
| 949 | |||
| 950 | (defun tramp-adb-maybe-open-connection (vec) | ||
| 951 | "Maybe open a connection VEC. | ||
| 952 | Does not do anything if a connection is already open, but re-opens the | ||
| 953 | connection if a previous connection has died for some reason." | ||
| 954 | (let* ((buf (tramp-get-connection-buffer vec)) | ||
| 955 | (p (get-buffer-process buf))) | ||
| 956 | (unless | ||
| 957 | (and p (processp p) (memq (process-status p) '(run open))) | ||
| 958 | (save-match-data | ||
| 959 | (when (and p (processp p)) (delete-process p)) | ||
| 960 | (with-tramp-progress-reporter vec 3 "Opening adb shell connection" | ||
| 961 | (let* ((coding-system-for-read 'utf-8-dos) ;is this correct? | ||
| 962 | (process-connection-type tramp-process-connection-type) | ||
| 963 | (args (if (tramp-file-name-host vec) | ||
| 964 | (list "-s" (tramp-file-name-host vec) "shell") | ||
| 965 | (list "shell"))) | ||
| 966 | (p (let ((default-directory | ||
| 967 | (tramp-compat-temporary-file-directory))) | ||
| 968 | (apply 'start-process (tramp-get-connection-name vec) buf | ||
| 969 | (tramp-adb-program) args)))) | ||
| 970 | (tramp-message | ||
| 971 | vec 6 "%s" (mapconcat 'identity (process-command p) " ")) | ||
| 972 | ;; Wait for initial prompt. | ||
| 973 | (tramp-adb-wait-for-output p) | ||
| 974 | (unless (eq 'run (process-status p)) | ||
| 975 | (tramp-error vec 'file-error "Terminated!")) | ||
| 976 | (set-process-query-on-exit-flag p nil))))))) | ||
| 977 | |||
| 978 | (provide 'tramp-adb) | ||
| 979 | ;;; tramp-adb.el ends here | ||