2018-06-08 10:46:00 +00:00
|
|
|
// Copyright (C) 2018 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,
|
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-06-26 09:40:34 +00:00
|
|
|
"fmt"
|
2018-06-08 10:46:00 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
|
|
|
)
|
|
|
|
|
2018-06-26 09:40:34 +00:00
|
|
|
// List of all dbVersion to dbMinSyncthingVersion pairs for convenience
|
|
|
|
// 0: v0.14.0
|
|
|
|
// 1: v0.14.46
|
|
|
|
// 2: v0.14.48
|
|
|
|
// 3: v0.14.49
|
|
|
|
// 4: v0.14.49
|
2018-07-10 15:32:34 +00:00
|
|
|
// 5: v0.14.49
|
|
|
|
// 6: v0.14.50
|
2018-10-30 04:40:51 +00:00
|
|
|
// 7: v0.14.53
|
2018-06-26 09:40:34 +00:00
|
|
|
const (
|
2018-10-30 04:40:51 +00:00
|
|
|
dbVersion = 7
|
|
|
|
dbMinSyncthingVersion = "v0.14.53"
|
2018-06-26 09:40:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type databaseDowngradeError struct {
|
|
|
|
minSyncthingVersion string
|
|
|
|
}
|
2018-06-08 10:46:00 +00:00
|
|
|
|
2018-06-26 09:40:34 +00:00
|
|
|
func (e databaseDowngradeError) Error() string {
|
|
|
|
if e.minSyncthingVersion == "" {
|
|
|
|
return "newer Syncthing required"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("Syncthing %s required", e.minSyncthingVersion)
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func UpdateSchema(ll *Lowlevel) error {
|
|
|
|
updater := &schemaUpdater{newInstance(ll)}
|
|
|
|
return updater.updateSchema()
|
|
|
|
}
|
|
|
|
|
|
|
|
type schemaUpdater struct {
|
|
|
|
*instance
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *schemaUpdater) updateSchema() error {
|
|
|
|
miscDB := NewMiscDataNamespace(db.Lowlevel)
|
2018-06-08 10:46:00 +00:00
|
|
|
prevVersion, _ := miscDB.Int64("dbVersion")
|
|
|
|
|
2018-06-26 09:40:34 +00:00
|
|
|
if prevVersion > dbVersion {
|
|
|
|
err := databaseDowngradeError{}
|
|
|
|
if minSyncthingVersion, ok := miscDB.String("dbMinSyncthingVersion"); ok {
|
|
|
|
err.minSyncthingVersion = minSyncthingVersion
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if prevVersion == dbVersion {
|
|
|
|
return nil
|
2018-06-08 10:46:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if prevVersion < 1 {
|
|
|
|
db.updateSchema0to1()
|
|
|
|
}
|
|
|
|
if prevVersion < 2 {
|
|
|
|
db.updateSchema1to2()
|
|
|
|
}
|
|
|
|
if prevVersion < 3 {
|
|
|
|
db.updateSchema2to3()
|
|
|
|
}
|
2018-07-10 15:32:34 +00:00
|
|
|
// This update fixes problems existing in versions 3 and 4
|
|
|
|
if prevVersion == 3 || prevVersion == 4 {
|
|
|
|
db.updateSchemaTo5()
|
2018-06-18 06:23:40 +00:00
|
|
|
}
|
2018-07-10 15:32:34 +00:00
|
|
|
if prevVersion < 6 {
|
|
|
|
db.updateSchema5to6()
|
2018-06-24 07:50:18 +00:00
|
|
|
}
|
2018-10-30 04:40:51 +00:00
|
|
|
if prevVersion < 7 {
|
|
|
|
db.updateSchema6to7()
|
|
|
|
}
|
2018-06-08 10:46:00 +00:00
|
|
|
|
|
|
|
miscDB.PutInt64("dbVersion", dbVersion)
|
2018-06-26 09:40:34 +00:00
|
|
|
miscDB.PutString("dbMinSyncthingVersion", dbMinSyncthingVersion)
|
|
|
|
|
|
|
|
return nil
|
2018-06-08 10:46:00 +00:00
|
|
|
}
|
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *schemaUpdater) updateSchema0to1() {
|
2018-06-08 10:46:00 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
|
|
|
|
defer dbi.Release()
|
|
|
|
|
|
|
|
symlinkConv := 0
|
|
|
|
changedFolders := make(map[string]struct{})
|
|
|
|
ignAdded := 0
|
|
|
|
meta := newMetadataTracker() // dummy metadata tracker
|
2019-01-20 07:47:20 +00:00
|
|
|
var gk, buf []byte
|
2018-06-08 10:46:00 +00:00
|
|
|
|
|
|
|
for dbi.Next() {
|
2018-09-18 08:41:06 +00:00
|
|
|
folder, ok := db.keyer.FolderFromDeviceFileKey(dbi.Key())
|
|
|
|
if !ok {
|
|
|
|
// not having the folder in the index is bad; delete and continue
|
|
|
|
t.Delete(dbi.Key())
|
|
|
|
t.checkFlush()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
device, ok := db.keyer.DeviceFromDeviceFileKey(dbi.Key())
|
|
|
|
if !ok {
|
|
|
|
// not having the device in the index is bad; delete and continue
|
|
|
|
t.Delete(dbi.Key())
|
|
|
|
t.checkFlush()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := db.keyer.NameFromDeviceFileKey(dbi.Key())
|
2018-06-08 10:46:00 +00:00
|
|
|
|
|
|
|
// Remove files with absolute path (see #4799)
|
|
|
|
if strings.HasPrefix(string(name), "/") {
|
|
|
|
if _, ok := changedFolders[string(folder)]; !ok {
|
|
|
|
changedFolders[string(folder)] = struct{}{}
|
|
|
|
}
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2019-01-20 07:47:20 +00:00
|
|
|
buf = t.removeFromGlobal(gk, buf, folder, device, nil, nil)
|
2018-06-08 10:46:00 +00:00
|
|
|
t.Delete(dbi.Key())
|
|
|
|
t.checkFlush()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change SYMLINK_FILE and SYMLINK_DIRECTORY types to the current SYMLINK
|
|
|
|
// type (previously SYMLINK_UNKNOWN). It does this for all devices, both
|
|
|
|
// local and remote, and does not reset delta indexes. It shouldn't really
|
|
|
|
// matter what the symlink type is, but this cleans it up for a possible
|
|
|
|
// future when SYMLINK_FILE and SYMLINK_DIRECTORY are no longer understood.
|
|
|
|
var f protocol.FileInfo
|
|
|
|
if err := f.Unmarshal(dbi.Value()); err != nil {
|
|
|
|
// probably can't happen
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if f.Type == protocol.FileInfoTypeDeprecatedSymlinkDirectory || f.Type == protocol.FileInfoTypeDeprecatedSymlinkFile {
|
|
|
|
f.Type = protocol.FileInfoTypeSymlink
|
|
|
|
bs, err := f.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
panic("can't happen: " + err.Error())
|
|
|
|
}
|
|
|
|
t.Put(dbi.Key(), bs)
|
|
|
|
t.checkFlush()
|
|
|
|
symlinkConv++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add invalid files to global list
|
2018-06-24 07:50:18 +00:00
|
|
|
if f.IsInvalid() {
|
2018-09-18 08:41:06 +00:00
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
2019-01-20 07:47:20 +00:00
|
|
|
if buf, ok = t.updateGlobal(gk, buf, folder, device, f, meta); ok {
|
|
|
|
if _, ok = changedFolders[string(folder)]; !ok {
|
2018-06-08 10:46:00 +00:00
|
|
|
changedFolders[string(folder)] = struct{}{}
|
|
|
|
}
|
|
|
|
ignAdded++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for folder := range changedFolders {
|
|
|
|
db.dropFolderMeta([]byte(folder))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateSchema1to2 introduces a sequenceKey->deviceKey bucket for local items
|
|
|
|
// to allow iteration in sequence order (simplifies sending indexes).
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *schemaUpdater) updateSchema1to2() {
|
2018-06-08 10:46:00 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var sk []byte
|
|
|
|
var dk []byte
|
|
|
|
for _, folderStr := range db.ListFolders() {
|
|
|
|
folder := []byte(folderStr)
|
|
|
|
db.withHave(folder, protocol.LocalDeviceID[:], nil, true, func(f FileIntf) bool {
|
2018-09-18 08:41:06 +00:00
|
|
|
sk = db.keyer.GenerateSequenceKey(sk, folder, f.SequenceNo())
|
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], []byte(f.FileName()))
|
2018-06-08 10:46:00 +00:00
|
|
|
t.Put(sk, dk)
|
|
|
|
t.checkFlush()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// updateSchema2to3 introduces a needKey->nil bucket for locally needed files.
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *schemaUpdater) updateSchema2to3() {
|
2018-06-08 10:46:00 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var nk []byte
|
|
|
|
var dk []byte
|
|
|
|
for _, folderStr := range db.ListFolders() {
|
|
|
|
folder := []byte(folderStr)
|
|
|
|
db.withGlobal(folder, nil, true, func(f FileIntf) bool {
|
|
|
|
name := []byte(f.FileName())
|
2018-09-18 08:41:06 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], name)
|
2018-06-08 10:46:00 +00:00
|
|
|
var v protocol.Vector
|
2019-01-18 10:34:18 +00:00
|
|
|
haveFile, ok := t.getFileTrunc(dk, true)
|
2018-06-08 10:46:00 +00:00
|
|
|
if ok {
|
|
|
|
v = haveFile.FileVersion()
|
|
|
|
}
|
|
|
|
if !need(f, ok, v) {
|
|
|
|
return true
|
|
|
|
}
|
2018-09-18 08:41:06 +00:00
|
|
|
nk = t.db.keyer.GenerateNeedFileKey(nk, folder, []byte(f.FileName()))
|
2018-06-08 10:46:00 +00:00
|
|
|
t.Put(nk, nil)
|
|
|
|
t.checkFlush()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-06-18 06:23:40 +00:00
|
|
|
|
2018-07-10 15:32:34 +00:00
|
|
|
// updateSchemaTo5 resets the need bucket due to bugs existing in the v0.14.49
|
|
|
|
// release candidates (dbVersion 3 and 4)
|
2018-06-18 06:23:40 +00:00
|
|
|
// https://github.com/syncthing/syncthing/issues/5007
|
2018-07-10 15:32:34 +00:00
|
|
|
// https://github.com/syncthing/syncthing/issues/5053
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *schemaUpdater) updateSchemaTo5() {
|
2018-06-18 06:23:40 +00:00
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
var nk []byte
|
|
|
|
for _, folderStr := range db.ListFolders() {
|
2018-09-18 08:41:06 +00:00
|
|
|
nk = db.keyer.GenerateNeedFileKey(nk, []byte(folderStr), nil)
|
2018-06-18 06:23:40 +00:00
|
|
|
t.deleteKeyPrefix(nk[:keyPrefixLen+keyFolderLen])
|
|
|
|
}
|
|
|
|
t.close()
|
|
|
|
|
|
|
|
db.updateSchema2to3()
|
|
|
|
}
|
2018-06-24 07:50:18 +00:00
|
|
|
|
2018-10-10 09:34:24 +00:00
|
|
|
func (db *schemaUpdater) updateSchema5to6() {
|
2018-06-24 07:50:18 +00:00
|
|
|
// For every local file with the Invalid bit set, clear the Invalid bit and
|
|
|
|
// set LocalFlags = FlagLocalIgnored.
|
|
|
|
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var dk []byte
|
|
|
|
|
|
|
|
for _, folderStr := range db.ListFolders() {
|
|
|
|
folder := []byte(folderStr)
|
|
|
|
db.withHave(folder, protocol.LocalDeviceID[:], nil, false, func(f FileIntf) bool {
|
|
|
|
if !f.IsInvalid() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fi := f.(protocol.FileInfo)
|
|
|
|
fi.RawInvalid = false
|
|
|
|
fi.LocalFlags = protocol.FlagLocalIgnored
|
|
|
|
bs, _ := fi.Marshal()
|
|
|
|
|
2018-09-18 08:41:06 +00:00
|
|
|
dk = db.keyer.GenerateDeviceFileKey(dk, folder, protocol.LocalDeviceID[:], []byte(fi.Name))
|
2018-06-24 07:50:18 +00:00
|
|
|
t.Put(dk, bs)
|
|
|
|
|
|
|
|
t.checkFlush()
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2018-10-30 04:40:51 +00:00
|
|
|
|
|
|
|
// updateSchema6to7 checks whether all currently locally needed files are really
|
|
|
|
// needed and removes them if not.
|
|
|
|
func (db *schemaUpdater) updateSchema6to7() {
|
|
|
|
t := db.newReadWriteTransaction()
|
|
|
|
defer t.close()
|
|
|
|
|
|
|
|
var gk []byte
|
|
|
|
var nk []byte
|
|
|
|
|
|
|
|
for _, folderStr := range db.ListFolders() {
|
|
|
|
folder := []byte(folderStr)
|
|
|
|
db.withNeedLocal(folder, false, func(f FileIntf) bool {
|
|
|
|
name := []byte(f.FileName())
|
|
|
|
global := f.(protocol.FileInfo)
|
|
|
|
gk = db.keyer.GenerateGlobalVersionKey(gk, folder, name)
|
|
|
|
svl, err := t.Get(gk, nil)
|
|
|
|
if err != nil {
|
|
|
|
// If there is no global list, we hardly need it.
|
|
|
|
t.Delete(t.db.keyer.GenerateNeedFileKey(nk, folder, name))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
var fl VersionList
|
|
|
|
err = fl.Unmarshal(svl)
|
|
|
|
if err != nil {
|
|
|
|
// This can't happen, but it's ignored everywhere else too,
|
|
|
|
// so lets not act on it.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if localFV, haveLocalFV := fl.Get(protocol.LocalDeviceID[:]); !need(global, haveLocalFV, localFV.Version) {
|
|
|
|
t.Delete(t.db.keyer.GenerateNeedFileKey(nk, folder, name))
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|