]> 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>
Sat, 9 Mar 2024 12:57:09 +0000 (13:57 +0100)
Perf optimization so that we do not call snprintf in the
common code path.

Ticket: 6796
(cherry picked from commit d255a5c7a309c067e15dde01560631f4ed427db1)

src/output-filestore.c

index 7d72a82999b44092e4ba14a1234e1489491bc0ed..d23560c31e44edf6dbecada62fd3572eadbe1e72 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));