all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
@ 2023-01-10 23:50 Jim Porter
  2023-01-11  2:12 ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-01-11  9:33 ` Michael Albinus
  0 siblings, 2 replies; 14+ messages in thread
From: Jim Porter @ 2023-01-10 23:50 UTC (permalink / raw)
  To: 60722; +Cc: michael.albinus

[-- Attachment #1: Type: text/plain, Size: 586 bytes --]

X-Debbugs-Cc: michael.albinus@gmx.de

Starting with "sudo emacs -Q -f eshell", notice that the prompt looks like:

   /home/user #

Now start "emacs -Q -f eshell":

   ~ $ cd /sudo::
   /sudo:root@huginn:~ $

Notice that the prompt sigil is still "$" instead of "#". I think it 
would be better to indicate that you have a root shell by using "#" here.

Attached is a patch to do this. It adds a new function, 
'user-uid-for-file', which is aware of file name handlers. Then, Tramp 
adds the appropriate handler. Now, Eshell can use that function and we 
get the prompt sigil we expect.

[-- Attachment #2: 0001-Add-user-uid-for-file-to-get-the-effective-UID-for-r.patch --]
[-- Type: text/plain, Size: 10713 bytes --]

From 2a9f33fa87d0825ef2f3a95f602e4f17bfb19b76 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Tue, 10 Jan 2023 15:35:18 -0800
Subject: [PATCH] Add 'user-uid-for-file' to get the effective UID for remote
 files

In particular, this lets Eshell show a "#" root prompt sigil when the
user has sudo'ed via "cd /sudo::".

* lisp/simple.el (user-uid-for-file): New function.

* lisp/net/tramp.el (tramp-file-name-for-operation): Add
'user-uid-for-file'.
(tramp-handle-user-uid-for-file): New function.

* lisp/net/tramp-adb.el (tramp-adb-file-name-handler-alist):
* lisp/net/tramp-gvfs.el (tramp-gvfs-file-name-handler-alist):
* lisp/net/tramp-sh.el (tramp-sh-file-name-handler-alist):
* lisp/net/tramp-sudoedit.el (tramp-sudoedit-file-name-handler-alist):
Add 'user-uid-for-file'.

* lisp/net/tramp-archive.el (tramp-archive-file-name-handler-alist):
* lisp/net/tramp-crypt.el (tramp-crypt-file-name-handler-alist):
* lisp/net/tramp-rclone.el (tramp-rclone-file-name-handler-alist):
* lisp/net/tramp-smb.el (tramp-smb-file-name-handler-alist):
* lisp/net/tramp-sshfs.el (tramp-sshfs-file-name-handler-alist):
Add comment about 'user-uid-for-file'.

* lisp/eshell/em-prompt.el (eshell-prompt-function): Use
'user-uid-for-file'.

* doc/lispref/files.texi (Magic File Names): Mention
'user-uid-for-file'.

* etc/NEWS: Announce 'user-uid-for-file'.
---
 doc/lispref/files.texi     | 2 ++
 etc/NEWS                   | 4 ++++
 lisp/eshell/em-prompt.el   | 5 +++--
 lisp/net/tramp-adb.el      | 1 +
 lisp/net/tramp-archive.el  | 1 +
 lisp/net/tramp-crypt.el    | 1 +
 lisp/net/tramp-gvfs.el     | 1 +
 lisp/net/tramp-rclone.el   | 1 +
 lisp/net/tramp-sh.el       | 1 +
 lisp/net/tramp-smb.el      | 1 +
 lisp/net/tramp-sshfs.el    | 1 +
 lisp/net/tramp-sudoedit.el | 1 +
 lisp/net/tramp.el          | 6 ++++++
 lisp/simple.el             | 9 +++++++++
 14 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index 91643530f7f..6e42086ac66 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -3407,6 +3407,7 @@ Magic File Names
 @code{temporary-file-directory},
 @code{unhandled-file-name-directory},
 @code{unlock-file},
+@code{user-uid-for-file},
 @code{vc-registered},
 @code{verify-visited-file-modtime},@*
 @code{write-region}.
@@ -3468,6 +3469,7 @@ Magic File Names
 @code{temporary-file-directory},
 @code{unhandled-file-name-directory},
 @code{unlock-file},
+@code{user-uid-for-file},
 @code{vc-regis@discretionary{}{}{}tered},
 @code{verify-visited-file-modtime},
 @code{write-region}.
diff --git a/etc/NEWS b/etc/NEWS
index 690e9c3faa9..4d903d1faae 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -190,6 +190,10 @@ compared reliably at all.
 This warning can be suppressed using 'with-suppressed-warnings' with
 the warning name 'suspicious'.
 
+** New function 'user-uid-for-file'.
+This function is like 'user-uid', but is aware of file name handlers,
+so it will return the remote UID for remote files.
+
 \f
 * Changes in Emacs 30.1 on Non-Free Operating Systems
 
diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el
index 575b5a595f1..bc6beaf3cde 100644
--- a/lisp/eshell/em-prompt.el
+++ b/lisp/eshell/em-prompt.el
@@ -49,8 +49,9 @@ eshell-prompt-load-hook
 
 (defcustom eshell-prompt-function
   (lambda ()
-    (concat (abbreviate-file-name (eshell/pwd))
-            (if (= (user-uid) 0) " # " " $ ")))
+    (let ((cwd (eshell/pwd)))
+      (concat (abbreviate-file-name cwd)
+              (if (= (user-uid-for-file cwd) 0) " # " " $ "))))
   "A function that returns the Eshell prompt string.
 Make sure to update `eshell-prompt-regexp' so that it will match your
 prompt."
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el
index 619d29bb4d6..8de47045b5e 100644
--- a/lisp/net/tramp-adb.el
+++ b/lisp/net/tramp-adb.el
@@ -188,6 +188,7 @@ tramp-adb-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-adb-handle-write-region))
diff --git a/lisp/net/tramp-archive.el b/lisp/net/tramp-archive.el
index a2add1ed73a..08e4a776cc3 100644
--- a/lisp/net/tramp-archive.el
+++ b/lisp/net/tramp-archive.el
@@ -300,6 +300,7 @@ tramp-archive-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . ignore)
+    ;; `user-uid-for-file' performed by default handler.
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-archive-handle-not-implemented))
diff --git a/lisp/net/tramp-crypt.el b/lisp/net/tramp-crypt.el
index 61d1c529619..1192bc75762 100644
--- a/lisp/net/tramp-crypt.el
+++ b/lisp/net/tramp-crypt.el
@@ -239,6 +239,7 @@ tramp-crypt-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-crypt-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-crypt-handle-unlock-file)
+    ;; `user-uid-for-file' performed by default handler.
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el
index bb81b3eb66c..177a3fb7ebe 100644
--- a/lisp/net/tramp-gvfs.el
+++ b/lisp/net/tramp-gvfs.el
@@ -833,6 +833,7 @@ tramp-gvfs-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-gvfs-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el
index 4018fa3aa29..3ac18a00278 100644
--- a/lisp/net/tramp-rclone.el
+++ b/lisp/net/tramp-rclone.el
@@ -153,6 +153,7 @@ tramp-rclone-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    ;; `user-uid-for-file' performed by default handler.
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index fbdd40dd1d2..00d7324a4d0 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -1121,6 +1121,7 @@ tramp-sh-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-sh-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . tramp-sh-handle-vc-registered)
     (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
     (write-region . tramp-sh-handle-write-region))
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el
index f31865d498d..10532c739c6 100644
--- a/lisp/net/tramp-smb.el
+++ b/lisp/net/tramp-smb.el
@@ -304,6 +304,7 @@ tramp-smb-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    ;; `user-uid-for-file' performed by default handler.
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-smb-handle-write-region))
diff --git a/lisp/net/tramp-sshfs.el b/lisp/net/tramp-sshfs.el
index 27b2854e451..8118a8cfa25 100644
--- a/lisp/net/tramp-sshfs.el
+++ b/lisp/net/tramp-sshfs.el
@@ -159,6 +159,7 @@ tramp-sshfs-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    ;; `user-uid-for-file' performed by default handler.
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-sshfs-handle-write-region))
diff --git a/lisp/net/tramp-sudoedit.el b/lisp/net/tramp-sudoedit.el
index c4e1d32f525..af7d5ba09ca 100644
--- a/lisp/net/tramp-sudoedit.el
+++ b/lisp/net/tramp-sudoedit.el
@@ -149,6 +149,7 @@ tramp-sudoedit-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-sudoedit-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 4bf0fdefc0b..a93f4bda605 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -2589,6 +2589,8 @@ tramp-file-name-for-operation
 	      file-locked-p lock-file make-lock-file-name unlock-file
 	      ;; Emacs 29+ only.
 	      abbreviate-file-name
+	      ;; Emacs 30+ only.
+	      user-uid-for-file
 	      ;; Tramp internal magic file name function.
 	      tramp-set-file-uid-gid))
     (if (file-name-absolute-p (nth 0 args))
@@ -3690,6 +3692,10 @@ tramp-handle-abbreviate-file-name
 	 vec (concat "~" (substring filename (match-beginning 1))))
       (tramp-make-tramp-file-name (tramp-dissect-file-name filename)))))
 
+(defun tramp-handle-user-uid-for-file (filename)
+  "Like `user-uid' for Tramp files."
+  (tramp-get-remote-uid (tramp-dissect-file-name filename) 'integer))
+
 (defun tramp-handle-access-file (filename string)
   "Like `access-file' for Tramp files."
   (setq filename (file-truename filename))
diff --git a/lisp/simple.el b/lisp/simple.el
index 690968ca938..07dd3196a3e 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4664,6 +4664,15 @@ shell-command--same-buffer-confirm
                      action))
       (user-error "Shell command in progress"))))
 
+(defun user-uid-for-file (filename)
+  "Return the effective uid for FILENAME.
+For local files, this is equivalent to `user-uid' (which see),
+but for remote files, this returns the effective uid for that
+remote connection."
+  (if-let ((handler (find-file-name-handler filename 'user-uid-for-file)))
+      (funcall handler 'user-uid-for-file filename)
+    (user-uid)))
+
 (defun max-mini-window-lines (&optional frame)
   "Compute maximum number of lines for echo area in FRAME.
 As defined by `max-mini-window-height'.  FRAME defaults to the
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-10 23:50 bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil Jim Porter
@ 2023-01-11  2:12 ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2023-01-11  9:35   ` Michael Albinus
  2023-01-11  9:33 ` Michael Albinus
  1 sibling, 1 reply; 14+ messages in thread
From: Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-11  2:12 UTC (permalink / raw)
  To: Jim Porter; +Cc: michael.albinus, 60722


Jim Porter <jporterbugs@gmail.com> writes:

> X-Debbugs-Cc: michael.albinus@gmx.de
>
> Starting with "sudo emacs -Q -f eshell", notice that the prompt looks like:
>
>   /home/user #
>
> Now start "emacs -Q -f eshell":
>
>   ~ $ cd /sudo::
>   /sudo:root@huginn:~ $
>
> Notice that the prompt sigil is still "$" instead of "#". I think it would be
> better to indicate that you have a root shell by using "#" here.
>
> Attached is a patch to do this. It adds a new function, 'user-uid-for-file',
> which is aware of file name handlers. Then, Tramp adds the appropriate
> handler. Now, Eshell can use that function and we get the prompt sigil we
> expect.
>
> [2. text/plain; 0001-Add-user-uid-for-file-to-get-the-effective-UID-for-r.patch]...

Haven't looked into the patch, but I wonder if it addresses multi-hop
scenarios like the following:

1. /sudo:admin@localhost|sudo::
2. /sudo:|sudo:another_user@localhost::
3. /sshx:admin@remote|sudo::
4. /sudo:|sshx:admin@remote::
5. /doas::
6. /sshx:root@localhost:

Best,


RY





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-10 23:50 bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil Jim Porter
  2023-01-11  2:12 ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-01-11  9:33 ` Michael Albinus
  2023-01-11 13:59   ` Michael Albinus
  1 sibling, 1 reply; 14+ messages in thread
From: Michael Albinus @ 2023-01-11  9:33 UTC (permalink / raw)
  To: Jim Porter; +Cc: 60722

Jim Porter <jporterbugs@gmail.com> writes:

Hi Jim,

> Attached is a patch to do this. It adds a new function,
> 'user-uid-for-file', which is aware of file name handlers. Then, Tramp
> adds the appropriate handler. Now, Eshell can use that function and we
> get the prompt sigil we expect.

I haven't tested, but in general it looks good. There are some minor
changes I'm missing, for example documentation of the new function in
the Lisp Reference Manual "(elisp) User Identification", and a new test
case in tramp-tests.el, but this can wait.

More serious are the following comments:

> +    ;; `user-uid-for-file' performed by default handler.

We shouldn't do this. user-uid-for-file would return the *local* uid,
which is wrong. So please, for all handlers apply

> +    (user-uid-for-file . tramp-handle-user-uid-for-file)

Except in tramp-archive.el, where ignore as handler seems to be appropriate.

> +(defun user-uid-for-file (filename)
> +  "Return the effective uid for FILENAME.
> +For local files, this is equivalent to `user-uid' (which see),
> +but for remote files, this returns the effective uid for that
> +remote connection."

Please add, that it returns -1 in case the remote uid could not be
determined.

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-11  2:12 ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-01-11  9:35   ` Michael Albinus
  0 siblings, 0 replies; 14+ messages in thread
From: Michael Albinus @ 2023-01-11  9:35 UTC (permalink / raw)
  To: Ruijie Yu; +Cc: Jim Porter, 60722

Ruijie Yu <ruijie@netyu.xyz> writes:

Hi,

>> Attached is a patch to do this. It adds a new function, 'user-uid-for-file',
>> which is aware of file name handlers. Then, Tramp adds the appropriate
>> handler. Now, Eshell can use that function and we get the prompt sigil we
>> expect.
>
> Haven't looked into the patch, but I wonder if it addresses multi-hop
> scenarios like the following:
>
> 1. /sudo:admin@localhost|sudo::
> 2. /sudo:|sudo:another_user@localhost::
> 3. /sshx:admin@remote|sudo::
> 4. /sudo:|sshx:admin@remote::
> 5. /doas::
> 6. /sshx:root@localhost:

Tramp handles these cases.

> Best,
>
> RY

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-11  9:33 ` Michael Albinus
@ 2023-01-11 13:59   ` Michael Albinus
  2023-01-14 21:59     ` Jim Porter
  0 siblings, 1 reply; 14+ messages in thread
From: Michael Albinus @ 2023-01-11 13:59 UTC (permalink / raw)
  To: Jim Porter; +Cc: 60722

Michael Albinus <michael.albinus@gmx.de> writes:

Hi Jim,

> We shouldn't do this. user-uid-for-file would return the *local* uid,
> which is wrong. So please, for all handlers apply
>
>> +    (user-uid-for-file . tramp-handle-user-uid-for-file)
>
> Except in tramp-archive.el, where ignore as handler seems to be appropriate.

Thinking about, I believe we need an own function for
tramp-archive.el. Something like (untested)

--8<---------------cut here---------------start------------->8---
(defun tramp-archive-user-uid-for-file (filename)
  "Like `user-uid-for-file' for file archives."
  (user-uid-for-file (tramp-archive-gvfs-file-name filename)))
--8<---------------cut here---------------end--------------->8---

This should return the local uid for local archives, and the remote uid
for remote archives.

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-11 13:59   ` Michael Albinus
@ 2023-01-14 21:59     ` Jim Porter
  2023-01-14 22:10       ` Jim Porter
  0 siblings, 1 reply; 14+ messages in thread
From: Jim Porter @ 2023-01-14 21:59 UTC (permalink / raw)
  To: Michael Albinus; +Cc: 60722

[-- Attachment #1: Type: text/plain, Size: 1439 bytes --]

On 1/11/2023 5:59 AM, Michael Albinus wrote:
> Michael Albinus <michael.albinus@gmx.de> writes:
> 
> Hi Jim,
> 
>> We shouldn't do this. user-uid-for-file would return the *local* uid,
>> which is wrong. So please, for all handlers apply
>>
>>> +    (user-uid-for-file . tramp-handle-user-uid-for-file)
>>
>> Except in tramp-archive.el, where ignore as handler seems to be appropriate.
> 
> Thinking about, I believe we need an own function for
> tramp-archive.el. Something like (untested)
> 
> --8<---------------cut here---------------start------------->8---
> (defun tramp-archive-user-uid-for-file (filename)
>    "Like `user-uid-for-file' for file archives."
>    (user-uid-for-file (tramp-archive-gvfs-file-name filename)))
> --8<---------------cut here---------------end--------------->8---
> 
> This should return the local uid for local archives, and the remote uid
> for remote archives.

Thanks. See attached. I also updated the base implementation of 
'tramp-handle-user-uid-for-file' to return -1 even if 
'tramp-get-remote-uid' returns nil (some Tramp methods set the latter 
handler to be 'ignore').

I also added a new Eshell special variable, '$UID', which does about 
what you'd expect. This makes Eshell work like other shells, which also 
have a special '$UID' variable.

Finally, I added documentation to the manuals. I didn't add any Tramp 
regression tests though, since I wasn't sure of the right way to test this.

[-- Attachment #2: 0001-Add-user-uid-for-file-to-get-the-effective-UID-for-r.patch --]
[-- Type: text/plain, Size: 15087 bytes --]

From c5814e7f24558a98e5151811228b637127d72968 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Tue, 10 Jan 2023 15:35:18 -0800
Subject: [PATCH] Add 'user-uid-for-file' to get the effective UID for remote
 files

In particular, this lets Eshell show a "#" root prompt sigil when the
user has sudo'ed via "cd /sudo::" (bug#60722).

* lisp/simple.el (user-uid-for-file): New function.

* lisp/net/tramp.el (tramp-file-name-for-operation): Add
'user-uid-for-file'.
(tramp-handle-user-uid-for-file): New function.

* lisp/net/tramp-adb.el (tramp-adb-file-name-handler-alist):
* lisp/net/tramp-crypt.el (tramp-crypt-file-name-handler-alist):
* lisp/net/tramp-gvfs.el (tramp-gvfs-file-name-handler-alist):
* lisp/net/tramp-rclone.el (tramp-rclone-file-name-handler-alist):
* lisp/net/tramp-sh.el (tramp-sh-file-name-handler-alist):
* lisp/net/tramp-smb.el (tramp-smb-file-name-handler-alist):
* lisp/net/tramp-sshfs.el (tramp-sshfs-file-name-handler-alist):
* lisp/net/tramp-sudoedit.el (tramp-sudoedit-file-name-handler-alist):
Add 'user-uid-for-file'.

* lisp/net/tramp-archive.el (tramp-archive-file-name-handler-alist):
Add comment about 'user-uid-for-file'.

* lisp/eshell/em-prompt.el (eshell-prompt-function): Use
'user-uid-for-file'.

* lisp/eshell/esh-var.el (eshell-variable-aliases-list): Add '$UID'.

* test/lisp/eshell/esh-var-tests.el (esh-var-test/uid-var): New test.

* doc/lispref/os.texi (User Identification): Document
'user-uid-for-file'.

* doc/lispref/files.texi (Magic File Names): Mention
'user-uid-for-file'.

* doc/misc/eshell.texi (Variables): Document '$UID'.  Add a missing
index entry for '$INSIDE_EMACS'.

* etc/NEWS: Announce 'user-uid-for-file'.
---
 doc/lispref/files.texi            |  2 ++
 doc/lispref/os.texi               |  9 +++++++++
 doc/misc/eshell.texi              |  8 ++++++++
 etc/NEWS                          |  5 +++++
 lisp/eshell/em-prompt.el          |  2 +-
 lisp/eshell/esh-var.el            |  1 +
 lisp/net/tramp-adb.el             |  1 +
 lisp/net/tramp-archive.el         |  5 +++++
 lisp/net/tramp-crypt.el           |  1 +
 lisp/net/tramp-gvfs.el            |  1 +
 lisp/net/tramp-rclone.el          |  1 +
 lisp/net/tramp-sh.el              |  1 +
 lisp/net/tramp-smb.el             |  1 +
 lisp/net/tramp-sshfs.el           |  1 +
 lisp/net/tramp-sudoedit.el        |  1 +
 lisp/net/tramp.el                 | 10 ++++++++++
 lisp/simple.el                    | 10 ++++++++++
 test/lisp/eshell/esh-var-tests.el |  4 ++++
 18 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index 5cc4c1e7ddf..7201bcc9c2c 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -3412,6 +3412,7 @@ Magic File Names
 @code{temporary-file-directory},
 @code{unhandled-file-name-directory},
 @code{unlock-file},
+@code{user-uid-for-file},
 @code{vc-registered},
 @code{verify-visited-file-modtime},@*
 @code{write-region}.
@@ -3473,6 +3474,7 @@ Magic File Names
 @code{temporary-file-directory},
 @code{unhandled-file-name-directory},
 @code{unlock-file},
+@code{user-uid-for-file},
 @code{vc-regis@discretionary{}{}{}tered},
 @code{verify-visited-file-modtime},
 @code{write-region}.
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index 3be7036f637..43f1b6976fc 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -1277,6 +1277,15 @@ User Identification
 This function returns the effective @acronym{UID} of the user.
 @end defun
 
+@defun user-uid-for-file filename
+This function returns the effective @acronym{UID} of the user
+associated with the file named @var{filename}.  If @var{filename} is
+local, this is equivalent to @code{user-uid}, but for remote files
+(@pxref{Remote Files, , , emacs, The GNU Emacs Manual}), it will
+return the @acronym{UID} for the user associated with that remote
+connection; if the remote connection has no associated user, it will
+instead return -1.
+
 @cindex GID
 @defun group-gid
 This function returns the effective @acronym{GID} of the Emacs process.
diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi
index fc7d52eb711..c40ff58f42c 100644
--- a/doc/misc/eshell.texi
+++ b/doc/misc/eshell.texi
@@ -983,6 +983,13 @@ Variables
 the value will automatically update to reflect the search path on that
 host.
 
+@vindex $UID
+@item $UID
+This returns the effective @acronym{UID} for the current user.  This
+variable is connection-aware, so when the current directory is remote,
+its value will be @acronym{UID} for the user associated with that
+remote connection.
+
 @vindex $_
 @item $_
 This refers to the last argument of the last command.  With a
@@ -1014,6 +1021,7 @@ Variables
 copied to the environment, so external commands invoked from
 Eshell can consult them to do the right thing.
 
+@vindex $INSIDE_EMACS
 @item $INSIDE_EMACS
 This variable indicates to external commands that they are being
 invoked from within Emacs so they can adjust their behavior if
diff --git a/etc/NEWS b/etc/NEWS
index 068f7a27db8..8a190600d44 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -235,6 +235,11 @@ compared reliably at all.
 This warning can be suppressed using 'with-suppressed-warnings' with
 the warning name 'suspicious'.
 
++++
+** New function 'user-uid-for-file'.
+This function is like 'user-uid', but is aware of file name handlers,
+so it will return the remote UID for remote files.
+
 \f
 * Changes in Emacs 30.1 on Non-Free Operating Systems
 
diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el
index 52d46282c52..36b6c5e0a1b 100644
--- a/lisp/eshell/em-prompt.el
+++ b/lisp/eshell/em-prompt.el
@@ -52,7 +52,7 @@ eshell-prompt-load-hook
 (defcustom eshell-prompt-function
   (lambda ()
     (concat (abbreviate-file-name (eshell/pwd))
-            (if (= (user-uid) 0) " # " " $ ")))
+            (if (= (user-uid-for-file default-directory) 0) " # " " $ ")))
   "A function that returns the Eshell prompt string.
 Make sure to update `eshell-prompt-regexp' so that it will match your
 prompt."
diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el
index 811bb9957cf..ac48fd01bec 100644
--- a/lisp/eshell/esh-var.el
+++ b/lisp/eshell/esh-var.el
@@ -162,6 +162,7 @@ eshell-variable-aliases-list
     ("COLUMNS" ,(lambda () (window-body-width nil 'remap)) t t)
     ("LINES" ,(lambda () (window-body-height nil 'remap)) t t)
     ("INSIDE_EMACS" eshell-inside-emacs t)
+    ("UID" ,(lambda () (user-uid-for-file default-directory)) nil t)
 
     ;; for esh-ext.el
     ("PATH" (,(lambda () (string-join (eshell-get-path t) (path-separator)))
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el
index 493a9fb39a9..6a473d03e9f 100644
--- a/lisp/net/tramp-adb.el
+++ b/lisp/net/tramp-adb.el
@@ -188,6 +188,7 @@ tramp-adb-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-adb-handle-write-region))
diff --git a/lisp/net/tramp-archive.el b/lisp/net/tramp-archive.el
index a2add1ed73a..e1f7a060783 100644
--- a/lisp/net/tramp-archive.el
+++ b/lisp/net/tramp-archive.el
@@ -300,6 +300,7 @@ tramp-archive-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . ignore)
+    (user-uid-for-file . tramp-archive-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-archive-handle-not-implemented))
@@ -701,6 +702,10 @@ tramp-archive-handle-temporary-file-directory
     (let ((default-directory (file-name-directory archive)))
       (temporary-file-directory))))
 
+(defun tramp-archive-handle-user-uid-for-file (filename)
+  "Like `user-uid-for-file' for file archives."
+  (user-uid-for-file (tramp-archive-gvfs-file-name filename)))
+
 (defun tramp-archive-handle-not-implemented (operation &rest args)
   "Generic handler for operations not implemented for file archives."
   (let ((v (ignore-errors
diff --git a/lisp/net/tramp-crypt.el b/lisp/net/tramp-crypt.el
index 507fd432419..d2e4805a737 100644
--- a/lisp/net/tramp-crypt.el
+++ b/lisp/net/tramp-crypt.el
@@ -239,6 +239,7 @@ tramp-crypt-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-crypt-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-crypt-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el
index cca7a5fe247..66e5313e516 100644
--- a/lisp/net/tramp-gvfs.el
+++ b/lisp/net/tramp-gvfs.el
@@ -833,6 +833,7 @@ tramp-gvfs-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-gvfs-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el
index 4018fa3aa29..2d25668b3ac 100644
--- a/lisp/net/tramp-rclone.el
+++ b/lisp/net/tramp-rclone.el
@@ -153,6 +153,7 @@ tramp-rclone-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index 4647600071c..c812300babf 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -1121,6 +1121,7 @@ tramp-sh-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-sh-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . tramp-sh-handle-vc-registered)
     (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
     (write-region . tramp-sh-handle-write-region))
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el
index d6f3cca9733..240a4523e36 100644
--- a/lisp/net/tramp-smb.el
+++ b/lisp/net/tramp-smb.el
@@ -304,6 +304,7 @@ tramp-smb-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-smb-handle-write-region))
diff --git a/lisp/net/tramp-sshfs.el b/lisp/net/tramp-sshfs.el
index 27b2854e451..b81e84a213d 100644
--- a/lisp/net/tramp-sshfs.el
+++ b/lisp/net/tramp-sshfs.el
@@ -159,6 +159,7 @@ tramp-sshfs-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-sshfs-handle-write-region))
diff --git a/lisp/net/tramp-sudoedit.el b/lisp/net/tramp-sudoedit.el
index 2660dbb1fac..c115b17f98f 100644
--- a/lisp/net/tramp-sudoedit.el
+++ b/lisp/net/tramp-sudoedit.el
@@ -149,6 +149,7 @@ tramp-sudoedit-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-sudoedit-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index b8475b7cb48..5be0347ff25 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -2589,6 +2589,8 @@ tramp-file-name-for-operation
 	      file-locked-p lock-file make-lock-file-name unlock-file
 	      ;; Emacs 29+ only.
 	      abbreviate-file-name
+	      ;; Emacs 30+ only.
+	      user-uid-for-file
 	      ;; Tramp internal magic file name function.
 	      tramp-set-file-uid-gid))
     (if (file-name-absolute-p (nth 0 args))
@@ -3710,6 +3712,14 @@ tramp-handle-abbreviate-file-name
 	 vec (concat "~" (substring filename (match-beginning 1))))
       (tramp-make-tramp-file-name (tramp-dissect-file-name filename)))))
 
+(defun tramp-handle-user-uid-for-file (filename)
+  "Like `user-uid' for Tramp files."
+  (or (tramp-get-remote-uid (tramp-dissect-file-name filename) 'integer)
+      ;; Some handlers for `tramp-get-remote-uid' return nil if they
+      ;; can't get the UID; always return -1 in this case for
+      ;; consistency.
+      -1))
+
 (defun tramp-handle-access-file (filename string)
   "Like `access-file' for Tramp files."
   (setq filename (file-truename filename))
diff --git a/lisp/simple.el b/lisp/simple.el
index bbcb32cb04f..adcbe835e5f 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4730,6 +4730,16 @@ shell-command--same-buffer-confirm
                      action))
       (user-error "Shell command in progress"))))
 
+(defun user-uid-for-file (filename)
+  "Return the effective uid for FILENAME.
+For local files, this is equivalent to `user-uid' (which see),
+but for remote files, this returns the effective uid for that
+remote connection; if the remote connection has no associated
+user, it will instead return -1."
+  (if-let ((handler (find-file-name-handler filename 'user-uid-for-file)))
+      (funcall handler 'user-uid-for-file filename)
+    (user-uid)))
+
 (defun max-mini-window-lines (&optional frame)
   "Compute maximum number of lines for echo area in FRAME.
 As defined by `max-mini-window-height'.  FRAME defaults to the
diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el
index 3f602798dbe..0cc1b92266f 100644
--- a/test/lisp/eshell/esh-var-tests.el
+++ b/test/lisp/eshell/esh-var-tests.el
@@ -746,6 +746,10 @@ esh-var-test/path-var/preserve-across-hosts
       (format "cd %s" ert-remote-temporary-file-directory))
      (eshell-match-command-output "echo $PATH" (regexp-quote remote-path)))))
 
+(ert-deftest esh-var-test/uid-var ()
+  "Test that $UID is equivalent to (user-uid) for local directories."
+  (eshell-command-result-equal "echo $UID" (user-uid)))
+
 (ert-deftest esh-var-test/last-status-var-lisp-command ()
   "Test using the \"last exit status\" ($?) variable with a Lisp command"
   (with-temp-eshell
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-14 21:59     ` Jim Porter
@ 2023-01-14 22:10       ` Jim Porter
  2023-01-15  9:23         ` Michael Albinus
  0 siblings, 1 reply; 14+ messages in thread
From: Jim Porter @ 2023-01-14 22:10 UTC (permalink / raw)
  To: Michael Albinus; +Cc: 60722

[-- Attachment #1: Type: text/plain, Size: 244 bytes --]

On 1/14/2023 1:59 PM, Jim Porter wrote:
> Finally, I added documentation to the manuals. I didn't add any Tramp 
> regression tests though, since I wasn't sure of the right way to test this.

Oops. I missed an "@end defun" in the manual. Fixed.

[-- Attachment #2: 0001-Add-user-uid-for-file-to-get-the-effective-UID-for-r.patch --]
[-- Type: text/plain, Size: 15100 bytes --]

From d66131038f3a013e87fd3079fe62082ece6d91a2 Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Tue, 10 Jan 2023 15:35:18 -0800
Subject: [PATCH] Add 'user-uid-for-file' to get the effective UID for remote
 files

In particular, this lets Eshell show a "#" root prompt sigil when the
user has sudo'ed via "cd /sudo::" (bug#60722).

* lisp/simple.el (user-uid-for-file): New function.

* lisp/net/tramp.el (tramp-file-name-for-operation): Add
'user-uid-for-file'.
(tramp-handle-user-uid-for-file): New function.

* lisp/net/tramp-adb.el (tramp-adb-file-name-handler-alist):
* lisp/net/tramp-crypt.el (tramp-crypt-file-name-handler-alist):
* lisp/net/tramp-gvfs.el (tramp-gvfs-file-name-handler-alist):
* lisp/net/tramp-rclone.el (tramp-rclone-file-name-handler-alist):
* lisp/net/tramp-sh.el (tramp-sh-file-name-handler-alist):
* lisp/net/tramp-smb.el (tramp-smb-file-name-handler-alist):
* lisp/net/tramp-sshfs.el (tramp-sshfs-file-name-handler-alist):
* lisp/net/tramp-sudoedit.el (tramp-sudoedit-file-name-handler-alist):
Add 'user-uid-for-file'.

* lisp/net/tramp-archive.el (tramp-archive-file-name-handler-alist):
Add comment about 'user-uid-for-file'.

* lisp/eshell/em-prompt.el (eshell-prompt-function): Use
'user-uid-for-file'.

* lisp/eshell/esh-var.el (eshell-variable-aliases-list): Add '$UID'.

* test/lisp/eshell/esh-var-tests.el (esh-var-test/uid-var): New test.

* doc/lispref/os.texi (User Identification): Document
'user-uid-for-file'.

* doc/lispref/files.texi (Magic File Names): Mention
'user-uid-for-file'.

* doc/misc/eshell.texi (Variables): Document '$UID'.  Add a missing
index entry for '$INSIDE_EMACS'.

* etc/NEWS: Announce 'user-uid-for-file'.
---
 doc/lispref/files.texi            |  2 ++
 doc/lispref/os.texi               | 10 ++++++++++
 doc/misc/eshell.texi              |  8 ++++++++
 etc/NEWS                          |  5 +++++
 lisp/eshell/em-prompt.el          |  2 +-
 lisp/eshell/esh-var.el            |  1 +
 lisp/net/tramp-adb.el             |  1 +
 lisp/net/tramp-archive.el         |  5 +++++
 lisp/net/tramp-crypt.el           |  1 +
 lisp/net/tramp-gvfs.el            |  1 +
 lisp/net/tramp-rclone.el          |  1 +
 lisp/net/tramp-sh.el              |  1 +
 lisp/net/tramp-smb.el             |  1 +
 lisp/net/tramp-sshfs.el           |  1 +
 lisp/net/tramp-sudoedit.el        |  1 +
 lisp/net/tramp.el                 | 10 ++++++++++
 lisp/simple.el                    | 10 ++++++++++
 test/lisp/eshell/esh-var-tests.el |  4 ++++
 18 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index 5cc4c1e7ddf..7201bcc9c2c 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -3412,6 +3412,7 @@ Magic File Names
 @code{temporary-file-directory},
 @code{unhandled-file-name-directory},
 @code{unlock-file},
+@code{user-uid-for-file},
 @code{vc-registered},
 @code{verify-visited-file-modtime},@*
 @code{write-region}.
@@ -3473,6 +3474,7 @@ Magic File Names
 @code{temporary-file-directory},
 @code{unhandled-file-name-directory},
 @code{unlock-file},
+@code{user-uid-for-file},
 @code{vc-regis@discretionary{}{}{}tered},
 @code{verify-visited-file-modtime},
 @code{write-region}.
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index 3be7036f637..9e76fbd2fbb 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -1277,6 +1277,16 @@ User Identification
 This function returns the effective @acronym{UID} of the user.
 @end defun
 
+@defun user-uid-for-file filename
+This function returns the effective @acronym{UID} of the user
+associated with the file named @var{filename}.  If @var{filename} is
+local, this is equivalent to @code{user-uid}, but for remote files
+(@pxref{Remote Files, , , emacs, The GNU Emacs Manual}), it will
+return the @acronym{UID} for the user associated with that remote
+connection; if the remote connection has no associated user, it will
+instead return -1.
+@end defun
+
 @cindex GID
 @defun group-gid
 This function returns the effective @acronym{GID} of the Emacs process.
diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi
index fc7d52eb711..c40ff58f42c 100644
--- a/doc/misc/eshell.texi
+++ b/doc/misc/eshell.texi
@@ -983,6 +983,13 @@ Variables
 the value will automatically update to reflect the search path on that
 host.
 
+@vindex $UID
+@item $UID
+This returns the effective @acronym{UID} for the current user.  This
+variable is connection-aware, so when the current directory is remote,
+its value will be @acronym{UID} for the user associated with that
+remote connection.
+
 @vindex $_
 @item $_
 This refers to the last argument of the last command.  With a
@@ -1014,6 +1021,7 @@ Variables
 copied to the environment, so external commands invoked from
 Eshell can consult them to do the right thing.
 
+@vindex $INSIDE_EMACS
 @item $INSIDE_EMACS
 This variable indicates to external commands that they are being
 invoked from within Emacs so they can adjust their behavior if
diff --git a/etc/NEWS b/etc/NEWS
index 068f7a27db8..8a190600d44 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -235,6 +235,11 @@ compared reliably at all.
 This warning can be suppressed using 'with-suppressed-warnings' with
 the warning name 'suspicious'.
 
++++
+** New function 'user-uid-for-file'.
+This function is like 'user-uid', but is aware of file name handlers,
+so it will return the remote UID for remote files.
+
 \f
 * Changes in Emacs 30.1 on Non-Free Operating Systems
 
diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el
index 52d46282c52..36b6c5e0a1b 100644
--- a/lisp/eshell/em-prompt.el
+++ b/lisp/eshell/em-prompt.el
@@ -52,7 +52,7 @@ eshell-prompt-load-hook
 (defcustom eshell-prompt-function
   (lambda ()
     (concat (abbreviate-file-name (eshell/pwd))
-            (if (= (user-uid) 0) " # " " $ ")))
+            (if (= (user-uid-for-file default-directory) 0) " # " " $ ")))
   "A function that returns the Eshell prompt string.
 Make sure to update `eshell-prompt-regexp' so that it will match your
 prompt."
diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el
index 811bb9957cf..ac48fd01bec 100644
--- a/lisp/eshell/esh-var.el
+++ b/lisp/eshell/esh-var.el
@@ -162,6 +162,7 @@ eshell-variable-aliases-list
     ("COLUMNS" ,(lambda () (window-body-width nil 'remap)) t t)
     ("LINES" ,(lambda () (window-body-height nil 'remap)) t t)
     ("INSIDE_EMACS" eshell-inside-emacs t)
+    ("UID" ,(lambda () (user-uid-for-file default-directory)) nil t)
 
     ;; for esh-ext.el
     ("PATH" (,(lambda () (string-join (eshell-get-path t) (path-separator)))
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el
index 493a9fb39a9..6a473d03e9f 100644
--- a/lisp/net/tramp-adb.el
+++ b/lisp/net/tramp-adb.el
@@ -188,6 +188,7 @@ tramp-adb-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-adb-handle-write-region))
diff --git a/lisp/net/tramp-archive.el b/lisp/net/tramp-archive.el
index a2add1ed73a..e1f7a060783 100644
--- a/lisp/net/tramp-archive.el
+++ b/lisp/net/tramp-archive.el
@@ -300,6 +300,7 @@ tramp-archive-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . ignore)
+    (user-uid-for-file . tramp-archive-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-archive-handle-not-implemented))
@@ -701,6 +702,10 @@ tramp-archive-handle-temporary-file-directory
     (let ((default-directory (file-name-directory archive)))
       (temporary-file-directory))))
 
+(defun tramp-archive-handle-user-uid-for-file (filename)
+  "Like `user-uid-for-file' for file archives."
+  (user-uid-for-file (tramp-archive-gvfs-file-name filename)))
+
 (defun tramp-archive-handle-not-implemented (operation &rest args)
   "Generic handler for operations not implemented for file archives."
   (let ((v (ignore-errors
diff --git a/lisp/net/tramp-crypt.el b/lisp/net/tramp-crypt.el
index 507fd432419..d2e4805a737 100644
--- a/lisp/net/tramp-crypt.el
+++ b/lisp/net/tramp-crypt.el
@@ -239,6 +239,7 @@ tramp-crypt-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-crypt-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-crypt-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el
index cca7a5fe247..66e5313e516 100644
--- a/lisp/net/tramp-gvfs.el
+++ b/lisp/net/tramp-gvfs.el
@@ -833,6 +833,7 @@ tramp-gvfs-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-gvfs-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el
index 4018fa3aa29..2d25668b3ac 100644
--- a/lisp/net/tramp-rclone.el
+++ b/lisp/net/tramp-rclone.el
@@ -153,6 +153,7 @@ tramp-rclone-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index 4647600071c..c812300babf 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -1121,6 +1121,7 @@ tramp-sh-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-sh-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . tramp-sh-handle-vc-registered)
     (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
     (write-region . tramp-sh-handle-write-region))
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el
index d6f3cca9733..240a4523e36 100644
--- a/lisp/net/tramp-smb.el
+++ b/lisp/net/tramp-smb.el
@@ -304,6 +304,7 @@ tramp-smb-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-smb-handle-write-region))
diff --git a/lisp/net/tramp-sshfs.el b/lisp/net/tramp-sshfs.el
index 27b2854e451..b81e84a213d 100644
--- a/lisp/net/tramp-sshfs.el
+++ b/lisp/net/tramp-sshfs.el
@@ -159,6 +159,7 @@ tramp-sshfs-file-name-handler-alist
     (tramp-set-file-uid-gid . ignore)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-sshfs-handle-write-region))
diff --git a/lisp/net/tramp-sudoedit.el b/lisp/net/tramp-sudoedit.el
index 2660dbb1fac..c115b17f98f 100644
--- a/lisp/net/tramp-sudoedit.el
+++ b/lisp/net/tramp-sudoedit.el
@@ -149,6 +149,7 @@ tramp-sudoedit-file-name-handler-alist
     (tramp-set-file-uid-gid . tramp-sudoedit-handle-set-file-uid-gid)
     (unhandled-file-name-directory . ignore)
     (unlock-file . tramp-handle-unlock-file)
+    (user-uid-for-file . tramp-handle-user-uid-for-file)
     (vc-registered . ignore)
     (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
     (write-region . tramp-handle-write-region))
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index b8475b7cb48..5be0347ff25 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -2589,6 +2589,8 @@ tramp-file-name-for-operation
 	      file-locked-p lock-file make-lock-file-name unlock-file
 	      ;; Emacs 29+ only.
 	      abbreviate-file-name
+	      ;; Emacs 30+ only.
+	      user-uid-for-file
 	      ;; Tramp internal magic file name function.
 	      tramp-set-file-uid-gid))
     (if (file-name-absolute-p (nth 0 args))
@@ -3710,6 +3712,14 @@ tramp-handle-abbreviate-file-name
 	 vec (concat "~" (substring filename (match-beginning 1))))
       (tramp-make-tramp-file-name (tramp-dissect-file-name filename)))))
 
+(defun tramp-handle-user-uid-for-file (filename)
+  "Like `user-uid' for Tramp files."
+  (or (tramp-get-remote-uid (tramp-dissect-file-name filename) 'integer)
+      ;; Some handlers for `tramp-get-remote-uid' return nil if they
+      ;; can't get the UID; always return -1 in this case for
+      ;; consistency.
+      -1))
+
 (defun tramp-handle-access-file (filename string)
   "Like `access-file' for Tramp files."
   (setq filename (file-truename filename))
diff --git a/lisp/simple.el b/lisp/simple.el
index bbcb32cb04f..adcbe835e5f 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4730,6 +4730,16 @@ shell-command--same-buffer-confirm
                      action))
       (user-error "Shell command in progress"))))
 
+(defun user-uid-for-file (filename)
+  "Return the effective uid for FILENAME.
+For local files, this is equivalent to `user-uid' (which see),
+but for remote files, this returns the effective uid for that
+remote connection; if the remote connection has no associated
+user, it will instead return -1."
+  (if-let ((handler (find-file-name-handler filename 'user-uid-for-file)))
+      (funcall handler 'user-uid-for-file filename)
+    (user-uid)))
+
 (defun max-mini-window-lines (&optional frame)
   "Compute maximum number of lines for echo area in FRAME.
 As defined by `max-mini-window-height'.  FRAME defaults to the
diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el
index 3f602798dbe..0cc1b92266f 100644
--- a/test/lisp/eshell/esh-var-tests.el
+++ b/test/lisp/eshell/esh-var-tests.el
@@ -746,6 +746,10 @@ esh-var-test/path-var/preserve-across-hosts
       (format "cd %s" ert-remote-temporary-file-directory))
      (eshell-match-command-output "echo $PATH" (regexp-quote remote-path)))))
 
+(ert-deftest esh-var-test/uid-var ()
+  "Test that $UID is equivalent to (user-uid) for local directories."
+  (eshell-command-result-equal "echo $UID" (user-uid)))
+
 (ert-deftest esh-var-test/last-status-var-lisp-command ()
   "Test using the \"last exit status\" ($?) variable with a Lisp command"
   (with-temp-eshell
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-14 22:10       ` Jim Porter
@ 2023-01-15  9:23         ` Michael Albinus
  2023-01-16  5:38           ` Jim Porter
  0 siblings, 1 reply; 14+ messages in thread
From: Michael Albinus @ 2023-01-15  9:23 UTC (permalink / raw)
  To: Jim Porter; +Cc: 60722

Jim Porter <jporterbugs@gmail.com> writes:

Hi Jim,

>> Finally, I added documentation to the manuals. I didn't add any
>> Tramp regression tests though, since I wasn't sure of the right way
>> to test this.
>
> Oops. I missed an "@end defun" in the manual. Fixed.

Thanks. LGTM. Don't care about the tests, I'll add them once your patch
has arrived Emacs master.

One question is left for me: do we really need FILENAME as argument? I
believe it would be sufficient to check default-directory; this would also
be consistent with the functions for remote hosts described in os.texi.

Btw, another idea is to simplify the implementation. Let Tramp set a
connection-local variable `tramp-user-uid' or alike, and your function
`remote-user-uid' or however you rename it will ask for this
connection-local variable, like we do it already in functions
`null-device' and `path-separator'. This would avoid the overhead of
running the file name handler mechanism.

WDYT?

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-15  9:23         ` Michael Albinus
@ 2023-01-16  5:38           ` Jim Porter
  2023-01-16  9:09             ` Michael Albinus
  0 siblings, 1 reply; 14+ messages in thread
From: Jim Porter @ 2023-01-16  5:38 UTC (permalink / raw)
  To: Michael Albinus; +Cc: 60722

[-- Attachment #1: Type: text/plain, Size: 1134 bytes --]

On 1/15/2023 1:23 AM, Michael Albinus wrote:
> One question is left for me: do we really need FILENAME as argument? I
> believe it would be sufficient to check default-directory; this would also
> be consistent with the functions for remote hosts described in os.texi.

Good point. I removed the FILENAME argument.

> Btw, another idea is to simplify the implementation. Let Tramp set a
> connection-local variable `tramp-user-uid' or alike, and your function
> `remote-user-uid' or however you rename it will ask for this
> connection-local variable, like we do it already in functions
> `null-device' and `path-separator'. This would avoid the overhead of
> running the file name handler mechanism.

I didn't do this part though, since it wasn't so simple. Instead, I made 
the function (now named 'file-user-uid'[1]) work like the function 
'exec-path'. Since the regular 'user-uid' is also a function, I think 
this implementation is the most straightforward.

[1] I named it this way to mimic the relationship between 
'start-process' and 'start-file-process'. (But 'user-file-uid' didn't 
sound right, so 'file-user-uid' it is.)

[-- Attachment #2: 0001-Add-file-user-uid-to-get-the-connection-local-effect.patch --]
[-- Type: text/plain, Size: 15791 bytes --]

From 8b3b1166790c533f05ef413dd25a518f4973561e Mon Sep 17 00:00:00 2001
From: Jim Porter <jporterbugs@gmail.com>
Date: Tue, 10 Jan 2023 15:35:18 -0800
Subject: [PATCH] Add 'file-user-uid' to get the connection-local effective UID

In particular, this lets Eshell show a "#" root prompt sigil when the
user has sudo'ed via "cd /sudo::" (bug#60722).

* lisp/simple.el (file-user-uid): New function.

* lisp/net/tramp.el (tramp-file-name-for-operation): Add
'file-user-uid'.
(tramp-handle-file-user-uid): New function.

* lisp/net/tramp-adb.el (tramp-adb-file-name-handler-alist):
* lisp/net/tramp-crypt.el (tramp-crypt-file-name-handler-alist):
* lisp/net/tramp-gvfs.el (tramp-gvfs-file-name-handler-alist):
* lisp/net/tramp-rclone.el (tramp-rclone-file-name-handler-alist):
* lisp/net/tramp-sh.el (tramp-sh-file-name-handler-alist):
* lisp/net/tramp-smb.el (tramp-smb-file-name-handler-alist):
* lisp/net/tramp-sshfs.el (tramp-sshfs-file-name-handler-alist):
* lisp/net/tramp-sudoedit.el (tramp-sudoedit-file-name-handler-alist):
Add 'file-user-uid'.

* lisp/net/tramp-archive.el (tramp-archive-handle-file-user-uid): New
function...
(tramp-archive-file-name-handler-alist): ... use it.

* lisp/eshell/em-prompt.el (eshell-prompt-function): Use
'file-user-uid'.

* lisp/eshell/esh-var.el (eshell-variable-aliases-list): Add '$UID'.

* test/lisp/eshell/esh-var-tests.el (esh-var-test/uid-var): New test.

* doc/lispref/os.texi (User Identification): Document 'file-user-uid'.

* doc/lispref/files.texi (Magic File Names): Mention 'file-user-uid'.

* doc/misc/eshell.texi (Variables): Document '$UID'.  Add a missing
index entry for '$INSIDE_EMACS'.

* etc/NEWS: Announce 'file-user-uid'.
---
 doc/lispref/files.texi            |  6 ++++--
 doc/lispref/os.texi               | 10 ++++++++++
 doc/misc/eshell.texi              |  8 ++++++++
 etc/NEWS                          |  5 +++++
 lisp/eshell/em-prompt.el          |  2 +-
 lisp/eshell/esh-var.el            |  1 +
 lisp/net/tramp-adb.el             |  1 +
 lisp/net/tramp-archive.el         |  7 +++++++
 lisp/net/tramp-crypt.el           |  1 +
 lisp/net/tramp-gvfs.el            |  1 +
 lisp/net/tramp-rclone.el          |  1 +
 lisp/net/tramp-sh.el              |  1 +
 lisp/net/tramp-smb.el             |  1 +
 lisp/net/tramp-sshfs.el           |  1 +
 lisp/net/tramp-sudoedit.el        |  1 +
 lisp/net/tramp.el                 | 13 ++++++++++++-
 lisp/simple.el                    | 12 ++++++++++++
 test/lisp/eshell/esh-var-tests.el |  4 ++++
 18 files changed, 72 insertions(+), 4 deletions(-)

diff --git a/doc/lispref/files.texi b/doc/lispref/files.texi
index 5cc4c1e7ddf..ad01e0f2886 100644
--- a/doc/lispref/files.texi
+++ b/doc/lispref/files.texi
@@ -3390,7 +3390,8 @@ Magic File Names
 @code{file-readable-p}, @code{file-regular-p},
 @code{file-remote-p}, @code{file-selinux-context},
 @code{file-symlink-p}, @code{file-system-info},
-@code{file-truename}, @code{file-writable-p},
+@code{file-truename}, @code{file-user-uid},
+@code{file-writable-p},
 @code{find-backup-file-name},@*
 @code{get-file-buffer},
 @code{insert-directory},
@@ -3451,7 +3452,8 @@ Magic File Names
 @code{file-readable-p}, @code{file-regular-p},
 @code{file-remote-p}, @code{file-selinux-context},
 @code{file-symlink-p}, @code{file-system-info},
-@code{file-truename}, @code{file-writable-p},
+@code{file-truename}, @code{file-user-uid},
+@code{file-writable-p},
 @code{find-backup-file-name},
 @code{get-file-buffer},
 @code{insert-directory},
diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
index 3be7036f637..268e93bef99 100644
--- a/doc/lispref/os.texi
+++ b/doc/lispref/os.texi
@@ -1277,6 +1277,16 @@ User Identification
 This function returns the effective @acronym{UID} of the user.
 @end defun
 
+@defun file-user-uid
+This function returns the connection-local for the user's effective
+@acronym{UID}.  If @code{default-directory} is local, this is
+equivalent to @code{user-uid}, but for remote files (@pxref{Remote
+Files, , , emacs, The GNU Emacs Manual}), it will return the
+@acronym{UID} for the user associated with that remote connection; if
+the remote connection has no associated user, it will instead return
+-1.
+@end defun
+
 @cindex GID
 @defun group-gid
 This function returns the effective @acronym{GID} of the Emacs process.
diff --git a/doc/misc/eshell.texi b/doc/misc/eshell.texi
index fc7d52eb711..c40ff58f42c 100644
--- a/doc/misc/eshell.texi
+++ b/doc/misc/eshell.texi
@@ -983,6 +983,13 @@ Variables
 the value will automatically update to reflect the search path on that
 host.
 
+@vindex $UID
+@item $UID
+This returns the effective @acronym{UID} for the current user.  This
+variable is connection-aware, so when the current directory is remote,
+its value will be @acronym{UID} for the user associated with that
+remote connection.
+
 @vindex $_
 @item $_
 This refers to the last argument of the last command.  With a
@@ -1014,6 +1021,7 @@ Variables
 copied to the environment, so external commands invoked from
 Eshell can consult them to do the right thing.
 
+@vindex $INSIDE_EMACS
 @item $INSIDE_EMACS
 This variable indicates to external commands that they are being
 invoked from within Emacs so they can adjust their behavior if
diff --git a/etc/NEWS b/etc/NEWS
index 068f7a27db8..daa5eca5c35 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -235,6 +235,11 @@ compared reliably at all.
 This warning can be suppressed using 'with-suppressed-warnings' with
 the warning name 'suspicious'.
 
++++
+** New function 'file-user-uid'.
+This function is like 'user-uid', but is aware of file name handlers,
+so it will return the remote UID for remote files.
+
 \f
 * Changes in Emacs 30.1 on Non-Free Operating Systems
 
diff --git a/lisp/eshell/em-prompt.el b/lisp/eshell/em-prompt.el
index 52d46282c52..b3a0fadf618 100644
--- a/lisp/eshell/em-prompt.el
+++ b/lisp/eshell/em-prompt.el
@@ -52,7 +52,7 @@ eshell-prompt-load-hook
 (defcustom eshell-prompt-function
   (lambda ()
     (concat (abbreviate-file-name (eshell/pwd))
-            (if (= (user-uid) 0) " # " " $ ")))
+            (if (= (file-user-uid) 0) " # " " $ ")))
   "A function that returns the Eshell prompt string.
 Make sure to update `eshell-prompt-regexp' so that it will match your
 prompt."
diff --git a/lisp/eshell/esh-var.el b/lisp/eshell/esh-var.el
index 811bb9957cf..fd76a2c6f09 100644
--- a/lisp/eshell/esh-var.el
+++ b/lisp/eshell/esh-var.el
@@ -162,6 +162,7 @@ eshell-variable-aliases-list
     ("COLUMNS" ,(lambda () (window-body-width nil 'remap)) t t)
     ("LINES" ,(lambda () (window-body-height nil 'remap)) t t)
     ("INSIDE_EMACS" eshell-inside-emacs t)
+    ("UID" ,(lambda () (file-user-uid)) nil t)
 
     ;; for esh-ext.el
     ("PATH" (,(lambda () (string-join (eshell-get-path t) (path-separator)))
diff --git a/lisp/net/tramp-adb.el b/lisp/net/tramp-adb.el
index 29b9f894ec3..10f33e5f929 100644
--- a/lisp/net/tramp-adb.el
+++ b/lisp/net/tramp-adb.el
@@ -153,6 +153,7 @@ tramp-adb-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-adb-handle-file-system-info)
     (file-truename . tramp-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-adb-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-archive.el b/lisp/net/tramp-archive.el
index 3b1d568c127..b9cf85bd843 100644
--- a/lisp/net/tramp-archive.el
+++ b/lisp/net/tramp-archive.el
@@ -265,6 +265,7 @@ tramp-archive-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-archive-handle-file-system-info)
     (file-truename . tramp-archive-handle-file-truename)
+    (file-user-uid . tramp-archive-handle-file-user-uid)
     (file-writable-p . ignore)
     (find-backup-file-name . ignore)
     ;; `get-file-buffer' performed by default handler.
@@ -701,6 +702,12 @@ tramp-archive-handle-temporary-file-directory
     (let ((default-directory (file-name-directory archive)))
       (temporary-file-directory))))
 
+(defun tramp-archive-handle-file-user-uid ()
+  "Like `user-uid' for file archives."
+  (with-parsed-tramp-archive-file-name default-directory nil
+    (let ((default-directory (file-name-directory archive)))
+      (file-user-uid))))
+
 (defun tramp-archive-handle-not-implemented (operation &rest args)
   "Generic handler for operations not implemented for file archives."
   (let ((v (ignore-errors
diff --git a/lisp/net/tramp-crypt.el b/lisp/net/tramp-crypt.el
index 507fd432419..b9d0d96eccc 100644
--- a/lisp/net/tramp-crypt.el
+++ b/lisp/net/tramp-crypt.el
@@ -204,6 +204,7 @@ tramp-crypt-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-crypt-handle-file-system-info)
     ;; `file-truename' performed by default handler.
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-crypt-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-gvfs.el b/lisp/net/tramp-gvfs.el
index ea774d0b9b7..02ceb2979f7 100644
--- a/lisp/net/tramp-gvfs.el
+++ b/lisp/net/tramp-gvfs.el
@@ -798,6 +798,7 @@ tramp-gvfs-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-gvfs-handle-file-system-info)
     (file-truename . tramp-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-rclone.el b/lisp/net/tramp-rclone.el
index b28f9f64952..9eb2a54cdcf 100644
--- a/lisp/net/tramp-rclone.el
+++ b/lisp/net/tramp-rclone.el
@@ -118,6 +118,7 @@ tramp-rclone-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-rclone-handle-file-system-info)
     (file-truename . tramp-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index acbb6aea135..46b1f612101 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -1086,6 +1086,7 @@ tramp-sh-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-sh-handle-file-system-info)
     (file-truename . tramp-sh-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-sh-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-smb.el b/lisp/net/tramp-smb.el
index b3f0344f818..9d03490f1d5 100644
--- a/lisp/net/tramp-smb.el
+++ b/lisp/net/tramp-smb.el
@@ -269,6 +269,7 @@ tramp-smb-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-smb-handle-file-system-info)
     (file-truename . tramp-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-smb-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-sshfs.el b/lisp/net/tramp-sshfs.el
index 27b2854e451..65c4bf23c38 100644
--- a/lisp/net/tramp-sshfs.el
+++ b/lisp/net/tramp-sshfs.el
@@ -124,6 +124,7 @@ tramp-sshfs-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-sshfs-handle-file-system-info)
     (file-truename . tramp-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-sshfs-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp-sudoedit.el b/lisp/net/tramp-sudoedit.el
index ce080918e0f..db7ac842871 100644
--- a/lisp/net/tramp-sudoedit.el
+++ b/lisp/net/tramp-sudoedit.el
@@ -114,6 +114,7 @@ tramp-sudoedit-file-name-handler-alist
     (file-symlink-p . tramp-handle-file-symlink-p)
     (file-system-info . tramp-sudoedit-handle-file-system-info)
     (file-truename . tramp-sudoedit-handle-file-truename)
+    (file-user-uid . tramp-handle-file-user-uid)
     (file-writable-p . tramp-sudoedit-handle-file-writable-p)
     (find-backup-file-name . tramp-handle-find-backup-file-name)
     ;; `get-file-buffer' performed by default handler.
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 169cf1982b4..fab1962d2b7 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -2632,7 +2632,9 @@ tramp-file-name-for-operation
 	    '(exec-path make-nearby-temp-file make-process process-file
 	      shell-command start-file-process temporary-file-directory
 	      ;; Emacs 29+ only.
-              list-system-processes memory-info process-attributes))
+              list-system-processes memory-info process-attributes
+              ;; Emacs 30+ only.
+	      file-user-uid))
     default-directory)
    ;; PROC.
    ((member operation '(file-notify-rm-watch file-notify-valid-p))
@@ -3714,6 +3716,15 @@ tramp-handle-abbreviate-file-name
 	 vec (concat "~" (substring filename (match-beginning 1))))
       (tramp-make-tramp-file-name (tramp-dissect-file-name filename)))))
 
+(defun tramp-handle-file-user-uid ()
+  "Like `user-uid' for Tramp files."
+  (let ((v (tramp-dissect-file-name default-directory)))
+    (or (tramp-get-remote-uid v 'integer)
+        ;; Some handlers for `tramp-get-remote-uid' return nil if they
+        ;; can't get the UID; always return -1 in this case for
+        ;; consistency.
+        -1)))
+
 (defun tramp-handle-access-file (filename string)
   "Like `access-file' for Tramp files."
   (setq filename (file-truename filename))
diff --git a/lisp/simple.el b/lisp/simple.el
index 4b09f41de55..844cfa68b08 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -4730,6 +4730,18 @@ shell-command--same-buffer-confirm
                      action))
       (user-error "Shell command in progress"))))
 
+(defun file-user-uid ()
+  "Return the connection-local effective uid.
+This is similar to `user-uid', but may invoke a file name handler
+based on `default-directory'.  See Info node `(elisp)Magic File
+Names'.
+
+If a file name handler is unable to retrieve the effective uid,
+this function will instead return -1."
+  (if-let ((handler (find-file-name-handler default-directory 'file-user-uid)))
+      (funcall handler 'file-user-uid)
+    (user-uid)))
+
 (defun max-mini-window-lines (&optional frame)
   "Compute maximum number of lines for echo area in FRAME.
 As defined by `max-mini-window-height'.  FRAME defaults to the
diff --git a/test/lisp/eshell/esh-var-tests.el b/test/lisp/eshell/esh-var-tests.el
index 3f602798dbe..0cc1b92266f 100644
--- a/test/lisp/eshell/esh-var-tests.el
+++ b/test/lisp/eshell/esh-var-tests.el
@@ -746,6 +746,10 @@ esh-var-test/path-var/preserve-across-hosts
       (format "cd %s" ert-remote-temporary-file-directory))
      (eshell-match-command-output "echo $PATH" (regexp-quote remote-path)))))
 
+(ert-deftest esh-var-test/uid-var ()
+  "Test that $UID is equivalent to (user-uid) for local directories."
+  (eshell-command-result-equal "echo $UID" (user-uid)))
+
 (ert-deftest esh-var-test/last-status-var-lisp-command ()
   "Test using the \"last exit status\" ($?) variable with a Lisp command"
   (with-temp-eshell
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-16  5:38           ` Jim Porter
@ 2023-01-16  9:09             ` Michael Albinus
  2023-01-17  1:21               ` Jim Porter
  0 siblings, 1 reply; 14+ messages in thread
From: Michael Albinus @ 2023-01-16  9:09 UTC (permalink / raw)
  To: Jim Porter; +Cc: 60722

Jim Porter <jporterbugs@gmail.com> writes:

Hi Jim,

> I didn't do this part though, since it wasn't so simple. Instead, I
> made the function (now named 'file-user-uid'[1]) work like the
> function 'exec-path'. Since the regular 'user-uid' is also a function,
> I think this implementation is the most straightforward.
>
> [1] I named it this way to mimic the relationship between
> 'start-process' and 'start-file-process'. (But 'user-file-uid' didn't
> sound right, so 'file-user-uid' it is.)

Fine with me. I've roughly scanned the patch, it looks OK. So I guess
you shall install it; if there's something left to do we can still do it
on master.

After that, I will add the implementation for ange-ftp.el and some tests
in tramp-tests.el, and I'll check whether the implementation of
tramp-archive-handle-file-user-uid is OK.

> diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
>
> +@defun file-user-uid
> +This function returns the connection-local for the user's effective

"connection-local value"

> diff --git a/etc/NEWS b/etc/NEWS
>
> +** New function 'file-user-uid'.
> +This function is like 'user-uid', but is aware of file name handlers,
> +so it will return the remote UID for remote files.

Perhaps you could also mention -1 as the "don't know" result.

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-16  9:09             ` Michael Albinus
@ 2023-01-17  1:21               ` Jim Porter
  2023-01-17  9:12                 ` Michael Albinus
  2023-01-17 15:03                 ` Michael Albinus
  0 siblings, 2 replies; 14+ messages in thread
From: Jim Porter @ 2023-01-17  1:21 UTC (permalink / raw)
  To: Michael Albinus; +Cc: 60722-done

On 1/16/2023 1:09 AM, Michael Albinus wrote:
>> diff --git a/doc/lispref/os.texi b/doc/lispref/os.texi
>>
>> +@defun file-user-uid
>> +This function returns the connection-local for the user's effective
> 
> "connection-local value"

Thanks, fixed.
> 
>> diff --git a/etc/NEWS b/etc/NEWS
>>
>> +** New function 'file-user-uid'.
>> +This function is like 'user-uid', but is aware of file name handlers,
>> +so it will return the remote UID for remote files.
> 
> Perhaps you could also mention -1 as the "don't know" result.

Also fixed.

I've now merged this to the master branch as 0bb8a011d5. Closing this 
bug, though we can obviously discuss further (here or elsewhere) if 
there's any fallout from the change.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-17  1:21               ` Jim Porter
@ 2023-01-17  9:12                 ` Michael Albinus
  2023-01-18  1:04                   ` Jim Porter
  2023-01-17 15:03                 ` Michael Albinus
  1 sibling, 1 reply; 14+ messages in thread
From: Michael Albinus @ 2023-01-17  9:12 UTC (permalink / raw)
  To: Jim Porter; +Cc: 60722

Jim Porter <jporterbugs@gmail.com> writes:

Hi Jim,

> I've now merged this to the master branch as 0bb8a011d5. Closing this
> bug, though we can obviously discuss further (here or elsewhere) if
> there's any fallout from the change.

Thanks. Pls note, that on emba two tests fail:

--8<---------------cut here---------------start------------->8---
lisp/eshell/esh-proc-tests.log:
  SKIPPED  esh-proc-test/kill-pipeline  ((skip-unless (not (getenv "EMACS_EMBA_CI"))) :form (not "1") :value nil)
lisp/eshell/em-prompt-tests.log:
   FAILED  em-prompt-test/field-properties  ((should (equal-including-properties last-prompt (propertize (format "%s $ " (directory-file-name default-directory)) 'read-only t 'field 'prompt 'font-lock-face 'eshell-prompt 'front-sticky '(read-only field font-lock-face) ...))) :form (equal-including-properties #("/checkout/test # " 0 17 (rear-nonsticky (read-only field font-lock-face) front-sticky (read-only field font-lock-face) font-lock-face eshell-prompt field prompt read-only t)) #("/checkout/test $ " 0 17 (read-only t field prompt font-lock-face eshell-prompt front-sticky (read-only field font-lock-face) rear-nonsticky (read-only field font-lock-face)))) :value nil :explanation (array-elt 15 (different-atoms (35 "#x23" "?#") (36 "#x24" "?$"))))
   FAILED  em-prompt-test/field-properties/no-highlight  ((should (equal-including-properties last-prompt (propertize (format "%s $ " (directory-file-name default-directory)) 'field 'prompt 'front-sticky '(field) 'rear-nonsticky '(field)))) :form (equal-including-properties #("/checkout/test # " 0 17 (rear-nonsticky (field) front-sticky (field) field prompt)) #("/checkout/test $ " 0 17 (field prompt front-sticky (field) rear-nonsticky (field)))) :value nil :explanation (array-elt 15 (different-atoms (35 "#x23" "?#") (36 "#x24" "?$"))))
--8<---------------cut here---------------end--------------->8---

Don't know whether this is related to your recent change, or something
else.

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-17  1:21               ` Jim Porter
  2023-01-17  9:12                 ` Michael Albinus
@ 2023-01-17 15:03                 ` Michael Albinus
  1 sibling, 0 replies; 14+ messages in thread
From: Michael Albinus @ 2023-01-17 15:03 UTC (permalink / raw)
  To: Jim Porter; +Cc: 60722

Jim Porter <jporterbugs@gmail.com> writes:

Hi Jim,

> I've now merged this to the master branch as 0bb8a011d5. Closing this
> bug, though we can obviously discuss further (here or elsewhere) if
> there's any fallout from the change.

I've pushed my part as 013ab7e2a8.

Best regards, Michael.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil
  2023-01-17  9:12                 ` Michael Albinus
@ 2023-01-18  1:04                   ` Jim Porter
  0 siblings, 0 replies; 14+ messages in thread
From: Jim Porter @ 2023-01-18  1:04 UTC (permalink / raw)
  To: Michael Albinus; +Cc: 60722

On 1/17/2023 1:12 AM, Michael Albinus wrote:
> Jim Porter <jporterbugs@gmail.com> writes:
> 
> Hi Jim,
> 
>> I've now merged this to the master branch as 0bb8a011d5. Closing this
>> bug, though we can obviously discuss further (here or elsewhere) if
>> there's any fallout from the change.
> 
> Thanks. Pls note, that on emba two tests fail:
[snip]
> Don't know whether this is related to your recent change, or something
> else.

That's probably a bug from when those tests were introduced 
(558f04c39e). The tests didn't work when they were run as root. I 
hopefully fixed it with commit 281f48f19e.





^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-01-18  1:04 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-10 23:50 bug#60722: 30.0.50; [PATCH] Using Tramp to sudo in Eshell doesn't change prompt sigil Jim Porter
2023-01-11  2:12 ` Ruijie Yu via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-11  9:35   ` Michael Albinus
2023-01-11  9:33 ` Michael Albinus
2023-01-11 13:59   ` Michael Albinus
2023-01-14 21:59     ` Jim Porter
2023-01-14 22:10       ` Jim Porter
2023-01-15  9:23         ` Michael Albinus
2023-01-16  5:38           ` Jim Porter
2023-01-16  9:09             ` Michael Albinus
2023-01-17  1:21               ` Jim Porter
2023-01-17  9:12                 ` Michael Albinus
2023-01-18  1:04                   ` Jim Porter
2023-01-17 15:03                 ` Michael Albinus

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.