From 9f2ca6f1b3151f3bc876856940354a4f429a4eca Mon Sep 17 00:00:00 2001 From: Janik Besendorf Date: Thu, 17 Jul 2025 17:41:24 +0200 Subject: [PATCH 1/2] Close log file before cleanup operations --- acquisition/acquisition.go | 3 +++ acquisition/secure.go | 4 ++++ log/logger.go | 7 ++++++- 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/acquisition/acquisition.go b/acquisition/acquisition.go index 0dd1a6f..460d020 100644 --- a/acquisition/acquisition.go +++ b/acquisition/acquisition.go @@ -85,6 +85,9 @@ func New(path string) (*Acquisition, error) { func (a *Acquisition) Complete() { a.Completed = time.Now().UTC() + // Close the log file before cleanup + log.CloseFileLog() + if a.Collector != nil { a.Collector.Clean() } diff --git a/acquisition/secure.go b/acquisition/secure.go index 8c109b5..56e6b33 100644 --- a/acquisition/secure.go +++ b/acquisition/secure.go @@ -87,6 +87,10 @@ func (a *Acquisition) StoreSecurely() error { if err != nil { return fmt.Errorf("failed to delete the unencrypted compressed archive: %v", err) } + + // Close the log file before removing the acquisition directory + log.CloseFileLog() + err = os.RemoveAll(a.StoragePath) if err != nil { return fmt.Errorf("failed to delete the original unencrypted acquisition folder: %v", err) diff --git a/log/logger.go b/log/logger.go index 1179b56..05e7ec6 100644 --- a/log/logger.go +++ b/log/logger.go @@ -143,10 +143,15 @@ func EnableFileLog(level LEVEL, filePath string) error { return nil } -func DisableFileLog() { +func CloseFileLog() { log.fd.Close() log.fd = nil log.fileName = "" + if log.fd != nil { + log.fd.Close() + log.fd = nil + log.fileName = "" + } } func Debug(v ...any) { From b0e6a60837ca1eb10206b1926079230ab49fa1d0 Mon Sep 17 00:00:00 2001 From: Janik Besendorf Date: Wed, 23 Jul 2025 17:43:06 +0200 Subject: [PATCH 2/2] use defer based approach --- acquisition/acquisition.go | 15 ++++++++++++--- acquisition/secure.go | 6 ++++-- log/logger.go | 16 +++++++++------- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/acquisition/acquisition.go b/acquisition/acquisition.go index 460d020..cca672f 100644 --- a/acquisition/acquisition.go +++ b/acquisition/acquisition.go @@ -34,6 +34,7 @@ type Acquisition struct { TmpDir string `json:"tmp_dir"` SdCard string `json:"sdcard"` Cpu string `json:"cpu"` + closeLog func() `json:"-"` } // New returns a new Acquisition instance. @@ -77,7 +78,13 @@ func New(path string) (*Acquisition, error) { // Init logging file logPath := filepath.Join(acq.StoragePath, "command.log") - log.EnableFileLog(log.DEBUG, logPath) + closeLog, err := log.EnableFileLog(log.DEBUG, logPath) + if err != nil { + return nil, fmt.Errorf("failed to enable file logging: %v", err) + } + + // Store cleanup function for later use + acq.closeLog = closeLog return &acq, nil } @@ -85,8 +92,10 @@ func New(path string) (*Acquisition, error) { func (a *Acquisition) Complete() { a.Completed = time.Now().UTC() - // Close the log file before cleanup - log.CloseFileLog() + // Ensure log file is closed before cleanup operations + if a.closeLog != nil { + defer a.closeLog() + } if a.Collector != nil { a.Collector.Clean() diff --git a/acquisition/secure.go b/acquisition/secure.go index 56e6b33..a5a389a 100644 --- a/acquisition/secure.go +++ b/acquisition/secure.go @@ -88,8 +88,10 @@ func (a *Acquisition) StoreSecurely() error { return fmt.Errorf("failed to delete the unencrypted compressed archive: %v", err) } - // Close the log file before removing the acquisition directory - log.CloseFileLog() + // Ensure log file is closed before removing the acquisition directory + if a.closeLog != nil { + defer a.closeLog() + } err = os.RemoveAll(a.StoragePath) if err != nil { diff --git a/log/logger.go b/log/logger.go index 05e7ec6..0fc49a2 100644 --- a/log/logger.go +++ b/log/logger.go @@ -129,24 +129,26 @@ func Coloring(enable bool) { log.Color = enable } -func EnableFileLog(level LEVEL, filePath string) error { +func EnableFileLog(level LEVEL, filePath string) (func(), error) { if filePath == "" { - return errors.New("invalid file path") + return nil, errors.New("invalid file path") } file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o666) if err != nil { - return err + return nil, err } log.fd = file log.fileName = filePath - return nil + + // Return cleanup function for defer pattern + cleanup := func() { + CloseFileLog() + } + return cleanup, nil } func CloseFileLog() { - log.fd.Close() - log.fd = nil - log.fileName = "" if log.fd != nil { log.fd.Close() log.fd = nil