Subject: Re: bug#29440: Tramp put drive_c in the backup file name when backuping in local PC/windows

From: Eli Zaretskii <[email-masked]>
Date: Mon, 27 Nov 2017 20:07:10 +0200

> From: Michael Albinus <[email-masked]>
> Date: Mon, 27 Nov 2017 18:24:45 +0100
> Cc: [email-masked]
> 
> When I enter line 4422, the (when (memq system-type '(windows-nt ms-dos cygwin))
> part, backup-directory is
> "c:/Users/lb01177/AppData/Roaming/.emacs.d/backup", and file is still
> "/plink:[email-masked]:/home/albinus/tmp/123". As expected.
> 
> The interesting part is here:
> 
> --8<---------------cut here---------------start------------->8---
> 	      ;; Replace any invalid file-name characters (for the
> 	      ;; case of backing up remote files).
> 	      (setq file
> 		    (expand-file-name (convert-standard-filename file)))
> --8<---------------cut here---------------end--------------->8---
> 
> (convert-standard-filename file) returns "\\[email-masked]!\\home\\albinus\\tmp\\123".
> And (expand-file-name ...) returns then "c:/[email-masked]!/home/albinus/tmp/123".
> 
> Unexpected, indeed. I believe, this must be fixed, because "c:" is
> converted to "/drive_c" later on. The final backup name is
> "c:/Users/lb01177/AppData/Roaming/.emacs.d/backup/[email-masked]!!!home!albinus!tmp!123.~3~"
> 
> Eli?

The code there is inappropriate for remote file names, it assumes a
local file name.  A remote file name should be processed as follows:

  . remove the leading slash
  . run the result through convert-standard-filename
  . then do this:

	    (expand-file-name
	     (subst-char-in-string
	      ?/ ?!
	      (replace-regexp-in-string "!" "!!" file))
	     backup-directory))

With your example, this produces

  "c:/Users/lb01177/AppData/Roaming/.emacs.d/backup/[email-masked]!!!home!albinus!tmp!123"

Or maybe prepend a slashe before calling subst-char-in-string?  That
would produce

  "c:/Users/lb01177/AppData/Roaming/.emacs.d/backup/[email-masked]!!!home!albinus!tmp!123"

WDYT?