| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- // 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 (
- "strings"
- "github.com/syncthing/syncthing/lib/protocol"
- "github.com/syndtr/goleveldb/leveldb/util"
- )
- const dbVersion = 3
- func (db *Instance) updateSchema() {
- miscDB := NewNamespacedKV(db, string(KeyTypeMiscData))
- prevVersion, _ := miscDB.Int64("dbVersion")
- if prevVersion >= dbVersion {
- return
- }
- l.Infof("Updating database schema version from %v to %v...", prevVersion, dbVersion)
- if prevVersion < 1 {
- db.updateSchema0to1()
- }
- if prevVersion < 2 {
- db.updateSchema1to2()
- }
- if prevVersion < 3 {
- db.updateSchema2to3()
- }
- miscDB.PutInt64("dbVersion", dbVersion)
- }
- func (db *Instance) updateSchema0to1() {
- 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
- var gk []byte
- for dbi.Next() {
- folder := db.deviceKeyFolder(dbi.Key())
- device := db.deviceKeyDevice(dbi.Key())
- name := db.deviceKeyName(dbi.Key())
- // Remove files with absolute path (see #4799)
- if strings.HasPrefix(string(name), "/") {
- if _, ok := changedFolders[string(folder)]; !ok {
- changedFolders[string(folder)] = struct{}{}
- }
- gk = db.globalKeyInto(gk, folder, name)
- t.removeFromGlobal(gk, folder, device, nil, nil)
- 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
- if f.Invalid {
- gk = db.globalKeyInto(gk, folder, name)
- if t.updateGlobal(gk, folder, device, f, meta) {
- if _, ok := changedFolders[string(folder)]; !ok {
- 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).
- func (db *Instance) updateSchema1to2() {
- 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 {
- sk = db.sequenceKeyInto(sk, folder, f.SequenceNo())
- dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], []byte(f.FileName()))
- t.Put(sk, dk)
- t.checkFlush()
- return true
- })
- }
- }
- // updateSchema2to3 introduces a needKey->nil bucket for locally needed files.
- func (db *Instance) updateSchema2to3() {
- 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())
- dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], name)
- var v protocol.Vector
- haveFile, ok := db.getFileTrunc(dk, true)
- if ok {
- v = haveFile.FileVersion()
- }
- if !need(f, ok, v) {
- return true
- }
- nk = t.db.needKeyInto(nk, folder, []byte(f.FileName()))
- t.Put(nk, nil)
- t.checkFlush()
- return true
- })
- }
- }
|