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