set.go 7.4 KB

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