db: unify kvdb file existence checks

Add a tombstone helper for checking whether a legacy kvdb file exists on
disk, without inspecting any tombstone state inside the database.

Use this helper in the tombstone deprecation and activity checks so
missing-file handling is centralized in one place instead of being
reimplemented in each function.

We also export this helper function, as we'll use it in the upcoming
commit which checks if the legacy any bbolt database file exists before
attempting to migrate it.
This commit is contained in:
Viktor Torstensson 2026-06-11 23:04:11 +02:00
parent 1903010be5
commit 27eba1e5c1
No known key found for this signature in database
GPG key ID: 961CC8259AE675D4

View file

@ -34,10 +34,31 @@ const (
dbFilePermission = 0600
)
// KVDBFileExists reports whether the legacy bbolt database file exists at the
// given path. This only checks file presence and intentionally ignores any
// tombstone state inside the database.
func KVDBFileExists(path string) (bool, error) {
fi, err := os.Stat(path)
switch {
case err == nil:
return !fi.IsDir(), nil
case os.IsNotExist(err):
return false, nil
default:
return false, err
}
}
// DeprecateKVDB marks the given legacy bbolt database as deprecated by
// writing the migration tombstone marker into the specified top-level bucket.
func DeprecateKVDB(path string, timeout time.Duration, bucketKey []byte) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
exists, err := KVDBFileExists(path)
if err != nil {
return err
}
if !exists {
return nil
}
@ -59,7 +80,11 @@ func DeprecateKVDB(path string, timeout time.Duration, bucketKey []byte) error {
func CheckKVDBDeprecated(path string, bucketKey []byte,
timeout time.Duration) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
exists, err := KVDBFileExists(path)
if err != nil {
return err
}
if !exists {
return nil
}
@ -93,11 +118,15 @@ func CheckKVDBDeprecated(path string, bucketKey []byte,
func HasActiveKVDB(path string, bucketKey []byte,
timeout time.Duration) (bool, error) {
if _, err := os.Stat(path); os.IsNotExist(err) {
exists, err := KVDBFileExists(path)
if err != nil {
return false, err
}
if !exists {
return false, nil
}
err := CheckKVDBDeprecated(path, bucketKey, timeout)
err = CheckKVDBDeprecated(path, bucketKey, timeout)
switch {
case errors.Is(err, ErrKVDBDeprecated):
return false, nil