]> git.eshelyaron.com Git - emacs.git/commitdiff
Lift restrictions on `tramp-androidsu's app data access
authorPo Lu <luangruo@yahoo.com>
Wed, 28 Feb 2024 04:05:59 +0000 (12:05 +0800)
committerEshel Yaron <me@eshelyaron.com>
Wed, 28 Feb 2024 17:53:41 +0000 (18:53 +0100)
* lisp/net/tramp-adb.el (tramp-adb-handle-copy-file): Remove
code now unnecessary.

* lisp/net/tramp-androidsu.el
(tramp-androidsu-mount-global-namespace): New user option.
(tramp-androidsu-su-mm-supported): New variable.
(tramp-androidsu-maybe-open-connection): Detect whether su
supports the -mm option, and provide it if so.
(tramp-androidsu-adb-handle-copy-file)
(tramp-androidsu-adb-handle-rename-file): Delete functions.
(tramp-androidsu-sh-handle-copy-file)
(tramp-androidsu-sh-handle-rename-file): New functions.
(tramp-androidsu-file-name-handler-alist): Switch to tramp-sh's
copy and rename handlers.

(cherry picked from commit f1db8cf9a0595f7db29b548b38ce98196f36e09b)

lisp/net/tramp-adb.el
lisp/net/tramp-androidsu.el

index 4f04912c0328f15eeaffd208e38cea235aaf449c..3f216ba403ae04fa851c6202ee2be38d6711b6c3 100644 (file)
@@ -641,23 +641,10 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
                  ;; because `file-attributes' reads the values from
                  ;; there.
                  (tramp-flush-file-properties v localname)
-                 (unless (if (tramp-adb-file-name-p v)
-                             (tramp-adb-execute-adb-command
-                              v "push"
-                              (file-name-unquote filename)
-                              (file-name-unquote localname))
-                           ;; Otherwise, this operation was initiated
-                           ;; by the androidsu backend, so both files
-                           ;; must be present on the local machine and
-                           ;; transferable with a simple local copy.
-                           (tramp-adb-send-command-and-check
-                            v
-                            (format
-                             "cp -f %s %s"
-                             (tramp-shell-quote-argument
-                              (file-name-unquote filename))
-                             (tramp-shell-quote-argument
-                              (file-name-unquote localname)))))
+                 (unless (tramp-adb-execute-adb-command
+                          v "push"
+                          (file-name-unquote filename)
+                          (file-name-unquote localname))
                    (tramp-error
                     v 'file-error
                     "Cannot copy `%s' `%s'" filename newname)))))))))
index cf6b0d7202c91ba6f5c37c735a3e951fb6ee66d6..6d4ac2c17f157098da4f90ee37ac3809abda2ad0 100644 (file)
 (defconst tramp-androidsu-method "androidsu"
   "When this method name is used, forward all calls to su.")
 
+;;;###tramp-autoload
+(defcustom tramp-androidsu-mount-global-namespace t
+  "When non-nil, browse files from within the global mount namespace.
+On systems that assign each application a unique view of the filesystem
+by executing them within individual mount namespaces and thus conceal
+each application's data directories from others, invoke `su' with the
+option `-mm' in order for the shell launched to run within the global
+mount namespace, so that TRAMP may edit files belonging to any and all
+applications."
+  :group 'tramp
+  :version "30.1"
+  :type 'boolean)
+
+(defvar tramp-androidsu-su-mm-supported 'unknown
+  "Whether `su -mm' is supported on this system.")
+
 ;;;###tramp-autoload
 (tramp--with-startup
  (add-to-list 'tramp-methods
@@ -94,7 +110,7 @@ multibyte mode and waits for the shell prompt to appear."
                                        ;; Disregard
                                        ;; tramp-encoding-shell, as
                                        ;; there's no guarantee that it's
-                                       ;; possible to execute with
+                                       ;; possible to execute it with
                                        ;; `android-use-exec-loader' off.
                                       "/system/bin/sh" "-i"))
                     (user (tramp-file-name-user vec))
@@ -103,13 +119,32 @@ multibyte mode and waits for the shell prompt to appear."
                (set-process-sentinel p #'tramp-process-sentinel)
                (tramp-post-process-creation p vec)
 
-               ;; Replace `login-args' place holders.
+                ;; Replace `login-args' place holders.
                (setq command (format "exec su - %s || exit"
                                      (or user "root")))
-               ;; Send the command.
+
+                ;; Attempt to execute the shell inside the global mount
+                ;; namespace if requested.
+                (when tramp-androidsu-mount-global-namespace
+                  (progn
+                    (when (eq tramp-androidsu-su-mm-supported 'unknown)
+                      ;; Change the prompt in advance so that
+                      ;; tramp-adb-send-command-and-check can call
+                      ;; tramp-search-regexp.
+                     (tramp-adb-send-command
+                      vec (format "PS1=%s"
+                                  (tramp-shell-quote-argument
+                                    tramp-end-of-output)))
+                      (setq tramp-androidsu-su-mm-supported
+                            ;; Detect support for `su -mm'.
+                            (tramp-adb-send-command-and-check
+                             vec "su -mm -c 'exit 24'" 24)))
+                    (when tramp-androidsu-su-mm-supported
+                     (setq command (format "exec su -mm - %s || exit"
+                                           (or user "root"))))))
+               ;; Send the command.
                (tramp-message vec 3 "Sending command `%s'" command)
                (tramp-adb-send-command vec command t t)
-
                ;; Android su binaries contact a background service to
                ;; obtain authentication; during this process, input
                ;; received is discarded, so input cannot be
@@ -204,8 +239,8 @@ FUNCTION."
 (defalias 'tramp-androidsu-handle-copy-directory
   (tramp-androidsu-generate-wrapper #'tramp-handle-copy-directory))
 
-(defalias 'tramp-androidsu-adb-handle-copy-file
-  (tramp-androidsu-generate-wrapper #'tramp-adb-handle-copy-file))
+(defalias 'tramp-androidsu-sh-handle-copy-file
+  (tramp-androidsu-generate-wrapper #'tramp-sh-handle-copy-file))
 
 (defalias 'tramp-androidsu-adb-handle-delete-directory
   (tramp-androidsu-generate-wrapper #'tramp-adb-handle-delete-directory))
@@ -367,8 +402,8 @@ FUNCTION."
 (defalias 'tramp-androidsu-adb-handle-process-file
   (tramp-androidsu-generate-wrapper #'tramp-adb-handle-process-file))
 
-(defalias 'tramp-androidsu-adb-handle-rename-file
-  (tramp-androidsu-generate-wrapper #'tramp-adb-handle-rename-file))
+(defalias 'tramp-androidsu-sh-handle-rename-file
+  (tramp-androidsu-generate-wrapper #'tramp-sh-handle-rename-file))
 
 (defalias 'tramp-androidsu-adb-handle-set-file-modes
   (tramp-androidsu-generate-wrapper #'tramp-adb-handle-set-file-modes))
@@ -416,7 +451,7 @@ FUNCTION."
     (add-name-to-file . tramp-androidsu-handle-add-name-to-file)
     ;; `byte-compiler-base-file-name' performed by default handler.
     (copy-directory . tramp-androidsu-handle-copy-directory)
-    (copy-file . tramp-androidsu-adb-handle-copy-file)
+    (copy-file . tramp-androidsu-sh-handle-copy-file)
     (delete-directory . tramp-androidsu-adb-handle-delete-directory)
     (delete-file . tramp-androidsu-adb-handle-delete-file)
     ;; `diff-latest-backup-file' performed by default handler.
@@ -478,7 +513,7 @@ FUNCTION."
     (memory-info . tramp-androidsu-handle-memory-info)
     (process-attributes . tramp-androidsu-handle-process-attributes)
     (process-file . tramp-androidsu-adb-handle-process-file)
-    (rename-file . tramp-androidsu-adb-handle-rename-file)
+    (rename-file . tramp-androidsu-sh-handle-rename-file)
     (set-file-acl . ignore)
     (set-file-modes . tramp-androidsu-adb-handle-set-file-modes)
     (set-file-selinux-context . ignore)