Repository: syncthing/syncthing
File: lib/db/set.go (Go)

Code before:
	})
	if err != nil {
		return err
	}

	s.meta.SetCreated()
	return s.meta.toDB(s.db, []byte(s.folder))
}

// Verify the local sequence number from actual sequence entries. Returns
// true if it was all good, or false if a fixup was necessary.
func (s *FileSet) verifyLocalSequence() (ok bool) {
	// Walk the sequence index from the current (supposedly) highest
	// sequence number and raise the alarm if we get anything. This recovers
	// from the occasion where we have written sequence entries to disk but
	// not yet written new metadata to disk.
	//
	// Note that we can have the same thing happen for remote devices but
	// there it's not a problem -- we'll simply advertise a lower sequence
	// number than we've actually seen and receive some duplicate updates
	// and then be in sync again.

	curSeq := s.meta.Sequence(protocol.LocalDeviceID)
	snap := s.Snapshot()
	ok = true
	snap.WithHaveSequence(curSeq, func(fi FileIntf) bool {
		if fi.SequenceNo() > curSeq {
			// Out of sync, we will recalculate the folder.
			ok = false
			return false // stop iteration
		}
		return true
	})
	snap.Release()

	return ok
}

func (s *FileSet) Drop(device protocol.DeviceID) {
	l.Debugf("%s Drop(%v)", s.folder, device)

	s.updateMutex.Lock()
	defer s.updateMutex.Unlock()

	if err := s.db.dropDeviceFolder(device[:], []byte(s.folder), s.meta); backend.IsClosed(err) {
		return
	} else if err != nil {
		panic(err)
	}

	if device == protocol.LocalDeviceID {

Review comment (suggestion):
To not have to unmarshal all those fileinfo, you could directly access the db with an iterator starting at curSeq, something like this:

```
	first, err := s.db.keyer.GenerateSequenceKey(nil, folder, curSeq)
	it, err := s.db.NewRangeIterator(first, nil)
	return !it.Next()
```

Code after:
	})
	if err != nil {
		return err
	}

	s.meta.SetCreated()
	return s.meta.toDB(s.db, []byte(s.folder))
}

// Verify the local sequence number from actual sequence entries. Returns
// true if it was all good, or false if a fixup was necessary.
func (s *FileSet) verifyLocalSequence() bool {
	// Walk the sequence index from the current (supposedly) highest
	// sequence number and raise the alarm if we get anything. This recovers
	// from the occasion where we have written sequence entries to disk but
	// not yet written new metadata to disk.
	//
	// Note that we can have the same thing happen for remote devices but
	// there it's not a problem -- we'll simply advertise a lower sequence
	// number than we've actually seen and receive some duplicate updates
	// and then be in sync again.

	curSeq := s.meta.Sequence(protocol.LocalDeviceID)

	snap := s.Snapshot()
	ok := true
	snap.WithHaveSequence(curSeq+1, func(fi FileIntf) bool {
		ok = false // we got something, which we should not have
		return false
	})
	snap.Release()

	return ok
}

func (s *FileSet) Drop(device protocol.DeviceID) {
	l.Debugf("%s Drop(%v)", s.folder, device)

	s.updateMutex.Lock()
	defer s.updateMutex.Unlock()

	if err := s.db.dropDeviceFolder(device[:], []byte(s.folder), s.meta); backend.IsClosed(err) {
		return
	} else if err != nil {
		panic(err)
	}

	if device == protocol.LocalDeviceID {
		s.meta.resetCounts(device)
		// We deliberately do not reset the sequence number here. Dropping
		// all files for the local device ID only happens in testing - which