set.go 7.3 KB

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