]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
file: optimize file pruning
authorVictor Julien <victor@inliniac.net>
Thu, 22 Jan 2015 18:24:35 +0000 (19:24 +0100)
committerVictor Julien <victor@inliniac.net>
Wed, 4 Feb 2015 10:39:36 +0000 (11:39 +0100)
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

index 7788413ff32c32086149c18f58d39e4e644deb06..908dc19b1b62fa8eb885d0fe292b231b8dec9cd0 100644 (file)
@@ -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;
     }
 }