updated NormFile::Rename() to always close/reopen files upon renaming for better performance due to way file handles are managed on Unix ... was already required for Windows

pull/72/head
bebopagogo 2022-08-21 00:18:29 -04:00
parent 5872195eaa
commit a8c5688d40
2 changed files with 13 additions and 15 deletions

View File

@ -2051,7 +2051,12 @@ int main(int argc, char* argv[])
} }
// TBD - should provide more error checking of NORM API calls // TBD - should provide more error checking of NORM API calls
TRACE("creating instance ...\n");
NormInstanceHandle normInstance = NormCreateInstance(boostPriority); NormInstanceHandle normInstance = NormCreateInstance(boostPriority);
TRACE("restarting instance ...\n");
//NormRestartInstance(normInstance); // xxx
TRACE("instance restarted.\n");
NormSetDebugLevel(debugLevel); NormSetDebugLevel(debugLevel);
if ((NULL != logFile) && !NormOpenDebugLog(normInstance, logFile)) if ((NULL != logFile) && !NormOpenDebugLog(normInstance, logFile))
{ {

View File

@ -243,7 +243,9 @@ bool NormFile::Rename(const char* oldName, const char* newName)
} }
#endif // if/else _WIN32_WCE #endif // if/else _WIN32_WCE
} }
// In Win32, the old file can't be open #endif // WIN32
// In Win32, the old file can't be open and on Unix (Linux at least), it's better
// performance to close/reopen a file being renamed
int oldFlags = 0; int oldFlags = 0;
if (IsOpen()) if (IsOpen())
{ {
@ -251,7 +253,7 @@ bool NormFile::Rename(const char* oldName, const char* newName)
oldFlags &= ~(O_CREAT | O_TRUNC); // unset these oldFlags &= ~(O_CREAT | O_TRUNC); // unset these
Close(); Close();
} }
#endif // WIN32
// Create sub-directories as needed. // Create sub-directories as needed.
char tempPath[PATH_MAX]; char tempPath[PATH_MAX];
strncpy(tempPath, newName, PATH_MAX); strncpy(tempPath, newName, PATH_MAX);
@ -318,30 +320,21 @@ bool NormFile::Rename(const char* oldName, const char* newName)
if (rename(oldName, newName)) if (rename(oldName, newName))
{ {
PLOG(PL_ERROR, "NormFile::Rename() rename() error: %s\n", GetErrorString()); PLOG(PL_ERROR, "NormFile::Rename() rename() error: %s\n", GetErrorString());
#endif // if/else _WIN32_WCE #endif // if/else _WIN32_WCE / WIN32|UNIX
#ifdef WIN32
if (oldFlags) if (oldFlags)
{ {
if (Open(oldName, oldFlags)) if (!Open(oldName, oldFlags))
offset = 0;
else
PLOG(PL_ERROR, "NormFile::Rename() error re-opening file w/ old name\n"); PLOG(PL_ERROR, "NormFile::Rename() error re-opening file w/ old name\n");
} }
#endif // WIN32
return false; return false;
} }
else else
{ {
#ifdef WIN32
// (TBD) Is the file offset OK doing this???
if (oldFlags) if (oldFlags)
{ {
if (Open(newName, oldFlags)) if (!Open(newName, oldFlags))
offset = 0;
else
PLOG(PL_ERROR, "NormFile::Rename() error opening file w/ new name\n"); PLOG(PL_ERROR, "NormFile::Rename() error opening file w/ new name\n");
} }
#endif // WIN32
return true; return true;
} }
} // end NormFile::Rename() } // end NormFile::Rename()