From e03fdebbc61499aa0c80c2dd251807ec9b303722 Mon Sep 17 00:00:00 2001 From: Darragh O'Reilly Date: Wed, 15 Nov 2023 14:21:21 +0000 Subject: [PATCH] ndb: add read lock to prevent NDB blob magic error This error can happen if the ndb packages file is being updated (e.g. zypper up) while we are reading it. The rpm binary uses a shared flock when running `rpm -qa` on the ndb backend. This commit does the same for go-rpmdb. --- pkg/ndb/ndb.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pkg/ndb/ndb.go b/pkg/ndb/ndb.go index fb4e5bf..4ba94dd 100644 --- a/pkg/ndb/ndb.go +++ b/pkg/ndb/ndb.go @@ -26,6 +26,7 @@ import ( "fmt" "io" "os" + "syscall" "unsafe" dbi "github.com/knqyf263/go-rpmdb/pkg/db" @@ -96,6 +97,11 @@ func Open(path string) (*RpmNDB, error) { return nil, err } + err = syscall.Flock(int(file.Fd()), syscall.LOCK_SH) + if err != nil { + return nil, err + } + hdrBuff := ndbHeader{} err = binary.Read(file, binary.LittleEndian, &hdrBuff) if err != nil { @@ -127,6 +133,7 @@ func Open(path string) (*RpmNDB, error) { } func (db *RpmNDB) Close() error { + _ = syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN) return db.file.Close() }