set.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. }
  40. func NewSet(folder string, db *leveldb.DB) *Set {
  41. var s = Set{
  42. localVersion: make(map[protocol.DeviceID]uint64),
  43. folder: folder,
  44. db: db,
  45. }
  46. var deviceID protocol.DeviceID
  47. ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f protocol.FileInfoTruncated) bool {
  48. copy(deviceID[:], device)
  49. if f.LocalVersion > s.localVersion[deviceID] {
  50. s.localVersion[deviceID] = f.LocalVersion
  51. }
  52. lamport.Default.Tick(f.Version)
  53. return true
  54. })
  55. if debug {
  56. l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
  57. }
  58. clock(s.localVersion[protocol.LocalDeviceID])
  59. return &s
  60. }
  61. func (s *Set) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
  62. if debug {
  63. l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
  64. }
  65. normalizeFilenames(fs)
  66. s.mutex.Lock()
  67. defer s.mutex.Unlock()
  68. s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs)
  69. if len(fs) == 0 {
  70. // Reset the local version if all files were removed.
  71. s.localVersion[device] = 0
  72. }
  73. }
  74. func (s *Set) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo) {
  75. if debug {
  76. l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.folder, device, len(fs))
  77. }
  78. normalizeFilenames(fs)
  79. s.mutex.Lock()
  80. defer s.mutex.Unlock()
  81. if lv := ldbReplaceWithDelete(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  82. s.localVersion[device] = lv
  83. }
  84. }
  85. func (s *Set) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
  86. if debug {
  87. l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
  88. }
  89. normalizeFilenames(fs)
  90. s.mutex.Lock()
  91. defer s.mutex.Unlock()
  92. if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  93. s.localVersion[device] = lv
  94. }
  95. }
  96. func (s *Set) WithNeed(device protocol.DeviceID, fn fileIterator) {
  97. if debug {
  98. l.Debugf("%s WithNeed(%v)", s.folder, device)
  99. }
  100. ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  101. }
  102. func (s *Set) WithNeedTruncated(device protocol.DeviceID, fn fileIterator) {
  103. if debug {
  104. l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
  105. }
  106. ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  107. }
  108. func (s *Set) WithHave(device protocol.DeviceID, fn fileIterator) {
  109. if debug {
  110. l.Debugf("%s WithHave(%v)", s.folder, device)
  111. }
  112. ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  113. }
  114. func (s *Set) WithHaveTruncated(device protocol.DeviceID, fn fileIterator) {
  115. if debug {
  116. l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
  117. }
  118. ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  119. }
  120. func (s *Set) WithGlobal(fn fileIterator) {
  121. if debug {
  122. l.Debugf("%s WithGlobal()", s.folder)
  123. }
  124. ldbWithGlobal(s.db, []byte(s.folder), false, nativeFileIterator(fn))
  125. }
  126. func (s *Set) WithGlobalTruncated(fn fileIterator) {
  127. if debug {
  128. l.Debugf("%s WithGlobalTruncated()", s.folder)
  129. }
  130. ldbWithGlobal(s.db, []byte(s.folder), true, nativeFileIterator(fn))
  131. }
  132. func (s *Set) Get(device protocol.DeviceID, file string) protocol.FileInfo {
  133. f := ldbGet(s.db, []byte(s.folder), device[:], []byte(normalizedFilename(file)))
  134. f.Name = nativeFilename(f.Name)
  135. return f
  136. }
  137. func (s *Set) GetGlobal(file string) protocol.FileInfo {
  138. f := ldbGetGlobal(s.db, []byte(s.folder), []byte(normalizedFilename(file)))
  139. f.Name = nativeFilename(f.Name)
  140. return f
  141. }
  142. func (s *Set) Availability(file string) []protocol.DeviceID {
  143. return ldbAvailability(s.db, []byte(s.folder), []byte(normalizedFilename(file)))
  144. }
  145. func (s *Set) LocalVersion(device protocol.DeviceID) uint64 {
  146. s.mutex.Lock()
  147. defer s.mutex.Unlock()
  148. return s.localVersion[device]
  149. }
  150. // ListFolders returns the folder IDs seen in the database.
  151. func ListFolders(db *leveldb.DB) []string {
  152. return ldbListFolders(db)
  153. }
  154. // DropFolder clears out all information related to the given folder from the
  155. // database.
  156. func DropFolder(db *leveldb.DB, folder string) {
  157. ldbDropFolder(db, []byte(folder))
  158. }
  159. func normalizeFilenames(fs []protocol.FileInfo) {
  160. for i := range fs {
  161. fs[i].Name = normalizedFilename(fs[i].Name)
  162. }
  163. }
  164. func nativeFileIterator(fn fileIterator) fileIterator {
  165. return func(fi protocol.FileIntf) bool {
  166. switch f := fi.(type) {
  167. case protocol.FileInfo:
  168. f.Name = nativeFilename(f.Name)
  169. return fn(f)
  170. case protocol.FileInfoTruncated:
  171. f.Name = nativeFilename(f.Name)
  172. return fn(f)
  173. default:
  174. panic("unknown interface type")
  175. }
  176. }
  177. }