Skip to content

Commit

Permalink
path_conv: special-case root directory to have trailing slash
Browse files Browse the repository at this point in the history
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
git-for-windows/git#1497 (comment),
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>
  • Loading branch information
Alexpux authored and dscho committed May 12, 2023
1 parent 57e1706 commit 3a974ad
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions winsup/cygwin/path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,12 @@ path_conv::check (const char *src, unsigned opt,
need_directory = 1;
*--tail = '\0';
}
/* Special case for "/" must set need_directory, without removing
trailing slash */
else if (tail == path_copy + 1 && isslash (tail[-1]))
{
need_directory = 1;
}
path_end = tail;

/* Scan path_copy from right to left looking either for a symlink
Expand Down Expand Up @@ -1248,6 +1254,7 @@ path_conv::check (const char *src, unsigned opt,
cfree (wide_path);
wide_path = NULL;
}

if (need_directory)
{
size_t n = strlen (this->path);
Expand Down

0 comments on commit 3a974ad

Please sign in to comment.