Skip to content

Commit 0e23938

Browse files
authored
(123636178) Resolving symlinks can remove prefix from path
1 parent db46428 commit 0e23938

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Sources/FoundationEssentials/String/String+Path.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,19 +361,18 @@ extension String {
361361
return nil
362362
}
363363

364+
let length = Int(buffer.fullPathAttr.attr_length) // Includes null byte
364365
return withUnsafePointer(to: buffer.fullPathBuf) { pathPtr in
365-
let start = UnsafeRawPointer(pathPtr).advanced(by: Int(buffer.fullPathAttr.attr_dataoffset))
366-
let length = Int(buffer.fullPathAttr.attr_length) // Includes null byte
367-
return start.withMemoryRebound(to: CChar.self, capacity: length) { ccharPtr in
368-
return String(cString: ccharPtr)
366+
pathPtr.withMemoryRebound(to: CChar.self, capacity: length) { ccharPtr in
367+
String(cString: ccharPtr)
369368
}
370369
}
371370
#else
372371
return nil
373372
#endif
374373
}
375374

376-
private func _resolvingSymlinksInPath() -> String? {
375+
func _resolvingSymlinksInPath() -> String? {
377376
guard !isEmpty else { return nil }
378377
return self.withFileSystemRepresentation { fsPtr -> String? in
379378
guard let fsPtr else { return nil }

Tests/FoundationEssentialsTests/FileManager/FileManagerTests.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,4 +566,15 @@ final class FileManagerTests : XCTestCase {
566566
throw XCTSkip("This test is not applicable to this platform")
567567
#endif
568568
}
569+
570+
func testResolveSymlinksViaGetAttrList() throws {
571+
try FileManagerPlayground {
572+
"destination"
573+
}.test {
574+
try $0.createSymbolicLink(atPath: "link", withDestinationPath: "destination")
575+
let absolutePath = $0.currentDirectoryPath.appendingPathComponent("link")
576+
let resolved = absolutePath._resolvingSymlinksInPath() // Call internal function to avoid path standardization
577+
XCTAssertEqual(resolved, $0.currentDirectoryPath.appendingPathComponent("destination"))
578+
}
579+
}
569580
}

0 commit comments

Comments
 (0)