Hi, all,
In this patch, I enabled the adb port access in tramp adb module. The patch
is attached, please help review, thanks a lot!

Best regards,
Zhongwei
From 803df58a74f6aa6b79e1709fd307ff247b20c2fe Mon Sep 17 00:00:00 2001
From: Zhongwei Yao <ashi08...@gmail.com>
Date: Wed, 24 Sep 2014 17:38:39 +0800
Subject: [PATCH] enable adb access with port format, e.g.
 /adb:164.2.168.1#5555:/

---
 lisp/tramp-adb.el | 172 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 94 insertions(+), 78 deletions(-)

diff --git a/lisp/tramp-adb.el b/lisp/tramp-adb.el
index 8c1cbbe..f4c3600 100644
--- a/lisp/tramp-adb.el
+++ b/lisp/tramp-adb.el
@@ -989,10 +989,24 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
 
 ;; Helper functions.
 
+(defun tramp-adb-get-real-host (host)
+  "Returns real host (with port or without port) from tramp host format
+e.g. input: 192.168.1.1#5555 return 192.168.1.1:5555
+     input: R38273882DE return R38273882DE"
+  (let ((l-port)
+        (l-host)
+        (real-host))
+    (if (string-match tramp-host-with-port-regexp host)
+        (setq l-port (match-string 2 host)
+              l-host (match-string 1 host)
+              real-host (concat l-host ":" l-port))
+      (setq real-host host))))
+
 (defun tramp-adb-execute-adb-command (vec &rest args)
   "Returns nil on success error-output on failure."
-  (when (> (length (tramp-file-name-host vec)) 0)
-    (setq args (append (list "-s" (tramp-file-name-host vec)) args)))
+  (let ((host (tramp-file-name-host vec)))
+    (when (> (length host) 0)
+      (setq args (append (list "-s" (tramp-adb-get-real-host host)) args))))
   (with-temp-buffer
     (prog1
 	(unless
@@ -1102,82 +1116,84 @@ connection if a previous connection has died for some reason."
     (when (and user (not (tramp-get-file-property vec "" "su-command-p" t)))
       (tramp-error vec 'file-error "Cannot switch to user `%s'" user))
 
-    (unless
-	(and p (processp p) (memq (process-status p) '(run open)))
-      (save-match-data
-	(when (and p (processp p)) (delete-process p))
-	(setq devices (mapcar 'cadr (tramp-adb-parse-device-names nil)))
-	(if (not devices)
-	    (tramp-error vec 'file-error "No device connected"))
-	(if (and (> (length host) 0) (not (member host devices)))
-	    (tramp-error vec 'file-error "Device %s not connected" host))
-	(if (and (> (length devices) 1) (zerop (length host)))
-	    (tramp-error
-	     vec 'file-error
-	     "Multiple Devices connected: No Host/Device specified"))
-	(with-tramp-progress-reporter vec 3 "Opening adb shell connection"
-	  (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
-		 (process-connection-type tramp-process-connection-type)
-		 (args (if (> (length host) 0)
-			   (list "-s" host "shell")
-			 (list "shell")))
-		 (p (let ((default-directory
-			    (tramp-compat-temporary-file-directory)))
-		      (apply 'start-process (tramp-get-connection-name vec) buf
-			     tramp-adb-program args))))
-	    (tramp-message
-	     vec 6 "%s" (mapconcat 'identity (process-command p) " "))
-	    ;; Wait for initial prompt.
-	    (tramp-adb-wait-for-output p 30)
-	    (unless (eq 'run (process-status p))
-	      (tramp-error  vec 'file-error "Terminated!"))
-	    (tramp-set-connection-property p "vector" vec)
-	    (tramp-compat-set-process-query-on-exit-flag p nil)
-
-	    ;; Check whether the properties have been changed.  If
-	    ;; yes, this is a strong indication that we must expire all
-	    ;; connection properties.  We start again.
-	    (tramp-message vec 5 "Checking system information")
-	    (tramp-adb-send-command
-	     vec "echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
-	    (let ((old-getprop
-		   (tramp-get-connection-property vec "getprop" nil))
-		  (new-getprop
-		   (tramp-set-connection-property
-		    vec "getprop"
-		    (with-current-buffer (tramp-get-connection-buffer vec)
-		      ;; Read the expression.
-		      (goto-char (point-min))
-		      (read (current-buffer))))))
-	      (when (and (stringp old-getprop)
-			 (not (string-equal old-getprop new-getprop)))
-		(tramp-message
-		 vec 3
-		 "Connection reset, because remote host changed from `%s' to `%s'"
-		 old-getprop new-getprop)
-		(tramp-cleanup-connection vec t)
-		(tramp-adb-maybe-open-connection vec)))
-
-	    ;; Change user if indicated.
-	    (when user
-	      (tramp-adb-send-command vec (format "su %s" user))
-	      (unless (tramp-adb-send-command-and-check vec nil)
-		(delete-process p)
-		(tramp-set-file-property vec "" "su-command-p" nil)
-		(tramp-error
-		 vec 'file-error "Cannot switch to user `%s'" user)))
-
-	    ;; Set "remote-path" connection property.  This is needed
-	    ;; for eshell.
-	    (tramp-adb-send-command vec "echo \\\"$PATH\\\"")
-	    (tramp-set-connection-property
-	     vec "remote-path"
-	     (split-string
-	      (with-current-buffer (tramp-get-connection-buffer vec)
-		;; Read the expression.
-		(goto-char (point-min))
-		(read (current-buffer)))
-	      ":" 'omit-nulls))))))))
+    (let* ((host (tramp-file-name-host vec))
+           (real-host (tramp-adb-get-real-host host)))
+      (unless
+          (and p (processp p) (memq (process-status p) '(run open)))
+        (save-match-data
+          (when (and p (processp p)) (delete-process p))
+          (setq devices (mapcar 'cadr (tramp-adb-parse-device-names nil)))
+          (if (not devices)
+              (tramp-error vec 'file-error "No device connected"))
+          (if (and (> (length host) 0) (not (member real-host devices)))
+              (tramp-error vec 'file-error "Device %s not connected" real-host))
+          (if (and (> (length devices) 1) (zerop (length real-host)))
+              (tramp-error
+               vec 'file-error
+               "Multiple Devices connected: No Host/Device specified"))
+          (with-tramp-progress-reporter vec 3 "Opening adb shell connection"
+            (let* ((coding-system-for-read 'utf-8-dos) ;is this correct?
+                   (process-connection-type tramp-process-connection-type)
+                   (args (if (> (length real-host) 0)
+                             (list "-s" real-host "shell")
+                           (list "shell")))
+                   (p (let ((default-directory
+                              (tramp-compat-temporary-file-directory)))
+                        (apply 'start-process (tramp-get-connection-name vec) buf
+                               tramp-adb-program args))))
+              (tramp-message
+               vec 6 "%s" (mapconcat 'identity (process-command p) " "))
+              ;; Wait for initial prompt.
+              (tramp-adb-wait-for-output p 30)
+              (unless (eq 'run (process-status p))
+                (tramp-error  vec 'file-error "Terminated!"))
+              (tramp-set-connection-property p "vector" vec)
+              (tramp-compat-set-process-query-on-exit-flag p nil)
+
+              ;; Check whether the properties have been changed.  If
+              ;; yes, this is a strong indication that we must expire all
+              ;; connection properties.  We start again.
+              (tramp-message vec 5 "Checking system information")
+              (tramp-adb-send-command
+               vec "echo \\\"`getprop ro.product.model` `getprop ro.product.version` `getprop ro.build.version.release`\\\"")
+              (let ((old-getprop
+                     (tramp-get-connection-property vec "getprop" nil))
+                    (new-getprop
+                     (tramp-set-connection-property
+                      vec "getprop"
+                      (with-current-buffer (tramp-get-connection-buffer vec)
+                        ;; Read the expression.
+                        (goto-char (point-min))
+                        (read (current-buffer))))))
+                (when (and (stringp old-getprop)
+                           (not (string-equal old-getprop new-getprop)))
+                  (tramp-message
+                   vec 3
+                   "Connection reset, because remote host changed from `%s' to `%s'"
+                   old-getprop new-getprop)
+                  (tramp-cleanup-connection vec t)
+                  (tramp-adb-maybe-open-connection vec)))
+
+              ;; Change user if indicated.
+              (when user
+                (tramp-adb-send-command vec (format "su %s" user))
+                (unless (tramp-adb-send-command-and-check vec nil)
+                  (delete-process p)
+                  (tramp-set-file-property vec "" "su-command-p" nil)
+                  (tramp-error
+                   vec 'file-error "Cannot switch to user `%s'" user)))
+
+              ;; Set "remote-path" connection property.  This is needed
+              ;; for eshell.
+              (tramp-adb-send-command vec "echo \\\"$PATH\\\"")
+              (tramp-set-connection-property
+               vec "remote-path"
+               (split-string
+                (with-current-buffer (tramp-get-connection-buffer vec)
+                  ;; Read the expression.
+                  (goto-char (point-min))
+                  (read (current-buffer)))
+                ":" 'omit-nulls)))))))))
 
 (add-hook 'tramp-unload-hook
 	  (lambda ()
-- 
1.8.3.2

_______________________________________________
Tramp-devel mailing list
Tramp-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/tramp-devel

Reply via email to