Skip to content

Fix usage of Windows path API. #472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions Sources/TSCBasic/PathShims.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
-------------------------------------------------------------------------

This file contains temporary shim functions for use during the adoption of
AbsolutePath and RelativePath. The eventual plan is to use the FileSystem
API for all of this, at which time this file will go way. But since it is
important to have a quality FileSystem API, we will evolve it slowly.

Meanwhile this file bridges the gap to let call sites be as clean as possible,
while making it fairly easy to find those calls later.
*/
Expand All @@ -24,17 +24,21 @@ import Foundation
public func resolveSymlinks(_ path: AbsolutePath) throws -> AbsolutePath {
#if os(Windows)
let handle: HANDLE = path.pathString.withCString(encodedAs: UTF16.self) {
CreateFileW($0, GENERIC_READ, DWORD(FILE_SHARE_READ), nil,
DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS), nil)
CreateFileW($0, GENERIC_READ, DWORD(FILE_SHARE_READ), nil,
DWORD(OPEN_EXISTING), DWORD(FILE_FLAG_BACKUP_SEMANTICS), nil)
}
if handle == INVALID_HANDLE_VALUE { return path }
defer { CloseHandle(handle) }
return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: 261) {
let dwLength: DWORD =
GetFinalPathNameByHandleW(handle, $0.baseAddress!, DWORD($0.count),
DWORD(FILE_NAME_NORMALIZED))
let path = String(decodingCString: $0.baseAddress!, as: UTF16.self)
return try AbsolutePath(path)

let dwLength: DWORD =
GetFinalPathNameByHandleW(handle, nil, 0, DWORD(FILE_NAME_NORMALIZED))
return try withUnsafeTemporaryAllocation(of: WCHAR.self, capacity: Int(dwLength)) {
guard GetFinalPathNameByHandleW(handle, $0.baseAddress!, DWORD($0.count),
DWORD(FILE_NAME_NORMALIZED)) == dwLength - 1 else {
throw FileSystemError(.unknownOSError, path)
}
let path = String(decodingCString: $0.baseAddress!, as: UTF16.self)
return try AbsolutePath(path)
}
#else
let pathStr = path.pathString
Expand Down Expand Up @@ -76,12 +80,12 @@ public func createSymlink(_ path: AbsolutePath, pointingAt dest: AbsolutePath, r
- Returns: a generator that walks the specified directory producing all
files therein. If recursively is true will enter any directories
encountered recursively.

- Warning: directories that cannot be entered due to permission problems
are silently ignored. So keep that in mind.

- Warning: Symbolic links that point to directories are *not* followed.

- Note: setting recursively to `false` still causes the generator to feed
you the directory; just not its contents.
*/
Expand All @@ -100,12 +104,12 @@ public func walk(
- Returns: a generator that walks the specified directory producing all
files therein. Directories are recursed based on the return value of
`recursing`.

- Warning: directories that cannot be entered due to permissions problems
are silently ignored. So keep that in mind.

- Warning: Symbolic links that point to directories are *not* followed.

- Note: returning `false` from `recursing` still produces that directory
from the generator; just not its contents.
*/
Expand Down