* net/tramp-sh.el (tramp-get-remote-id): Do not raise an error.
authorMichael Albinus <michael.albinus@gmx.de>
Thu, 19 Sep 2013 11:08:01 +0000 (13:08 +0200)
committerMichael Albinus <michael.albinus@gmx.de>
Thu, 19 Sep 2013 11:08:01 +0000 (13:08 +0200)
(tramp-get-remote-uid-with-id, tramp-get-remote-gid-with-id)
(tramp-get-remote-python): New defuns.
(tramp-get-remote-uid-with-perl)
(tramp-get-remote-gid-with-perl): New defuns.  Perl code
contributed by yary <not.com@gmail.com> (tiny change).
(tramp-get-remote-uid-with-python)
(tramp-get-remote-gid-with-python): New defuns.  Python code
contributed by Andrey Tykhonov <atykhonov@gmail.com> (tiny change).
(tramp-get-remote-uid, tramp-get-remote-gid): Use new defuns.

lisp/ChangeLog
lisp/net/tramp-sh.el

index fe5e82c..a557359 100644 (file)
@@ -1,3 +1,16 @@
+2013-09-19  Michael Albinus  <michael.albinus@gmx.de>
+
+       * net/tramp-sh.el (tramp-get-remote-id): Do not raise an error.
+       (tramp-get-remote-uid-with-id, tramp-get-remote-gid-with-id)
+       (tramp-get-remote-python): New defuns.
+       (tramp-get-remote-uid-with-perl)
+       (tramp-get-remote-gid-with-perl): New defuns.  Perl code
+       contributed by yary <not.com@gmail.com> (tiny change).
+       (tramp-get-remote-uid-with-python)
+       (tramp-get-remote-gid-with-python): New defuns.  Python code
+       contributed by Andrey Tykhonov <atykhonov@gmail.com> (tiny change).
+       (tramp-get-remote-uid, tramp-get-remote-gid): Use new defuns.
+
 2013-09-19  Glenn Morris  <rgm@gnu.org>
 
        * emacs-lisp/eieio.el (class-parent): Don't use defalias with macros.
index e37c34e..def19e9 100644 (file)
@@ -4950,38 +4950,97 @@ Return ATTR."
 (defun tramp-get-remote-id (vec)
   (with-tramp-connection-property vec "id"
     (tramp-message vec 5 "Finding POSIX `id' command")
-    (or
-     (catch 'id-found
-       (let ((dl (tramp-get-remote-path vec))
-            result)
-        (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
-          ;; Check POSIX parameter.
-          (when (tramp-send-command-and-check vec (format "%s -u" result))
-            (throw 'id-found result))
-          (setq dl (cdr dl)))))
-     (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
+    (catch 'id-found
+      (let ((dl (tramp-get-remote-path vec))
+           result)
+       (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
+         ;; Check POSIX parameter.
+         (when (tramp-send-command-and-check vec (format "%s -u" result))
+           (throw 'id-found result))
+         (setq dl (cdr dl)))))))
+
+(defun tramp-get-remote-uid-with-id (vec id-format)
+  (tramp-send-command-and-read
+   vec
+   (format "%s -u%s %s"
+          (tramp-get-remote-id vec)
+          (if (equal id-format 'integer) "" "n")
+          (if (equal id-format 'integer)
+              "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
+
+(defun tramp-get-remote-uid-with-perl (vec id-format)
+  (tramp-send-command-and-read
+   vec
+   (format "%s -le '%s'"
+          (tramp-get-remote-perl vec)
+          (if (equal id-format 'integer)
+              "print $>"
+            "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
+
+(defun tramp-get-remote-python (vec)
+  (with-tramp-connection-property vec "python"
+    (tramp-message vec 5 "Finding a suitable `python' command")
+    (tramp-find-executable vec "python" (tramp-get-remote-path vec))))
+
+(defun tramp-get-remote-uid-with-python (vec id-format)
+  (tramp-send-command-and-read
+   vec
+   (format "%s -c \"%s\""
+          (tramp-get-remote-python vec)
+          (if (equal id-format 'integer)
+              "import os; print os.getuid()"
+            "import os, pwd; print '\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"'"))))
 
 (defun tramp-get-remote-uid (vec id-format)
   (with-tramp-connection-property vec (format "uid-%s" id-format)
-    (let ((res (tramp-send-command-and-read
-               vec
-               (format "%s -u%s %s"
-                       (tramp-get-remote-id vec)
-                       (if (equal id-format 'integer) "" "n")
-                       (if (equal id-format 'integer)
-                           "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
+    (let ((res (cond
+               ((tramp-get-remote-id vec)
+                (tramp-get-remote-uid-with-id vec id-format))
+               ((tramp-get-remote-perl vec)
+                (tramp-get-remote-uid-with-perl vec id-format))
+               ((tramp-get-remote-python vec)
+                (tramp-get-remote-uid-with-python vec id-format))
+               (t (tramp-error vec "Cannot determine remote uid")))))
       ;; The command might not always return a number.
       (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
 
+(defun tramp-get-remote-gid-with-id (vec id-format)
+  (tramp-send-command-and-read
+   vec
+   (format "%s -g%s %s"
+          (tramp-get-remote-id vec)
+          (if (equal id-format 'integer) "" "n")
+          (if (equal id-format 'integer)
+              "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
+
+(defun tramp-get-remote-gid-with-perl (vec id-format)
+  (tramp-send-command-and-read
+   vec
+   (format "%s -le '%s'"
+          (tramp-get-remote-perl vec)
+          (if (equal id-format 'integer)
+              "print ($)=~/(\\d+)/)"
+            "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
+
+(defun tramp-get-remote-gid-with-python (vec id-format)
+  (tramp-send-command-and-read
+   vec
+   (format "%s -c \"%s\""
+          (tramp-get-remote-python vec)
+          (if (equal id-format 'integer)
+              "import os; print os.getgid()"
+            "import os, grp; print '\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"'"))))
+
 (defun tramp-get-remote-gid (vec id-format)
   (with-tramp-connection-property vec (format "gid-%s" id-format)
-    (let ((res (tramp-send-command-and-read
-               vec
-               (format "%s -g%s %s"
-                       (tramp-get-remote-id vec)
-                       (if (equal id-format 'integer) "" "n")
-                       (if (equal id-format 'integer)
-                           "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
+    (let ((res (cond
+               ((tramp-get-remote-id vec)
+                (tramp-get-remote-gid-with-id vec id-format))
+               ((tramp-get-remote-perl vec)
+                (tramp-get-remote-gid-with-perl vec id-format))
+               ((tramp-get-remote-python vec)
+                (tramp-get-remote-gid-with-python vec id-format))
+               (t (tramp-error vec "Cannot determine remote gid")))))
       ;; The command might not always return a number.
       (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))