From 7018172b3d0b23d9a14dbd4b867d35cf1cd9cd17 Mon Sep 17 00:00:00 2001 From: Jaydip Gabani Date: Tue, 26 Nov 2024 20:34:28 +0000 Subject: [PATCH] fixing writer Signed-off-by: Jaydip Gabani --- pkg/pubsub/diskwriter/diskwriter.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pkg/pubsub/diskwriter/diskwriter.go b/pkg/pubsub/diskwriter/diskwriter.go index 94702eb7b52..6f7b54ba01c 100644 --- a/pkg/pubsub/diskwriter/diskwriter.go +++ b/pkg/pubsub/diskwriter/diskwriter.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "fmt" - "log" "os" "path" "syscall" @@ -39,7 +38,7 @@ func (r *DiskWriter) Publish(_ context.Context, data interface{}, _ string) erro if r.file == nil { // Open a new file and acquire a lock filePath := path.Join(r.Path, "violations.txt") - file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) + file, err := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { return fmt.Errorf("failed to open file: %w", err) } @@ -53,6 +52,11 @@ func (r *DiskWriter) Publish(_ context.Context, data interface{}, _ string) erro } r.file = file + err = r.file.Truncate(0) + if err != nil { + r.file = nil + return fmt.Errorf("failed to truncate file: %w", err) + } } _, err = r.file.WriteString(string(jsonData) + "\n")