Skip to content

Commit db58ec0

Browse files
committed
win32: fix several errors in longpath and realpath implementations
1 parent d7f5688 commit db58ec0

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

base/loading.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ elseif OS_NAME == :Windows
1111
# GetLongPathName Win32 function returns the case-preserved filename on NTFS.
1212
function isfile_casesensitive(path)
1313
isfile(path) || return false # Fail fast
14-
longpath(path) == path
14+
Filesystem.longpath(path) == path
1515
end
1616
elseif OS_NAME == :Darwin
1717
# HFS+ filesystem is case-preserving. The getattrlist API returns

base/path.jl

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,35 +126,34 @@ abspath(a::AbstractString, b::AbstractString...) = abspath(joinpath(a,b...))
126126

127127
@windows_only realpath(path::AbstractString) = realpath(utf16(path))
128128
@windows_only function realpath(path::UTF16String)
129-
p = UInt32((sizeof(path)>>2) + 1)
129+
p::UInt32 = sizeof(path)>>1
130130
while true
131-
buflength = p
132-
buf = zeros(UInt16,buflength)
133-
p = ccall((:GetFullPathNameW, "Kernel32"), stdcall,
131+
buf = zeros(UInt16, p + 1)
132+
p = ccall((:GetFullPathNameW, "kernel32"), stdcall,
134133
UInt32, (Cwstring, UInt32, Ptr{UInt16}, Ptr{Void}),
135-
path, buflength, buf, C_NULL)
134+
path, length(buf), buf, C_NULL)
136135
systemerror(:realpath, p == 0)
137-
if (p < buflength)
138-
resize!(buf, p+1)
136+
if (p < length(buf))
137+
resize!(buf, p + 1)
139138
return utf8(UTF16String(buf))
140139
end
141140
end
142141
end
143142

144143
@windows_only longpath(path::AbstractString) = longpath(utf16(path))
145144
@windows_only function longpath(path::UTF16String)
146-
buf = Array(UInt16, length(path.data))
145+
p::UInt32 = sizeof(path)>>1
147146
while true
148-
p = ccall((:GetLongPathNameW, "Kernel32"), stdcall, UInt32,
147+
buf = zeros(UInt16, p + 1)
148+
p = ccall((:GetLongPathNameW, "kernel32"), stdcall, UInt32,
149149
(Cwstring, Ptr{UInt16}, UInt32),
150150
path, buf, length(buf))
151151
systemerror(:longpath, p == 0)
152152
# Buffer wasn't big enough, in which case `p` is the necessary buffer size
153-
if (p > length(buf))
154-
resize!(buf, p)
155-
continue
153+
if (p < length(buf))
154+
resize!(buf, p + 1)
155+
return utf8(UTF16String(buf))
156156
end
157-
return utf8(UTF16String(buf))
158157
end
159158
end
160159

0 commit comments

Comments
 (0)