From fa96b35c6981d5bd60201c1c45ae1c311a6627cd Mon Sep 17 00:00:00 2001 From: James Spurin Date: Mon, 4 Nov 2024 18:37:29 +0000 Subject: [PATCH] check file exists before using bolt.Open Signed-off-by: James Spurin --- pkg/data/data.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/data/data.go b/pkg/data/data.go index 62f0312..5527d88 100644 --- a/pkg/data/data.go +++ b/pkg/data/data.go @@ -22,6 +22,7 @@ import ( "encoding/json" "fmt" "hash/crc32" + "os" "sort" "strings" "text/template" @@ -151,7 +152,15 @@ type Checksum struct { CompactRevision int64 } +// boltOpen checks if the file exists and opens it in read-only mode. +// Returns an error if the file does not exist. func boltOpen(path string) (*bolt.DB, error) { + // Check if the file exists + if _, err := os.Stat(path); os.IsNotExist(err) { + return nil, fmt.Errorf("file does not exist: %s", path) + } + + // Open the file in read-only mode return bolt.Open(path, 0400, &bolt.Options{ ReadOnly: true, })