set.go 6.9 KB

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