Skip to content

Improve path normalization and judgment on Windows #191

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

Closed
wants to merge 8 commits into from
Closed
Changes from 2 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
35 changes: 28 additions & 7 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,16 @@ private struct UNIXPath: Path {
defer { fsr.deallocate() }

let path: String = String(cString: fsr)
return path.withCString(encodedAs: UTF16.self) {
let dir: String = path.withCString(encodedAs: UTF16.self) {
let data = UnsafeMutablePointer(mutating: $0)
PathCchRemoveFileSpec(data, path.count)
return String(decodingCString: data, as: UTF16.self)
}
// These two expressions represent for the current directory.
if dir == "\\" || dir == "" {
return "."
}
return dir
#else
// FIXME: This method seems too complicated; it should be simplified,
// if possible, and certainly optimized (using UTF8View).
Expand Down Expand Up @@ -544,11 +549,13 @@ private struct UNIXPath: Path {

init(normalizingAbsolutePath path: String) {
#if os(Windows)
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH + 1))
var result: PWSTR?
defer { LocalFree(result) }

_ = path.withCString(encodedAs: UTF16.self) {
PathCanonicalizeW(&buffer, $0)
PathAllocCanonicalize($0, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &result)
}
self.init(string: String(decodingCString: buffer, as: UTF16.self))
self.init(string: String(decodingCString: result!, as: UTF16.self))
#else
precondition(path.first == "/", "Failure normalizing \(path), absolute paths should start with '/'")

Expand Down Expand Up @@ -614,11 +621,18 @@ private struct UNIXPath: Path {

init(normalizingRelativePath path: String) {
#if os(Windows)
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH + 1))
var result: PWSTR?
defer { LocalFree(result) }

_ = path.replacingOccurrences(of: "/", with: "\\").withCString(encodedAs: UTF16.self) {
PathCanonicalizeW(&buffer, $0)
PathAllocCanonicalize($0, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &result)
}

var canonicalized: String = String(decodingCString: result!, as: UTF16.self)
if canonicalized == "" || canonicalized == "\\" {
canonicalized = "."
}
self.init(string: String(decodingCString: buffer, as: UTF16.self))
self.init(string: canonicalized)
#else
precondition(path.first != "/")

Expand Down Expand Up @@ -685,6 +699,9 @@ private struct UNIXPath: Path {

init(validatingAbsolutePath path: String) throws {
#if os(Windows)
guard path != "" else {
throw PathValidationError.invalidAbsolutePath(path)
}
let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
defer { fsr.deallocate() }

Expand All @@ -707,6 +724,10 @@ private struct UNIXPath: Path {

init(validatingRelativePath path: String) throws {
#if os(Windows)
guard path != "" else {
self.init(normalizingRelativePath: path)
return
}
let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
defer { fsr.deallocate() }

Expand Down