Skip to content

Instantly share code, notes, and snippets.

@mkitti
Created April 26, 2021 23:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mkitti/6365155ca9f84292bd1627e3eee4056d to your computer and use it in GitHub Desktop.
Save mkitti/6365155ca9f84292bd1627e3eee4056d to your computer and use it in GitHub Desktop.
WinApi.isfile_casesensitive
module WinApi
const MAX_PATH = 260
const FILE_ATTRIBUTE_DIRECTORY = 0x10
const INVALID_HANDLE_VALUE = -1
struct FileTime
dwLowDateTime::UInt32
dwHighDateTime::UInt32
FileTime() = new(0,0)
end
#=
typedef struct _WIN32_FIND_DATAW {
DWORD dwFileAttributes;
FILETIME ftCreationTime;
FILETIME ftLastAccessTime;
FILETIME ftLastWriteTime;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
DWORD dwReserved0;
DWORD dwReserved1;
WCHAR cFileName[MAX_PATH];
WCHAR cAlternateFileName[14];
DWORD dwFileType;
DWORD dwCreatorType;
WORD wFinderFlags;
} WIN32_FIND_DATAW, *PWIN32_FIND_DATAW, *LPWIN32_FIND_DATAW;
=#
mutable struct Win32FindDataW
dwFileAttributes::UInt32
ftCreationTime::FileTime
ftLastAccessTime::FileTime
ftLastWriteTime::FileTime
nFileSizeHigh::UInt32
nFileSizeLow::UInt32
dwReserved0::UInt32
dwReserved1::UInt32
cFileName::NTuple{MAX_PATH, Cwchar_t} # 9th field
cAlternateFileName::NTuple{14, Cwchar_t}
dwFileType::UInt32
dwCreatorType::UInt32
wFinderFlags::UInt16
function Win32FindDataW()
new()
end
end
cFileName(data::Win32FindDataW) =
pointer_from_objref(data) + fieldoffset(Win32FindDataW, 9) |>
Ptr{UInt16} |>
x->unsafe_wrap(Array, x, MAX_PATH) |>
x->transcode(UInt8,x) |>
x->x[1 : findfirst(==(0), x)-1 ] |>
String
GetFileAttributesW(path) = ccall((:GetFileAttributesW, "kernel32"), Int32, (Cwstring,), path)
FindFirstFileW(path, data) = ccall((:FindFirstFileW,"kernel32"), Cint, (Cwstring, Ptr{Win32FindDataW}), path, Ref(data))
FindNextFileW(hFindFile, data) = ccall((:FindNextFileW,"kernel32"), Cint, (Cint, Ptr{Win32FindDataW}), hFindFile, Ref(data) )
FindClose(handle) = ccall((:FindClose, "kernel32"), Cint, (Cint,), handle)
function isfile_casesensitive(path)
data = Win32FindDataW()
hFindData = FindFirstFileW(path, data)
result = false
if hFindData != INVALID_HANDLE_VALUE # -1
# Not a directory and not -1 (INVALID_FILE_ATTRIBUTES)
if data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY == 0
result = cFileName(data) == basename(path)
end
FindClose(hFindData)
end
return result
end
end # Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment