Skip to content

Commit 4aff4de

Browse files
committed
Fix normalizing paths
1 parent c13a770 commit 4aff4de

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -449,18 +449,20 @@ private struct UNIXPath: Path {
449449

450450
var dirname: String {
451451
#if os(Windows)
452-
guard string != "" else {
453-
return "."
454-
}
455452
let fsr: UnsafePointer<Int8> = string.fileSystemRepresentation
456453
defer { fsr.deallocate() }
457454

458455
let path: String = String(cString: fsr)
459-
return path.withCString(encodedAs: UTF16.self) {
460-
let data = UnsafeMutaßlePointer(mutating: $0)
456+
let dir: String = path.withCString(encodedAs: UTF16.self) {
457+
let data = UnsafeMutablePointer(mutating: $0)
461458
PathCchRemoveFileSpec(data, path.count)
462459
return String(decodingCString: data, as: UTF16.self)
463460
}
461+
// These two expressions represent for the current directory.
462+
if dir == "\\" || dir == "" {
463+
return "."
464+
}
465+
return dir
464466
#else
465467
// FIXME: This method seems too complicated; it should be simplified,
466468
// if possible, and certainly optimized (using UTF8View).
@@ -547,11 +549,13 @@ private struct UNIXPath: Path {
547549

548550
init(normalizingAbsolutePath path: String) {
549551
#if os(Windows)
550-
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH + 1))
552+
var result: PWSTR?
553+
defer { LocalFree(result) }
554+
551555
_ = path.withCString(encodedAs: UTF16.self) {
552-
PathCanonicalizeW(&buffer, $0)
556+
PathAllocCanonicalize($0, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &result)
553557
}
554-
self.init(string: String(decodingCString: buffer, as: UTF16.self))
558+
self.init(string: String(decodingCString: result!, as: UTF16.self))
555559
#else
556560
precondition(path.first == "/", "Failure normalizing \(path), absolute paths should start with '/'")
557561

@@ -617,11 +621,18 @@ private struct UNIXPath: Path {
617621

618622
init(normalizingRelativePath path: String) {
619623
#if os(Windows)
620-
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH + 1))
624+
var result: PWSTR?
625+
defer { LocalFree(result) }
626+
621627
_ = path.replacingOccurrences(of: "/", with: "\\").withCString(encodedAs: UTF16.self) {
622-
PathCanonicalizeW(&buffer, $0)
628+
PathAllocCanonicalize($0, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &result)
629+
}
630+
631+
var canonicalized: String = String(decodingCString: result!, as: UTF16.self)
632+
if canonicalized == "" || canonicalized == "\\" {
633+
canonicalized = "."
623634
}
624-
self.init(string: String(decodingCString: buffer, as: UTF16.self))
635+
self.init(string: canonicalized)
625636
#else
626637
precondition(path.first != "/")
627638

@@ -715,6 +726,7 @@ private struct UNIXPath: Path {
715726
#if os(Windows)
716727
guard path != "" else {
717728
self.init(normalizingRelativePath: path)
729+
return
718730
}
719731
let fsr: UnsafePointer<Int8> = path.fileSystemRepresentation
720732
defer { fsr.deallocate() }

0 commit comments

Comments
 (0)