From 9db6da393703cccf1fb8a85b3d45d48d2aca1465 Mon Sep 17 00:00:00 2001 From: Niels Huylebroeck Date: Sun, 28 Nov 2021 12:20:39 +0000 Subject: [PATCH 1/2] Moved the glob_match call within getFileList This reduces the number of files being read/opened when doing a search. --- src/bzfs/RecordReplay.cxx | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/bzfs/RecordReplay.cxx b/src/bzfs/RecordReplay.cxx index a69bba2128..30f51df7dc 100644 --- a/src/bzfs/RecordReplay.cxx +++ b/src/bzfs/RecordReplay.cxx @@ -162,7 +162,7 @@ static bool replaceWorldDatabase(ReplayHeader *h); static bool flagIsActive(FlagType *type); static bool packFlagTypes(char *flags, u32 *flagsSize); -static bool getFileList(int playerIndex, std::vector& entries); +static bool getFileList(int playerIndex, std::vector& entries, std::string pattern); static FILE *openFile(const char *filename, const char *mode); static FILE *openWriteFile(int playerIndex, const char *filename); static bool badFilename(const char *name); @@ -718,7 +718,8 @@ bool Replay::loadFile(int playerIndex, const char *filename) if (filename[0] == '#') { std::vector entries; - if (!getFileList(playerIndex, entries)) + // When selecting by index, we assume they searched without a query + if (!getFileList(playerIndex, entries, "*")) { sendMessage(ServerPlayer, playerIndex, "Could not get file index list"); return false; @@ -862,7 +863,7 @@ static FILE *getRecordFile(const char *filename) } -static bool getFileList(int playerIndex, std::vector& entries) +static bool getFileList(int playerIndex, std::vector& entries, std::string pattern) { entries.clear(); int entNum = 0; @@ -883,6 +884,7 @@ static bool getFileList(int playerIndex, std::vector& entries) { std::string name = RecordDir; name += de->d_name; + if (!glob_match(name, pattern)) continue; FILE *file = getRecordFile(name.c_str()); if (file != NULL) { @@ -916,6 +918,7 @@ static bool getFileList(int playerIndex, std::vector& entries) { std::string name = RecordDir; name += findData.cFileName; + if (!glob_match(name, pattern)) continue; FILE *file = getRecordFile(name.c_str()); if (file != NULL) { @@ -1018,7 +1021,7 @@ bool Replay::sendFileList(int playerIndex, const char* options) return false; std::vector entries; - if (!getFileList(playerIndex, entries)) + if (!getFileList(playerIndex, entries, pattern)) return false; char buffer[MessageLen]; @@ -1034,19 +1037,16 @@ bool Replay::sendFileList(int playerIndex, const char* options) for (unsigned int i = 0; i < entries.size(); i++) { const FileEntry& entry = entries[i]; - if (glob_match(pattern, entry.file)) + entriesSent++; + snprintf(buffer, MessageLen, "#%02i: %-30s [%9.1f seconds]", + entry.entryNum + 1, entry.file.c_str(), entry.time); + sendMessage(ServerPlayer, playerIndex, buffer); + if (entriesSent >= MaxListOutput) { - entriesSent++; - snprintf(buffer, MessageLen, "#%02i: %-30s [%9.1f seconds]", - entry.entryNum + 1, entry.file.c_str(), entry.time); + snprintf(buffer, MessageLen, "Not listing more then %i entries, " + "try using pattern matching.", MaxListOutput); sendMessage(ServerPlayer, playerIndex, buffer); - if (entriesSent >= MaxListOutput) - { - snprintf(buffer, MessageLen, "Not listing more then %i entries, " - "try using pattern matching.", MaxListOutput); - sendMessage(ServerPlayer, playerIndex, buffer); - break; - } + break; } } From fed0d59743843c087c5cf2be361827b9b682ea59 Mon Sep 17 00:00:00 2001 From: Niels Huylebroeck Date: Sun, 28 Nov 2021 16:45:32 +0000 Subject: [PATCH 2/2] Add limit to listing files, MaxListOutput (=100) --- src/bzfs/RecordReplay.cxx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/bzfs/RecordReplay.cxx b/src/bzfs/RecordReplay.cxx index 30f51df7dc..fec581bb86 100644 --- a/src/bzfs/RecordReplay.cxx +++ b/src/bzfs/RecordReplay.cxx @@ -885,6 +885,7 @@ static bool getFileList(int playerIndex, std::vector& entries, std::s std::string name = RecordDir; name += de->d_name; if (!glob_match(name, pattern)) continue; + if (entNum > MaxListOutput) break; FILE *file = getRecordFile(name.c_str()); if (file != NULL) { @@ -919,6 +920,7 @@ static bool getFileList(int playerIndex, std::vector& entries, std::s std::string name = RecordDir; name += findData.cFileName; if (!glob_match(name, pattern)) continue; + if (entNum > MaxListOutput) break; FILE *file = getRecordFile(name.c_str()); if (file != NULL) {