public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Takashi Yano <tyan0@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin/cygwin-3_3-branch] Cygwin: path: Convert UNC path prefix back to drive letter.
Date: Wed,  8 Dec 2021 11:55:32 +0000 (GMT)	[thread overview]
Message-ID: <20211208115532.A60C63858D3C@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=1b40f151cd5ad86241e98fe3b60d4700f04bfb99

commit 1b40f151cd5ad86241e98fe3b60d4700f04bfb99
Author: Takashi Yano <takashi.yano@nifty.ne.jp>
Date:   Wed Dec 8 20:15:45 2021 +0900

    Cygwin: path: Convert UNC path prefix back to drive letter.
    
    - When symlink_info::check() is called with the path having drive
      letter and UNC path is mounted to the drive, the path is replaced
      with UNC path. With this patch, UNC path prefix is converted back
      to drive letter.  This fixes the issue:
      https://cygwin.com/pipermail/cygwin/2021-November/250087.html
      https://cygwin.com/pipermail/cygwin/2021-December/250103.html

Diff:
---
 winsup/cygwin/path.cc       | 37 ++++++++++++++++++++++++++++++++++++-
 winsup/cygwin/release/3.3.4 |  5 +++++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index baf04ce89..eb1255849 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -3492,10 +3492,45 @@ restart:
 	    {
 	      UNICODE_STRING fpath;
 
-	      RtlInitCountedUnicodeString (&fpath, fpbuf, ret * sizeof (WCHAR));
+	      /* If incoming path has no trailing backslash, but final path
+		 has one, drop trailing backslash from final path so the
+		 below string comparison has a chance to succeed. */
+	      if (upath.Buffer[(upath.Length - 1) / sizeof (WCHAR)] != L'\\'
+                  && fpbuf[ret - 1] == L'\\')
+                fpbuf[--ret] = L'\0';
 	      fpbuf[1] = L'?';	/* \\?\ --> \??\ */
+	      RtlInitCountedUnicodeString (&fpath, fpbuf, ret * sizeof (WCHAR));
 	      if (!RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
 	        {
+		  /* Check if the final path is an UNC path and the incoming
+		     path isn't.  If so... */
+		  if (RtlEqualUnicodePathPrefix (&fpath, &ro_u_uncp, TRUE)
+		      && !RtlEqualUnicodePathPrefix (&upath, &ro_u_uncp, TRUE))
+		    {
+		      /* ...get the remote path from the volume path name,
+			 replace remote path with drive letter, check again. */
+		      WCHAR remote[MAX_PATH];
+
+		      fpbuf[1] = L'\\';
+		      BOOL r = GetVolumePathNameW (fpbuf, remote, MAX_PATH);
+		      fpbuf[1] = L'?';
+		      if (r)
+			{
+			  int remlen = wcslen (remote);
+			  if (remote[remlen - 1] == L'\\')
+			    remlen--;
+			  /* Hackfest */
+			  fpath.Buffer[4] = upath.Buffer[4]; /* Drive letter */
+			  fpath.Buffer[5] = L':';
+			  WCHAR *to = fpath.Buffer + 6;
+			  WCHAR *from = to + remlen - 6;
+			  memmove (to, from,
+				   (wcslen (from) + 1) * sizeof (WCHAR));
+			  fpath.Length -= (from - to) * sizeof (WCHAR);
+			  if (RtlEqualUnicodeString (&upath, &fpath, !!ci_flag))
+			    goto file_not_symlink;
+			}
+		    }
 		  issymlink = true;
 		  /* upath.Buffer is big enough and unused from this point on.
 		     Reuse it here, avoiding yet another buffer allocation. */
diff --git a/winsup/cygwin/release/3.3.4 b/winsup/cygwin/release/3.3.4
index f1c32a1a5..4cbfba8eb 100644
--- a/winsup/cygwin/release/3.3.4
+++ b/winsup/cygwin/release/3.3.4
@@ -4,3 +4,8 @@ Bug Fixes
 - Fix a bug in fhandler_dev_clipboard::read() that the second read
   fails with 'Bad address'.
   Addresses: https://cygwin.com/pipermail/cygwin/2021-December/250141.html
+
+- Convert UNC path prefix back to drive letter in symlink_info::check().
+  This solves the following issues:
+  Addresses: https://cygwin.com/pipermail/cygwin/2021-November/250087.html
+             https://cygwin.com/pipermail/cygwin/2021-December/250103.html


                 reply	other threads:[~2021-12-08 11:55 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20211208115532.A60C63858D3C@sourceware.org \
    --to=tyan0@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).