set.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. stdsync "sync"
  15. "github.com/syncthing/syncthing/lib/osutil"
  16. "github.com/syncthing/syncthing/lib/protocol"
  17. "github.com/syncthing/syncthing/lib/sync"
  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. sizeTracker
  27. }
  28. // FileIntf is the set of methods implemented by both protocol.FileInfo and
  29. // protocol.FileInfoTruncated.
  30. type FileIntf interface {
  31. Size() int64
  32. IsDeleted() bool
  33. IsInvalid() bool
  34. IsDirectory() bool
  35. IsSymlink() bool
  36. HasPermissionBits() bool
  37. }
  38. // The Iterator is called with either a protocol.FileInfo or a
  39. // protocol.FileInfoTruncated (depending on the method) and returns true to
  40. // continue iteration, false to stop.
  41. type Iterator func(f FileIntf) bool
  42. type sizeTracker struct {
  43. files int
  44. deleted int
  45. bytes int64
  46. mut stdsync.Mutex
  47. }
  48. func (s *sizeTracker) addFile(f FileIntf) {
  49. if f.IsInvalid() {
  50. return
  51. }
  52. s.mut.Lock()
  53. if f.IsDeleted() {
  54. s.deleted++
  55. } else {
  56. s.files++
  57. }
  58. s.bytes += f.Size()
  59. s.mut.Unlock()
  60. }
  61. func (s *sizeTracker) removeFile(f FileIntf) {
  62. if f.IsInvalid() {
  63. return
  64. }
  65. s.mut.Lock()
  66. if f.IsDeleted() {
  67. s.deleted--
  68. } else {
  69. s.files--
  70. }
  71. s.bytes -= f.Size()
  72. s.mut.Unlock()
  73. }
  74. func (s *sizeTracker) Size() (files, deleted int, bytes int64) {
  75. s.mut.Lock()
  76. defer s.mut.Unlock()
  77. return s.files, s.deleted, s.bytes
  78. }
  79. func NewFileSet(folder string, db *leveldb.DB) *FileSet {
  80. var s = FileSet{
  81. localVersion: make(map[protocol.DeviceID]int64),
  82. folder: folder,
  83. db: db,
  84. blockmap: NewBlockMap(db, folder),
  85. mutex: sync.NewMutex(),
  86. }
  87. ldbCheckGlobals(db, []byte(folder))
  88. var deviceID protocol.DeviceID
  89. ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f FileInfoTruncated) bool {
  90. copy(deviceID[:], device)
  91. if f.LocalVersion > s.localVersion[deviceID] {
  92. s.localVersion[deviceID] = f.LocalVersion
  93. }
  94. if deviceID == protocol.LocalDeviceID {
  95. s.addFile(f)
  96. }
  97. return true
  98. })
  99. l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
  100. clock(s.localVersion[protocol.LocalDeviceID])
  101. return &s
  102. }
  103. func (s *FileSet) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
  104. l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
  105. normalizeFilenames(fs)
  106. s.mutex.Lock()
  107. defer s.mutex.Unlock()
  108. s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs)
  109. if len(fs) == 0 {
  110. // Reset the local version if all files were removed.
  111. s.localVersion[device] = 0
  112. }
  113. if device == protocol.LocalDeviceID {
  114. s.blockmap.Drop()
  115. s.blockmap.Add(fs)
  116. }
  117. }
  118. func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
  119. l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
  120. normalizeFilenames(fs)
  121. s.mutex.Lock()
  122. defer s.mutex.Unlock()
  123. if device == protocol.LocalDeviceID {
  124. discards := make([]protocol.FileInfo, 0, len(fs))
  125. updates := make([]protocol.FileInfo, 0, len(fs))
  126. for _, newFile := range fs {
  127. existingFile, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(newFile.Name))
  128. if !ok || !existingFile.Version.Equal(newFile.Version) {
  129. discards = append(discards, existingFile)
  130. updates = append(updates, newFile)
  131. }
  132. }
  133. s.blockmap.Discard(discards)
  134. s.blockmap.Update(updates)
  135. }
  136. if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs, &s.sizeTracker); lv > s.localVersion[device] {
  137. s.localVersion[device] = lv
  138. }
  139. }
  140. func (s *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
  141. l.Debugf("%s WithNeed(%v)", s.folder, device)
  142. ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  143. }
  144. func (s *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
  145. l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
  146. ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  147. }
  148. func (s *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
  149. l.Debugf("%s WithHave(%v)", s.folder, device)
  150. ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  151. }
  152. func (s *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
  153. l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
  154. ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  155. }
  156. func (s *FileSet) WithGlobal(fn Iterator) {
  157. l.Debugf("%s WithGlobal()", s.folder)
  158. ldbWithGlobal(s.db, []byte(s.folder), nil, false, nativeFileIterator(fn))
  159. }
  160. func (s *FileSet) WithGlobalTruncated(fn Iterator) {
  161. l.Debugf("%s WithGlobalTruncated()", s.folder)
  162. ldbWithGlobal(s.db, []byte(s.folder), nil, true, nativeFileIterator(fn))
  163. }
  164. func (s *FileSet) WithPrefixedGlobalTruncated(prefix string, fn Iterator) {
  165. l.Debugf("%s WithPrefixedGlobalTruncated()", s.folder, prefix)
  166. ldbWithGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
  167. }
  168. func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
  169. f, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
  170. f.Name = osutil.NativeFilename(f.Name)
  171. return f, ok
  172. }
  173. func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
  174. fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
  175. if !ok {
  176. return protocol.FileInfo{}, false
  177. }
  178. f := fi.(protocol.FileInfo)
  179. f.Name = osutil.NativeFilename(f.Name)
  180. return f, true
  181. }
  182. func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
  183. fi, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
  184. if !ok {
  185. return FileInfoTruncated{}, false
  186. }
  187. f := fi.(FileInfoTruncated)
  188. f.Name = osutil.NativeFilename(f.Name)
  189. return f, true
  190. }
  191. func (s *FileSet) Availability(file string) []protocol.DeviceID {
  192. return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
  193. }
  194. func (s *FileSet) LocalVersion(device protocol.DeviceID) int64 {
  195. s.mutex.Lock()
  196. defer s.mutex.Unlock()
  197. return s.localVersion[device]
  198. }
  199. // ListFolders returns the folder IDs seen in the database.
  200. func ListFolders(db *leveldb.DB) []string {
  201. return ldbListFolders(db)
  202. }
  203. // DropFolder clears out all information related to the given folder from the
  204. // database.
  205. func DropFolder(db *leveldb.DB, folder string) {
  206. ldbDropFolder(db, []byte(folder))
  207. bm := &BlockMap{
  208. db: db,
  209. folder: folder,
  210. }
  211. bm.Drop()
  212. NewVirtualMtimeRepo(db, folder).Drop()
  213. }
  214. func normalizeFilenames(fs []protocol.FileInfo) {
  215. for i := range fs {
  216. fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
  217. }
  218. }
  219. func nativeFileIterator(fn Iterator) Iterator {
  220. return func(fi FileIntf) bool {
  221. switch f := fi.(type) {
  222. case protocol.FileInfo:
  223. f.Name = osutil.NativeFilename(f.Name)
  224. return fn(f)
  225. case FileInfoTruncated:
  226. f.Name = osutil.NativeFilename(f.Name)
  227. return fn(f)
  228. default:
  229. panic("unknown interface type")
  230. }
  231. }
  232. }