set.go 6.6 KB

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