Skip to content

Commit

Permalink
Fix NSURL path on Windows for UNC paths
Browse files Browse the repository at this point in the history
  • Loading branch information
triplef committed Jul 26, 2023
1 parent f0e33a4 commit 48c8a1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
6 changes: 6 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2023-07-26 Frederik Seiffert <[email protected]>

* Source/NSURL.m:
* Tests/base/NSURL/basic.m:
Fix NSURL path on Windows for UNC paths.

2023-07-25 Frederik Seiffert <[email protected]>

* Headers/Foundation/NSFileManager.h:
Expand Down
26 changes: 13 additions & 13 deletions Source/NSURL.m
Original file line number Diff line number Diff line change
Expand Up @@ -1497,24 +1497,24 @@ - (char*) _path: (char*)buf withEscapes: (BOOL)withEscapes
}

#if defined(_WIN32)
/* On windows a file URL path may be of the form C:\xxx (ie we should
* not insert the leading slash).
/* On Windows a file URL path may be of the form C:\xxx or \\xxx,
* and in both cases we should not insert the leading slash.
* Also the vertical bar symbol may have been used instead of the
* colon, so we need to convert that.
*/
if (myData->isFile == YES)
{
if (ptr[1] && isalpha(ptr[1]))
{
if (ptr[2] == ':' || ptr[2] == '|')
{
if (ptr[3] == '\0' || ptr[3] == '/' || ptr[3] == '\\')
{
ptr[2] = ':';
ptr++;
}
}
}
if ((ptr[1] && isalpha(ptr[1]))
&& (ptr[2] == ':' || ptr[2] == '|')
&& (ptr[3] == '\0' || ptr[3] == '/' || ptr[3] == '\\'))
{
ptr[2] = ':';
ptr++; // remove leading slash
}
else if (ptr[1] == '\\' && ptr[2] == '\\')
{
ptr++; // remove leading slash
}
}
#endif
return ptr;
Expand Down
10 changes: 10 additions & 0 deletions Tests/base/NSURL/basic.m
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ int main()
"File URL C:\\WINDOWS is file:///C:%%5CWINDOWS/");
PASS_EQUAL([url resourceSpecifier], @"/C:%5CWINDOWS/",
"resourceSpecifier of C:\\WINDOWS is /C:%5CWINDOWS/");

// UNC path
url = [NSURL fileURLWithPath: @"\\\\SERVER\\SHARE\\"];
str = [url path];
PASS_EQUAL(str, @"\\\\SERVER\\SHARE\\",
"Path of file URL \\\\SERVER\\SHARE\\ is \\\\SERVER\\SHARE\\");
PASS_EQUAL([url description], @"file:///%5C%5CSERVER%5CSHARE%5C",
"File URL \\\\SERVER\\SHARE\\ is file:///%5C%5CSERVER%5CSHARE%5C");
PASS_EQUAL([url resourceSpecifier], @"/%5C%5CSERVER%5CSHARE%5C",
"resourceSpecifier of \\\\SERVER\\SHARE\\ is /%5C%5CSERVER%5CSHARE%5C");
#else
url = [NSURL fileURLWithPath: @"/usr"];
str = [url path];
Expand Down

0 comments on commit 48c8a1a

Please sign in to comment.