set.go 6.7 KB

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