Subject: Re: bug#69085: 29.2; Tramp: Extend tramp-make-copy-program-file-name via tramp-methods

From: Sean Devlin <[email-masked]>
Date: Wed, 14 Feb 2024 19:57:13 -0500

Hi Michael,

> On Feb 14, 2024, at 1:59 AM, Michael Albinus <[email-masked]> wrote:
> 
> Sean Devlin <[email-masked]> writes:
> 
>> Hi Michael,
> 
> Hi Sean,
> 
>>> - And if HOST is a kind of container, would it be possible for you to
>>> use the "docker" or "podman" method of Tramp directly?
>> 
>> I’ve looked a little bit at the code supporting these methods in tramp-container.el.
>> They seem similar, but not exactly the same. Also, they do not support OOB
>> copying, at least in the version of Tramp I’m reading (i.e. the version packaged with
>> Emacs 29.2).
> 
> Could you pls elaborate verbosely, what is missing in your use case?
> Perhaps we could extend tramp-container.el that it is applicable for you
> as well.

Sure, let’s focus on tramp-container.el. I think this will allow us to be
concrete, which will make things simpler to discuss.

Suppose we want to add OOB copying to the Docker method. Docker
has a copy command with usage like this:

Usage:  docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|-
	docker cp [OPTIONS] SRC_PATH|- CONTAINER:DEST_PATH

We could try to add support to the Docker method for OOB copying by
starting Emacs -Q and evaluating these forms:

(require 'tramp-container)
(add-to-list 'tramp-methods
             `(,tramp-docker-method
               (tramp-login-program ,tramp-docker-program)
               (tramp-login-args (("exec")
                                  ("-it")
                                  ("-u" "%u")
                                  ("%h")
			          ("%l")))
	       (tramp-direct-async (,tramp-default-remote-shell "-c"))
               (tramp-remote-shell ,tramp-default-remote-shell)
               (tramp-remote-shell-login ("-l"))
               (tramp-remote-shell-args ("-i" "-c"))

	       (tramp-copy-program ,tramp-docker-program)
	       (tramp-copy-args (("cp")))
	       ))

Outside of Emacs, start a Docker container running some image (e.g.
ubuntu:latest). An example command is:

$ docker run -it ubuntu:latest bash -l

Leave this running.

Next, create a large (i.e. greater than tramp-copy-size-limit) temporary
file for copying:

$ head -c 20000 /dev/random > /tmp/bigfile

Back in Emacs, evaluate these forms (with “my_container” replaced with
the name of your container):

(copy-file "/tmp/bigfile" "/docker:my_container:/tmp/bigfile")

(copy-file "/tmp/bigfile" "/docker:root@my_container:/tmp/bigfile2”)

The first copy will succeed, but the second will fail. This is because the
connection has an explicit user, so it will try to run a command like:

docker cp /tmp/bigfile root@my_container:/tmp/bigfile2

It will fail because it will interpret “root@my_container” as the container’s
name, and it will say that there is no container by that name.

I think what is needed is some way for the tramp-docker method to specify
the format for remote paths in the OOB copy command, since
tramp-make-copy-program-file-name will include the user if it is specified
in the connection.

One method might be to add a key to tramp-methods that lets you specify
the format similar to tramp-login-args and tramp-copy-args:

(tramp-copy-remote-file-format (("%h" ":" "%f")))

Where “%h” and “%f” are the host and the file’s local name, respectively.

Like tramp-login-args, the format could be a list of lists, and sublists could
be ignored when the required properties are absent. For example, the
format for scp-like copy programs might look like this:

(tramp-copy-remote-file-format
 (("%u" "@")
  ("%h" ":" "%f")))

Where “%u” is the user. So the leading “user@“ could be dropped if the
user is unspecified.

Unlike tramp-login-args and tramp-copy-args, this would need to
concatenate the strings together without spaces.

The function tramp-make-copy-program-file-name could check for this
method parameter and, if present, use it to format the result. For example,
the cond at the end could be changed like so:

(let ((fmt (tramp-get-method-parameter vec 'tramp-copy-remote-file-format)))
  (cond
   ((tramp-get-method-parameter vec 'tramp-remote-copy-program)
    localname)
   (fmt
    ;; Format the file name using fmt.
    )
   ((tramp-string-empty-or-nil-p user) (format "%s:%s" host localname))
   (t (format "%s@%s:%s" user host localname)))

Instead of a list of lists of strings, another method would be to specify a
function that does the work. The function could take the vector as input and
return the formatted file name. The tramp-make-copy-program-file-name
function could call that function (if specified) at the same place in that cond
form. This would be more flexible than the list of lists of strings, but it also
would be more complicated for the implementer and may be overkill.

> 
> (I take the time to invest it with you, because your urgent problem seems
> to be mitigated by your defadvice.)

I appreciate your help—thank you for taking the time!

>> Thanks for your help!
> Best regards, Michael.