blockmap.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (C) 2014 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 http://mozilla.org/MPL/2.0/.
  6. // Package db provides a set type to track local/remote files with newness
  7. // checks. We must do a certain amount of normalization in here. We will get
  8. // fed paths with either native or wire-format separators and encodings
  9. // depending on who calls us. We transform paths to wire-format (NFC and
  10. // slashes) on the way to the database, and transform to native format
  11. // (varying separator and encoding) on the way back out.
  12. package db
  13. import (
  14. "bytes"
  15. "encoding/binary"
  16. "fmt"
  17. "github.com/syncthing/syncthing/lib/osutil"
  18. "github.com/syncthing/syncthing/lib/protocol"
  19. "github.com/syndtr/goleveldb/leveldb"
  20. "github.com/syndtr/goleveldb/leveldb/util"
  21. )
  22. var blockFinder *BlockFinder
  23. type BlockMap struct {
  24. db *leveldb.DB
  25. folder string
  26. }
  27. func NewBlockMap(db *leveldb.DB, folder string) *BlockMap {
  28. return &BlockMap{
  29. db: db,
  30. folder: folder,
  31. }
  32. }
  33. // Add files to the block map, ignoring any deleted or invalid files.
  34. func (m *BlockMap) Add(files []protocol.FileInfo) error {
  35. batch := new(leveldb.Batch)
  36. buf := make([]byte, 4)
  37. for _, file := range files {
  38. if file.IsDirectory() || file.IsDeleted() || file.IsInvalid() {
  39. continue
  40. }
  41. for i, block := range file.Blocks {
  42. binary.BigEndian.PutUint32(buf, uint32(i))
  43. batch.Put(m.blockKey(block.Hash, file.Name), buf)
  44. }
  45. }
  46. return m.db.Write(batch, nil)
  47. }
  48. // Update block map state, removing any deleted or invalid files.
  49. func (m *BlockMap) Update(files []protocol.FileInfo) error {
  50. batch := new(leveldb.Batch)
  51. buf := make([]byte, 4)
  52. for _, file := range files {
  53. if file.IsDirectory() {
  54. continue
  55. }
  56. if file.IsDeleted() || file.IsInvalid() {
  57. for _, block := range file.Blocks {
  58. batch.Delete(m.blockKey(block.Hash, file.Name))
  59. }
  60. continue
  61. }
  62. for i, block := range file.Blocks {
  63. binary.BigEndian.PutUint32(buf, uint32(i))
  64. batch.Put(m.blockKey(block.Hash, file.Name), buf)
  65. }
  66. }
  67. return m.db.Write(batch, nil)
  68. }
  69. // Discard block map state, removing the given files
  70. func (m *BlockMap) Discard(files []protocol.FileInfo) error {
  71. batch := new(leveldb.Batch)
  72. for _, file := range files {
  73. for _, block := range file.Blocks {
  74. batch.Delete(m.blockKey(block.Hash, file.Name))
  75. }
  76. }
  77. return m.db.Write(batch, nil)
  78. }
  79. // Drop block map, removing all entries related to this block map from the db.
  80. func (m *BlockMap) Drop() error {
  81. batch := new(leveldb.Batch)
  82. iter := m.db.NewIterator(util.BytesPrefix(m.blockKey(nil, "")[:1+64]), nil)
  83. defer iter.Release()
  84. for iter.Next() {
  85. batch.Delete(iter.Key())
  86. }
  87. if iter.Error() != nil {
  88. return iter.Error()
  89. }
  90. return m.db.Write(batch, nil)
  91. }
  92. func (m *BlockMap) blockKey(hash []byte, file string) []byte {
  93. return toBlockKey(hash, m.folder, file)
  94. }
  95. type BlockFinder struct {
  96. db *leveldb.DB
  97. }
  98. func NewBlockFinder(db *leveldb.DB) *BlockFinder {
  99. if blockFinder != nil {
  100. return blockFinder
  101. }
  102. f := &BlockFinder{
  103. db: db,
  104. }
  105. return f
  106. }
  107. func (f *BlockFinder) String() string {
  108. return fmt.Sprintf("BlockFinder@%p", f)
  109. }
  110. // Iterate takes an iterator function which iterates over all matching blocks
  111. // for the given hash. The iterator function has to return either true (if
  112. // they are happy with the block) or false to continue iterating for whatever
  113. // reason. The iterator finally returns the result, whether or not a
  114. // satisfying block was eventually found.
  115. func (f *BlockFinder) Iterate(folders []string, hash []byte, iterFn func(string, string, int32) bool) bool {
  116. for _, folder := range folders {
  117. key := toBlockKey(hash, folder, "")
  118. iter := f.db.NewIterator(util.BytesPrefix(key), nil)
  119. defer iter.Release()
  120. for iter.Next() && iter.Error() == nil {
  121. folder, file := fromBlockKey(iter.Key())
  122. index := int32(binary.BigEndian.Uint32(iter.Value()))
  123. if iterFn(folder, osutil.NativeFilename(file), index) {
  124. return true
  125. }
  126. }
  127. }
  128. return false
  129. }
  130. // Fix repairs incorrect blockmap entries, removing the old entry and
  131. // replacing it with a new entry for the given block
  132. func (f *BlockFinder) Fix(folder, file string, index int32, oldHash, newHash []byte) error {
  133. buf := make([]byte, 4)
  134. binary.BigEndian.PutUint32(buf, uint32(index))
  135. batch := new(leveldb.Batch)
  136. batch.Delete(toBlockKey(oldHash, folder, file))
  137. batch.Put(toBlockKey(newHash, folder, file), buf)
  138. return f.db.Write(batch, nil)
  139. }
  140. // m.blockKey returns a byte slice encoding the following information:
  141. // keyTypeBlock (1 byte)
  142. // folder (64 bytes)
  143. // block hash (32 bytes)
  144. // file name (variable size)
  145. func toBlockKey(hash []byte, folder, file string) []byte {
  146. o := make([]byte, 1+64+32+len(file))
  147. o[0] = KeyTypeBlock
  148. copy(o[1:], []byte(folder))
  149. copy(o[1+64:], []byte(hash))
  150. copy(o[1+64+32:], []byte(file))
  151. return o
  152. }
  153. func fromBlockKey(data []byte) (string, string) {
  154. if len(data) < 1+64+32+1 {
  155. panic("Incorrect key length")
  156. }
  157. if data[0] != KeyTypeBlock {
  158. panic("Incorrect key type")
  159. }
  160. file := string(data[1+64+32:])
  161. slice := data[1 : 1+64]
  162. izero := bytes.IndexByte(slice, 0)
  163. if izero > -1 {
  164. return string(slice[:izero]), file
  165. }
  166. return string(slice), file
  167. }