Skip to content

Commit b170d46

Browse files
authored
Fix usage of Windows path API. (#472)
1 parent 930e82e commit b170d46

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

Sources/TSCBasic/PathShims.swift

+20-16
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
-------------------------------------------------------------------------
10-
10+
1111
This file contains temporary shim functions for use during the adoption of
1212
AbsolutePath and RelativePath. The eventual plan is to use the FileSystem
1313
API for all of this, at which time this file will go way. But since it is
1414
important to have a quality FileSystem API, we will evolve it slowly.
15-
15+
1616
Meanwhile this file bridges the gap to let call sites be as clean as possible,
1717
while making it fairly easy to find those calls later.
1818
*/
@@ -24,17 +24,21 @@ import Foundation
2424
public func resolveSymlinks(_ path: AbsolutePath) throws -> AbsolutePath {
2525
#if os(Windows)
2626
let handle: HANDLE = path.pathString.withCString(encodedAs: UTF16.self) {
27-
CreateFileW($0, GENERIC_READ, DWORD(FILE_SHARE_READ), nil,
28-
DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS), nil)
27+
CreateFileW($0, GENERIC_READ, DWORD(FILE_SHARE_READ), nil,
28+
DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS), nil)
2929
}
3030
if handle == INVALID_HANDLE_VALUE { return path }
3131
defer { CloseHandle(handle) }
32-
return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: 261) {
33-
let dwLength: DWORD =
34-
GetFinalPathNameByHandleW(handle, $0.baseAddress!, DWORD($0.count),
35-
DWORD(FILE_NAME_NORMALIZED))
36-
let path = String(decodingCString: $0.baseAddress!, as: UTF16.self)
37-
return try AbsolutePath(path)
32+
33+
let dwLength: DWORD =
34+
GetFinalPathNameByHandleW(handle, nil, 0, DWORD(FILE_NAME_NORMALIZED))
35+
return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
36+
guard GetFinalPathNameByHandleW(handle, $0.baseAddress!, DWORD($0.count),
37+
DWORD(FILE_NAME_NORMALIZED)) == dwLength - 1 else {
38+
throw FileSystemError(.unknownOSError, path)
39+
}
40+
let path = String(decodingCString: $0.baseAddress!, as: UTF16.self)
41+
return try AbsolutePath(path)
3842
}
3943
#else
4044
let pathStr = path.pathString
@@ -76,12 +80,12 @@ public func createSymlink(_ path: AbsolutePath, pointingAt dest: AbsolutePath, r
7680
- Returns: a generator that walks the specified directory producing all
7781
files therein. If recursively is true will enter any directories
7882
encountered recursively.
79-
83+
8084
- Warning: directories that cannot be entered due to permission problems
8185
are silently ignored. So keep that in mind.
82-
86+
8387
- Warning: Symbolic links that point to directories are *not* followed.
84-
88+
8589
- Note: setting recursively to `false` still causes the generator to feed
8690
you the directory; just not its contents.
8791
*/
@@ -100,12 +104,12 @@ public func walk(
100104
- Returns: a generator that walks the specified directory producing all
101105
files therein. Directories are recursed based on the return value of
102106
`recursing`.
103-
107+
104108
- Warning: directories that cannot be entered due to permissions problems
105109
are silently ignored. So keep that in mind.
106-
110+
107111
- Warning: Symbolic links that point to directories are *not* followed.
108-
112+
109113
- Note: returning `false` from `recursing` still produces that directory
110114
from the generator; just not its contents.
111115
*/

0 commit comments

Comments
 (0)