leveldb_dbinstance_updateschema.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // Copyright (C) 2018 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package db
  7. import (
  8. "strings"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. "github.com/syndtr/goleveldb/leveldb/util"
  11. )
  12. const dbVersion = 5
  13. func (db *Instance) updateSchema() {
  14. miscDB := NewNamespacedKV(db, string(KeyTypeMiscData))
  15. prevVersion, _ := miscDB.Int64("dbVersion")
  16. if prevVersion >= dbVersion {
  17. return
  18. }
  19. if prevVersion < 1 {
  20. db.updateSchema0to1()
  21. }
  22. if prevVersion < 2 {
  23. db.updateSchema1to2()
  24. }
  25. if prevVersion < 3 {
  26. db.updateSchema2to3()
  27. }
  28. // This update fixes a problem that only exists in dbVersion 3.
  29. if prevVersion == 3 {
  30. db.updateSchema3to4()
  31. }
  32. if prevVersion < 5 {
  33. db.updateSchema4to5()
  34. }
  35. miscDB.PutInt64("dbVersion", dbVersion)
  36. }
  37. func (db *Instance) updateSchema0to1() {
  38. t := db.newReadWriteTransaction()
  39. defer t.close()
  40. dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
  41. defer dbi.Release()
  42. symlinkConv := 0
  43. changedFolders := make(map[string]struct{})
  44. ignAdded := 0
  45. meta := newMetadataTracker() // dummy metadata tracker
  46. var gk []byte
  47. for dbi.Next() {
  48. folder := db.deviceKeyFolder(dbi.Key())
  49. device := db.deviceKeyDevice(dbi.Key())
  50. name := db.deviceKeyName(dbi.Key())
  51. // Remove files with absolute path (see #4799)
  52. if strings.HasPrefix(string(name), "/") {
  53. if _, ok := changedFolders[string(folder)]; !ok {
  54. changedFolders[string(folder)] = struct{}{}
  55. }
  56. gk = db.globalKeyInto(gk, folder, name)
  57. t.removeFromGlobal(gk, folder, device, nil, nil)
  58. t.Delete(dbi.Key())
  59. t.checkFlush()
  60. continue
  61. }
  62. // Change SYMLINK_FILE and SYMLINK_DIRECTORY types to the current SYMLINK
  63. // type (previously SYMLINK_UNKNOWN). It does this for all devices, both
  64. // local and remote, and does not reset delta indexes. It shouldn't really
  65. // matter what the symlink type is, but this cleans it up for a possible
  66. // future when SYMLINK_FILE and SYMLINK_DIRECTORY are no longer understood.
  67. var f protocol.FileInfo
  68. if err := f.Unmarshal(dbi.Value()); err != nil {
  69. // probably can't happen
  70. continue
  71. }
  72. if f.Type == protocol.FileInfoTypeDeprecatedSymlinkDirectory || f.Type == protocol.FileInfoTypeDeprecatedSymlinkFile {
  73. f.Type = protocol.FileInfoTypeSymlink
  74. bs, err := f.Marshal()
  75. if err != nil {
  76. panic("can't happen: " + err.Error())
  77. }
  78. t.Put(dbi.Key(), bs)
  79. t.checkFlush()
  80. symlinkConv++
  81. }
  82. // Add invalid files to global list
  83. if f.IsInvalid() {
  84. gk = db.globalKeyInto(gk, folder, name)
  85. if t.updateGlobal(gk, folder, device, f, meta) {
  86. if _, ok := changedFolders[string(folder)]; !ok {
  87. changedFolders[string(folder)] = struct{}{}
  88. }
  89. ignAdded++
  90. }
  91. }
  92. }
  93. for folder := range changedFolders {
  94. db.dropFolderMeta([]byte(folder))
  95. }
  96. }
  97. // updateSchema1to2 introduces a sequenceKey->deviceKey bucket for local items
  98. // to allow iteration in sequence order (simplifies sending indexes).
  99. func (db *Instance) updateSchema1to2() {
  100. t := db.newReadWriteTransaction()
  101. defer t.close()
  102. var sk []byte
  103. var dk []byte
  104. for _, folderStr := range db.ListFolders() {
  105. folder := []byte(folderStr)
  106. db.withHave(folder, protocol.LocalDeviceID[:], nil, true, func(f FileIntf) bool {
  107. sk = db.sequenceKeyInto(sk, folder, f.SequenceNo())
  108. dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], []byte(f.FileName()))
  109. t.Put(sk, dk)
  110. t.checkFlush()
  111. return true
  112. })
  113. }
  114. }
  115. // updateSchema2to3 introduces a needKey->nil bucket for locally needed files.
  116. func (db *Instance) updateSchema2to3() {
  117. t := db.newReadWriteTransaction()
  118. defer t.close()
  119. var nk []byte
  120. var dk []byte
  121. for _, folderStr := range db.ListFolders() {
  122. folder := []byte(folderStr)
  123. db.withGlobal(folder, nil, true, func(f FileIntf) bool {
  124. name := []byte(f.FileName())
  125. dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], name)
  126. var v protocol.Vector
  127. haveFile, ok := db.getFileTrunc(dk, true)
  128. if ok {
  129. v = haveFile.FileVersion()
  130. }
  131. if !need(f, ok, v) {
  132. return true
  133. }
  134. nk = t.db.needKeyInto(nk, folder, []byte(f.FileName()))
  135. t.Put(nk, nil)
  136. t.checkFlush()
  137. return true
  138. })
  139. }
  140. }
  141. // updateSchema3to4 resets the need bucket due a bug existing in dbVersion 3 /
  142. // v0.14.49-rc.1
  143. // https://github.com/syncthing/syncthing/issues/5007
  144. func (db *Instance) updateSchema3to4() {
  145. t := db.newReadWriteTransaction()
  146. var nk []byte
  147. for _, folderStr := range db.ListFolders() {
  148. nk = db.needKeyInto(nk, []byte(folderStr), nil)
  149. t.deleteKeyPrefix(nk[:keyPrefixLen+keyFolderLen])
  150. }
  151. t.close()
  152. db.updateSchema2to3()
  153. }
  154. func (db *Instance) updateSchema4to5() {
  155. // For every local file with the Invalid bit set, clear the Invalid bit and
  156. // set LocalFlags = FlagLocalIgnored.
  157. t := db.newReadWriteTransaction()
  158. defer t.close()
  159. var dk []byte
  160. for _, folderStr := range db.ListFolders() {
  161. folder := []byte(folderStr)
  162. db.withHave(folder, protocol.LocalDeviceID[:], nil, false, func(f FileIntf) bool {
  163. if !f.IsInvalid() {
  164. return true
  165. }
  166. fi := f.(protocol.FileInfo)
  167. fi.RawInvalid = false
  168. fi.LocalFlags = protocol.FlagLocalIgnored
  169. bs, _ := fi.Marshal()
  170. dk = db.deviceKeyInto(dk, folder, protocol.LocalDeviceID[:], []byte(fi.Name))
  171. t.Put(dk, bs)
  172. t.checkFlush()
  173. return true
  174. })
  175. }
  176. }