Skip to content

Commit 3ccda36

Browse files
committed
[Windows] Fix Symbolic Link Creation
- Don't error out if the target doesn't exist - Check the correct path when determining if it's a directory
1 parent 0186a12 commit 3ccda36

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

Foundation/FileManager+Win32.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,9 @@ extension FileManager {
264264
}
265265

266266
internal func _createSymbolicLink(atPath path: String, withDestinationPath destPath: String) throws {
267-
let faAttributes: WIN32_FILE_ATTRIBUTE_DATA = try windowsFileAttributes(atPath: path)
268-
var dwFlags: DWORD = DWORD(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
267+
var dwFlags = DWORD(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)
268+
// destPath may not exist in which case we default to creating a file symlink
269+
let faAttributes = (try? windowsFileAttributes(atPath: destPath)) ?? WIN32_FILE_ATTRIBUTE_DATA()
269270
if faAttributes.dwFileAttributes & DWORD(FILE_ATTRIBUTE_DIRECTORY) == DWORD(FILE_ATTRIBUTE_DIRECTORY) {
270271
dwFlags |= DWORD(SYMBOLIC_LINK_FLAG_DIRECTORY)
271272
}
@@ -286,12 +287,12 @@ extension FileManager {
286287
internal func _canonicalizedPath(toFileAtPath path: String) throws -> String {
287288
var hFile: HANDLE = INVALID_HANDLE_VALUE
288289
path.withCString(encodedAs: UTF16.self) { link in
289-
hFile = CreateFileW(link, GENERIC_READ, DWORD(FILE_SHARE_WRITE), nil, DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS), nil)
290+
hFile = CreateFileW(link, GENERIC_READ, DWORD(FILE_SHARE_READ | FILE_SHARE_WRITE), nil, DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS), nil)
290291
}
291-
defer { CloseHandle(hFile) }
292292
if hFile == INVALID_HANDLE_VALUE {
293293
throw _NSErrorWithWindowsError(GetLastError(), reading: true)
294294
}
295+
defer { CloseHandle(hFile) }
295296

296297
let dwLength: DWORD = GetFinalPathNameByHandleW(hFile, nil, 0, DWORD(FILE_NAME_NORMALIZED))
297298
var szPath: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(dwLength + 1))

0 commit comments

Comments
 (0)