all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: "Mattias Engdegård" <mattiase@acm.org>
Cc: 26911@debbugs.gnu.org, Michael Albinus <michael.albinus@gmx.de>,
	Yegor Timoshenko <yegortimoshenko@gmail.com>
Subject: bug#26911: 25.2; eshell "cd .." doesn't work correctly with TRAMP
Date: Thu, 27 Aug 2020 14:53:52 -0700	[thread overview]
Message-ID: <693aa189-03fa-b963-89eb-ce19c51ba325@cs.ucla.edu> (raw)
In-Reply-To: <DE3A07C6-6BFE-49DA-A0E6-949A64685A17@acm.org>

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

On 8/27/20 11:31 AM, Mattias Engdegård wrote:
> No doubt the change (14fb657ba82) is fine in isolation, but now if Emacs is started from $HOME/somedir and I do find-file, the minibuffer prompt is "/home/mattias/somedir/" instead of "~/somedir/" which does not seem to be an improvement.
> 
> Worse, if cwd is $HOME, the minibuffer prompt becomes "~" instead of "~/" which is inconvenient since that slash has to be typed explicitly.

Thanks for reporting that. Sigh, too often when I fix one bug in 
expand-file-name I introduce another. I installed the attached patch to fix this 
bug (I and I hope it doesn't introduce yet another :-).

[-- Attachment #2: 0001-Fix-recently-introduced-expand-file-name-bug.patch --]
[-- Type: text/x-patch, Size: 4982 bytes --]

From 0bbc84630f12e848e19c39dce01f3d14559bf70b Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Thu, 27 Aug 2020 14:46:52 -0700
Subject: [PATCH] Fix recently-introduced expand-file-name bug
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

The bug was that (expand-file-name "~") returned something
like "/home/eggert/" instead of "/home/eggert".
Problem	reported by Mattias Engdegård (Bug#26911#27).
* src/fileio.c (Fexpand_file_name): When concatenating NEWDIR to
NM, instead of stripping trailing slashes from NEWDIR (which can
turn non-symlinks into symlinks), strip leading slashes from NM.
This also simplifies the code by removing no-longer-needed DOS_NT
special-casing.  Also, remove an unnecessary ‘target[length] = 0;’
as that byte will be overwritten by the next memcpy anyway.
* test/src/fileio-tests.el (fileio-tests--HOME-trailing-slash):
New test.
---
 src/fileio.c             | 38 +++++++++++++-------------------------
 test/src/fileio-tests.el |  8 ++++++++
 2 files changed, 21 insertions(+), 25 deletions(-)

diff --git a/src/fileio.c b/src/fileio.c
index b70dff1c22..47e5e46a00 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -827,9 +827,9 @@ DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
   ptrdiff_t tlen;
 #ifdef DOS_NT
   int drive = 0;
-  bool collapse_newdir = true;
   bool is_escaped = 0;
 #endif /* DOS_NT */
+  bool collapse_newdir = true;
   ptrdiff_t length, nbytes;
   Lisp_Object handler, result, handled_name;
   bool multibyte;
@@ -1183,9 +1183,7 @@ DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
 	      newdir = SSDATA (hdir);
 	      newdirlim = newdir + SBYTES (hdir);
 	    }
-#ifdef DOS_NT
 	  collapse_newdir = false;
-#endif
 	}
       else			/* ~user/filename */
 	{
@@ -1205,9 +1203,7 @@ DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
 
 	      while (*++nm && !IS_DIRECTORY_SEP (*nm))
 		continue;
-#ifdef DOS_NT
 	      collapse_newdir = false;
-#endif
 	    }
 
 	  /* If we don't find a user of that name, leave the name
@@ -1374,12 +1370,7 @@ DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
     }
 #endif /* DOS_NT */
 
-  /* Ignore any slash at the end of newdir, unless newdir is
-     just "/" or "//".  */
   length = newdirlim - newdir;
-  while (length > 1 && IS_DIRECTORY_SEP (newdir[length - 1])
-	 && ! (length == 2 && IS_DIRECTORY_SEP (newdir[0])))
-    length--;
 
   /* Now concatenate the directory and name to new space in the stack frame.  */
   tlen = length + file_name_as_directory_slop + (nmlim - nm) + 1;
@@ -1398,25 +1389,22 @@ DEFUN ("expand-file-name", Fexpand_file_name, Sexpand_file_name, 1, 2, 0,
 
   if (newdir)
     {
-      if (IS_DIRECTORY_SEP (nm[0]))
+      if (!collapse_newdir)
 	{
-#ifdef DOS_NT
-	  /* If newdir is effectively "C:/", then the drive letter will have
-	     been stripped and newdir will be "/".  Concatenating with an
-	     absolute directory in nm produces "//", which will then be
-	     incorrectly treated as a network share.  Ignore newdir in
-	     this case (keeping the drive letter).  */
-	  if (!(drive && nm[0] && IS_DIRECTORY_SEP (newdir[0])
-		&& newdir[1] == '\0'))
-#endif
-	    {
-	      memcpy (target, newdir, length);
-	      target[length] = 0;
-	      nbytes = length;
-	    }
+	  /* With ~ or ~user, leave NEWDIR as-is to avoid transforming
+	     it from a symlink (or a regular file!) into a directory.  */
+	  memcpy (target, newdir, length);
+	  nbytes = length;
 	}
       else
 	nbytes = file_name_as_directory (target, newdir, length, multibyte);
+
+      /* If TARGET ends in a directory separator, omit leading
+	 directory separators from NM so that concatenating a TARGET "/"
+	 to an NM "/foo" does not result in the incorrect "//foo".  */
+      if (nbytes && IS_DIRECTORY_SEP (target[nbytes - 1]))
+	while (IS_DIRECTORY_SEP (nm[0]))
+	  nm++;
     }
 
   memcpy (target + nbytes, nm, nmlim - nm + 1);
diff --git a/test/src/fileio-tests.el b/test/src/fileio-tests.el
index 1516590795..8b76912f5e 100644
--- a/test/src/fileio-tests.el
+++ b/test/src/fileio-tests.el
@@ -108,6 +108,14 @@ fileio-tests--relative-HOME
       (should (equal (expand-file-name "~/bar") "x:/foo/bar")))
     (setenv "HOME" old-home)))
 
+(ert-deftest fileio-tests--HOME-trailing-slash ()
+  "Test that expand-file-name of \"~\" respects trailing slash."
+  (let ((old-home (getenv "HOME")))
+    (dolist (home '("/a/b/c" "/a/b/c/"))
+      (setenv "HOME" home)
+      (should (equal (expand-file-name "~") (expand-file-name home))))
+    (setenv "HOME" old-home)))
+
 (ert-deftest fileio-tests--expand-file-name-trailing-slash ()
   (dolist (fooslashalias '("foo/" "foo//" "foo/." "foo//." "foo///././."
                            "foo/a/.."))
-- 
2.17.1


  parent reply	other threads:[~2020-08-27 21:53 UTC|newest]

Thread overview: 49+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-13 16:15 bug#26911: 25.2; eshell "cd .." doesn't work correctly with TRAMP Yegor Timoshenko
2017-05-13 18:25 ` Michael Albinus
2017-05-13 18:30   ` Yegor Timoshenko
2017-05-15 15:53     ` Michael Albinus
2020-08-26 20:39 ` Paul Eggert
2020-08-27 11:46   ` Michael Albinus
2020-08-27 18:31 ` Mattias Engdegård
2020-08-27 18:38   ` Eli Zaretskii
2020-08-27 18:54     ` Stephen Berman
2020-08-27 21:53   ` Paul Eggert [this message]
2020-08-28  6:39     ` Eli Zaretskii
2020-08-28  7:01       ` Eli Zaretskii
2020-08-28 10:48         ` Eli Zaretskii
2020-08-29  5:52           ` Paul Eggert
2020-08-29  6:31             ` Eli Zaretskii
2020-08-29 16:46               ` Paul Eggert
2020-08-29 16:59                 ` Michael Albinus
2020-08-29 18:29                   ` Eli Zaretskii
2020-08-29 19:12                     ` Michael Albinus
2020-08-29 19:31                       ` Eli Zaretskii
2020-08-30  9:46                         ` Michael Albinus
2020-08-30 14:14                           ` Eli Zaretskii
2020-08-29 18:26                 ` Eli Zaretskii
2020-08-29 20:42                   ` Paul Eggert
2020-08-30 14:09                     ` Eli Zaretskii
2020-08-30 21:39                       ` Paul Eggert
2020-08-31 14:58                         ` Eli Zaretskii
2020-08-31 18:15                           ` Paul Eggert
2020-08-31 18:56                             ` Eli Zaretskii
2020-08-31 23:36                               ` Paul Eggert
2020-09-01  2:33                                 ` Eli Zaretskii
2020-09-03 17:27                           ` Eli Zaretskii
2020-09-03 17:42                             ` Michael Albinus
2020-09-04 11:55                             ` Michael Albinus
2020-09-04 12:25                               ` Eli Zaretskii
2020-09-04 13:53                                 ` Michael Albinus
2020-09-04 14:40                                   ` Eli Zaretskii
2020-09-04 15:20                                     ` Eli Zaretskii
2020-09-04 15:59                                       ` Michael Albinus
2020-09-04 17:42                                         ` Eli Zaretskii
2020-09-05  8:34                                           ` Michael Albinus
2020-09-05 11:18                                             ` Eli Zaretskii
2020-09-05 11:32                                               ` Eli Zaretskii
2020-09-05 15:57                                               ` Michael Albinus
2020-09-05 16:33                                                 ` Eli Zaretskii
2020-09-05 17:08                                                   ` Eli Zaretskii
2020-09-05 17:36                                                   ` Michael Albinus
2020-09-05 17:56                                                     ` Eli Zaretskii
2020-09-04 16:09                                     ` Michael Albinus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=693aa189-03fa-b963-89eb-ce19c51ba325@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=26911@debbugs.gnu.org \
    --cc=mattiase@acm.org \
    --cc=michael.albinus@gmx.de \
    --cc=yegortimoshenko@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.