2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// 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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-09-04 06:31:38 +00:00
|
|
|
|
2014-12-06 13:23:10 +00:00
|
|
|
//go:generate -command genxdr go run ../../Godeps/_workspace/src/github.com/calmh/xdr/cmd/genxdr/main.go
|
|
|
|
//go:generate genxdr -o leveldb_xdr.go leveldb.go
|
|
|
|
|
2015-01-12 13:50:30 +00:00
|
|
|
package db
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-10-09 08:44:18 +00:00
|
|
|
"fmt"
|
2014-08-07 20:56:30 +00:00
|
|
|
"runtime"
|
2014-07-06 12:46:48 +00:00
|
|
|
"sort"
|
|
|
|
|
2015-01-13 12:22:56 +00:00
|
|
|
"github.com/syncthing/protocol"
|
2015-04-22 22:54:31 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/sync"
|
2014-07-06 12:46:48 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2014-07-15 11:04:37 +00:00
|
|
|
var (
|
2015-01-18 01:12:06 +00:00
|
|
|
clockTick int64
|
2015-04-28 20:32:10 +00:00
|
|
|
clockMut = sync.NewMutex()
|
2014-07-15 11:04:37 +00:00
|
|
|
)
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
func clock(v int64) int64 {
|
2014-07-15 11:04:37 +00:00
|
|
|
clockMut.Lock()
|
|
|
|
defer clockMut.Unlock()
|
|
|
|
if v > clockTick {
|
|
|
|
clockTick = v + 1
|
|
|
|
} else {
|
|
|
|
clockTick++
|
|
|
|
}
|
|
|
|
return clockTick
|
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
const (
|
2015-01-17 19:53:33 +00:00
|
|
|
KeyTypeDevice = iota
|
|
|
|
KeyTypeGlobal
|
|
|
|
KeyTypeBlock
|
|
|
|
KeyTypeDeviceStatistic
|
|
|
|
KeyTypeFolderStatistic
|
2015-05-13 14:57:29 +00:00
|
|
|
KeyTypeVirtualMtime
|
2014-07-06 12:46:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type fileVersion struct {
|
2015-03-25 21:37:35 +00:00
|
|
|
version protocol.Vector
|
2014-09-28 11:05:25 +00:00
|
|
|
device []byte
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type versionList struct {
|
|
|
|
versions []fileVersion
|
|
|
|
}
|
|
|
|
|
2014-10-09 08:44:18 +00:00
|
|
|
func (l versionList) String() string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
var id protocol.DeviceID
|
|
|
|
b.WriteString("{")
|
|
|
|
for i, v := range l.versions {
|
|
|
|
if i > 0 {
|
|
|
|
b.WriteString(", ")
|
|
|
|
}
|
|
|
|
copy(id[:], v.device)
|
|
|
|
fmt.Fprintf(&b, "{%d, %v}", v.version, id)
|
|
|
|
}
|
|
|
|
b.WriteString("}")
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
type fileList []protocol.FileInfo
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
func (l fileList) Len() int {
|
|
|
|
return len(l)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l fileList) Swap(a, b int) {
|
|
|
|
l[a], l[b] = l[b], l[a]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l fileList) Less(a, b int) bool {
|
|
|
|
return l[a].Name < l[b].Name
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbReader interface {
|
|
|
|
Get([]byte, *opt.ReadOptions) ([]byte, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type dbWriter interface {
|
|
|
|
Put([]byte, []byte)
|
|
|
|
Delete([]byte)
|
|
|
|
}
|
|
|
|
|
2015-02-10 19:12:17 +00:00
|
|
|
// Flush batches to disk when they contain this many records.
|
|
|
|
const batchFlushSize = 64
|
|
|
|
|
2014-10-07 13:05:04 +00:00
|
|
|
// deviceKey returns a byte slice encoding the following information:
|
|
|
|
// keyTypeDevice (1 byte)
|
|
|
|
// folder (64 bytes)
|
|
|
|
// device (32 bytes)
|
|
|
|
// name (variable size)
|
2014-09-28 11:00:38 +00:00
|
|
|
func deviceKey(folder, device, file []byte) []byte {
|
2015-05-24 11:08:10 +00:00
|
|
|
return deviceKeyInto(nil, folder, device, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func deviceKeyInto(k []byte, folder, device, file []byte) []byte {
|
|
|
|
reqLen := 1 + 64 + 32 + len(file)
|
|
|
|
if len(k) < reqLen {
|
|
|
|
k = make([]byte, reqLen)
|
|
|
|
}
|
2015-01-17 19:53:33 +00:00
|
|
|
k[0] = KeyTypeDevice
|
2014-10-07 13:05:04 +00:00
|
|
|
if len(folder) > 64 {
|
|
|
|
panic("folder name too long")
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
copy(k[1:], []byte(folder))
|
|
|
|
copy(k[1+64:], device[:])
|
2014-07-06 12:46:48 +00:00
|
|
|
copy(k[1+64+32:], []byte(file))
|
2015-05-24 11:08:10 +00:00
|
|
|
return k[:reqLen]
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func deviceKeyName(key []byte) []byte {
|
2014-07-06 12:46:48 +00:00
|
|
|
return key[1+64+32:]
|
|
|
|
}
|
2014-10-07 13:05:04 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func deviceKeyFolder(key []byte) []byte {
|
|
|
|
folder := key[1 : 1+64]
|
|
|
|
izero := bytes.IndexByte(folder, 0)
|
2014-10-07 13:05:04 +00:00
|
|
|
if izero < 0 {
|
|
|
|
return folder
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
return folder[:izero]
|
2014-07-17 09:00:54 +00:00
|
|
|
}
|
2014-10-07 13:05:04 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func deviceKeyDevice(key []byte) []byte {
|
2014-07-17 09:00:54 +00:00
|
|
|
return key[1+64 : 1+64+32]
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2014-10-07 13:05:04 +00:00
|
|
|
// globalKey returns a byte slice encoding the following information:
|
|
|
|
// keyTypeGlobal (1 byte)
|
|
|
|
// folder (64 bytes)
|
|
|
|
// name (variable size)
|
|
|
|
func globalKey(folder, file []byte) []byte {
|
|
|
|
k := make([]byte, 1+64+len(file))
|
2015-01-17 19:53:33 +00:00
|
|
|
k[0] = KeyTypeGlobal
|
2014-10-07 13:05:04 +00:00
|
|
|
if len(folder) > 64 {
|
|
|
|
panic("folder name too long")
|
|
|
|
}
|
|
|
|
copy(k[1:], []byte(folder))
|
|
|
|
copy(k[1+64:], []byte(file))
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
func globalKeyName(key []byte) []byte {
|
|
|
|
return key[1+64:]
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func globalKeyFolder(key []byte) []byte {
|
|
|
|
folder := key[1 : 1+64]
|
|
|
|
izero := bytes.IndexByte(folder, 0)
|
2014-10-07 13:05:04 +00:00
|
|
|
if izero < 0 {
|
|
|
|
return folder
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
return folder[:izero]
|
2014-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
type deletionHandler func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) int64
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo, deleteFn deletionHandler) int64 {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-07 20:56:30 +00:00
|
|
|
|
2014-10-10 21:12:01 +00:00
|
|
|
sort.Sort(fileList(fs)) // sort list on name, same as in the database
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
start := deviceKey(folder, device, nil) // before all folder/device files
|
|
|
|
limit := deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
batch := new(leveldb.Batch)
|
2014-10-30 19:32:54 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("new batch %p", batch)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
moreDb := dbi.Next()
|
|
|
|
fsi := 0
|
2015-01-18 01:12:06 +00:00
|
|
|
var maxLocalVer int64
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
var newName, oldName []byte
|
|
|
|
moreFs := fsi < len(fs)
|
|
|
|
|
|
|
|
if !moreDb && !moreFs {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if moreFs {
|
|
|
|
newName = []byte(fs[fsi].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if moreDb {
|
2014-09-28 11:00:38 +00:00
|
|
|
oldName = deviceKeyName(dbi.Key())
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cmp := bytes.Compare(newName, oldName)
|
|
|
|
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("generic replace; folder=%q device=%v moreFs=%v moreDb=%v cmp=%d newName=%q oldName=%q", folder, protocol.DeviceIDFromBytes(device), moreFs, moreDb, cmp, newName, oldName)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case moreFs && (!moreDb || cmp == -1):
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugln("generic replace; missing - insert")
|
|
|
|
}
|
2014-10-10 21:12:01 +00:00
|
|
|
// Database is missing this file. Insert it.
|
|
|
|
if lv := ldbInsert(batch, folder, device, fs[fsi]); lv > maxLocalVer {
|
2014-07-15 11:04:37 +00:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
if fs[fsi].IsInvalid() {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, newName)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, newName, fs[fsi].Version)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
fsi++
|
|
|
|
|
2014-07-14 22:06:54 +00:00
|
|
|
case moreFs && moreDb && cmp == 0:
|
2014-09-04 20:29:53 +00:00
|
|
|
// File exists on both sides - compare versions. We might get an
|
2014-09-28 11:00:38 +00:00
|
|
|
// update with the same version and different flags if a device has
|
2014-09-04 20:29:53 +00:00
|
|
|
// marked a file as invalid, so handle that too.
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugln("generic replace; exists - compare")
|
|
|
|
}
|
2015-01-09 07:19:32 +00:00
|
|
|
var ef FileInfoTruncated
|
2014-07-06 12:46:48 +00:00
|
|
|
ef.UnmarshalXDR(dbi.Value())
|
2015-03-25 21:37:35 +00:00
|
|
|
if !fs[fsi].Version.Equal(ef.Version) || fs[fsi].Flags != ef.Flags {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugln("generic replace; differs - insert")
|
|
|
|
}
|
2014-10-10 21:12:01 +00:00
|
|
|
if lv := ldbInsert(batch, folder, device, fs[fsi]); lv > maxLocalVer {
|
2014-07-15 11:04:37 +00:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
if fs[fsi].IsInvalid() {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, newName)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, newName, fs[fsi].Version)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
} else if debugDB {
|
|
|
|
l.Debugln("generic replace; equal - ignore")
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
fsi++
|
|
|
|
moreDb = dbi.Next()
|
|
|
|
|
|
|
|
case moreDb && (!moreFs || cmp == 1):
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugln("generic replace; exists - remove")
|
|
|
|
}
|
2014-10-10 21:12:01 +00:00
|
|
|
if lv := deleteFn(snap, batch, folder, device, oldName, dbi); lv > maxLocalVer {
|
|
|
|
maxLocalVer = lv
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
moreDb = dbi.Next()
|
|
|
|
}
|
2015-02-10 19:12:17 +00:00
|
|
|
|
|
|
|
// Write out and reuse the batch every few records, to avoid the batch
|
|
|
|
// growing too large and thus allocating unnecessarily much memory.
|
|
|
|
if batch.Len() > batchFlushSize {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("db.Write %p", batch)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Write(batch, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Reset()
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("db.Write %p", batch)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
err = db.Write(batch, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-15 11:04:37 +00:00
|
|
|
return maxLocalVer
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
func ldbReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) int64 {
|
2014-07-15 11:04:37 +00:00
|
|
|
// TODO: Return the remaining maxLocalVer?
|
2015-01-18 01:12:06 +00:00
|
|
|
return ldbGenericReplace(db, folder, device, fs, func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) int64 {
|
2014-10-10 21:12:01 +00:00
|
|
|
// Database has a file that we are missing. Remove it.
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("delete; folder=%q device=%v name=%q", folder, protocol.DeviceIDFromBytes(device), name)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbRemoveFromGlobal(db, batch, folder, device, name)
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("batch.Delete %p %x", batch, dbi.Key())
|
|
|
|
}
|
2014-07-15 15:54:00 +00:00
|
|
|
batch.Delete(dbi.Key())
|
2014-07-15 11:04:37 +00:00
|
|
|
return 0
|
2014-07-06 12:46:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-03-25 21:37:35 +00:00
|
|
|
func ldbReplaceWithDelete(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo, myID uint64) int64 {
|
2015-05-13 14:57:29 +00:00
|
|
|
mtimeRepo := NewVirtualMtimeRepo(db, string(folder))
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
return ldbGenericReplace(db, folder, device, fs, func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) int64 {
|
2015-01-09 07:19:32 +00:00
|
|
|
var tf FileInfoTruncated
|
2014-08-12 11:53:31 +00:00
|
|
|
err := tf.UnmarshalXDR(dbi.Value())
|
2014-07-06 12:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-08-12 11:53:31 +00:00
|
|
|
if !tf.IsDeleted() {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("mark deleted; folder=%q device=%v name=%q", folder, protocol.DeviceIDFromBytes(device), name)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2014-08-12 11:53:31 +00:00
|
|
|
ts := clock(tf.LocalVersion)
|
|
|
|
f := protocol.FileInfo{
|
|
|
|
Name: tf.Name,
|
2015-03-25 21:37:35 +00:00
|
|
|
Version: tf.Version.Update(myID),
|
2014-08-12 11:53:31 +00:00
|
|
|
LocalVersion: ts,
|
|
|
|
Flags: tf.Flags | protocol.FlagDeleted,
|
|
|
|
Modified: tf.Modified,
|
|
|
|
}
|
2014-10-21 06:40:05 +00:00
|
|
|
bs, _ := f.MarshalXDR()
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("batch.Put %p %x", batch, dbi.Key())
|
|
|
|
}
|
2014-10-21 06:40:05 +00:00
|
|
|
batch.Put(dbi.Key(), bs)
|
2015-05-13 14:57:29 +00:00
|
|
|
mtimeRepo.DeleteMtime(tf.Name)
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbUpdateGlobal(db, batch, folder, device, deviceKeyName(dbi.Key()), f.Version)
|
2014-07-15 11:04:37 +00:00
|
|
|
return ts
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2014-07-15 11:04:37 +00:00
|
|
|
return 0
|
2014-07-06 12:46:48 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
func ldbUpdate(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) int64 {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-07 20:56:30 +00:00
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
batch := new(leveldb.Batch)
|
2014-10-30 19:32:54 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("new batch %p", batch)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
var maxLocalVer int64
|
2015-05-24 11:08:10 +00:00
|
|
|
var fk []byte
|
2014-07-06 12:46:48 +00:00
|
|
|
for _, f := range fs {
|
|
|
|
name := []byte(f.Name)
|
2015-05-24 11:08:10 +00:00
|
|
|
fk = deviceKeyInto(fk[:cap(fk)], folder, device, name)
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("snap.Get %p %x", snap, fk)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
bs, err := snap.Get(fk, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2014-10-10 21:12:01 +00:00
|
|
|
if lv := ldbInsert(batch, folder, device, f); lv > maxLocalVer {
|
2014-07-15 11:04:37 +00:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
if f.IsInvalid() {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, name)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, name, f.Version)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-01-09 07:19:32 +00:00
|
|
|
var ef FileInfoTruncated
|
2014-07-06 12:46:48 +00:00
|
|
|
err = ef.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
// Flags might change without the version being bumped when we set the
|
|
|
|
// invalid flag on an existing file.
|
2015-03-25 21:37:35 +00:00
|
|
|
if !ef.Version.Equal(f.Version) || ef.Flags != f.Flags {
|
2014-10-10 21:12:01 +00:00
|
|
|
if lv := ldbInsert(batch, folder, device, f); lv > maxLocalVer {
|
2014-07-15 11:04:37 +00:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 20:29:53 +00:00
|
|
|
if f.IsInvalid() {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, name)
|
2014-09-04 20:29:53 +00:00
|
|
|
} else {
|
2014-09-28 11:00:38 +00:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, name, f.Version)
|
2014-09-04 20:29:53 +00:00
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2015-02-10 19:12:17 +00:00
|
|
|
|
|
|
|
// Write out and reuse the batch every few records, to avoid the batch
|
|
|
|
// growing too large and thus allocating unnecessarily much memory.
|
|
|
|
if batch.Len() > batchFlushSize {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("db.Write %p", batch)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Write(batch, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
batch.Reset()
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("db.Write %p", batch)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
err = db.Write(batch, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-15 11:04:37 +00:00
|
|
|
return maxLocalVer
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-18 01:12:06 +00:00
|
|
|
func ldbInsert(batch dbWriter, folder, device []byte, file protocol.FileInfo) int64 {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("insert; folder=%q device=%v %v", folder, protocol.DeviceIDFromBytes(device), file)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-07-15 11:04:37 +00:00
|
|
|
if file.LocalVersion == 0 {
|
|
|
|
file.LocalVersion = clock(0)
|
|
|
|
}
|
|
|
|
|
2014-10-10 21:12:01 +00:00
|
|
|
name := []byte(file.Name)
|
2014-09-28 11:00:38 +00:00
|
|
|
nk := deviceKey(folder, device, name)
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("batch.Put %p %x", batch, nk)
|
|
|
|
}
|
2014-10-21 06:40:05 +00:00
|
|
|
batch.Put(nk, file.MustMarshalXDR())
|
2014-07-15 11:04:37 +00:00
|
|
|
|
|
|
|
return file.LocalVersion
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// ldbUpdateGlobal adds this device+version to the version list for the given
|
|
|
|
// file. If the device is already present in the list, the version is updated.
|
2014-07-06 12:46:48 +00:00
|
|
|
// If the file does not have an entry in the global list, it is created.
|
2015-03-25 21:37:35 +00:00
|
|
|
func ldbUpdateGlobal(db dbReader, batch dbWriter, folder, device, file []byte, version protocol.Vector) bool {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("update global; folder=%q device=%v file=%q version=%d", folder, protocol.DeviceIDFromBytes(device), file, version)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
gk := globalKey(folder, file)
|
2014-07-06 12:46:48 +00:00
|
|
|
svl, err := db.Get(gk, nil)
|
|
|
|
if err != nil && err != leveldb.ErrNotFound {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var fl versionList
|
2015-03-25 21:37:35 +00:00
|
|
|
|
|
|
|
// Remove the device from the current version list
|
2014-07-06 12:46:48 +00:00
|
|
|
if svl != nil {
|
|
|
|
err = fl.UnmarshalXDR(svl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range fl.versions {
|
2014-09-28 11:00:38 +00:00
|
|
|
if bytes.Compare(fl.versions[i].device, device) == 0 {
|
2015-03-25 21:37:35 +00:00
|
|
|
if fl.versions[i].version.Equal(version) {
|
2014-07-06 12:46:48 +00:00
|
|
|
// No need to do anything
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fl.versions = append(fl.versions[:i], fl.versions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 21:37:35 +00:00
|
|
|
nv := fileVersion{
|
|
|
|
device: device,
|
|
|
|
version: version,
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
for i := range fl.versions {
|
2015-03-25 21:37:35 +00:00
|
|
|
// We compare against ConcurrentLesser as well here because we need
|
|
|
|
// to enforce a consistent ordering of versions even in the case of
|
|
|
|
// conflicts.
|
|
|
|
if comp := fl.versions[i].version.Compare(version); comp == protocol.Equal || comp == protocol.Lesser || comp == protocol.ConcurrentLesser {
|
2014-07-06 12:46:48 +00:00
|
|
|
t := append(fl.versions, fileVersion{})
|
|
|
|
copy(t[i+1:], t[i:])
|
|
|
|
t[i] = nv
|
|
|
|
fl.versions = t
|
|
|
|
goto done
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fl.versions = append(fl.versions, nv)
|
|
|
|
|
|
|
|
done:
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("batch.Put %p %x", batch, gk)
|
|
|
|
l.Debugf("new global after update: %v", fl)
|
|
|
|
}
|
2014-10-21 06:40:05 +00:00
|
|
|
batch.Put(gk, fl.MustMarshalXDR())
|
2014-07-06 12:46:48 +00:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// ldbRemoveFromGlobal removes the device from the global version list for the
|
2014-07-06 12:46:48 +00:00
|
|
|
// given file. If the version list is empty after this, the file entry is
|
|
|
|
// removed entirely.
|
2014-09-28 11:00:38 +00:00
|
|
|
func ldbRemoveFromGlobal(db dbReader, batch dbWriter, folder, device, file []byte) {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
gk := globalKey(folder, file)
|
2014-07-06 12:46:48 +00:00
|
|
|
svl, err := db.Get(gk, nil)
|
|
|
|
if err != nil {
|
2014-09-04 20:29:53 +00:00
|
|
|
// We might be called to "remove" a global version that doesn't exist
|
|
|
|
// if the first update for the file is already marked invalid.
|
|
|
|
return
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var fl versionList
|
|
|
|
err = fl.UnmarshalXDR(svl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range fl.versions {
|
2014-09-28 11:00:38 +00:00
|
|
|
if bytes.Compare(fl.versions[i].device, device) == 0 {
|
2014-07-06 12:46:48 +00:00
|
|
|
fl.versions = append(fl.versions[:i], fl.versions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fl.versions) == 0 {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("batch.Delete %p %x", batch, gk)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
batch.Delete(gk)
|
|
|
|
} else {
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("batch.Put %p %x", batch, gk)
|
|
|
|
l.Debugf("new global after remove: %v", fl)
|
|
|
|
}
|
2014-10-21 06:40:05 +00:00
|
|
|
batch.Put(gk, fl.MustMarshalXDR())
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 07:18:42 +00:00
|
|
|
func ldbWithHave(db *leveldb.DB, folder, device []byte, truncate bool, fn Iterator) {
|
2014-09-28 11:00:38 +00:00
|
|
|
start := deviceKey(folder, device, nil) // before all folder/device files
|
|
|
|
limit := deviceKey(folder, device, []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
2014-07-06 12:46:48 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
2014-08-12 11:53:31 +00:00
|
|
|
f, err := unmarshalTrunc(dbi.Value(), truncate)
|
2014-07-06 12:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if cont := fn(f); !cont {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-09 07:19:32 +00:00
|
|
|
func ldbWithAllFolderTruncated(db *leveldb.DB, folder []byte, fn func(device []byte, f FileInfoTruncated) bool) {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-07 20:56:30 +00:00
|
|
|
|
2014-09-28 11:05:25 +00:00
|
|
|
start := deviceKey(folder, nil, nil) // before all folder/device files
|
2014-09-28 11:00:38 +00:00
|
|
|
limit := deviceKey(folder, protocol.LocalDeviceID[:], []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
2014-07-17 09:00:54 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
2014-07-17 09:00:54 +00:00
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
2014-09-28 11:00:38 +00:00
|
|
|
device := deviceKeyDevice(dbi.Key())
|
2015-01-09 07:19:32 +00:00
|
|
|
var f FileInfoTruncated
|
2014-07-17 09:00:54 +00:00
|
|
|
err := f.UnmarshalXDR(dbi.Value())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-07 14:44:36 +00:00
|
|
|
|
2015-01-13 11:28:35 +00:00
|
|
|
switch f.Name {
|
|
|
|
case "", ".", "..", "/": // A few obviously invalid filenames
|
|
|
|
l.Infof("Dropping invalid filename %q from database", f.Name)
|
2015-01-07 14:44:36 +00:00
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
ldbRemoveFromGlobal(db, batch, folder, device, nil)
|
|
|
|
batch.Delete(dbi.Key())
|
|
|
|
db.Write(batch, nil)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
if cont := fn(device, f); !cont {
|
2014-07-17 09:00:54 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-06 21:12:45 +00:00
|
|
|
func ldbGet(db *leveldb.DB, folder, device, file []byte) (protocol.FileInfo, bool) {
|
2014-09-28 11:00:38 +00:00
|
|
|
nk := deviceKey(folder, device, file)
|
2014-07-06 12:46:48 +00:00
|
|
|
bs, err := db.Get(nk, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2015-01-06 21:12:45 +00:00
|
|
|
return protocol.FileInfo{}, false
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:06:48 +00:00
|
|
|
var f protocol.FileInfo
|
2014-07-06 12:46:48 +00:00
|
|
|
err = f.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-06 21:12:45 +00:00
|
|
|
return f, true
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:41:02 +00:00
|
|
|
func ldbGetGlobal(db *leveldb.DB, folder, file []byte, truncate bool) (FileIntf, bool) {
|
2014-09-28 11:00:38 +00:00
|
|
|
k := globalKey(folder, file)
|
2014-07-06 12:46:48 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("snap.Get %p %x", snap, k)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
bs, err := snap.Get(k, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2015-01-09 07:41:02 +00:00
|
|
|
return nil, false
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vl versionList
|
|
|
|
err = vl.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(vl.versions) == 0 {
|
|
|
|
l.Debugln(k)
|
|
|
|
panic("no versions?")
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
k = deviceKey(folder, vl.versions[0].device, file)
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("snap.Get %p %x", snap, k)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
bs, err = snap.Get(k, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2015-01-09 07:41:02 +00:00
|
|
|
fi, err := unmarshalTrunc(bs, truncate)
|
2014-07-06 12:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2015-01-09 07:41:02 +00:00
|
|
|
return fi, true
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-02-07 10:52:42 +00:00
|
|
|
func ldbWithGlobal(db *leveldb.DB, folder, prefix []byte, truncate bool, fn Iterator) {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-07 20:56:30 +00:00
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
2015-02-07 10:52:42 +00:00
|
|
|
dbi := snap.NewIterator(util.BytesPrefix(globalKey(folder, prefix)), nil)
|
2014-07-06 12:46:48 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
2015-05-24 11:08:10 +00:00
|
|
|
var fk []byte
|
2014-07-06 12:46:48 +00:00
|
|
|
for dbi.Next() {
|
|
|
|
var vl versionList
|
|
|
|
err := vl.UnmarshalXDR(dbi.Value())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(vl.versions) == 0 {
|
|
|
|
l.Debugln(dbi.Key())
|
|
|
|
panic("no versions?")
|
|
|
|
}
|
2014-10-09 08:44:18 +00:00
|
|
|
name := globalKeyName(dbi.Key())
|
2015-05-24 11:08:10 +00:00
|
|
|
fk = deviceKeyInto(fk[:cap(fk)], folder, vl.versions[0].device, name)
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("snap.Get %p %x", snap, fk)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
bs, err := snap.Get(fk, nil)
|
|
|
|
if err != nil {
|
2014-10-09 08:44:18 +00:00
|
|
|
l.Debugf("folder: %q (%x)", folder, folder)
|
|
|
|
l.Debugf("key: %q (%x)", dbi.Key(), dbi.Key())
|
|
|
|
l.Debugf("vl: %v", vl)
|
2014-10-30 13:28:09 +00:00
|
|
|
l.Debugf("vl.versions[0].device: %x", vl.versions[0].device)
|
2014-10-09 08:44:18 +00:00
|
|
|
l.Debugf("name: %q (%x)", name, name)
|
2014-10-30 13:28:09 +00:00
|
|
|
l.Debugf("fk: %q", fk)
|
|
|
|
l.Debugf("fk: %x %x %x", fk[1:1+64], fk[1+64:1+64+32], fk[1+64+32:])
|
2014-07-06 12:46:48 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-08-12 11:53:31 +00:00
|
|
|
f, err := unmarshalTrunc(bs, truncate)
|
2014-07-06 12:46:48 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cont := fn(f); !cont {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func ldbAvailability(db *leveldb.DB, folder, file []byte) []protocol.DeviceID {
|
|
|
|
k := globalKey(folder, file)
|
2014-07-06 12:46:48 +00:00
|
|
|
bs, err := db.Get(k, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vl versionList
|
|
|
|
err = vl.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
var devices []protocol.DeviceID
|
2014-07-06 12:46:48 +00:00
|
|
|
for _, v := range vl.versions {
|
2015-03-25 21:37:35 +00:00
|
|
|
if !v.version.Equal(vl.versions[0].version) {
|
2014-07-06 12:46:48 +00:00
|
|
|
break
|
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
n := protocol.DeviceIDFromBytes(v.device)
|
|
|
|
devices = append(devices, n)
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
return devices
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 07:18:42 +00:00
|
|
|
func ldbWithNeed(db *leveldb.DB, folder, device []byte, truncate bool, fn Iterator) {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-07 20:56:30 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
start := globalKey(folder, nil)
|
|
|
|
limit := globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
2014-07-06 12:46:48 +00:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
2014-07-06 12:46:48 +00:00
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
2015-05-24 11:08:10 +00:00
|
|
|
var fk []byte
|
2015-03-25 21:37:35 +00:00
|
|
|
nextFile:
|
2014-07-06 12:46:48 +00:00
|
|
|
for dbi.Next() {
|
|
|
|
var vl versionList
|
|
|
|
err := vl.UnmarshalXDR(dbi.Value())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if len(vl.versions) == 0 {
|
|
|
|
l.Debugln(dbi.Key())
|
|
|
|
panic("no versions?")
|
|
|
|
}
|
|
|
|
|
|
|
|
have := false // If we have the file, any version
|
|
|
|
need := false // If we have a lower version of the file
|
2015-03-25 21:37:35 +00:00
|
|
|
var haveVersion protocol.Vector
|
2014-07-06 12:46:48 +00:00
|
|
|
for _, v := range vl.versions {
|
2014-09-28 11:00:38 +00:00
|
|
|
if bytes.Compare(v.device, device) == 0 {
|
2014-07-06 12:46:48 +00:00
|
|
|
have = true
|
|
|
|
haveVersion = v.version
|
2015-03-25 21:37:35 +00:00
|
|
|
// XXX: This marks Concurrent (i.e. conflicting) changes as
|
|
|
|
// needs. Maybe we should do that, but it needs special
|
|
|
|
// handling in the puller.
|
|
|
|
need = !v.version.GreaterEqual(vl.versions[0].version)
|
2014-07-06 12:46:48 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if need || !have {
|
|
|
|
name := globalKeyName(dbi.Key())
|
2014-09-01 07:07:51 +00:00
|
|
|
needVersion := vl.versions[0].version
|
2015-03-25 21:37:35 +00:00
|
|
|
|
|
|
|
nextVersion:
|
2014-09-01 07:07:51 +00:00
|
|
|
for i := range vl.versions {
|
2015-03-25 21:37:35 +00:00
|
|
|
if !vl.versions[i].version.Equal(needVersion) {
|
2014-09-01 07:07:51 +00:00
|
|
|
// We haven't found a valid copy of the file with the needed version.
|
2015-03-25 21:37:35 +00:00
|
|
|
continue nextFile
|
2014-09-01 07:07:51 +00:00
|
|
|
}
|
2015-05-24 11:08:10 +00:00
|
|
|
fk = deviceKeyInto(fk[:cap(fk)], folder, vl.versions[i].device, name)
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("snap.Get %p %x", snap, fk)
|
|
|
|
}
|
2014-09-01 07:07:51 +00:00
|
|
|
bs, err := snap.Get(fk, nil)
|
|
|
|
if err != nil {
|
2014-10-09 08:44:18 +00:00
|
|
|
var id protocol.DeviceID
|
|
|
|
copy(id[:], device)
|
|
|
|
l.Debugf("device: %v", id)
|
|
|
|
l.Debugf("need: %v, have: %v", need, have)
|
|
|
|
l.Debugf("key: %q (%x)", dbi.Key(), dbi.Key())
|
|
|
|
l.Debugf("vl: %v", vl)
|
|
|
|
l.Debugf("i: %v", i)
|
|
|
|
l.Debugf("fk: %q (%x)", fk, fk)
|
|
|
|
l.Debugf("name: %q (%x)", name, name)
|
2014-09-01 07:07:51 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2014-09-01 07:07:51 +00:00
|
|
|
gf, err := unmarshalTrunc(bs, truncate)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2014-09-01 07:07:51 +00:00
|
|
|
if gf.IsInvalid() {
|
|
|
|
// The file is marked invalid for whatever reason, don't use it.
|
2015-03-25 21:37:35 +00:00
|
|
|
continue nextVersion
|
2014-09-01 07:07:51 +00:00
|
|
|
}
|
2014-07-06 12:46:48 +00:00
|
|
|
|
2014-09-01 07:07:51 +00:00
|
|
|
if gf.IsDeleted() && !have {
|
|
|
|
// We don't need deleted files that we don't have
|
2015-03-25 21:37:35 +00:00
|
|
|
continue nextFile
|
2014-09-01 07:07:51 +00:00
|
|
|
}
|
2014-07-29 09:06:52 +00:00
|
|
|
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Debugf("need folder=%q device=%v name=%q need=%v have=%v haveV=%d globalV=%d", folder, protocol.DeviceIDFromBytes(device), name, need, have, haveVersion, vl.versions[0].version)
|
2014-09-01 07:07:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cont := fn(gf); !cont {
|
|
|
|
return
|
|
|
|
}
|
2014-09-08 07:37:42 +00:00
|
|
|
|
|
|
|
// This file is handled, no need to look further in the version list
|
2015-03-25 21:37:35 +00:00
|
|
|
continue nextFile
|
2014-07-06 12:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 11:53:31 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func ldbListFolders(db *leveldb.DB) []string {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-31 11:34:17 +00:00
|
|
|
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
2015-01-17 19:53:33 +00:00
|
|
|
dbi := snap.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
2014-08-31 11:34:17 +00:00
|
|
|
defer dbi.Release()
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
folderExists := make(map[string]bool)
|
2014-08-31 11:34:17 +00:00
|
|
|
for dbi.Next() {
|
2014-09-28 11:00:38 +00:00
|
|
|
folder := string(globalKeyFolder(dbi.Key()))
|
|
|
|
if !folderExists[folder] {
|
|
|
|
folderExists[folder] = true
|
2014-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
folders := make([]string, 0, len(folderExists))
|
|
|
|
for k := range folderExists {
|
|
|
|
folders = append(folders, k)
|
2014-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
sort.Strings(folders)
|
|
|
|
return folders
|
2014-08-31 11:34:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
func ldbDropFolder(db *leveldb.DB, folder []byte) {
|
2014-09-18 08:04:37 +00:00
|
|
|
runtime.GC()
|
2014-08-31 11:34:17 +00:00
|
|
|
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-10-30 13:28:09 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
2014-08-31 11:34:17 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// Remove all items related to the given folder from the device->file bucket
|
2015-01-17 19:53:33 +00:00
|
|
|
dbi := snap.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
|
2014-08-31 11:34:17 +00:00
|
|
|
for dbi.Next() {
|
2014-09-28 11:00:38 +00:00
|
|
|
itemFolder := deviceKeyFolder(dbi.Key())
|
|
|
|
if bytes.Compare(folder, itemFolder) == 0 {
|
2014-08-31 11:34:17 +00:00
|
|
|
db.Delete(dbi.Key(), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbi.Release()
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
// Remove all items related to the given folder from the global bucket
|
2015-01-17 19:53:33 +00:00
|
|
|
dbi = snap.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
|
2014-08-31 11:34:17 +00:00
|
|
|
for dbi.Next() {
|
2014-09-28 11:00:38 +00:00
|
|
|
itemFolder := globalKeyFolder(dbi.Key())
|
|
|
|
if bytes.Compare(folder, itemFolder) == 0 {
|
2014-08-31 11:34:17 +00:00
|
|
|
db.Delete(dbi.Key(), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbi.Release()
|
|
|
|
}
|
|
|
|
|
2015-01-09 07:18:42 +00:00
|
|
|
func unmarshalTrunc(bs []byte, truncate bool) (FileIntf, error) {
|
2014-08-12 11:53:31 +00:00
|
|
|
if truncate {
|
2015-01-09 07:19:32 +00:00
|
|
|
var tf FileInfoTruncated
|
2014-08-12 11:53:31 +00:00
|
|
|
err := tf.UnmarshalXDR(bs)
|
|
|
|
return tf, err
|
|
|
|
}
|
2015-04-28 20:32:10 +00:00
|
|
|
|
|
|
|
var tf protocol.FileInfo
|
|
|
|
err := tf.UnmarshalXDR(bs)
|
|
|
|
return tf, err
|
2014-08-12 11:53:31 +00:00
|
|
|
}
|
2014-10-30 15:48:14 +00:00
|
|
|
|
|
|
|
func ldbCheckGlobals(db *leveldb.DB, folder []byte) {
|
|
|
|
defer runtime.GC()
|
|
|
|
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("created snapshot %p", snap)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("close snapshot %p", snap)
|
|
|
|
}
|
|
|
|
snap.Release()
|
|
|
|
}()
|
|
|
|
|
|
|
|
start := globalKey(folder, nil)
|
|
|
|
limit := globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
2014-10-30 19:32:54 +00:00
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
if debugDB {
|
|
|
|
l.Debugf("new batch %p", batch)
|
|
|
|
}
|
2015-05-24 11:08:10 +00:00
|
|
|
|
|
|
|
var fk []byte
|
2014-10-30 15:48:14 +00:00
|
|
|
for dbi.Next() {
|
|
|
|
gk := dbi.Key()
|
|
|
|
var vl versionList
|
|
|
|
err := vl.UnmarshalXDR(dbi.Value())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
name := globalKeyName(gk)
|
|
|
|
var newVL versionList
|
|
|
|
for _, version := range vl.versions {
|
2015-05-24 11:08:10 +00:00
|
|
|
fk = deviceKeyInto(fk[:cap(fk)], folder, version.device, name)
|
2014-10-30 15:48:14 +00:00
|
|
|
if debugDB {
|
|
|
|
l.Debugf("snap.Get %p %x", snap, fk)
|
|
|
|
}
|
|
|
|
_, err := snap.Get(fk, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
newVL.versions = append(newVL.versions, version)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newVL.versions) != len(vl.versions) {
|
|
|
|
l.Infof("db repair: rewriting global version list for %x %x", gk[1:1+64], gk[1+64:])
|
|
|
|
batch.Put(dbi.Key(), newVL.MustMarshalXDR())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if debugDB {
|
|
|
|
l.Infoln("db check completed for %q", folder)
|
|
|
|
}
|
|
|
|
db.Write(batch, nil)
|
|
|
|
}
|