set.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // Package files provides a set type to track local/remote files with newness
  16. // checks. We must do a certain amount of normalization in here. We will get
  17. // fed paths with either native or wire-format separators and encodings
  18. // depending on who calls us. We transform paths to wire-format (NFC and
  19. // slashes) on the way to the database, and transform to native format
  20. // (varying separator and encoding) on the way back out.
  21. package files
  22. import (
  23. "sync"
  24. "github.com/syncthing/syncthing/internal/lamport"
  25. "github.com/syncthing/syncthing/internal/protocol"
  26. "github.com/syndtr/goleveldb/leveldb"
  27. )
  28. type fileRecord struct {
  29. File protocol.FileInfo
  30. Usage int
  31. Global bool
  32. }
  33. type bitset uint64
  34. type Set struct {
  35. localVersion map[protocol.DeviceID]uint64
  36. mutex sync.Mutex
  37. folder string
  38. db *leveldb.DB
  39. blockmap *BlockMap
  40. }
  41. func NewSet(folder string, db *leveldb.DB) *Set {
  42. var s = Set{
  43. localVersion: make(map[protocol.DeviceID]uint64),
  44. folder: folder,
  45. db: db,
  46. blockmap: NewBlockMap(db, folder),
  47. }
  48. var deviceID protocol.DeviceID
  49. ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f protocol.FileInfoTruncated) bool {
  50. copy(deviceID[:], device)
  51. if f.LocalVersion > s.localVersion[deviceID] {
  52. s.localVersion[deviceID] = f.LocalVersion
  53. }
  54. lamport.Default.Tick(f.Version)
  55. return true
  56. })
  57. if debug {
  58. l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
  59. }
  60. clock(s.localVersion[protocol.LocalDeviceID])
  61. return &s
  62. }
  63. func (s *Set) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
  64. if debug {
  65. l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
  66. }
  67. normalizeFilenames(fs)
  68. s.mutex.Lock()
  69. defer s.mutex.Unlock()
  70. s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs)
  71. if len(fs) == 0 {
  72. // Reset the local version if all files were removed.
  73. s.localVersion[device] = 0
  74. }
  75. if device == protocol.LocalDeviceID {
  76. s.blockmap.Drop()
  77. s.blockmap.Add(fs)
  78. }
  79. }
  80. func (s *Set) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo) {
  81. if debug {
  82. l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.folder, device, len(fs))
  83. }
  84. normalizeFilenames(fs)
  85. s.mutex.Lock()
  86. defer s.mutex.Unlock()
  87. if lv := ldbReplaceWithDelete(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  88. s.localVersion[device] = lv
  89. }
  90. if device == protocol.LocalDeviceID {
  91. s.blockmap.Drop()
  92. s.blockmap.Add(fs)
  93. }
  94. }
  95. func (s *Set) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
  96. if debug {
  97. l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
  98. }
  99. normalizeFilenames(fs)
  100. s.mutex.Lock()
  101. defer s.mutex.Unlock()
  102. if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  103. s.localVersion[device] = lv
  104. }
  105. if device == protocol.LocalDeviceID {
  106. s.blockmap.Update(fs)
  107. }
  108. }
  109. func (s *Set) WithNeed(device protocol.DeviceID, fn fileIterator) {
  110. if debug {
  111. l.Debugf("%s WithNeed(%v)", s.folder, device)
  112. }
  113. ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  114. }
  115. func (s *Set) WithNeedTruncated(device protocol.DeviceID, fn fileIterator) {
  116. if debug {
  117. l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
  118. }
  119. ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  120. }
  121. func (s *Set) WithHave(device protocol.DeviceID, fn fileIterator) {
  122. if debug {
  123. l.Debugf("%s WithHave(%v)", s.folder, device)
  124. }
  125. ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  126. }
  127. func (s *Set) WithHaveTruncated(device protocol.DeviceID, fn fileIterator) {
  128. if debug {
  129. l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
  130. }
  131. ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  132. }
  133. func (s *Set) WithGlobal(fn fileIterator) {
  134. if debug {
  135. l.Debugf("%s WithGlobal()", s.folder)
  136. }
  137. ldbWithGlobal(s.db, []byte(s.folder), false, nativeFileIterator(fn))
  138. }
  139. func (s *Set) WithGlobalTruncated(fn fileIterator) {
  140. if debug {
  141. l.Debugf("%s WithGlobalTruncated()", s.folder)
  142. }
  143. ldbWithGlobal(s.db, []byte(s.folder), true, nativeFileIterator(fn))
  144. }
  145. func (s *Set) Get(device protocol.DeviceID, file string) protocol.FileInfo {
  146. f := ldbGet(s.db, []byte(s.folder), device[:], []byte(normalizedFilename(file)))
  147. f.Name = nativeFilename(f.Name)
  148. return f
  149. }
  150. func (s *Set) GetGlobal(file string) protocol.FileInfo {
  151. f := ldbGetGlobal(s.db, []byte(s.folder), []byte(normalizedFilename(file)))
  152. f.Name = nativeFilename(f.Name)
  153. return f
  154. }
  155. func (s *Set) Availability(file string) []protocol.DeviceID {
  156. return ldbAvailability(s.db, []byte(s.folder), []byte(normalizedFilename(file)))
  157. }
  158. func (s *Set) LocalVersion(device protocol.DeviceID) uint64 {
  159. s.mutex.Lock()
  160. defer s.mutex.Unlock()
  161. return s.localVersion[device]
  162. }
  163. // ListFolders returns the folder IDs seen in the database.
  164. func ListFolders(db *leveldb.DB) []string {
  165. return ldbListFolders(db)
  166. }
  167. // DropFolder clears out all information related to the given folder from the
  168. // database.
  169. func DropFolder(db *leveldb.DB, folder string) {
  170. ldbDropFolder(db, []byte(folder))
  171. bm := &BlockMap{
  172. db: db,
  173. folder: folder,
  174. }
  175. bm.Drop()
  176. }
  177. func normalizeFilenames(fs []protocol.FileInfo) {
  178. for i := range fs {
  179. fs[i].Name = normalizedFilename(fs[i].Name)
  180. }
  181. }
  182. func nativeFileIterator(fn fileIterator) fileIterator {
  183. return func(fi protocol.FileIntf) bool {
  184. switch f := fi.(type) {
  185. case protocol.FileInfo:
  186. f.Name = nativeFilename(f.Name)
  187. return fn(f)
  188. case protocol.FileInfoTruncated:
  189. f.Name = nativeFilename(f.Name)
  190. return fn(f)
  191. default:
  192. panic("unknown interface type")
  193. }
  194. }
  195. }