Skip to content

Commit 498fc2a

Browse files
authored
Follow symlinks to reach target file (#460)
1 parent 9027245 commit 498fc2a

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

Sources/Swiftly/Unlink.swift

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,35 @@ extension SwiftlyCommand {
9292
continue
9393
}
9494

95-
let potentialProxyPath = swiftlyBinDir / file
96-
if let linkTarget = try? await fs.readlink(atPath: potentialProxyPath), linkTarget == proxyTo {
95+
if await (swiftlyBinDir / file).linksTo(proxyTo) {
9796
return true
9897
}
9998
}
10099

101100
return false
102101
}
103102
}
103+
104+
extension FilePath {
105+
fileprivate func linksTo(_ other: FilePath) async -> Bool {
106+
var path = self
107+
var jumps = 0
108+
while jumps < 10 { // More than 10 jumps is probably a symlink loop.
109+
if path == other {
110+
return true
111+
}
112+
jumps += 1
113+
do {
114+
let jumpTarget = try await fs.readlink(atPath: path)
115+
path = if jumpTarget.isAbsolute {
116+
jumpTarget
117+
} else {
118+
path.parent.pushing(jumpTarget).lexicallyNormalized()
119+
}
120+
} catch {
121+
break
122+
}
123+
}
124+
return false
125+
}
126+
}

0 commit comments

Comments
 (0)