From e26a3b58b0d07fc47a2549df33d171ed9fd6d0f7 Mon Sep 17 00:00:00 2001 From: mappu Date: Wed, 17 May 2023 18:54:30 +1200 Subject: [PATCH] upload: return error if the short IDs collide for whatever reason --- Metadata.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Metadata.go b/Metadata.go index ef5eefe..8574a5a 100644 --- a/Metadata.go +++ b/Metadata.go @@ -3,6 +3,7 @@ package contented import ( "encoding/json" "errors" + "fmt" "log" "net/http" "os" @@ -62,9 +63,17 @@ func (this *Server) AddMetadata(m Metadata) (string, error) { err = this.db.Update(func(tx *bolt.Tx) error { b := tx.Bucket(this.metadataBucket) - seq, _ := b.NextSequence() // cannot fail + seq, err := b.NextSequence() + if err != nil { + return fmt.Errorf("NextSequence: %w", err) + } shortRef = idToString(seq) - return tx.Bucket(this.metadataBucket).Put([]byte(shortRef), jb) + + if b.Get([]byte(shortRef)) != nil { + return fmt.Errorf("Next bucket sequence %d creates colliding ID %s", seq, shortRef) + } + + return b.Put([]byte(shortRef), jb) }) if err != nil { return "", err