From 358b4547f7c159ca544a24938aca350b3aac84dd Mon Sep 17 00:00:00 2001
From: Johannes Schindelin <johannes.schindelin@gmx.de>
Date: Tue, 20 Sep 2022 21:47:34 +0200
Subject: [PATCH 40/N] amend! Special case for converting root directory to
 have training slash

path_conv: special-case root directory to have trailing slash

When converting `/c/` to `C:\`, the trailing slash is actually really
necessary, as `C:` is not an absolute path.

We must be very careful to do this only for root directories, though. If
we kept the trailing slash also for, say, `/y/directory/`, we would run
into the following issue: On FAT file systems, the normalized path is
used to fake inode numbers. As a result, `Y:\directory\` and
`Y:\directory` have different inode numbers!!!

This would result in very non-obvious symptoms. Back when we were too
careless about keeping the trailing slash, it was reported to the Git
for Windows project that the `find` and `rm` commands can error out on
FAT file systems with very confusing "No such file or directory" errors,
for no good reason.

During the original investigation, Vasil Minkov pointed out in
https://github.com/git-for-windows/git/issues/1497#issuecomment-372665870,
that this bug had been fixed in Cygwin as early as 1997... and the bug
was unfortunately reintroduced into early MSYS2 versions.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 winsup/cygwin/path.cc | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index 9a3ad7a..e191b9e 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -1254,17 +1254,17 @@ path_conv::check (const char *src, unsigned opt,
 		cfree (wide_path);
 	      wide_path = NULL;
 	    }
-	}
 
-      if (need_directory)
-	{
-	  size_t n = strlen (this->path);
-	  /* Do not add trailing \ to UNC device names like \\.\a: */
-	  if (this->path[n - 1] != '\\' &&
-	      (strncmp (this->path, "\\\\.\\", 4) != 0))
+	  if (need_directory)
 	    {
-	      this->modifiable_path ()[n] = '\\';
-	      this->modifiable_path ()[n + 1] = '\0';
+	      size_t n = strlen (this->path);
+	      /* Do not add trailing \ to UNC device names like \\.\a: */
+	      if (this->path[n - 1] != '\\' &&
+		  (strncmp (this->path, "\\\\.\\", 4) != 0))
+		{
+		  this->modifiable_path ()[n] = '\\';
+		  this->modifiable_path ()[n + 1] = '\0';
+		}
 	    }
 	}
 
