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