set.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // Package db provides a set type to track local/remote files with newness
  7. // checks. We must do a certain amount of normalization in here. We will get
  8. // fed paths with either native or wire-format separators and encodings
  9. // depending on who calls us. We transform paths to wire-format (NFC and
  10. // slashes) on the way to the database, and transform to native format
  11. // (varying separator and encoding) on the way back out.
  12. package db
  13. import (
  14. "github.com/syncthing/syncthing/lib/osutil"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. "github.com/syncthing/syncthing/lib/sync"
  17. "github.com/syndtr/goleveldb/leveldb"
  18. )
  19. type FileSet struct {
  20. localVersion map[protocol.DeviceID]int64
  21. mutex sync.Mutex
  22. folder string
  23. db *leveldb.DB
  24. blockmap *BlockMap
  25. }
  26. // FileIntf is the set of methods implemented by both protocol.FileInfo and
  27. // protocol.FileInfoTruncated.
  28. type FileIntf interface {
  29. Size() int64
  30. IsDeleted() bool
  31. IsInvalid() bool
  32. IsDirectory() bool
  33. IsSymlink() bool
  34. HasPermissionBits() bool
  35. }
  36. // The Iterator is called with either a protocol.FileInfo or a
  37. // protocol.FileInfoTruncated (depending on the method) and returns true to
  38. // continue iteration, false to stop.
  39. type Iterator func(f FileIntf) bool
  40. func NewFileSet(folder string, db *leveldb.DB) *FileSet {
  41. var s = FileSet{
  42. localVersion: make(map[protocol.DeviceID]int64),
  43. folder: folder,
  44. db: db,
  45. blockmap: NewBlockMap(db, folder),
  46. mutex: sync.NewMutex(),
  47. }
  48. ldbCheckGlobals(db, []byte(folder))
  49. var deviceID protocol.DeviceID
  50. ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f FileInfoTruncated) bool {
  51. copy(deviceID[:], device)
  52. if f.LocalVersion > s.localVersion[deviceID] {
  53. s.localVersion[deviceID] = f.LocalVersion
  54. }
  55. return true
  56. })
  57. l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
  58. clock(s.localVersion[protocol.LocalDeviceID])
  59. return &s
  60. }
  61. func (s *FileSet) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
  62. l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
  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 *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
  77. l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
  78. normalizeFilenames(fs)
  79. s.mutex.Lock()
  80. defer s.mutex.Unlock()
  81. if device == protocol.LocalDeviceID {
  82. discards := make([]protocol.FileInfo, 0, len(fs))
  83. updates := make([]protocol.FileInfo, 0, len(fs))
  84. for _, newFile := range fs {
  85. existingFile, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(newFile.Name))
  86. if !ok || !existingFile.Version.Equal(newFile.Version) {
  87. discards = append(discards, existingFile)
  88. updates = append(updates, newFile)
  89. }
  90. }
  91. s.blockmap.Discard(discards)
  92. s.blockmap.Update(updates)
  93. }
  94. if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  95. s.localVersion[device] = lv
  96. }
  97. }
  98. func (s *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
  99. l.Debugf("%s WithNeed(%v)", s.folder, device)
  100. ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  101. }
  102. func (s *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
  103. l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
  104. ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  105. }
  106. func (s *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
  107. l.Debugf("%s WithHave(%v)", s.folder, device)
  108. ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  109. }
  110. func (s *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
  111. l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
  112. ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  113. }
  114. func (s *FileSet) WithGlobal(fn Iterator) {
  115. l.Debugf("%s WithGlobal()", s.folder)
  116. ldbWithGlobal(s.db, []byte(s.folder), nil, false, nativeFileIterator(fn))
  117. }
  118. func (s *FileSet) WithGlobalTruncated(fn Iterator) {
  119. l.Debugf("%s WithGlobalTruncated()", s.folder)
  120. ldbWithGlobal(s.db, []byte(s.folder), nil, true, nativeFileIterator(fn))
  121. }
  122. func (s *FileSet) WithPrefixedGlobalTruncated(prefix string, fn Iterator) {
  123. l.Debugf("%s WithPrefixedGlobalTruncated()", s.folder, prefix)
  124. ldbWithGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
  125. }
  126. func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
  127. f, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
  128. f.Name = osutil.NativeFilename(f.Name)
  129. return f, ok
  130. }
  131. func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
  132. fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
  133. if !ok {
  134. return protocol.FileInfo{}, false
  135. }
  136. f := fi.(protocol.FileInfo)
  137. f.Name = osutil.NativeFilename(f.Name)
  138. return f, true
  139. }
  140. func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
  141. fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
  142. if !ok {
  143. return FileInfoTruncated{}, false
  144. }
  145. f := fi.(FileInfoTruncated)
  146. f.Name = osutil.NativeFilename(f.Name)
  147. return f, true
  148. }
  149. func (s *FileSet) Availability(file string) []protocol.DeviceID {
  150. return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
  151. }
  152. func (s *FileSet) LocalVersion(device protocol.DeviceID) int64 {
  153. s.mutex.Lock()
  154. defer s.mutex.Unlock()
  155. return s.localVersion[device]
  156. }
  157. // ListFolders returns the folder IDs seen in the database.
  158. func ListFolders(db *leveldb.DB) []string {
  159. return ldbListFolders(db)
  160. }
  161. // DropFolder clears out all information related to the given folder from the
  162. // database.
  163. func DropFolder(db *leveldb.DB, folder string) {
  164. ldbDropFolder(db, []byte(folder))
  165. bm := &BlockMap{
  166. db: db,
  167. folder: folder,
  168. }
  169. bm.Drop()
  170. NewVirtualMtimeRepo(db, folder).Drop()
  171. }
  172. func normalizeFilenames(fs []protocol.FileInfo) {
  173. for i := range fs {
  174. fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
  175. }
  176. }
  177. func nativeFileIterator(fn Iterator) Iterator {
  178. return func(fi FileIntf) bool {
  179. switch f := fi.(type) {
  180. case protocol.FileInfo:
  181. f.Name = osutil.NativeFilename(f.Name)
  182. return fn(f)
  183. case FileInfoTruncated:
  184. f.Name = osutil.NativeFilename(f.Name)
  185. return fn(f)
  186. default:
  187. panic("unknown interface type")
  188. }
  189. }
  190. }