mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
Handle arbitrarily long symlink target in expandSymlinks() (#23650)
For now, `expandSymlinks()` can handle only symlinks with lengths up to 1024. We can improve this logic and retry inside a loop with increasing lengths until we succeed. The same approach is used in [Go](377646589d/src/os/file_unix.go (L446)), [Rust](785eb65377/library/std/src/sys/pal/unix/fs.rs (L1700)) and [Nim's `getCurrentDir()`](https://github.com/nim-lang/Nim/blob/devel/lib/std/private/ospaths2.nim#L877), so maybe it's a good idea to use the same logic in `expandSymlinks()` also. (cherry picked from commit3bda5fc840)
This commit is contained in:
committed by
narimiran
parent
c2b4d8a968
commit
37965bd591
@@ -66,11 +66,13 @@ proc expandSymlink*(symlinkPath: string): string {.noWeirdTarget.} =
|
||||
when defined(windows) or defined(nintendoswitch):
|
||||
result = symlinkPath
|
||||
else:
|
||||
result = newString(maxSymlinkLen)
|
||||
var len = readlink(symlinkPath, result.cstring, maxSymlinkLen)
|
||||
if len < 0:
|
||||
raiseOSError(osLastError(), symlinkPath)
|
||||
if len > maxSymlinkLen:
|
||||
result = newString(len+1)
|
||||
len = readlink(symlinkPath, result.cstring, len)
|
||||
setLen(result, len)
|
||||
var bufLen = 1024
|
||||
while true:
|
||||
result = newString(bufLen)
|
||||
let len = readlink(symlinkPath.cstring, result.cstring, bufLen)
|
||||
if len < 0:
|
||||
raiseOSError(osLastError(), symlinkPath)
|
||||
if len < bufLen:
|
||||
result.setLen(len)
|
||||
break
|
||||
bufLen = bufLen shl 1
|
||||
|
||||
Reference in New Issue
Block a user