Commit | Line | Data |
---|---|---|
0ec3f7ea JH |
1 | ;;; tramp-adb.el --- Functions for calling Android Debug Bridge from Tramp |
2 | ||
7fc5e83f | 3 | ;; Copyright (C) 2011-2012 Free Software Foundation, Inc. |
0ec3f7ea JH |
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 | |
51b890ac MA |
42 | :version "24.4" |
43 | :group 'tramp) | |
0ec3f7ea JH |
44 | |
45 | ;;;###tramp-autoload | |
46 | (defconst tramp-adb-method "adb" | |
47 | "*When this method name is used, forward all calls to Android Debug Bridge.") | |
48 | ||
bd8c13f9 JH |
49 | (defcustom tramp-adb-prompt |
50 | "^\\(?:[[:alnum:]]*@[[:alnum:]]*[^#\\$]*\\)?[#\\$][[:space:]]" | |
51b890ac | 51 | "Regexp used as prompt in almquist shell." |
0ec3f7ea | 52 | :type 'string |
51b890ac MA |
53 | :version "24.4" |
54 | :group 'tramp) | |
0ec3f7ea | 55 | |
bd8c13f9 JH |
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:]]") | |
0ec3f7ea JH |
58 | |
59 | ;;;###tramp-autoload | |
60 | (add-to-list 'tramp-methods `(,tramp-adb-method)) | |
61 | ||
62 | ;;;###tramp-autoload | |
63 | (eval-after-load 'tramp | |
64 | '(tramp-set-completion-function | |
65 | tramp-adb-method '((tramp-adb-parse-device-names "")))) | |
66 | ||
67 | ;;;###tramp-autoload | |
68 | (add-to-list 'tramp-foreign-file-name-handler-alist | |
69 | (cons 'tramp-adb-file-name-p 'tramp-adb-file-name-handler)) | |
70 | ||
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.") | |
115 | ||
b421decc MA |
116 | ;; It must be a `defsubst' in order to push the whole code into |
117 | ;; tramp-loaddefs.el. Otherwise, there would be recursive autoloading. | |
0ec3f7ea | 118 | ;;;###tramp-autoload |
6ce21463 | 119 | (defsubst tramp-adb-file-name-p (filename) |
0ec3f7ea JH |
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))) | |
123 | ||
124 | ;;;###tramp-autoload | |
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 | |
131 | ;; useful for us. | |
132 | (tramp-default-host | |
133 | (unless (equal (eval (car (get 'tramp-default-host 'standard-value))) | |
134 | tramp-default-host) | |
135 | tramp-default-host))) | |
136 | (if fn | |
137 | (save-match-data (apply (cdr fn) args)) | |
138 | (tramp-run-real-handler operation args)))) | |
139 | ||
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)) | |
144 | ||
145 | ;;;###tramp-autoload | |
146 | (defun tramp-adb-parse-device-names (ignore) | |
147 | "Return a list of (nil host) tuples allowed to access." | |
148 | (with-temp-buffer | |
149 | (when (zerop (call-process (tramp-adb-program) nil t nil "devices")) | |
150 | (let (result) | |
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)))) | |
154 | result)))) | |
155 | ||
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)) | |
166 | ;; Dissect NAME. | |
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 | |
178 | method user host | |
179 | (tramp-drop-volume-letter | |
180 | (tramp-run-real-handler | |
181 | 'expand-file-name (list localname)))))))) | |
182 | ||
183 | (defun tramp-adb-handle-file-directory-p (filename) | |
184 | "Like `file-directory-p' for Tramp files." | |
185 | (car (file-attributes (file-truename filename)))) | |
186 | ||
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)) | |
200 | (thisstep nil) | |
201 | (numchase 0) | |
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. | |
207 | (numchase-limit 20) | |
208 | symlink-target) | |
209 | (while (and steps (< numchase numchase-limit)) | |
210 | (setq thisstep (pop steps)) | |
211 | (tramp-message | |
212 | v 5 "Check %s" | |
213 | (mapconcat 'identity | |
214 | (append '("") (reverse result) (list thisstep)) | |
215 | "/")) | |
216 | (setq symlink-target | |
217 | (nth 0 (file-attributes | |
218 | (tramp-make-tramp-file-name | |
219 | method user host | |
220 | (mapconcat 'identity | |
221 | (append '("") | |
222 | (reverse result) | |
223 | (list thisstep)) | |
224 | "/"))))) | |
225 | (cond ((string= "." thisstep) | |
226 | (tramp-message v 5 "Ignoring step `.'")) | |
227 | ((string= ".." thisstep) | |
228 | (tramp-message v 5 "Processing step `..'") | |
229 | (pop result)) | |
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) | |
235 | (setq result nil)) | |
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) | |
241 | (tramp-error | |
242 | v 'file-error | |
243 | "Symlink target `%s' on wrong host" symlink-target)) | |
244 | (setq symlink-target localname)) | |
245 | (setq steps | |
246 | (append (tramp-compat-split-string | |
247 | symlink-target "/") | |
248 | steps))) | |
249 | (t | |
250 | ;; It's a file. | |
251 | (setq result (cons thisstep result))))) | |
252 | (when (>= numchase numchase-limit) | |
253 | (tramp-error | |
254 | v 'file-error | |
255 | "Maximum number (%d) of symlinks exceeded" numchase-limit)) | |
256 | (setq result (reverse result)) | |
257 | ;; Combine list to form string. | |
258 | (setq result | |
259 | (if result | |
260 | (mapconcat 'identity (cons "" result) "/") | |
261 | "/")) | |
262 | (when (and is-dir (or (string= "" result) | |
263 | (not (string= (substring result -1) "/")))) | |
264 | (setq result (concat result "/")))) | |
265 | ||
266 | (tramp-message v 4 "True name of `%s' is `%s'" filename result) | |
267 | (tramp-make-tramp-file-name method user host result))))) | |
268 | ||
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)) | |
272 | (ignore-errors | |
273 | (with-parsed-tramp-file-name filename nil | |
bd8c13f9 JH |
274 | (with-tramp-file-property |
275 | v localname (format "file-attributes-%s" id-format) | |
0ec3f7ea | 276 | (tramp-adb-barf-unless-okay |
bd8c13f9 JH |
277 | v (format "%s -d -l %s" |
278 | (tramp-adb-get-ls-command v) | |
279 | (tramp-shell-quote-argument localname)) "") | |
0ec3f7ea JH |
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))) | |
bd8c13f9 JH |
286 | (symlink-target |
287 | (and is-symlink | |
288 | (cadr (split-string (buffer-string) "\\( -> \\|\n\\)")))) | |
0ec3f7ea JH |
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)))) | |
293 | (list | |
294 | (or is-dir symlink-target) | |
295 | 1 ;link-count | |
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) | |
299 | '(0 0) ; atime | |
300 | (date-to-time date) ; mtime | |
301 | '(0 0) ; ctime | |
302 | size | |
303 | mod-string | |
304 | ;; fake | |
305 | t 1 1))))))) | |
306 | ||
bd8c13f9 JH |
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 | |
314 | ;; when possible. | |
315 | "ls --color=never" | |
316 | "ls"))) | |
317 | ||
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") | |
323 | (cond | |
324 | ((zerop (tramp-adb-command-exit-status vec "busybox")) 'busybox) | |
325 | ((zerop (tramp-adb-command-exit-status vec "toolbox")) 'toolbox) | |
326 | (t 'unkown)))) | |
327 | ||
0ec3f7ea JH |
328 | (defun tramp-adb--gnu-switches-to-ash |
329 | (switches) | |
330 | "Almquist shell can't handle multiple arguments. | |
331 | Convert (\"-al\") to (\"-a\" \"-l\"). Remove arguments like \"--dired\"." | |
332 | (split-string | |
333 | (apply 'concat | |
334 | (mapcar (lambda (s) | |
335 | (replace-regexp-in-string | |
336 | "\\(.\\)" " -\\1" | |
337 | (replace-regexp-in-string "^-" "" s))) | |
338 | ;; FIXME: Warning about removed switches (long and non-dash). | |
339 | (delq nil | |
340 | (mapcar | |
bd8c13f9 JH |
341 | (lambda (s) |
342 | (and (not (string-match "\\(^--\\|^[^-]\\)" s)) s)) | |
0ec3f7ea JH |
343 | switches)))))) |
344 | ||
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 | |
bd8c13f9 | 357 | v (format "%s %s %s" (tramp-adb-get-ls-command v) switches name) |
0ec3f7ea JH |
358 | "Cannot insert directory listing: %s" filename) |
359 | (unless switch-d | |
360 | ;; We insert also filename/. and filename/.., because "ls" doesn't. | |
361 | (narrow-to-region (point) (point)) | |
362 | (ignore-errors | |
363 | (tramp-adb-barf-unless-okay | |
bd8c13f9 JH |
364 | v (format "%s -d %s %s %s" |
365 | (tramp-adb-get-ls-command v) | |
0ec3f7ea JH |
366 | switches |
367 | (concat (file-name-as-directory name) ".") | |
368 | (concat (file-name-as-directory name) "..")) | |
369 | "Cannot insert directory listing: %s" filename)) | |
370 | (widen)) | |
371 | (tramp-adb-sh-fix-ls-output switch-t))) | |
372 | (insert-buffer-substring (tramp-get-buffer v)))) | |
373 | ||
374 | (defun tramp-adb-sh-fix-ls-output (&optional sort-by-time) | |
bd8c13f9 JH |
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." | |
0ec3f7ea JH |
378 | (save-excursion |
379 | ;; Insert missing size. | |
380 | (goto-char (point-min)) | |
bd8c13f9 JH |
381 | (while |
382 | (search-forward-regexp | |
383 | "[[:space:]]\\([[:space:]][0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9][[:space:]]\\)" nil t) | |
0ec3f7ea JH |
384 | (replace-match "0\\1" "\\1" nil) |
385 | ;; Insert missing "/". | |
386 | (when (looking-at "[0-9][0-9]:[0-9][0-9][[:space:]]+$") | |
387 | (end-of-line) | |
388 | (insert "/"))) | |
389 | ;; Sort entries. | |
390 | (let* ((lines (split-string (buffer-string) "\n" t)) | |
391 | (sorted-lines | |
392 | (sort | |
393 | lines | |
394 | (if sort-by-time | |
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)) | |
402 | (insert "\n")))) | |
403 | ||
404 | ||
405 | (defun tramp-adb-ls-output-time-less-p (a b) | |
406 | "Sort \"ls\" output by time, descending." | |
407 | (let (time-a time-b) | |
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))) | |
413 | ||
414 | (defun tramp-adb-ls-output-name-less-p (a b) | |
415 | "Sort \"ls\" output by name, ascending." | |
416 | (let (posa posb) | |
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)))) | |
422 | ||
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 | |
427 | (when parents | |
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)))) | |
435 | ||
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 | |
443 | v (format "%s %s" | |
444 | (if recursive "rm -r" "rmdir") | |
445 | (tramp-shell-quote-argument localname)) | |
446 | "Couldn't delete %s" directory))) | |
447 | ||
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))) | |
457 | ||
458 | (defun tramp-adb-handle-file-name-all-completions (filename directory) | |
459 | "Like `file-name-all-completions' for Tramp files." | |
460 | (all-completions | |
461 | filename | |
462 | (with-parsed-tramp-file-name directory nil | |
463 | (with-tramp-file-property v localname "file-name-all-completions" | |
464 | (save-match-data | |
465 | (tramp-adb-send-command | |
bd8c13f9 JH |
466 | v (format "%s %s" |
467 | (tramp-adb-get-ls-command v) | |
468 | (tramp-shell-quote-argument localname))) | |
0ec3f7ea JH |
469 | (mapcar |
470 | (lambda (f) | |
471 | (if (file-directory-p f) | |
472 | (file-name-as-directory f) | |
473 | f)) | |
474 | (with-current-buffer (tramp-get-buffer v) | |
475 | (delq | |
476 | nil | |
477 | (mapcar | |
478 | (lambda (l) (and (not (string-match "^[[:space:]]*$" l)) l)) | |
479 | (split-string (buffer-string) "\n")))))))))) | |
480 | ||
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)) | |
485 | (tramp-error | |
486 | v 'file-error | |
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) | |
493 | (tramp-error | |
494 | v 'file-error "Cannot make local copy of file `%s'" filename)) | |
495 | (set-file-modes tmpfile (file-modes filename))) | |
496 | tmpfile))) | |
497 | ||
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) | |
505 | (zerop | |
506 | (tramp-adb-command-exit-status | |
507 | v (format "test -w %s" (tramp-shell-quote-argument localname)))) | |
508 | (and | |
509 | (file-directory-p (file-name-directory filename)) | |
510 | (file-writable-p (file-name-directory filename)))) | |
511 | ||
512 | ;; Missing "test" command on Android < 4. | |
513 | (let ((rw-path "/data/data")) | |
514 | (tramp-message | |
515 | v 5 | |
516 | "Not implemented yet (assuming \"/data/data\" is writable): %s" | |
517 | localname) | |
518 | (and (>= (length localname) (length rw-path)) | |
519 | (string= (substring localname 0 (length rw-path)) | |
520 | rw-path))))))) | |
521 | ||
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 | |
527 | (when append | |
528 | (tramp-error | |
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? " | |
532 | filename)) | |
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 | |
541 | 'write-region | |
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) | |
545 | (unwind-protect | |
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))) | |
549 | ||
550 | (unless (equal curbuf (current-buffer)) | |
551 | (tramp-error | |
552 | v 'file-error | |
553 | "Buffer has changed from `%s' to `%s'" curbuf (current-buffer)))))) | |
554 | ||
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))) | |
562 | ||
563 | (defun tramp-adb-handle-copy-file | |
564 | (filename newname &optional ok-if-already-exists keep-date | |
53b6a8b1 | 565 | preserve-uid-gid preserve-extended-attributes) |
0ec3f7ea | 566 | "Like `copy-file' for Tramp files. |
53b6a8b1 | 567 | PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored." |
0ec3f7ea JH |
568 | (setq filename (expand-file-name filename) |
569 | newname (expand-file-name newname)) | |
570 | ||
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) | |
576 | ||
577 | (let ((tmpfile (file-local-copy filename))) | |
578 | ||
579 | (if tmpfile | |
580 | ;; Remote filename. | |
581 | (condition-case err | |
582 | (rename-file tmpfile newname ok-if-already-exists) | |
583 | ((error quit) | |
584 | (delete-file tmpfile) | |
585 | (signal (car err) (cdr err)))) | |
586 | ||
587 | ;; Remote newname. | |
588 | (when (file-directory-p newname) | |
589 | (setq newname | |
590 | (expand-file-name (file-name-nondirectory filename) newname))) | |
591 | ||
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)) | |
596 | ||
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) | |
602 | (tramp-error | |
603 | v 'file-error "Cannot copy `%s' `%s'" filename newname)))))) | |
604 | ||
605 | ;; KEEP-DATE handling. | |
606 | (when keep-date | |
607 | (set-file-times newname (nth 5 (file-attributes filename)))))) | |
608 | ||
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)) | |
614 | ||
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) | |
619 | ||
620 | (if (and (tramp-equal-remote filename newname) | |
621 | (not (file-directory-p filename))) | |
622 | (progn | |
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) | |
630 | ;; Short track. | |
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)) | |
634 | ||
635 | ;; Rename by copy. | |
636 | (copy-file filename newname ok-if-already-exists t t) | |
637 | (delete-file filename))))) | |
638 | ||
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")) | |
645 | ||
646 | (with-parsed-tramp-file-name default-directory nil | |
647 | (let (command input tmpinput stderr tmpstderr outbuf ret) | |
648 | ;; Compute command. | |
649 | (setq command (mapconcat 'tramp-shell-quote-argument | |
650 | (cons program args) " ")) | |
651 | ;; Determine input. | |
652 | (if (null infile) | |
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))) | |
663 | ||
664 | ;; Determine output. | |
665 | (cond | |
666 | ;; Just a buffer. | |
667 | ((bufferp destination) | |
668 | (setq outbuf destination)) | |
669 | ;; A buffer name. | |
670 | ((stringp destination) | |
671 | (setq outbuf (get-buffer-create destination))) | |
672 | ;; (REAL-DESTINATION ERROR-DESTINATION) | |
673 | ((consp destination) | |
674 | ;; output. | |
675 | (cond | |
676 | ((bufferp (car destination)) | |
677 | (setq outbuf (car destination))) | |
678 | ((stringp (car destination)) | |
679 | (setq outbuf (get-buffer-create (car destination)))) | |
680 | ((car destination) | |
681 | (setq outbuf (current-buffer)))) | |
682 | ;; stderr. | |
683 | (cond | |
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")))) | |
698 | ;; 't | |
699 | (destination | |
700 | (setq outbuf (current-buffer)))) | |
701 | (when stderr (setq command (format "%s 2>%s" command stderr))) | |
702 | ||
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 | |
705 | ;; directory. | |
706 | (condition-case nil | |
707 | (progn | |
708 | (setq ret 0 | |
709 | ret | |
710 | (tramp-adb-barf-unless-okay | |
711 | v (format "(cd %s; %s)" | |
712 | (tramp-shell-quote-argument localname) | |
713 | command) | |
714 | "")) | |
715 | ;; We should show the output anyway. | |
716 | (when outbuf | |
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. | |
722 | (quit | |
723 | (kill-buffer (tramp-get-connection-buffer v)) | |
724 | (setq ret -1)) | |
725 | ;; Handle errors. | |
726 | (error | |
727 | (kill-buffer (tramp-get-connection-buffer v)) | |
728 | (setq ret 1))) | |
729 | ||
730 | ;; Provide error file. | |
731 | (when tmpstderr (rename-file tmpstderr (cadr destination) t)) | |
732 | ||
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)) | |
736 | ||
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 | |
740 | ;; value 't'. | |
741 | (unless (and (boundp 'process-file-side-effects) | |
742 | (not (symbol-value 'process-file-side-effects))) | |
743 | (tramp-flush-directory-property v "")) | |
744 | ||
745 | ;; Return exit status. | |
746 | (if (equal ret -1) | |
747 | (keyboard-quit) | |
748 | ret)))) | |
749 | ||
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))) | |
757 | current-buffer-p | |
758 | (output-buffer | |
759 | (cond | |
760 | ((bufferp output-buffer) output-buffer) | |
761 | ((stringp output-buffer) (get-buffer-create output-buffer)) | |
762 | (output-buffer | |
763 | (setq current-buffer-p t) | |
764 | (current-buffer)) | |
765 | (t (get-buffer-create | |
766 | (if asynchronous | |
767 | "*Async Shell Command*" | |
768 | "*Shell Command Output*"))))) | |
769 | (error-buffer | |
770 | (cond | |
771 | ((bufferp error-buffer) error-buffer) | |
772 | ((stringp error-buffer) (get-buffer-create error-buffer)))) | |
773 | (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))) | |
777 | output-buffer)) | |
778 | (p (get-buffer-process output-buffer))) | |
779 | ||
780 | ;; Check whether there is another process running. Tramp does not | |
781 | ;; support 2 (asynchronous) processes in parallel. | |
782 | (when p | |
783 | (if (yes-or-no-p "A command is running. Kill it? ") | |
784 | (ignore-errors (kill-process p)) | |
785 | (error "Shell command in progress"))) | |
786 | ||
787 | (if current-buffer-p | |
788 | (progn | |
789 | (barf-if-buffer-read-only) | |
790 | (push-mark nil t)) | |
791 | (with-current-buffer output-buffer | |
792 | (setq buffer-read-only nil) | |
793 | (erase-buffer))) | |
794 | ||
795 | (if (and (not current-buffer-p) (integerp asynchronous)) | |
796 | (prog1 | |
797 | ;; Run the process. | |
798 | (apply 'start-file-process "*Async Shell*" buffer args) | |
799 | ;; Display output. | |
800 | (pop-to-buffer output-buffer) | |
801 | (setq mode-line-process '(":%s")) | |
802 | (shell-mode)) | |
803 | ||
804 | (prog1 | |
805 | ;; Run the process. | |
806 | (apply 'process-file (car args) nil buffer nil (cdr args)) | |
807 | ;; Insert error messages if they were separated. | |
808 | (when (listp buffer) | |
809 | (with-current-buffer error-buffer | |
810 | (insert-file-contents (cadr buffer))) | |
811 | (delete-file (cadr buffer))) | |
812 | (if current-buffer-p | |
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) | |
819 | (current-buffer)))) | |
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)))))))) | |
825 | ||
bd8c13f9 | 826 | ;; We use BUFFER also as connection buffer during setup. Because of |
0ec3f7ea JH |
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. | |
833 | (let ((command | |
834 | (when (stringp program) | |
835 | (format "cd %s; %s" | |
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))) | |
842 | (name1 name) | |
843 | (i 0)) | |
844 | (unwind-protect | |
845 | (save-excursion | |
846 | (save-restriction | |
847 | (unless 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. | |
852 | (setq i (1+ i) | |
853 | name1 (format "%s<%d>" name i))) | |
854 | (setq name name1) | |
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)) | |
865 | (if command | |
866 | ;; Send the command. | |
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) | |
875 | ;; Return process. | |
876 | p))) | |
877 | ;; Save exit. | |
878 | (with-current-buffer (tramp-get-connection-buffer v) | |
879 | (if (string-match tramp-temp-buffer-name (buffer-name)) | |
880 | (progn | |
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))))) | |
886 | ||
887 | ;; Android < 4 doesn't provide test command. | |
888 | ||
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)))) | |
894 | ||
895 | ;; Helper functions. | |
896 | ||
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))) | |
901 | (with-temp-buffer | |
902 | (prog1 | |
903 | (unless (zerop (apply 'call-process (tramp-adb-program) nil t nil args)) | |
904 | (buffer-string)) | |
905 | (tramp-message | |
906 | vec 6 "%s %s\n%s" | |
907 | (tramp-adb-program) (mapconcat 'identity args " ") (buffer-string))))) | |
908 | ||
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")))) | |
914 | ||
915 | ;; Connection functions | |
916 | ||
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) | |
925 | (save-excursion | |
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))))) | |
935 | ||
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) | |
943 | (tramp-error | |
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))))) | |
950 | ||
951 | (defun tramp-adb-command-exit-status | |
952 | (vec command) | |
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) | |
960 | (tramp-error | |
961 | vec 'file-error "Couldn't find exit status of `%s'" command)) | |
962 | (skip-chars-forward "^ ") | |
963 | (read (current-buffer)))) | |
964 | ||
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)) | |
d754b364 JH |
974 | ;; ADB terminal sends "^H" sequences. |
975 | (when (re-search-forward "<\b+" (point-at-eol) t) | |
0ec3f7ea JH |
976 | (forward-line 1) |
977 | (delete-region (point-min) (point))) | |
978 | ;; Delete the prompt. | |
d754b364 JH |
979 | (goto-char (point-min)) |
980 | (when (re-search-forward tramp-adb-prompt (point-at-eol) t) | |
981 | (forward-line 1) | |
982 | (delete-region (point-min) (point))) | |
0ec3f7ea JH |
983 | (goto-char (point-max)) |
984 | (re-search-backward tramp-adb-prompt nil t) | |
985 | (delete-region (point) (point-max))) | |
986 | (if timeout | |
987 | (tramp-error | |
988 | proc 'file-error | |
989 | "[[Remote adb prompt `%s' not found in %d secs]]" | |
990 | tramp-adb-prompt timeout) | |
991 | (tramp-error | |
992 | proc 'file-error | |
993 | "[[Remote prompt `%s' not found]]" tramp-adb-prompt))))) | |
994 | ||
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))) | |
1001 | (unless | |
1002 | (and p (processp p) (memq (process-status p) '(run open))) | |
1003 | (save-match-data | |
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") | |
1010 | (list "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)))) | |
1015 | (tramp-message | |
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!")) | |
cdb07539 MA |
1021 | (set-process-query-on-exit-flag p nil) |
1022 | ||
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`\\\"") | |
1029 | (let ((old-getprop | |
1030 | (tramp-get-connection-property vec "getprop" nil)) | |
1031 | (new-getprop | |
1032 | (tramp-set-connection-property | |
1033 | vec "getprop" | |
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))) | |
1040 | (tramp-cleanup vec) | |
1041 | (tramp-message | |
1042 | vec 3 | |
1043 | "Connection reset, because remote host changed from `%s' to `%s'" | |
1044 | old-getprop new-getprop) | |
1045 | (tramp-adb-maybe-open-connection vec))))))))) | |
0ec3f7ea JH |
1046 | |
1047 | (provide 'tramp-adb) | |
1048 | ;;; tramp-adb.el ends here |