Skip to content

Commit

Permalink
ndb: add read lock to prevent NDB blob magic error
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
djoreilly committed Nov 16, 2023
1 parent 1399746 commit e03fdeb
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/ndb/ndb.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"io"
"os"
"syscall"
"unsafe"

dbi "github.com/knqyf263/go-rpmdb/pkg/db"
Expand Down Expand Up @@ -96,6 +97,11 @@ func Open(path string) (*RpmNDB, error) {
return nil, err
}

err = syscall.Flock(int(file.Fd()), syscall.LOCK_SH)

Check failure on line 100 in pkg/ndb/ndb.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: syscall.Flock

Check failure on line 100 in pkg/ndb/ndb.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: syscall.LOCK_SH
if err != nil {
return nil, err
}

hdrBuff := ndbHeader{}
err = binary.Read(file, binary.LittleEndian, &hdrBuff)
if err != nil {
Expand Down Expand Up @@ -127,6 +133,7 @@ func Open(path string) (*RpmNDB, error) {
}

func (db *RpmNDB) Close() error {
_ = syscall.Flock(int(db.file.Fd()), syscall.LOCK_UN)

Check failure on line 136 in pkg/ndb/ndb.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: syscall.Flock

Check failure on line 136 in pkg/ndb/ndb.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: syscall.LOCK_UN
return db.file.Close()
}

Expand Down

0 comments on commit e03fdeb

Please sign in to comment.