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"
|
2018-06-26 09:40:34 +00:00
|
|
|
"fmt"
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
|
|
|
"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
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
type instance struct {
|
|
|
|
*Lowlevel
|
|
|
|
keyer keyer
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func newInstance(ll *Lowlevel) *instance {
|
|
|
|
return &instance{
|
|
|
|
Lowlevel: ll,
|
|
|
|
keyer: newDefaultKeyer(ll.folderIdx, ll.deviceIdx),
|
2015-10-31 11:31:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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-10-10 09:34:24 +00:00
|
|
|
func (db *instance) addSequences(folder []byte, fs []protocol.FileInfo) {
|
2018-05-01 21:39:15 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *instance) removeSequences(folder []byte, fs []protocol.FileInfo) {
|
2018-05-01 21:39:15 +00:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *instance) withHaveSequence(folder []byte, startSeq int64, fn Iterator) {
|
2018-05-01 21:39:15 +00:00
|
|
|
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() {
|
2018-10-11 10:05:57 +00:00
|
|
|
if seq := db.keyer.SequenceFromSequenceKey(dbi.Key()); f.Sequence != seq {
|
|
|
|
panic(fmt.Sprintf("sequence index corruption (folder %v, file %v): sequence %d != expected %d", string(folder), f.Name, f.Sequence, seq))
|
2018-09-02 18:58:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-01 21:39:15 +00:00
|
|
|
if !fn(f) {
|
2015-10-29 07:07:51 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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-10-11 10:09:44 +00:00
|
|
|
dbi := t.NewIterator(util.BytesPrefix(db.keyer.GenerateDeviceFileKey(nil, folder, nil, nil).WithoutNameAndDevice()), 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-10-10 09:34:24 +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
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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-10-10 09:34:24 +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-10-10 09:34:24 +00:00
|
|
|
func (db *instance) withNeedLocal(folder []byte, truncate bool, fn Iterator) {
|
2018-06-02 13:08:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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-10-11 10:09:44 +00:00
|
|
|
db.keyer.GenerateDeviceFileKey(nil, folder, nil, nil).WithoutNameAndDevice(),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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)
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
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-10-10 09:34:24 +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
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +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
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|