2015-10-29 07:07:51 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-01-03 18:08:19 +00:00
|
|
|
"encoding/binary"
|
2018-06-26 09:40:34 +00:00
|
|
|
"fmt"
|
2015-10-31 11:31:25 +00:00
|
|
|
"os"
|
2015-10-29 07:07:51 +00:00
|
|
|
"sort"
|
2015-10-31 11:31:25 +00:00
|
|
|
"strings"
|
2016-06-13 17:44:03 +00:00
|
|
|
"sync/atomic"
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2015-10-31 11:31:25 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/errors"
|
2015-10-29 07:07:51 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
2015-10-31 11:31:25 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
2015-10-29 07:07:51 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2016-07-23 12:46:31 +00:00
|
|
|
type deletionHandler func(t readWriteTransaction, folder, device, name []byte, dbi iterator.Iterator)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
type Instance struct {
|
2016-06-26 13:40:51 +00:00
|
|
|
committed int64 // this must be the first attribute in the struct to ensure 64 bit alignment on 32 bit plaforms
|
2015-10-29 07:07:51 +00:00
|
|
|
*leveldb.DB
|
2016-07-27 21:38:43 +00:00
|
|
|
location string
|
2016-01-03 18:08:19 +00:00
|
|
|
folderIdx *smallIndex
|
|
|
|
deviceIdx *smallIndex
|
2018-09-18 08:41:06 +00:00
|
|
|
keyer keyer
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func Open(file string) (*Instance, error) {
|
|
|
|
opts := &opt.Options{
|
|
|
|
OpenFilesCacheCapacity: 100,
|
|
|
|
WriteBuffer: 4 << 20,
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := leveldb.OpenFile(file, opts)
|
|
|
|
if leveldbIsCorrupted(err) {
|
|
|
|
db, err = leveldb.RecoverFile(file, opts)
|
|
|
|
}
|
|
|
|
if leveldbIsCorrupted(err) {
|
|
|
|
// The database is corrupted, and we've tried to recover it but it
|
|
|
|
// didn't work. At this point there isn't much to do beyond dropping
|
|
|
|
// the database and reindexing...
|
|
|
|
l.Infoln("Database corruption detected, unable to recover. Reinitializing...")
|
|
|
|
if err := os.RemoveAll(file); err != nil {
|
2018-06-26 09:40:34 +00:00
|
|
|
return nil, errorSuggestion{err, "failed to delete corrupted database"}
|
2015-10-31 11:31:25 +00:00
|
|
|
}
|
|
|
|
db, err = leveldb.OpenFile(file, opts)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-06-26 09:40:34 +00:00
|
|
|
return nil, errorSuggestion{err, "is another instance of Syncthing running?"}
|
2015-10-31 11:31:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 09:40:34 +00:00
|
|
|
return newDBInstance(db, file)
|
2015-10-31 11:31:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func OpenMemory() *Instance {
|
|
|
|
db, _ := leveldb.Open(storage.NewMemStorage(), nil)
|
2018-06-26 09:40:34 +00:00
|
|
|
ldb, _ := newDBInstance(db, "<memory>")
|
|
|
|
return ldb
|
2015-10-31 11:31:25 +00:00
|
|
|
}
|
|
|
|
|
2018-06-26 09:40:34 +00:00
|
|
|
func newDBInstance(db *leveldb.DB, location string) (*Instance, error) {
|
2016-01-03 18:08:19 +00:00
|
|
|
i := &Instance{
|
2018-09-18 08:41:06 +00:00
|
|
|
DB: db,
|
|
|
|
location: location,
|
|
|
|
folderIdx: newSmallIndex(db, []byte{KeyTypeFolderIdx}),
|
|
|
|
deviceIdx: newSmallIndex(db, []byte{KeyTypeDeviceIdx}),
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
2018-09-18 08:41:06 +00:00
|
|
|
i.keyer = newDefaultKeyer(i.folderIdx, i.deviceIdx)
|
2018-06-26 09:40:34 +00:00
|
|
|
err := i.updateSchema()
|
|
|
|
return i, err
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 17:44:03 +00:00
|
|
|
// Committed returns the number of items committed to the database since startup
|
|
|
|
func (db *Instance) Committed() int64 {
|
|
|
|
return atomic.LoadInt64(&db.committed)
|
|
|
|
}
|
|
|
|
|
2016-07-27 21:38:43 +00:00
|
|
|
// Location returns the filesystem path where the database is stored
|
|
|
|
func (db *Instance) Location() string {
|
|
|
|
return db.location
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
func (db *Instance) updateFiles(folder, device []byte, fs []protocol.FileInfo, meta *metadataTracker) {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var fk []byte
|
2018-05-01 21:39:15 +00:00
|
|
|
var gk []byte
|
2015-10-29 07:07:51 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
name := []byte(f.Name)
|
2018-09-18 08:41:06 +00:00
|
|
|
fk = db.keyer.GenerateDeviceFileKey(fk, folder, device, name)
|
2017-04-25 22:52:37 +00:00
|
|
|
|
|
|
|
// Get and unmarshal the file entry. If it doesn't exist or can't be
|
|
|
|
// unmarshalled we'll add it as a new entry.
|
2015-10-29 07:07:51 +00:00
|
|
|
bs, err := t.Get(fk, nil)
|
2017-04-25 22:52:37 +00:00
|
|
|
var ef FileInfoTruncated
|
|
|
|
if err == nil {
|
|
|
|
err = ef.Unmarshal(bs)
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:50:18 +00:00
|
|
|
// Local flags or the invalid bit might change without the version
|
|
|
|
// being bumped. The IsInvalid() method handles both.
|
|
|
|
if err == nil && ef.Version.Equal(f.Version) && ef.IsInvalid() == f.IsInvalid() {
|
2015-10-29 07:07:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
devID := protocol.DeviceIDFromBytes(device)
|
|
|
|
if err == nil {
|
|
|
|
meta.removeFile(devID, ef)
|
2017-10-24 20:05:29 +00:00
|
|
|
}
|
2017-12-14 09:51:17 +00:00
|
|
|
meta.addFile(devID, f)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
t.insertFile(fk, folder, device, f)
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2018-05-01 21:39:15 +00:00
|
|
|
t.updateGlobal(gk, folder, device, f, meta)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
// Write out and reuse the batch every few records, to avoid the batch
|
|
|
|
// growing too large and thus allocating unnecessarily much memory.
|
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
func (db *Instance) addSequences(folder []byte, fs []protocol.FileInfo) {
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var sk []byte
|
|
|
|
var dk []byte
|
|
|
|
for _, f := range fs {
|
2018-09-18 08:41:06 +00:00
|
|
|
sk = db.keyer.GenerateSequenceKey(sk, folder, f.Sequence)
|
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], []byte(f.Name))
|
2018-05-01 21:39:15 +00:00
|
|
|
t.Put(sk, dk)
|
|
|
|
l.Debugf("adding sequence; folder=%q sequence=%v %v", folder, f.Sequence, f.Name)
|
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) removeSequences(folder []byte, fs []protocol.FileInfo) {
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var sk []byte
|
|
|
|
for _, f := range fs {
|
2018-09-18 08:41:06 +00:00
|
|
|
t.Delete(db.keyer.GenerateSequenceKey(sk, folder, f.Sequence))
|
2018-05-01 21:39:15 +00:00
|
|
|
l.Debugf("removing sequence; folder=%q sequence=%v %v", folder, f.Sequence, f.Name)
|
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-18 12:16:33 +00:00
|
|
|
func (db *Instance) withHave(folder, device, prefix []byte, truncate bool, fn Iterator) {
|
2018-05-17 07:26:40 +00:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
unslashedPrefix := prefix
|
|
|
|
if bytes.HasSuffix(prefix, []byte{'/'}) {
|
|
|
|
unslashedPrefix = unslashedPrefix[:len(unslashedPrefix)-1]
|
|
|
|
} else {
|
|
|
|
prefix = append(prefix, '/')
|
|
|
|
}
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
if f, ok := db.getFileTrunc(db.keyer.GenerateDeviceFileKey(nil, folder, device, unslashedPrefix), true); ok && !fn(f) {
|
2018-05-17 07:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateDeviceFileKey(nil, folder, device, prefix)), nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
name := db.keyer.NameFromDeviceFileKey(dbi.Key())
|
2018-05-17 07:26:40 +00:00
|
|
|
if len(prefix) > 0 && !bytes.HasPrefix(name, prefix) {
|
2016-05-09 12:56:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-02 11:48:09 +00:00
|
|
|
// The iterator function may keep a reference to the unmarshalled
|
|
|
|
// struct, which in turn references the buffer it was unmarshalled
|
|
|
|
// from. dbi.Value() just returns an internal slice that it reuses, so
|
|
|
|
// we need to copy it.
|
|
|
|
f, err := unmarshalTrunc(append([]byte{}, dbi.Value()...), truncate)
|
2015-10-29 07:07:51 +00:00
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("unmarshal error:", err)
|
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
2018-05-01 21:39:15 +00:00
|
|
|
if !fn(f) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) withHaveSequence(folder []byte, startSeq int64, fn Iterator) {
|
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(&util.Range{Start: db.keyer.GenerateSequenceKey(nil, folder, startSeq), Limit: db.keyer.GenerateSequenceKey(nil, folder, maxInt64)}, nil)
|
2018-05-01 21:39:15 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
|
|
|
f, ok := db.getFile(dbi.Value())
|
|
|
|
if !ok {
|
2018-09-18 08:41:06 +00:00
|
|
|
l.Debugln("missing file for sequence number", db.keyer.SequenceFromSequenceKey(dbi.Key()))
|
2018-05-01 21:39:15 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-09-02 18:58:32 +00:00
|
|
|
|
|
|
|
if shouldDebug() {
|
|
|
|
key := dbi.Key()
|
|
|
|
seq := int64(binary.BigEndian.Uint64(key[keyPrefixLen+keyFolderLen:]))
|
|
|
|
if f.Sequence != seq {
|
|
|
|
panic(fmt.Sprintf("sequence index corruption, file sequence %d != expected %d", f.Sequence, seq))
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 21:39:15 +00:00
|
|
|
if !fn(f) {
|
2015-10-29 07:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) withAllFolderTruncated(folder []byte, fn func(device []byte, f FileInfoTruncated) bool) {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateDeviceFileKey(nil, folder, nil, nil).WithoutName()), nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
var gk []byte
|
|
|
|
|
2015-10-29 07:07:51 +00:00
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
device, ok := db.keyer.DeviceFromDeviceFileKey(dbi.Key())
|
|
|
|
if !ok {
|
|
|
|
// Not having the device in the index is bad. Clear it.
|
|
|
|
t.Delete(dbi.Key())
|
|
|
|
t.checkFlush()
|
|
|
|
continue
|
|
|
|
}
|
2015-10-29 07:07:51 +00:00
|
|
|
var f FileInfoTruncated
|
2016-02-02 11:48:09 +00:00
|
|
|
// The iterator function may keep a reference to the unmarshalled
|
|
|
|
// struct, which in turn references the buffer it was unmarshalled
|
|
|
|
// from. dbi.Value() just returns an internal slice that it reuses, so
|
|
|
|
// we need to copy it.
|
2016-07-04 10:40:29 +00:00
|
|
|
err := f.Unmarshal(append([]byte{}, dbi.Value()...))
|
2015-10-29 07:07:51 +00:00
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("unmarshal error:", err)
|
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch f.Name {
|
|
|
|
case "", ".", "..", "/": // A few obviously invalid filenames
|
|
|
|
l.Infof("Dropping invalid filename %q from database", f.Name)
|
2018-05-01 21:39:15 +00:00
|
|
|
name := []byte(f.Name)
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2018-05-01 21:39:15 +00:00
|
|
|
t.removeFromGlobal(gk, folder, device, name, nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
t.Delete(dbi.Key())
|
|
|
|
t.checkFlush()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
if !fn(device, f) {
|
2015-10-29 07:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
func (db *Instance) getFile(key []byte) (protocol.FileInfo, bool) {
|
2018-05-17 07:26:40 +00:00
|
|
|
if f, ok := db.getFileTrunc(key, false); ok {
|
|
|
|
return f.(protocol.FileInfo), true
|
|
|
|
}
|
|
|
|
return protocol.FileInfo{}, false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) getFileTrunc(key []byte, trunc bool) (FileIntf, bool) {
|
2018-05-01 21:39:15 +00:00
|
|
|
bs, err := db.Get(key, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2018-05-17 07:26:40 +00:00
|
|
|
return nil, false
|
2018-05-01 21:39:15 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln("surprise error:", err)
|
2018-05-17 07:26:40 +00:00
|
|
|
return nil, false
|
2018-05-01 21:39:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:26:40 +00:00
|
|
|
f, err := unmarshalTrunc(bs, trunc)
|
2018-05-01 21:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Debugln("unmarshal error:", err)
|
2018-05-17 07:26:40 +00:00
|
|
|
return nil, false
|
2018-05-01 21:39:15 +00:00
|
|
|
}
|
|
|
|
return f, true
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) getGlobal(folder, file []byte, truncate bool) (FileIntf, bool) {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
_, _, f, ok := db.getGlobalInto(t, nil, nil, folder, file, truncate)
|
|
|
|
return f, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) getGlobalInto(t readOnlyTransaction, gk, dk, folder, file []byte, truncate bool) ([]byte, []byte, FileIntf, bool) {
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, file)
|
2018-06-02 13:08:32 +00:00
|
|
|
|
|
|
|
bs, err := t.Get(gk, nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
if err != nil {
|
2018-06-02 13:08:32 +00:00
|
|
|
return gk, dk, nil, false
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:26:40 +00:00
|
|
|
vl, ok := unmarshalVersionList(bs)
|
|
|
|
if !ok {
|
2018-06-02 13:08:32 +00:00
|
|
|
return gk, dk, nil, false
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[0].Device, file)
|
2018-06-02 13:08:32 +00:00
|
|
|
if fi, ok := db.getFileTrunc(dk, truncate); ok {
|
|
|
|
return gk, dk, fi, true
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
return gk, dk, nil, false
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator) {
|
2018-05-17 07:26:40 +00:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
unslashedPrefix := prefix
|
|
|
|
if bytes.HasSuffix(prefix, []byte{'/'}) {
|
|
|
|
unslashedPrefix = unslashedPrefix[:len(unslashedPrefix)-1]
|
|
|
|
} else {
|
|
|
|
prefix = append(prefix, '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, ok := db.getGlobal(folder, unslashedPrefix, truncate); ok && !fn(f) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, prefix)), nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
var fk []byte
|
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
2018-05-17 07:26:40 +00:00
|
|
|
if len(prefix) > 0 && !bytes.HasPrefix(name, prefix) {
|
2016-05-09 12:56:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-05-17 07:26:40 +00:00
|
|
|
vl, ok := unmarshalVersionList(dbi.Value())
|
|
|
|
if !ok {
|
2017-04-25 22:52:37 +00:00
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
fk = db.keyer.GenerateDeviceFileKey(fk, folder, vl.Versions[0].Device, name)
|
2018-05-17 07:26:40 +00:00
|
|
|
|
|
|
|
f, ok := db.getFileTrunc(fk, truncate)
|
|
|
|
if !ok {
|
2017-04-25 22:52:37 +00:00
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
if !fn(f) {
|
2015-10-29 07:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) availability(folder, file []byte) []protocol.DeviceID {
|
2018-09-18 08:41:06 +00:00
|
|
|
k := db.keyer.GenerateGlobalVersionKey(nil, folder, file)
|
2015-10-29 07:07:51 +00:00
|
|
|
bs, err := db.Get(k, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("surprise error:", err)
|
|
|
|
return nil
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-05-17 07:26:40 +00:00
|
|
|
vl, ok := unmarshalVersionList(bs)
|
|
|
|
if !ok {
|
2017-04-25 22:52:37 +00:00
|
|
|
return nil
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var devices []protocol.DeviceID
|
2016-07-04 10:40:29 +00:00
|
|
|
for _, v := range vl.Versions {
|
|
|
|
if !v.Version.Equal(vl.Versions[0].Version) {
|
2015-10-29 07:07:51 +00:00
|
|
|
break
|
|
|
|
}
|
2017-11-11 19:18:17 +00:00
|
|
|
if v.Invalid {
|
|
|
|
continue
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
n := protocol.DeviceIDFromBytes(v.Device)
|
2015-10-29 07:07:51 +00:00
|
|
|
devices = append(devices, n)
|
|
|
|
}
|
|
|
|
|
|
|
|
return devices
|
|
|
|
}
|
|
|
|
|
2018-02-25 08:39:00 +00:00
|
|
|
func (db *Instance) withNeed(folder, device []byte, truncate bool, fn Iterator) {
|
2018-06-02 13:08:32 +00:00
|
|
|
if bytes.Equal(device, protocol.LocalDeviceID[:]) {
|
|
|
|
db.withNeedLocal(folder, truncate, fn)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName()), nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
var fk []byte
|
|
|
|
for dbi.Next() {
|
2018-05-17 07:26:40 +00:00
|
|
|
vl, ok := unmarshalVersionList(dbi.Value())
|
|
|
|
if !ok {
|
2017-04-25 22:52:37 +00:00
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
haveFV, have := vl.Get(device)
|
|
|
|
// XXX: This marks Concurrent (i.e. conflicting) changes as
|
|
|
|
// needs. Maybe we should do that, but it needs special
|
|
|
|
// handling in the puller.
|
|
|
|
if have && haveFV.Version.GreaterEqual(vl.Versions[0].Version) {
|
2017-10-24 20:05:29 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
2017-10-24 20:05:29 +00:00
|
|
|
needVersion := vl.Versions[0].Version
|
2017-11-11 19:18:17 +00:00
|
|
|
needDevice := protocol.DeviceIDFromBytes(vl.Versions[0].Device)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2017-10-24 20:05:29 +00:00
|
|
|
for i := range vl.Versions {
|
|
|
|
if !vl.Versions[i].Version.Equal(needVersion) {
|
|
|
|
// We haven't found a valid copy of the file with the needed version.
|
|
|
|
break
|
|
|
|
}
|
2017-11-11 19:18:17 +00:00
|
|
|
|
|
|
|
if vl.Versions[i].Invalid {
|
|
|
|
// The file is marked invalid, don't use it.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
fk = db.keyer.GenerateDeviceFileKey(fk, folder, vl.Versions[i].Device, name)
|
2017-10-24 20:05:29 +00:00
|
|
|
bs, err := t.Get(fk, nil)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln("surprise error:", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2017-10-24 20:05:29 +00:00
|
|
|
gf, err := unmarshalTrunc(bs, truncate)
|
|
|
|
if err != nil {
|
|
|
|
l.Debugln("unmarshal error:", err)
|
|
|
|
continue
|
|
|
|
}
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2017-10-24 20:05:29 +00:00
|
|
|
if gf.IsDeleted() && !have {
|
|
|
|
// We don't need deleted files that we don't have
|
|
|
|
break
|
|
|
|
}
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
l.Debugf("need folder=%q device=%v name=%q have=%v invalid=%v haveV=%v globalV=%v globalDev=%v", folder, protocol.DeviceIDFromBytes(device), name, have, haveFV.Invalid, haveFV.Version, needVersion, needDevice)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
if !fn(gf) {
|
2017-10-24 20:05:29 +00:00
|
|
|
return
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
2017-10-24 20:05:29 +00:00
|
|
|
|
|
|
|
// This file is handled, no need to look further in the version list
|
|
|
|
break
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
func (db *Instance) withNeedLocal(folder []byte, truncate bool, fn Iterator) {
|
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateNeedFileKey(nil, folder, nil).WithoutName()), nil)
|
2018-06-02 13:08:32 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
var dk []byte
|
|
|
|
var gk []byte
|
|
|
|
var f FileIntf
|
|
|
|
var ok bool
|
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
gk, dk, f, ok = db.getGlobalInto(t, gk, dk, folder, db.keyer.NameFromGlobalVersionKey(dbi.Key()), truncate)
|
2018-06-02 13:08:32 +00:00
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !fn(f) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) ListFolders() []string {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
folderExists := make(map[string]bool)
|
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
folder, ok := db.keyer.FolderFromGlobalVersionKey(dbi.Key())
|
2017-01-04 10:34:52 +00:00
|
|
|
if ok && !folderExists[string(folder)] {
|
|
|
|
folderExists[string(folder)] = true
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
folders := make([]string, 0, len(folderExists))
|
|
|
|
for k := range folderExists {
|
|
|
|
folders = append(folders, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(folders)
|
|
|
|
return folders
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
func (db *Instance) dropFolder(folder []byte) {
|
2018-06-02 13:08:32 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
2015-10-29 07:07:51 +00:00
|
|
|
defer t.close()
|
|
|
|
|
2018-06-02 13:08:32 +00:00
|
|
|
for _, key := range [][]byte{
|
|
|
|
// Remove all items related to the given folder from the device->file bucket
|
2018-09-18 08:41:06 +00:00
|
|
|
db.keyer.GenerateDeviceFileKey(nil, folder, nil, nil).WithoutName(),
|
2018-06-02 13:08:32 +00:00
|
|
|
// Remove all sequences related to the folder
|
2018-09-18 08:41:06 +00:00
|
|
|
db.keyer.GenerateSequenceKey(nil, []byte(folder), 0).WithoutSequence(),
|
2018-06-02 13:08:32 +00:00
|
|
|
// Remove all items related to the given folder from the global bucket
|
2018-09-18 08:41:06 +00:00
|
|
|
db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName(),
|
2018-06-02 13:08:32 +00:00
|
|
|
// Remove all needs related to the folder
|
2018-09-18 08:41:06 +00:00
|
|
|
db.keyer.GenerateNeedFileKey(nil, folder, nil).WithoutName(),
|
2018-06-02 13:08:32 +00:00
|
|
|
} {
|
|
|
|
t.deleteKeyPrefix(key)
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
func (db *Instance) dropDeviceFolder(device, folder []byte, meta *metadataTracker) {
|
2017-11-12 20:20:34 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateDeviceFileKey(nil, folder, device, nil)), nil)
|
2017-11-12 20:20:34 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
2018-05-01 21:39:15 +00:00
|
|
|
var gk []byte
|
|
|
|
|
2017-11-12 20:20:34 +00:00
|
|
|
for dbi.Next() {
|
|
|
|
key := dbi.Key()
|
2018-09-18 08:41:06 +00:00
|
|
|
name := db.keyer.NameFromDeviceFileKey(key)
|
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2018-05-01 21:39:15 +00:00
|
|
|
t.removeFromGlobal(gk, folder, device, name, meta)
|
2017-11-12 20:20:34 +00:00
|
|
|
t.Delete(key)
|
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
func (db *Instance) checkGlobals(folder []byte, meta *metadataTracker) {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateGlobalVersionKey(nil, folder, nil).WithoutName()), nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
var fk []byte
|
|
|
|
for dbi.Next() {
|
2018-05-17 07:26:40 +00:00
|
|
|
vl, ok := unmarshalVersionList(dbi.Value())
|
|
|
|
if !ok {
|
2017-04-25 22:52:37 +00:00
|
|
|
continue
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the global version list for consistency. An issue in previous
|
|
|
|
// versions of goleveldb could result in reordered writes so that
|
|
|
|
// there are global entries pointing to no longer existing files. Here
|
|
|
|
// we find those and clear them out.
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
name := db.keyer.NameFromGlobalVersionKey(dbi.Key())
|
2016-05-31 19:29:26 +00:00
|
|
|
var newVL VersionList
|
2016-07-04 10:40:29 +00:00
|
|
|
for i, version := range vl.Versions {
|
2018-09-18 08:41:06 +00:00
|
|
|
fk = db.keyer.GenerateDeviceFileKey(fk, folder, version.Device, name)
|
2015-10-29 07:07:51 +00:00
|
|
|
_, err := t.Get(fk, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-04-25 22:52:37 +00:00
|
|
|
l.Debugln("surprise error:", err)
|
|
|
|
return
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
newVL.Versions = append(newVL.Versions, version)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
if i == 0 {
|
2018-05-01 21:39:15 +00:00
|
|
|
if fi, ok := db.getFile(fk); ok {
|
2018-07-12 08:15:57 +00:00
|
|
|
meta.addFile(protocol.GlobalDeviceID, fi)
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 10:40:29 +00:00
|
|
|
if len(newVL.Versions) != len(vl.Versions) {
|
|
|
|
t.Put(dbi.Key(), mustMarshal(&newVL))
|
2015-10-29 07:07:51 +00:00
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.Debugf("db check completed for %q", folder)
|
|
|
|
}
|
|
|
|
|
2016-07-23 12:46:31 +00:00
|
|
|
func (db *Instance) getIndexID(device, folder []byte) protocol.IndexID {
|
2018-09-18 08:41:06 +00:00
|
|
|
key := db.keyer.GenerateIndexIDKey(nil, device, folder)
|
2016-07-23 12:46:31 +00:00
|
|
|
cur, err := db.Get(key, nil)
|
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
var id protocol.IndexID
|
|
|
|
if err := id.Unmarshal(cur); err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) setIndexID(device, folder []byte, id protocol.IndexID) {
|
2018-09-18 08:41:06 +00:00
|
|
|
key := db.keyer.GenerateIndexIDKey(nil, device, folder)
|
2016-07-23 12:46:31 +00:00
|
|
|
bs, _ := id.Marshal() // marshalling can't fail
|
|
|
|
if err := db.Put(key, bs, nil); err != nil {
|
|
|
|
panic("storing index ID: " + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 10:42:01 +00:00
|
|
|
// DropLocalDeltaIndexIDs removes all index IDs for the local device ID from
|
|
|
|
// the database. This will cause a full index transmission on the next
|
|
|
|
// connection.
|
|
|
|
func (db *Instance) DropLocalDeltaIndexIDs() {
|
|
|
|
db.dropDeltaIndexIDs(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DropRemoteDeltaIndexIDs removes all index IDs for the other devices than
|
|
|
|
// the local one from the database. This will cause them to send us a full
|
|
|
|
// index on the next connection.
|
|
|
|
func (db *Instance) DropRemoteDeltaIndexIDs() {
|
|
|
|
db.dropDeltaIndexIDs(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) dropDeltaIndexIDs(local bool) {
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeIndexID}), nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
device, _ := db.keyer.DeviceFromIndexIDKey(dbi.Key())
|
2018-03-10 10:42:01 +00:00
|
|
|
if bytes.Equal(device, protocol.LocalDeviceID[:]) == local {
|
|
|
|
t.Delete(dbi.Key())
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *Instance) dropMtimes(folder []byte) {
|
2018-09-18 08:41:06 +00:00
|
|
|
db.dropPrefix(db.keyer.GenerateMtimesKey(nil, folder))
|
2016-08-05 17:45:45 +00:00
|
|
|
}
|
|
|
|
|
2017-12-14 09:51:17 +00:00
|
|
|
func (db *Instance) dropFolderMeta(folder []byte) {
|
2018-09-18 08:41:06 +00:00
|
|
|
db.dropPrefix(db.keyer.GenerateFolderMetaKey(nil, folder))
|
2017-12-14 09:51:17 +00:00
|
|
|
}
|
|
|
|
|
2016-08-05 17:45:45 +00:00
|
|
|
func (db *Instance) dropPrefix(prefix []byte) {
|
2016-08-05 07:13:52 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2016-08-05 17:45:45 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(prefix), nil)
|
2016-08-05 07:13:52 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
|
|
|
t.Delete(dbi.Key())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-29 07:07:51 +00:00
|
|
|
func unmarshalTrunc(bs []byte, truncate bool) (FileIntf, error) {
|
|
|
|
if truncate {
|
|
|
|
var tf FileInfoTruncated
|
2016-07-04 10:40:29 +00:00
|
|
|
err := tf.Unmarshal(bs)
|
2015-10-29 07:07:51 +00:00
|
|
|
return tf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var tf protocol.FileInfo
|
2016-07-04 10:40:29 +00:00
|
|
|
err := tf.Unmarshal(bs)
|
2015-10-29 07:07:51 +00:00
|
|
|
return tf, err
|
|
|
|
}
|
2015-10-31 11:31:25 +00:00
|
|
|
|
2018-05-17 07:26:40 +00:00
|
|
|
func unmarshalVersionList(data []byte) (VersionList, bool) {
|
|
|
|
var vl VersionList
|
|
|
|
if err := vl.Unmarshal(data); err != nil {
|
|
|
|
l.Debugln("unmarshal error:", err)
|
|
|
|
return VersionList{}, false
|
|
|
|
}
|
|
|
|
if len(vl.Versions) == 0 {
|
|
|
|
l.Debugln("empty version list")
|
|
|
|
return VersionList{}, false
|
|
|
|
}
|
|
|
|
return vl, true
|
|
|
|
}
|
|
|
|
|
2015-10-31 11:31:25 +00:00
|
|
|
// A "better" version of leveldb's errors.IsCorrupted.
|
|
|
|
func leveldbIsCorrupted(err error) bool {
|
|
|
|
switch {
|
|
|
|
case err == nil:
|
|
|
|
return false
|
|
|
|
|
|
|
|
case errors.IsCorrupted(err):
|
|
|
|
return true
|
|
|
|
|
|
|
|
case strings.Contains(err.Error(), "corrupted"):
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2016-01-03 18:08:19 +00:00
|
|
|
|
2018-06-26 09:40:34 +00:00
|
|
|
type errorSuggestion struct {
|
|
|
|
inner error
|
|
|
|
suggestion string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e errorSuggestion) Error() string {
|
|
|
|
return fmt.Sprintf("%s (%s)", e.inner.Error(), e.suggestion)
|
|
|
|
}
|