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"
|
2019-01-23 09:22:33 +00:00
|
|
|
"encoding/binary"
|
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/util"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 09:22:33 +00:00
|
|
|
// updateRemoteFiles adds a list of fileinfos to the database and updates the
|
|
|
|
// global versionlist and metadata.
|
|
|
|
func (db *instance) updateRemoteFiles(folder, device []byte, fs []protocol.FileInfo, meta *metadataTracker) {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2019-01-20 07:47:20 +00:00
|
|
|
var dk, gk, keyBuf []byte
|
2019-01-19 19:26:46 +00:00
|
|
|
devID := protocol.DeviceIDFromBytes(device)
|
2015-10-29 07:07:51 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
name := []byte(f.Name)
|
2019-01-19 19:26:46 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, device, name)
|
2017-04-25 22:52:37 +00:00
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
ef, ok := t.getFileTrunc(dk, true)
|
2019-01-19 19:21:58 +00:00
|
|
|
if ok && unchanged(f, ef) {
|
2015-10-29 07:07:51 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:21:58 +00:00
|
|
|
if ok {
|
2017-12-14 09:51:17 +00:00
|
|
|
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
|
|
|
|
2019-01-20 09:24:39 +00:00
|
|
|
l.Debugf("insert; folder=%q device=%v %v", folder, devID, f)
|
|
|
|
t.Put(dk, mustMarshal(&f))
|
2018-05-01 21:39:15 +00:00
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2019-01-20 07:47:20 +00:00
|
|
|
keyBuf, _ = t.updateGlobal(gk, keyBuf, folder, device, f, meta)
|
2015-10-29 07:07:51 +00:00
|
|
|
|
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 09:22:33 +00:00
|
|
|
// updateLocalFiles adds fileinfos to the db, and updates the global versionlist,
|
|
|
|
// metadata, sequence and blockmap buckets.
|
|
|
|
func (db *instance) updateLocalFiles(folder []byte, fs []protocol.FileInfo, meta *metadataTracker) {
|
2018-05-01 21:39:15 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
2019-01-23 09:22:33 +00:00
|
|
|
var dk, gk, keyBuf []byte
|
|
|
|
blockBuf := make([]byte, 4)
|
2018-05-01 21:39:15 +00:00
|
|
|
for _, f := range fs {
|
2019-01-23 09:22:33 +00:00
|
|
|
name := []byte(f.Name)
|
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], name)
|
|
|
|
|
|
|
|
ef, ok := t.getFileByKey(dk)
|
|
|
|
if ok && unchanged(f, ef) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
if !ef.IsDirectory() && !ef.IsDeleted() && !ef.IsInvalid() {
|
|
|
|
for _, block := range ef.Blocks {
|
2019-02-14 23:15:13 +00:00
|
|
|
keyBuf = db.keyer.GenerateBlockMapKey(keyBuf, folder, block.Hash, name)
|
2019-01-23 09:22:33 +00:00
|
|
|
t.Delete(keyBuf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keyBuf = db.keyer.GenerateSequenceKey(keyBuf, folder, ef.SequenceNo())
|
|
|
|
t.Delete(keyBuf)
|
|
|
|
l.Debugf("removing sequence; folder=%q sequence=%v %v", folder, ef.SequenceNo(), ef.FileName())
|
|
|
|
}
|
|
|
|
|
|
|
|
f.Sequence = meta.nextLocalSeq()
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
meta.removeFile(protocol.LocalDeviceID, ef)
|
|
|
|
}
|
|
|
|
meta.addFile(protocol.LocalDeviceID, f)
|
|
|
|
|
|
|
|
l.Debugf("insert (local); folder=%q %v", folder, f)
|
|
|
|
t.Put(dk, mustMarshal(&f))
|
|
|
|
|
2019-02-14 23:15:13 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, []byte(f.Name))
|
2019-01-23 09:22:33 +00:00
|
|
|
keyBuf, _ = t.updateGlobal(gk, keyBuf, folder, protocol.LocalDeviceID[:], f, meta)
|
|
|
|
|
|
|
|
keyBuf = db.keyer.GenerateSequenceKey(keyBuf, folder, f.Sequence)
|
|
|
|
t.Put(keyBuf, dk)
|
2018-05-01 21:39:15 +00:00
|
|
|
l.Debugf("adding sequence; folder=%q sequence=%v %v", folder, f.Sequence, f.Name)
|
|
|
|
|
2019-01-23 09:22:33 +00:00
|
|
|
if !f.IsDirectory() && !f.IsDeleted() && !f.IsInvalid() {
|
|
|
|
for i, block := range f.Blocks {
|
|
|
|
binary.BigEndian.PutUint32(blockBuf, uint32(i))
|
2019-02-14 23:15:13 +00:00
|
|
|
keyBuf = db.keyer.GenerateBlockMapKey(keyBuf, folder, block.Hash, name)
|
2019-01-23 09:22:33 +00:00
|
|
|
t.Put(keyBuf, blockBuf)
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 21:39:15 +00:00
|
|
|
|
|
|
|
t.checkFlush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *instance) withHave(folder, device, prefix []byte, truncate bool, fn Iterator) {
|
2019-01-18 10:34:18 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
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, '/')
|
|
|
|
}
|
|
|
|
|
2019-01-18 10:34:18 +00:00
|
|
|
if f, ok := t.getFileTrunc(db.keyer.GenerateDeviceFileKey(nil, folder, device, unslashedPrefix), true); ok && !fn(f) {
|
2018-05-17 07:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-18 10:34:18 +00:00
|
|
|
f, err := unmarshalTrunc(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() {
|
2019-01-18 10:34:18 +00:00
|
|
|
f, ok := t.getFileByKey(dbi.Value())
|
2018-05-01 21:39:15 +00:00
|
|
|
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()
|
|
|
|
|
2019-01-20 09:24:39 +00:00
|
|
|
var gk, keyBuf []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)
|
2019-01-20 09:24:39 +00:00
|
|
|
keyBuf = t.removeFromGlobal(gk, keyBuf, 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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-18 10:34:18 +00:00
|
|
|
func (db *instance) getFileDirty(folder, device, file []byte) (protocol.FileInfo, bool) {
|
2019-01-19 19:21:58 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
return t.getFile(folder, device, file)
|
2015-10-29 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 12:01:39 +00:00
|
|
|
func (db *instance) getGlobalDirty(folder, file []byte, truncate bool) (FileIntf, bool) {
|
2015-10-29 07:07:51 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
2019-01-20 07:47:20 +00:00
|
|
|
_, f, ok := t.getGlobal(nil, folder, file, truncate)
|
2018-06-02 13:08:32 +00:00
|
|
|
return f, ok
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *instance) withGlobal(folder, prefix []byte, truncate bool, fn Iterator) {
|
2019-01-18 10:34:18 +00:00
|
|
|
t := db.newReadOnlyTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
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, '/')
|
|
|
|
}
|
|
|
|
|
2019-01-20 07:47:20 +00:00
|
|
|
if _, f, ok := t.getGlobal(nil, folder, unslashedPrefix, truncate); ok && !fn(f) {
|
2018-05-17 07:26:40 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
var dk []byte
|
2015-10-29 07:07:51 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[0].Device, name)
|
2018-05-17 07:26:40 +00:00
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
f, ok := t.getFileTrunc(dk, truncate)
|
2018-05-17 07:26:40 +00:00
|
|
|
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()
|
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
var dk []byte
|
|
|
|
devID := protocol.DeviceIDFromBytes(device)
|
2015-10-29 07:07:51 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, vl.Versions[i].Device, name)
|
2019-01-20 09:24:39 +00:00
|
|
|
gf, ok := t.getFileTrunc(dk, truncate)
|
|
|
|
if !ok {
|
2017-10-24 20:05:29 +00:00
|
|
|
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
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
l.Debugf("need folder=%q device=%v name=%q have=%v invalid=%v haveV=%v globalV=%v globalDev=%v", folder, devID, 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()
|
|
|
|
|
2019-01-20 09:24:39 +00:00
|
|
|
var keyBuf []byte
|
2018-06-02 13:08:32 +00:00
|
|
|
var f FileIntf
|
|
|
|
var ok bool
|
|
|
|
for dbi.Next() {
|
2019-01-20 09:24:39 +00:00
|
|
|
keyBuf, f, ok = t.getGlobal(keyBuf, 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(),
|
2019-01-23 09:22:33 +00:00
|
|
|
// Remove the blockmap of the folder
|
|
|
|
db.keyer.GenerateBlockMapKey(nil, folder, nil, nil).WithoutHashAndName(),
|
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()
|
|
|
|
|
2019-01-20 09:24:39 +00:00
|
|
|
var gk, keyBuf []byte
|
2017-11-12 20:20:34 +00:00
|
|
|
for dbi.Next() {
|
2019-01-20 07:47:20 +00:00
|
|
|
name := db.keyer.NameFromDeviceFileKey(dbi.Key())
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2019-01-20 09:24:39 +00:00
|
|
|
keyBuf = t.removeFromGlobal(gk, keyBuf, folder, device, name, meta)
|
2019-01-20 07:47:20 +00:00
|
|
|
t.Delete(dbi.Key())
|
2017-11-12 20:20:34 +00:00
|
|
|
t.checkFlush()
|
|
|
|
}
|
2019-01-23 09:22:33 +00:00
|
|
|
if bytes.Equal(device, protocol.LocalDeviceID[:]) {
|
|
|
|
t.deleteKeyPrefix(db.keyer.GenerateBlockMapKey(nil, folder, nil, nil).WithoutHashAndName())
|
|
|
|
}
|
2017-11-12 20:20:34 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
var dk []byte
|
2015-10-29 07:07:51 +00:00
|
|
|
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 {
|
2019-01-19 19:26:46 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, version.Device, name)
|
|
|
|
_, err := t.Get(dk, nil)
|
2015-10-29 07:07:51 +00:00
|
|
|
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 {
|
2019-01-19 19:26:46 +00:00
|
|
|
if fi, ok := t.getFileByKey(dk); 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 {
|
2019-01-19 19:26:46 +00:00
|
|
|
cur, err := db.Get(db.keyer.GenerateIndexIDKey(nil, device, folder), nil)
|
2016-07-23 12:46:31 +00:00
|
|
|
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) {
|
2016-07-23 12:46:31 +00:00
|
|
|
bs, _ := id.Marshal() // marshalling can't fail
|
2019-05-02 09:15:00 +00:00
|
|
|
if err := db.Put(db.keyer.GenerateIndexIDKey(nil, device, folder), bs, nil); err != nil && err != leveldb.ErrClosed {
|
2016-07-23 12:46:31 +00:00
|
|
|
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()
|
|
|
|
|
2019-01-19 19:26:46 +00:00
|
|
|
t.deleteKeyPrefix(prefix)
|
2016-08-05 07:13:52 +00:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
2019-01-18 21:19:56 +00:00
|
|
|
|
|
|
|
// unchanged checks if two files are the same and thus don't need to be updated.
|
|
|
|
// Local flags or the invalid bit might change without the version
|
|
|
|
// being bumped. The IsInvalid() method handles both.
|
|
|
|
func unchanged(nf, ef FileIntf) bool {
|
|
|
|
return ef.FileVersion().Equal(nf.FileVersion()) && ef.IsInvalid() == nf.IsInvalid()
|
|
|
|
}
|