[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Finding buffers visiting TRAMP sudo files

From: Michael Albinus
Subject: Re: Finding buffers visiting TRAMP sudo files
Date: Fri, 27 Oct 2017 11:09:04 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (gnu/linux)

"Basil L. Contovounesios" <address@hidden> writes:

Hi Basil,

> ### Issue
> Normally the function find-buffer-visiting (or even the
> simpler get-file-buffer) is up to this challenge, but not so
> when the file has been opened via TRAMP sudo syntax.

First of all: which Emacs / Tramp versions are you using? My answers
will be based on Emacs 26.0.90 / Tramp 2.3.3-pre. Emacs 25.x comes with
Tramp 2.2.y, which differs often.

> ### Observations
> As an example, when opening the write-protected file
> /etc/mpd.conf, with inode 123 and device number 456 on host
> foo, as /sudo::/etc/mpd.conf, I observe the following:

Since I have no file /etc/mpd.conf, I use /etc/pam.conf instead.

>   (get-buffer "/etc/mpd.conf")
>       ⇒ #<buffer /etc/mpd.conf>

This does not work, the buffer name is "pam.conf". I must call

  (get-buffer "pam.conf")
      ⇒ #<buffer pam.conf>

>   (get-file-buffer "/etc/mpd.conf")
>       ⇒ nil

Same here for "/etc/pam.conf". However, this works:

  (get-file-buffer "/sudo::/etc/pam.conf")
      ⇒ #<buffer pam.conf>

>   (find-buffer-visiting "/etc/mpd.conf")
>       ⇒ nil

Same here for "/etc/pam.conf". However, this works:

  (find-buffer-visiting "/sudo::/etc/pam.conf")
      ⇒ #<buffer pam.conf>

>   (nthcdr 10 (file-attributes "/etc/mpd.conf"))
>       ⇒ (123 456)

Same here. However, for Emacs (Tramp), the files "/etc/pam.conf" and
"/sudo::/etc/pam.conf" are different. And so are the inode
numbers. Well, this is not a must (since the device numbers differ
anyway), but it's the current implementation.

> Inside the file's corresponding buffer:
>   buffer-file-name
>       ⇒ #("/sudo:address@hidden:/etc/mpd.conf"
>           6 10 (tramp-default t))
>   (equal buffer-file-name buffer-file-truename)
>       ⇒ t
>   buffer-file-number
>       ⇒ (123 (-1 . 1))

Similar here, when using "pam.conf".

> ### Diagnosis
> AFAICT, (find-buffer-visiting "/etc/mpd.conf") fails when
> the file has been opened via TRAMP because the function
> compares:
> a) the given plain filename with the TRAMP-qualified values
>    of buffer-file-{,true}name; and
> b) both inode and device numbers to determine file equality,
>    but opening a file via TRAMP seems to modify its device
>    number.
> ### Questions
> 1) Why does the file-attribute-device-number of
>    TRAMP-visited files change? Is this necessary?

Yes. "/etc/pam.conf" and "/sudo::/etc/pam.conf" are different files for
Emacs (Tramp). Maybe one could argue that the inode number shall equal
for both files, but the device numbers must differ.

> 2) Does TRAMP provide a means of comparing visited files for
>    equality independently of TRAMP method, or at least for
>    particular TRAMP methods, e.g. sudo?

What do you mean "comparing"? Whether the files are equal? Plain Emacs
functionality, like diff or ediff, exists.

If both files are already loaded in buffers, you could compare them via
`compare-buffer-substrings' and friends.

Tramp does not offer special functionality for this. After all, Tramp is
just a library, which carries alternative implementations of some basic
file operations.

What else do you need?

> 3) Given a plain, non-TRAMP-qualified filename, how can I
>    lookup a TRAMP buffer visiting it?

Look for (concat "/sudo::" filename) or alike.

> 3.1 How do I get find-buffer-visiting to work with TRAMP
>     buffers?

See above.

> TIA for any and all help with or insights on this.

Best regards, Michael.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]