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