Use FileExists/fs_is_file instead of opening file

This is more concise and works more reliable e.g. on Windows.

See: https://devblogs.microsoft.com/oldnewthing/20071023-00/?p=24713
This commit is contained in:
Robert Müller 2023-05-09 23:01:43 +02:00
parent 33970e7057
commit d9307ca756
5 changed files with 8 additions and 23 deletions

View file

@ -3314,10 +3314,8 @@ void CClient::Run()
s_SavedConfig = true;
}
IOHANDLE File = m_pStorage->OpenFile(m_aDDNetInfoTmp, IOFLAG_READ | IOFLAG_SKIP_BOM, IStorage::TYPE_SAVE);
if(File)
if(m_pStorage->FileExists(m_aDDNetInfoTmp, IStorage::TYPE_SAVE))
{
io_close(File);
m_pStorage->RemoveFile(m_aDDNetInfoTmp, IStorage::TYPE_SAVE);
}
@ -4683,18 +4681,14 @@ int main(int argc, const char **argv)
pClient->InitInterfaces();
// execute config file
IOHANDLE File = pStorage->OpenFile(CONFIG_FILE, IOFLAG_READ, IStorage::TYPE_ALL);
if(File)
if(pStorage->FileExists(CONFIG_FILE, IStorage::TYPE_ALL))
{
io_close(File);
pConsole->ExecuteFile(CONFIG_FILE);
}
// execute autoexec file
File = pStorage->OpenFile(AUTOEXEC_CLIENT_FILE, IOFLAG_READ, IStorage::TYPE_ALL);
if(File)
if(pStorage->FileExists(AUTOEXEC_CLIENT_FILE, IStorage::TYPE_ALL))
{
io_close(File);
pConsole->ExecuteFile(AUTOEXEC_CLIENT_FILE);
}
else // fallback

View file

@ -153,10 +153,8 @@ int main(int argc, const char **argv)
pServer->RegisterCommands();
// execute autoexec file
IOHANDLE File = pStorage->OpenFile(AUTOEXEC_SERVER_FILE, IOFLAG_READ, IStorage::TYPE_ALL);
if(File)
if(pStorage->FileExists(AUTOEXEC_SERVER_FILE, IStorage::TYPE_ALL))
{
io_close(File);
pConsole->ExecuteFile(AUTOEXEC_SERVER_FILE);
}
else // fallback

View file

@ -289,19 +289,15 @@ public:
char aBuf[IO_MAX_PATH_LENGTH];
str_copy(m_aBinarydir, pArgv0, Pos + 1);
str_format(aBuf, sizeof(aBuf), "%s/" PLAT_SERVER_EXEC, m_aBinarydir);
IOHANDLE File = io_open(aBuf, IOFLAG_READ);
if(File)
if(fs_is_file(aBuf))
{
io_close(File);
return;
}
#if defined(CONF_PLATFORM_MACOS)
str_append(m_aBinarydir, "/../../../DDNet-Server.app/Contents/MacOS", sizeof(m_aBinarydir));
str_format(aBuf, sizeof(aBuf), "%s/" PLAT_SERVER_EXEC, m_aBinarydir);
IOHANDLE FileBis = io_open(aBuf, IOFLAG_READ);
if(FileBis)
if(fs_is_file(aBuf))
{
io_close(FileBis);
return;
}
#endif

View file

@ -149,9 +149,8 @@ void CMenus::RenderStartMenu(CUIRect MainView)
{
m_ServerProcess.m_Process = shell_execute(aBuf);
}
else if(IOHANDLE File = io_open(aBuf, IOFLAG_READ))
else if(fs_is_file(aBuf))
{
io_close(File);
m_ServerProcess.m_Process = shell_execute(aBuf);
}
else

View file

@ -4975,10 +4975,8 @@ void CEditor::RenderFileDialog()
str_format(m_aFileSaveName, sizeof(m_aFileSaveName), "%s/%s", m_pFileDialogPath, m_FileDialogFileNameInput.GetString());
if(!str_comp(m_pFileDialogButtonText, "Save"))
{
IOHANDLE File = Storage()->OpenFile(m_aFileSaveName, IOFLAG_READ, IStorage::TYPE_SAVE);
if(File)
if(Storage()->FileExists(m_aFileSaveName, IStorage::TYPE_SAVE))
{
io_close(File);
m_PopupEventType = POPEVENT_SAVE;
m_PopupEventActivated = true;
}