2014-09-03 23:31:38 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
|
|
// any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
// more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
2014-09-03 23:31:38 -07:00
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
package files
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-08-07 13:56:30 -07:00
|
|
|
"runtime"
|
2014-07-06 05:46:48 -07:00
|
|
|
"sort"
|
2014-07-15 04:04:37 -07:00
|
|
|
"sync"
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-09-22 12:42:11 -07:00
|
|
|
"github.com/syncthing/syncthing/internal/lamport"
|
|
|
|
"github.com/syncthing/syncthing/internal/protocol"
|
2014-07-06 05:46:48 -07: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 04:04:37 -07:00
|
|
|
var (
|
|
|
|
clockTick uint64
|
|
|
|
clockMut sync.Mutex
|
|
|
|
)
|
|
|
|
|
|
|
|
func clock(v uint64) uint64 {
|
|
|
|
clockMut.Lock()
|
|
|
|
defer clockMut.Unlock()
|
|
|
|
if v > clockTick {
|
|
|
|
clockTick = v + 1
|
|
|
|
} else {
|
|
|
|
clockTick++
|
|
|
|
}
|
|
|
|
return clockTick
|
|
|
|
}
|
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
const (
|
2014-09-28 04:00:38 -07:00
|
|
|
keyTypeDevice = iota
|
2014-07-06 05:46:48 -07:00
|
|
|
keyTypeGlobal
|
|
|
|
)
|
|
|
|
|
|
|
|
type fileVersion struct {
|
|
|
|
version uint64
|
2014-09-28 04:05:25 -07:00
|
|
|
device []byte
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type versionList struct {
|
|
|
|
versions []fileVersion
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
type fileList []protocol.FileInfo
|
2014-07-06 05:46:48 -07: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)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
keyTypeDevice (1 byte)
|
|
|
|
folder (64 bytes)
|
|
|
|
device (32 bytes)
|
2014-07-06 05:46:48 -07:00
|
|
|
name (variable size)
|
2014-09-22 12:42:11 -07:00
|
|
|
|
|
|
|
|
scanner.File
|
2014-07-06 05:46:48 -07:00
|
|
|
|
|
|
|
keyTypeGlobal (1 byte)
|
2014-09-28 04:00:38 -07:00
|
|
|
folder (64 bytes)
|
2014-07-06 05:46:48 -07:00
|
|
|
name (variable size)
|
|
|
|
|
|
|
|
|
[]fileVersion (sorted)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKey(folder, device, file []byte) []byte {
|
2014-07-06 05:46:48 -07:00
|
|
|
k := make([]byte, 1+64+32+len(file))
|
2014-09-28 04:00:38 -07:00
|
|
|
k[0] = keyTypeDevice
|
|
|
|
copy(k[1:], []byte(folder))
|
|
|
|
copy(k[1+64:], device[:])
|
2014-07-06 05:46:48 -07:00
|
|
|
copy(k[1+64+32:], []byte(file))
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func globalKey(folder, file []byte) []byte {
|
2014-07-06 05:46:48 -07:00
|
|
|
k := make([]byte, 1+64+len(file))
|
|
|
|
k[0] = keyTypeGlobal
|
2014-09-28 04:00:38 -07:00
|
|
|
copy(k[1:], []byte(folder))
|
2014-07-06 05:46:48 -07:00
|
|
|
copy(k[1+64:], []byte(file))
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKeyName(key []byte) []byte {
|
2014-07-06 05:46:48 -07:00
|
|
|
return key[1+64+32:]
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKeyFolder(key []byte) []byte {
|
|
|
|
folder := key[1 : 1+64]
|
|
|
|
izero := bytes.IndexByte(folder, 0)
|
|
|
|
return folder[:izero]
|
2014-07-17 02:00:54 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
func deviceKeyDevice(key []byte) []byte {
|
2014-07-17 02:00:54 -07:00
|
|
|
return key[1+64 : 1+64+32]
|
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
|
|
|
|
func globalKeyName(key []byte) []byte {
|
|
|
|
return key[1+64:]
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func globalKeyFolder(key []byte) []byte {
|
|
|
|
folder := key[1 : 1+64]
|
|
|
|
izero := bytes.IndexByte(folder, 0)
|
|
|
|
return folder[:izero]
|
2014-08-31 04:34:17 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
type deletionHandler func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) uint64
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-08-12 04:53:31 -07:00
|
|
|
type fileIterator func(f protocol.FileIntf) bool
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbGenericReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo, deleteFn deletionHandler) uint64 {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-07 13:56:30 -07:00
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
sort.Sort(fileList(fs)) // sort list on name, same as on disk
|
|
|
|
|
2014-09-28 04:00:38 -07: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 05:46:48 -07:00
|
|
|
|
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
moreDb := dbi.Next()
|
|
|
|
fsi := 0
|
2014-07-15 04:04:37 -07:00
|
|
|
var maxLocalVer uint64
|
2014-07-06 05:46:48 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
var newName, oldName []byte
|
|
|
|
moreFs := fsi < len(fs)
|
|
|
|
|
|
|
|
if !moreDb && !moreFs {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !moreFs && deleteFn == nil {
|
|
|
|
// We don't have any more updated files to process and deletion
|
|
|
|
// has not been requested, so we can exit early
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if moreFs {
|
|
|
|
newName = []byte(fs[fsi].Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if moreDb {
|
2014-09-28 04:00:38 -07:00
|
|
|
oldName = deviceKeyName(dbi.Key())
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
cmp := bytes.Compare(newName, oldName)
|
|
|
|
|
|
|
|
if debug {
|
2014-09-28 04:00:38 -07: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 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case moreFs && (!moreDb || cmp == -1):
|
|
|
|
// Disk is missing this file. Insert it.
|
2014-09-28 04:00:38 -07:00
|
|
|
if lv := ldbInsert(batch, folder, device, newName, fs[fsi]); lv > maxLocalVer {
|
2014-07-15 04:04:37 -07:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
if fs[fsi].IsInvalid() {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, newName)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, newName, fs[fsi].Version)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
fsi++
|
|
|
|
|
2014-07-14 15:06:54 -07:00
|
|
|
case moreFs && moreDb && cmp == 0:
|
2014-09-04 13:29:53 -07:00
|
|
|
// File exists on both sides - compare versions. We might get an
|
2014-09-28 04:00:38 -07:00
|
|
|
// update with the same version and different flags if a device has
|
2014-09-04 13:29:53 -07:00
|
|
|
// marked a file as invalid, so handle that too.
|
2014-08-12 04:53:31 -07:00
|
|
|
var ef protocol.FileInfoTruncated
|
2014-07-06 05:46:48 -07:00
|
|
|
ef.UnmarshalXDR(dbi.Value())
|
2014-09-04 13:29:53 -07:00
|
|
|
if fs[fsi].Version > ef.Version || fs[fsi].Version != ef.Version {
|
2014-09-28 04:00:38 -07:00
|
|
|
if lv := ldbInsert(batch, folder, device, newName, fs[fsi]); lv > maxLocalVer {
|
2014-07-15 04:04:37 -07:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
if fs[fsi].IsInvalid() {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, newName)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, newName, fs[fsi].Version)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
// Iterate both sides.
|
|
|
|
fsi++
|
|
|
|
moreDb = dbi.Next()
|
|
|
|
|
|
|
|
case moreDb && (!moreFs || cmp == 1):
|
|
|
|
if deleteFn != nil {
|
2014-09-28 04:00:38 -07:00
|
|
|
if lv := deleteFn(snap, batch, folder, device, oldName, dbi); lv > maxLocalVer {
|
2014-07-15 04:04:37 -07:00
|
|
|
maxLocalVer = lv
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
moreDb = dbi.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Write(batch, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-15 04:04:37 -07:00
|
|
|
return maxLocalVer
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbReplace(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) uint64 {
|
2014-07-15 04:04:37 -07:00
|
|
|
// TODO: Return the remaining maxLocalVer?
|
2014-09-28 04:00:38 -07:00
|
|
|
return ldbGenericReplace(db, folder, device, fs, func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) uint64 {
|
2014-07-06 05:46:48 -07:00
|
|
|
// Disk has files that we are missing. Remove it.
|
|
|
|
if debug {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Debugf("delete; folder=%q device=%v name=%q", folder, protocol.DeviceIDFromBytes(device), name)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbRemoveFromGlobal(db, batch, folder, device, name)
|
2014-07-15 08:54:00 -07:00
|
|
|
batch.Delete(dbi.Key())
|
2014-07-15 04:04:37 -07:00
|
|
|
return 0
|
2014-07-06 05:46:48 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbReplaceWithDelete(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) uint64 {
|
|
|
|
return ldbGenericReplace(db, folder, device, fs, func(db dbReader, batch dbWriter, folder, device, name []byte, dbi iterator.Iterator) uint64 {
|
2014-08-12 04:53:31 -07:00
|
|
|
var tf protocol.FileInfoTruncated
|
|
|
|
err := tf.UnmarshalXDR(dbi.Value())
|
2014-07-06 05:46:48 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-08-12 04:53:31 -07:00
|
|
|
if !tf.IsDeleted() {
|
2014-07-06 05:46:48 -07:00
|
|
|
if debug {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Debugf("mark deleted; folder=%q device=%v name=%q", folder, protocol.DeviceIDFromBytes(device), name)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
2014-08-12 04:53:31 -07:00
|
|
|
ts := clock(tf.LocalVersion)
|
|
|
|
f := protocol.FileInfo{
|
|
|
|
Name: tf.Name,
|
|
|
|
Version: lamport.Default.Tick(tf.Version),
|
|
|
|
LocalVersion: ts,
|
|
|
|
Flags: tf.Flags | protocol.FlagDeleted,
|
|
|
|
Modified: tf.Modified,
|
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
batch.Put(dbi.Key(), f.MarshalXDR())
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbUpdateGlobal(db, batch, folder, device, deviceKeyName(dbi.Key()), f.Version)
|
2014-07-15 04:04:37 -07:00
|
|
|
return ts
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
2014-07-15 04:04:37 -07:00
|
|
|
return 0
|
2014-07-06 05:46:48 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbUpdate(db *leveldb.DB, folder, device []byte, fs []protocol.FileInfo) uint64 {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-07 13:56:30 -07:00
|
|
|
|
2014-07-06 05:46:48 -07:00
|
|
|
batch := new(leveldb.Batch)
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
|
2014-07-15 04:04:37 -07:00
|
|
|
var maxLocalVer uint64
|
2014-07-06 05:46:48 -07:00
|
|
|
for _, f := range fs {
|
|
|
|
name := []byte(f.Name)
|
2014-09-28 04:00:38 -07:00
|
|
|
fk := deviceKey(folder, device, name)
|
2014-07-06 05:46:48 -07:00
|
|
|
bs, err := snap.Get(fk, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2014-09-28 04:00:38 -07:00
|
|
|
if lv := ldbInsert(batch, folder, device, name, f); lv > maxLocalVer {
|
2014-07-15 04:04:37 -07:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
if f.IsInvalid() {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, name)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, name, f.Version)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-08-12 04:53:31 -07:00
|
|
|
var ef protocol.FileInfoTruncated
|
2014-07-06 05:46:48 -07:00
|
|
|
err = ef.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
// Flags might change without the version being bumped when we set the
|
|
|
|
// invalid flag on an existing file.
|
|
|
|
if ef.Version != f.Version || ef.Flags != f.Flags {
|
2014-09-28 04:00:38 -07:00
|
|
|
if lv := ldbInsert(batch, folder, device, name, f); lv > maxLocalVer {
|
2014-07-15 04:04:37 -07:00
|
|
|
maxLocalVer = lv
|
|
|
|
}
|
2014-09-04 13:29:53 -07:00
|
|
|
if f.IsInvalid() {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbRemoveFromGlobal(snap, batch, folder, device, name)
|
2014-09-04 13:29:53 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
ldbUpdateGlobal(snap, batch, folder, device, name, f.Version)
|
2014-09-04 13:29:53 -07:00
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = db.Write(batch, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-15 04:04:37 -07:00
|
|
|
return maxLocalVer
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbInsert(batch dbWriter, folder, device, name []byte, file protocol.FileInfo) uint64 {
|
2014-07-06 05:46:48 -07:00
|
|
|
if debug {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Debugf("insert; folder=%q device=%v %v", folder, protocol.DeviceIDFromBytes(device), file)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-07-15 04:04:37 -07:00
|
|
|
if file.LocalVersion == 0 {
|
|
|
|
file.LocalVersion = clock(0)
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
nk := deviceKey(folder, device, name)
|
2014-07-06 05:46:48 -07:00
|
|
|
batch.Put(nk, file.MarshalXDR())
|
2014-07-15 04:04:37 -07:00
|
|
|
|
|
|
|
return file.LocalVersion
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07: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 05:46:48 -07:00
|
|
|
// If the file does not have an entry in the global list, it is created.
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbUpdateGlobal(db dbReader, batch dbWriter, folder, device, file []byte, version uint64) bool {
|
2014-07-06 05:46:48 -07:00
|
|
|
if debug {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Debugf("update global; folder=%q device=%v file=%q version=%d", folder, protocol.DeviceIDFromBytes(device), file, version)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
gk := globalKey(folder, file)
|
2014-07-06 05:46:48 -07:00
|
|
|
svl, err := db.Get(gk, nil)
|
|
|
|
if err != nil && err != leveldb.ErrNotFound {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var fl versionList
|
|
|
|
nv := fileVersion{
|
2014-09-28 04:05:25 -07:00
|
|
|
device: device,
|
2014-07-06 05:46:48 -07:00
|
|
|
version: version,
|
|
|
|
}
|
|
|
|
if svl != nil {
|
|
|
|
err = fl.UnmarshalXDR(svl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range fl.versions {
|
2014-09-28 04:00:38 -07:00
|
|
|
if bytes.Compare(fl.versions[i].device, device) == 0 {
|
2014-07-06 05:46:48 -07:00
|
|
|
if fl.versions[i].version == version {
|
|
|
|
// No need to do anything
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
fl.versions = append(fl.versions[:i], fl.versions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range fl.versions {
|
|
|
|
if fl.versions[i].version <= version {
|
|
|
|
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:
|
|
|
|
batch.Put(gk, fl.MarshalXDR())
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// ldbRemoveFromGlobal removes the device from the global version list for the
|
2014-07-06 05:46:48 -07:00
|
|
|
// given file. If the version list is empty after this, the file entry is
|
|
|
|
// removed entirely.
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbRemoveFromGlobal(db dbReader, batch dbWriter, folder, device, file []byte) {
|
2014-07-06 05:46:48 -07:00
|
|
|
if debug {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Debugf("remove from global; folder=%q device=%v file=%q", folder, protocol.DeviceIDFromBytes(device), file)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
gk := globalKey(folder, file)
|
2014-07-06 05:46:48 -07:00
|
|
|
svl, err := db.Get(gk, nil)
|
|
|
|
if err != nil {
|
2014-09-04 13:29:53 -07: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 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
var fl versionList
|
|
|
|
err = fl.UnmarshalXDR(svl)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range fl.versions {
|
2014-09-28 04:00:38 -07:00
|
|
|
if bytes.Compare(fl.versions[i].device, device) == 0 {
|
2014-07-06 05:46:48 -07:00
|
|
|
fl.versions = append(fl.versions[:i], fl.versions[i+1:]...)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(fl.versions) == 0 {
|
|
|
|
batch.Delete(gk)
|
|
|
|
} else {
|
|
|
|
batch.Put(gk, fl.MarshalXDR())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbWithHave(db *leveldb.DB, folder, device []byte, truncate bool, fn fileIterator) {
|
|
|
|
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 05:46:48 -07:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
2014-08-12 04:53:31 -07:00
|
|
|
f, err := unmarshalTrunc(dbi.Value(), truncate)
|
2014-07-06 05:46:48 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if cont := fn(f); !cont {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbWithAllFolderTruncated(db *leveldb.DB, folder []byte, fn func(device []byte, f protocol.FileInfoTruncated) bool) {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-07 13:56:30 -07:00
|
|
|
|
2014-09-28 04:05:25 -07:00
|
|
|
start := deviceKey(folder, nil, nil) // before all folder/device files
|
2014-09-28 04:00:38 -07:00
|
|
|
limit := deviceKey(folder, protocol.LocalDeviceID[:], []byte{0xff, 0xff, 0xff, 0xff}) // after all folder/device files
|
2014-07-17 02:00:54 -07:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
for dbi.Next() {
|
2014-09-28 04:00:38 -07:00
|
|
|
device := deviceKeyDevice(dbi.Key())
|
2014-08-12 04:53:31 -07:00
|
|
|
var f protocol.FileInfoTruncated
|
2014-07-17 02:00:54 -07:00
|
|
|
err := f.UnmarshalXDR(dbi.Value())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
if cont := fn(device, f); !cont {
|
2014-07-17 02:00:54 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbGet(db *leveldb.DB, folder, device, file []byte) protocol.FileInfo {
|
|
|
|
nk := deviceKey(folder, device, file)
|
2014-07-06 05:46:48 -07:00
|
|
|
bs, err := db.Get(nk, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2014-07-12 14:06:48 -07:00
|
|
|
return protocol.FileInfo{}
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
var f protocol.FileInfo
|
2014-07-06 05:46:48 -07:00
|
|
|
err = f.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbGetGlobal(db *leveldb.DB, folder, file []byte) protocol.FileInfo {
|
|
|
|
k := globalKey(folder, file)
|
2014-07-06 05:46:48 -07:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
|
|
|
|
bs, err := snap.Get(k, nil)
|
|
|
|
if err == leveldb.ErrNotFound {
|
2014-07-12 14:06:48 -07:00
|
|
|
return protocol.FileInfo{}
|
2014-07-06 05:46:48 -07: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 04:00:38 -07:00
|
|
|
k = deviceKey(folder, vl.versions[0].device, file)
|
2014-07-06 05:46:48 -07:00
|
|
|
bs, err = snap.Get(k, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-07-12 14:06:48 -07:00
|
|
|
var f protocol.FileInfo
|
2014-07-06 05:46:48 -07:00
|
|
|
err = f.UnmarshalXDR(bs)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbWithGlobal(db *leveldb.DB, folder []byte, truncate bool, fn fileIterator) {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-07 13:56:30 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
start := globalKey(folder, nil)
|
|
|
|
limit := globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
2014-07-06 05:46:48 -07:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
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-09-28 04:00:38 -07:00
|
|
|
fk := deviceKey(folder, vl.versions[0].device, globalKeyName(dbi.Key()))
|
2014-07-06 05:46:48 -07:00
|
|
|
bs, err := snap.Get(fk, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2014-08-12 04:53:31 -07:00
|
|
|
f, err := unmarshalTrunc(bs, truncate)
|
2014-07-06 05:46:48 -07:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cont := fn(f); !cont {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbAvailability(db *leveldb.DB, folder, file []byte) []protocol.DeviceID {
|
|
|
|
k := globalKey(folder, file)
|
2014-07-06 05:46:48 -07: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 04:00:38 -07:00
|
|
|
var devices []protocol.DeviceID
|
2014-07-06 05:46:48 -07:00
|
|
|
for _, v := range vl.versions {
|
|
|
|
if v.version != vl.versions[0].version {
|
|
|
|
break
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
n := protocol.DeviceIDFromBytes(v.device)
|
|
|
|
devices = append(devices, n)
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
return devices
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbWithNeed(db *leveldb.DB, folder, device []byte, truncate bool, fn fileIterator) {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-07 13:56:30 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
start := globalKey(folder, nil)
|
|
|
|
limit := globalKey(folder, []byte{0xff, 0xff, 0xff, 0xff})
|
2014-07-06 05:46:48 -07:00
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
2014-09-01 00:07:51 -07:00
|
|
|
outer:
|
2014-07-06 05:46:48 -07: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
|
|
|
|
var haveVersion uint64
|
|
|
|
for _, v := range vl.versions {
|
2014-09-28 04:00:38 -07:00
|
|
|
if bytes.Compare(v.device, device) == 0 {
|
2014-07-06 05:46:48 -07:00
|
|
|
have = true
|
|
|
|
haveVersion = v.version
|
|
|
|
need = v.version < vl.versions[0].version
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if need || !have {
|
|
|
|
name := globalKeyName(dbi.Key())
|
2014-09-01 00:07:51 -07:00
|
|
|
needVersion := vl.versions[0].version
|
|
|
|
inner:
|
|
|
|
for i := range vl.versions {
|
|
|
|
if vl.versions[i].version != needVersion {
|
|
|
|
// We haven't found a valid copy of the file with the needed version.
|
|
|
|
continue outer
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
fk := deviceKey(folder, vl.versions[i].device, name)
|
2014-09-01 00:07:51 -07:00
|
|
|
bs, err := snap.Get(fk, nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-09-01 00:07:51 -07:00
|
|
|
gf, err := unmarshalTrunc(bs, truncate)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-09-01 00:07:51 -07:00
|
|
|
if gf.IsInvalid() {
|
|
|
|
// The file is marked invalid for whatever reason, don't use it.
|
|
|
|
continue inner
|
|
|
|
}
|
2014-07-06 05:46:48 -07:00
|
|
|
|
2014-09-01 00:07:51 -07:00
|
|
|
if gf.IsDeleted() && !have {
|
|
|
|
// We don't need deleted files that we don't have
|
|
|
|
continue outer
|
|
|
|
}
|
2014-07-29 02:06:52 -07:00
|
|
|
|
2014-09-01 00:07:51 -07:00
|
|
|
if debug {
|
2014-09-28 04:00:38 -07: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 00:07:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if cont := fn(gf); !cont {
|
|
|
|
return
|
|
|
|
}
|
2014-09-08 00:37:42 -07:00
|
|
|
|
|
|
|
// This file is handled, no need to look further in the version list
|
|
|
|
continue outer
|
2014-07-06 05:46:48 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-12 04:53:31 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbListFolders(db *leveldb.DB) []string {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-31 04:34:17 -07:00
|
|
|
|
|
|
|
start := []byte{keyTypeGlobal}
|
|
|
|
limit := []byte{keyTypeGlobal + 1}
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
folderExists := make(map[string]bool)
|
2014-08-31 04:34:17 -07:00
|
|
|
for dbi.Next() {
|
2014-09-28 04:00:38 -07:00
|
|
|
folder := string(globalKeyFolder(dbi.Key()))
|
|
|
|
if !folderExists[folder] {
|
|
|
|
folderExists[folder] = true
|
2014-08-31 04:34:17 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
folders := make([]string, 0, len(folderExists))
|
|
|
|
for k := range folderExists {
|
|
|
|
folders = append(folders, k)
|
2014-08-31 04:34:17 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
sort.Strings(folders)
|
|
|
|
return folders
|
2014-08-31 04:34:17 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func ldbDropFolder(db *leveldb.DB, folder []byte) {
|
2014-09-18 01:04:37 -07:00
|
|
|
runtime.GC()
|
2014-08-31 04:34:17 -07:00
|
|
|
|
|
|
|
snap, err := db.GetSnapshot()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
defer snap.Release()
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Remove all items related to the given folder from the device->file bucket
|
|
|
|
start := []byte{keyTypeDevice}
|
|
|
|
limit := []byte{keyTypeDevice + 1}
|
2014-08-31 04:34:17 -07:00
|
|
|
dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
for dbi.Next() {
|
2014-09-28 04:00:38 -07:00
|
|
|
itemFolder := deviceKeyFolder(dbi.Key())
|
|
|
|
if bytes.Compare(folder, itemFolder) == 0 {
|
2014-08-31 04:34:17 -07:00
|
|
|
db.Delete(dbi.Key(), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbi.Release()
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Remove all items related to the given folder from the global bucket
|
2014-08-31 04:34:17 -07:00
|
|
|
start = []byte{keyTypeGlobal}
|
|
|
|
limit = []byte{keyTypeGlobal + 1}
|
|
|
|
dbi = snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
|
|
|
|
for dbi.Next() {
|
2014-09-28 04:00:38 -07:00
|
|
|
itemFolder := globalKeyFolder(dbi.Key())
|
|
|
|
if bytes.Compare(folder, itemFolder) == 0 {
|
2014-08-31 04:34:17 -07:00
|
|
|
db.Delete(dbi.Key(), nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbi.Release()
|
|
|
|
}
|
|
|
|
|
2014-08-12 04:53:31 -07:00
|
|
|
func unmarshalTrunc(bs []byte, truncate bool) (protocol.FileIntf, error) {
|
|
|
|
if truncate {
|
|
|
|
var tf protocol.FileInfoTruncated
|
|
|
|
err := tf.UnmarshalXDR(bs)
|
|
|
|
return tf, err
|
|
|
|
} else {
|
|
|
|
var tf protocol.FileInfo
|
|
|
|
err := tf.UnmarshalXDR(bs)
|
|
|
|
return tf, err
|
|
|
|
}
|
|
|
|
}
|