1
0

set.go 6.6 KB

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