]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
output/filestore: delay snprintf until needed
authorPhilippe Antoine <pantoine@oisf.net>
Tue, 27 Feb 2024 20:45:03 +0000 (21:45 +0100)
committerVictor Julien <victor@inliniac.net>
Wed, 28 Feb 2024 13:24:03 +0000 (14:24 +0100)
Perf optimization so that we do not call snprintf in the
common code path.

Ticket: 6796

src/output-filestore.c

index d67014d49e37c41aab3b38d7dd22bbd676827bc9..5960f4ea473c95808a6bbbdf610158f0a53458f8 100644 (file)
@@ -194,9 +194,8 @@ static int OutputFilestoreLogger(ThreadVars *tv, void *thread_data, const Packet
 
     SCLogDebug("ff %p, data %p, data_len %u", ff, data, data_len);
 
-    snprintf(filename, sizeof(filename), "%s/file.%u", ctx->tmpdir, ff->file_store_id);
-
     if (flags & OUTPUT_FILEDATA_FLAG_OPEN) {
+        snprintf(filename, sizeof(filename), "%s/file.%u", ctx->tmpdir, ff->file_store_id);
         file_fd = open(filename, O_CREAT | O_TRUNC | O_NOFOLLOW | O_WRONLY,
                 0644);
         if (file_fd == -1) {
@@ -217,6 +216,7 @@ static int OutputFilestoreLogger(ThreadVars *tv, void *thread_data, const Packet
     /* we can get called with a NULL ffd when we need to close */
     } else if (data != NULL) {
         if (ff->fd == -1) {
+            snprintf(filename, sizeof(filename), "%s/file.%u", ctx->tmpdir, ff->file_store_id);
             file_fd = open(filename, O_APPEND | O_NOFOLLOW | O_WRONLY);
             if (file_fd == -1) {
                 StatsIncr(tv, aft->fs_error_counter);
@@ -232,6 +232,7 @@ static int OutputFilestoreLogger(ThreadVars *tv, void *thread_data, const Packet
     if (file_fd != -1) {
         ssize_t r = write(file_fd, (const void *)data, (size_t)data_len);
         if (r == -1) {
+            snprintf(filename, sizeof(filename), "%s/file.%u", ctx->tmpdir, ff->file_store_id);
             StatsIncr(tv, aft->fs_error_counter);
             WARN_ONCE(WOT_WRITE, "Filestore (v2) failed to write to %s: %s", filename,
                     strerror(errno));