From a8c5688d409ff70fe605f5dc0d403cd9f79cd34c Mon Sep 17 00:00:00 2001 From: bebopagogo Date: Sun, 21 Aug 2022 00:18:29 -0400 Subject: [PATCH] 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 --- examples/normStreamer.cpp | 5 +++++ src/common/normFile.cpp | 23 ++++++++--------------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/examples/normStreamer.cpp b/examples/normStreamer.cpp index fb4f452..371e21d 100755 --- a/examples/normStreamer.cpp +++ b/examples/normStreamer.cpp @@ -2051,7 +2051,12 @@ int main(int argc, char* argv[]) } // TBD - should provide more error checking of NORM API calls + TRACE("creating instance ...\n"); NormInstanceHandle normInstance = NormCreateInstance(boostPriority); + TRACE("restarting instance ...\n"); + //NormRestartInstance(normInstance); // xxx + TRACE("instance restarted.\n"); + NormSetDebugLevel(debugLevel); if ((NULL != logFile) && !NormOpenDebugLog(normInstance, logFile)) { diff --git a/src/common/normFile.cpp b/src/common/normFile.cpp index dce2d7a..a723586 100755 --- a/src/common/normFile.cpp +++ b/src/common/normFile.cpp @@ -243,7 +243,9 @@ bool NormFile::Rename(const char* oldName, const char* newName) } #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; if (IsOpen()) { @@ -251,7 +253,7 @@ bool NormFile::Rename(const char* oldName, const char* newName) oldFlags &= ~(O_CREAT | O_TRUNC); // unset these Close(); } -#endif // WIN32 + // Create sub-directories as needed. char tempPath[PATH_MAX]; strncpy(tempPath, newName, PATH_MAX); @@ -318,30 +320,21 @@ bool NormFile::Rename(const char* oldName, const char* newName) if (rename(oldName, newName)) { PLOG(PL_ERROR, "NormFile::Rename() rename() error: %s\n", GetErrorString()); -#endif // if/else _WIN32_WCE -#ifdef WIN32 +#endif // if/else _WIN32_WCE / WIN32|UNIX if (oldFlags) { - if (Open(oldName, oldFlags)) - offset = 0; - else + if (!Open(oldName, oldFlags)) PLOG(PL_ERROR, "NormFile::Rename() error re-opening file w/ old name\n"); - } -#endif // WIN32 + } return false; } else { -#ifdef WIN32 - // (TBD) Is the file offset OK doing this??? if (oldFlags) { - if (Open(newName, oldFlags)) - offset = 0; - else + if (!Open(newName, oldFlags)) PLOG(PL_ERROR, "NormFile::Rename() error opening file w/ new name\n"); } -#endif // WIN32 return true; } } // end NormFile::Rename()