lib/db: Prevent IndexID creation race (#7211)
This commit is contained in:
+22
-11
@@ -39,13 +39,27 @@ type FileSet struct {
|
||||
type Iterator func(f protocol.FileIntf) bool
|
||||
|
||||
func NewFileSet(folder string, fs fs.Filesystem, db *Lowlevel) *FileSet {
|
||||
return &FileSet{
|
||||
select {
|
||||
case <-db.oneFileSetCreated:
|
||||
default:
|
||||
close(db.oneFileSetCreated)
|
||||
}
|
||||
s := &FileSet{
|
||||
folder: folder,
|
||||
fs: fs,
|
||||
db: db,
|
||||
meta: db.loadMetadataTracker(folder),
|
||||
updateMutex: sync.NewMutex(),
|
||||
}
|
||||
if id := s.IndexID(protocol.LocalDeviceID); id == 0 {
|
||||
// No index ID set yet. We create one now.
|
||||
id = protocol.NewIndexID()
|
||||
err := s.db.setIndexID(protocol.LocalDeviceID[:], []byte(s.folder), id)
|
||||
if err != nil && !backend.IsClosed(err) {
|
||||
fatalError(err, fmt.Sprintf("%s Creating new IndexID", s.folder), s.db)
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *FileSet) Drop(device protocol.DeviceID) {
|
||||
@@ -357,16 +371,6 @@ func (s *FileSet) IndexID(device protocol.DeviceID) protocol.IndexID {
|
||||
} else if err != nil {
|
||||
fatalError(err, opStr, s.db)
|
||||
}
|
||||
if id == 0 && device == protocol.LocalDeviceID {
|
||||
// No index ID set yet. We create one now.
|
||||
id = protocol.NewIndexID()
|
||||
err := s.db.setIndexID(device[:], []byte(s.folder), id)
|
||||
if backend.IsClosed(err) {
|
||||
return 0
|
||||
} else if err != nil {
|
||||
fatalError(err, opStr, s.db)
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
@@ -433,7 +437,14 @@ func DropFolder(db *Lowlevel, folder string) {
|
||||
|
||||
// DropDeltaIndexIDs removes all delta index IDs from the database.
|
||||
// This will cause a full index transmission on the next connection.
|
||||
// Must be called before using FileSets, i.e. before NewFileSet is called for
|
||||
// the first time.
|
||||
func DropDeltaIndexIDs(db *Lowlevel) {
|
||||
select {
|
||||
case <-db.oneFileSetCreated:
|
||||
panic("DropDeltaIndexIDs must not be called after NewFileSet for the same Lowlevel")
|
||||
default:
|
||||
}
|
||||
opStr := "DropDeltaIndexIDs"
|
||||
l.Debugf(opStr)
|
||||
dbi, err := db.NewPrefixIterator([]byte{KeyTypeIndexID})
|
||||
|
||||
Reference in New Issue
Block a user