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