set.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. "sync"
  15. "github.com/syncthing/protocol"
  16. "github.com/syncthing/syncthing/internal/lamport"
  17. "github.com/syncthing/syncthing/internal/osutil"
  18. "github.com/syndtr/goleveldb/leveldb"
  19. )
  20. type FileSet struct {
  21. localVersion map[protocol.DeviceID]int64
  22. mutex sync.Mutex
  23. folder string
  24. db *leveldb.DB
  25. blockmap *BlockMap
  26. }
  27. // FileIntf is the set of methods implemented by both protocol.FileInfo and
  28. // protocol.FileInfoTruncated.
  29. type FileIntf interface {
  30. Size() int64
  31. IsDeleted() bool
  32. IsInvalid() bool
  33. IsDirectory() bool
  34. IsSymlink() bool
  35. HasPermissionBits() bool
  36. }
  37. // The Iterator is called with either a protocol.FileInfo or a
  38. // protocol.FileInfoTruncated (depending on the method) and returns true to
  39. // continue iteration, false to stop.
  40. type Iterator func(f FileIntf) bool
  41. func NewFileSet(folder string, db *leveldb.DB) *FileSet {
  42. var s = FileSet{
  43. localVersion: make(map[protocol.DeviceID]int64),
  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 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 *FileSet) 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 *FileSet) 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 *FileSet) 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, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(newFile.Name))
  108. if !ok || 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 *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
  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 *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
  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 *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
  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 *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
  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 *FileSet) WithGlobal(fn Iterator) {
  145. if debug {
  146. l.Debugf("%s WithGlobal()", s.folder)
  147. }
  148. ldbWithGlobal(s.db, []byte(s.folder), nil, false, nativeFileIterator(fn))
  149. }
  150. func (s *FileSet) WithGlobalTruncated(fn Iterator) {
  151. if debug {
  152. l.Debugf("%s WithGlobalTruncated()", s.folder)
  153. }
  154. ldbWithGlobal(s.db, []byte(s.folder), nil, true, nativeFileIterator(fn))
  155. }
  156. func (s *FileSet) WithPrefixedGlobalTruncated(prefix string, fn Iterator) {
  157. if debug {
  158. l.Debugf("%s WithPrefixedGlobalTruncated()", s.folder, prefix)
  159. }
  160. ldbWithGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
  161. }
  162. func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
  163. f, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
  164. f.Name = osutil.NativeFilename(f.Name)
  165. return f, ok
  166. }
  167. func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
  168. fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
  169. if !ok {
  170. return protocol.FileInfo{}, false
  171. }
  172. f := fi.(protocol.FileInfo)
  173. f.Name = osutil.NativeFilename(f.Name)
  174. return f, true
  175. }
  176. func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
  177. fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
  178. if !ok {
  179. return FileInfoTruncated{}, false
  180. }
  181. f := fi.(FileInfoTruncated)
  182. f.Name = osutil.NativeFilename(f.Name)
  183. return f, true
  184. }
  185. func (s *FileSet) Availability(file string) []protocol.DeviceID {
  186. return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
  187. }
  188. func (s *FileSet) LocalVersion(device protocol.DeviceID) int64 {
  189. s.mutex.Lock()
  190. defer s.mutex.Unlock()
  191. return s.localVersion[device]
  192. }
  193. // ListFolders returns the folder IDs seen in the database.
  194. func ListFolders(db *leveldb.DB) []string {
  195. return ldbListFolders(db)
  196. }
  197. // DropFolder clears out all information related to the given folder from the
  198. // database.
  199. func DropFolder(db *leveldb.DB, folder string) {
  200. ldbDropFolder(db, []byte(folder))
  201. bm := &BlockMap{
  202. db: db,
  203. folder: folder,
  204. }
  205. bm.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. }