Merge pull request #1485 from LORgames/ssurtees/touchfile

Added Windows implementation to os.touchfile to avoid issues with truncating the file
This commit is contained in:
starkos 2020-07-10 15:02:08 -04:00 committed by GitHub
commit 591194dfcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -68,6 +68,44 @@ int os_touchfile(lua_State* L)
// if destination exist, mark the file as modified
if (do_isfile(L, dst))
{
#if PLATFORM_WINDOWS
SYSTEMTIME systemTime;
FILETIME fileTime;
HANDLE fileHandle;
wchar_t wide_path[PATH_MAX];
if (MultiByteToWideChar(CP_UTF8, 0, dst, -1, wide_path, PATH_MAX) == 0)
{
lua_pushinteger(L, -1);
lua_pushstring(L, "unable to encode path");
return 2;
}
fileHandle = CreateFileW(wide_path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fileHandle == NULL)
{
lua_pushinteger(L, -1);
lua_pushfstring(L, "unable to touch file '%s'", dst);
return 2;
}
GetSystemTime(&systemTime);
if (SystemTimeToFileTime(&systemTime, &fileTime) == 0)
{
lua_pushinteger(L, -1);
lua_pushfstring(L, "unable to touch file '%s'", dst);
return 2;
}
if (SetFileTime(fileHandle, NULL, NULL, &fileTime) == 0)
{
lua_pushinteger(L, -1);
lua_pushfstring(L, "unable to touch file '%s'", dst);
return 2;
}
lua_pushinteger(L, 0);
return 1;
#else
if (truncate_file(dst))
{
lua_pushinteger(L, 0);
@ -77,6 +115,7 @@ int os_touchfile(lua_State* L)
lua_pushfstring(L, "unable to touch file '%s'", dst);
return 2;
}
#endif
}
#if PLATFORM_WINDOWS