From fbe6dac1ae013d4d118cbe3da2fe9351f19dd9d4 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Thu, 22 Jan 2015 19:24:35 +0100 Subject: [PATCH] file: optimize file pruning FilePrune would clear the files, but not free them and remove them from the list. This lead to ever growing lists in some cases. Especially in HTTP sessions with many transactions, this could slow us down. --- src/util-file.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/util-file.c b/src/util-file.c index 7788413ff3..908dc19b1b 100644 --- a/src/util-file.c +++ b/src/util-file.c @@ -132,7 +132,7 @@ static int FileAppendFileData(FileContainer *ffc, FileData *ffd) -static void FilePruneFile(File *file) +static int FilePruneFile(File *file) { SCEnter(); @@ -141,7 +141,7 @@ static void FilePruneFile(File *file) if (!(file->flags & FILE_NOMAGIC)) { /* need magic but haven't set it yet, bail out */ if (file->magic == NULL) - SCReturn; + SCReturnInt(0); else SCLogDebug("file->magic %s", file->magic); } else { @@ -168,19 +168,36 @@ static void FilePruneFile(File *file) #endif } else if (fd->stored == 0) { fd = NULL; + SCReturnInt(0); break; } } - SCReturn; + if (file->state >= FILE_STATE_CLOSED) + SCReturnInt(1); + else + SCReturnInt(0); } void FilePrune(FileContainer *ffc) { - File *file; + File *file = ffc->head; + + while (file) { + if (FilePruneFile(file) == 0) + break; + + BUG_ON(file != ffc->head); + + File *file_next = file->next; + + /* update head and tail */ + ffc->head = file_next; + if (file == ffc->tail) + ffc->tail = NULL; - for (file = ffc->head; file != NULL; file = file->next) { - FilePruneFile(file); + FileFree(file); + file = file_next; } }