public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
From: Takashi Yano <takashi.yano@nifty.ne.jp>
To: cygwin-patches@cygwin.com
Subject: [PATCH] Cygwin: path: Fix path conversion of virtual drive.
Date: Thu,  9 Dec 2021 17:17:50 +0900	[thread overview]
Message-ID: <20211209081750.4970-1-takashi.yano@nifty.ne.jp> (raw)

- The last change in path.cc introduced a bug that causes an error
  when accessing a virtual drive which mounts UNC path such as
  "\\server\share\dir" rather than "\\server\share". This patch
  fixes the issue.
---
 winsup/cygwin/path.cc | 50 +++++++++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 21 deletions(-)

diff --git a/winsup/cygwin/path.cc b/winsup/cygwin/path.cc
index eb1255849..6682d2a58 100644
--- a/winsup/cygwin/path.cc
+++ b/winsup/cygwin/path.cc
@@ -3507,29 +3507,37 @@ restart:
 		  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. */
+		      /* ...get the remote path, replace remote path
+			 with drive letter, check again. */
+		      WCHAR drive[3] =
+			{(WCHAR) towupper (upath.Buffer[4]), L':', L'\0'};
 		      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;
-			}
+		      if (!QueryDosDeviceW (drive, remote, MAX_PATH))
+			goto file_not_symlink; /* fallback */
+
+		      int remlen = wcslen (remote);
+		      if (remote[remlen - 1] == L'\\')
+			remlen--;
+		      WCHAR *p;
+		      if (wcsstr (remote, L"\\??\\UNC\\") == remote)
+			remlen -= 6;
+		      else if ((p = wcschr (remote, L';') + 1)
+			       && wcsstr (p, drive) == p
+			       && (p = wcschr (p + 2, L'\\')))
+			remlen -= p - remote - 1;
+		      else
+			goto file_not_symlink; /* fallback */
+		      /* Hackfest */
+		      fpath.Buffer[4] = drive[0]; /* Drive letter */
+		      fpath.Buffer[5] = L':';
+		      WCHAR *to = fpath.Buffer + 6;
+		      WCHAR *from = to + remlen;
+		      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.
-- 
2.34.1


             reply	other threads:[~2021-12-09  8:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-09  8:17 Takashi Yano [this message]
2021-12-09 10:08 ` Corinna Vinschen
2021-12-09 11:14   ` Takashi Yano

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=20211209081750.4970-1-takashi.yano@nifty.ne.jp \
    --to=takashi.yano@nifty.ne.jp \
    --cc=cygwin-patches@cygwin.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.
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).