set.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. )
  19. type FileSet struct {
  20. localVersion map[protocol.DeviceID]int64
  21. mutex sync.Mutex
  22. folder string
  23. db *Instance
  24. blockmap *BlockMap
  25. localSize sizeTracker
  26. globalSize 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. if s.deleted < 0 || s.files < 0 {
  73. panic("bug: removed more than added")
  74. }
  75. s.mut.Unlock()
  76. }
  77. func (s *sizeTracker) Size() (files, deleted int, bytes int64) {
  78. s.mut.Lock()
  79. defer s.mut.Unlock()
  80. return s.files, s.deleted, s.bytes
  81. }
  82. func NewFileSet(folder string, db *Instance) *FileSet {
  83. var s = FileSet{
  84. localVersion: make(map[protocol.DeviceID]int64),
  85. folder: folder,
  86. db: db,
  87. blockmap: NewBlockMap(db, db.folderIdx.ID([]byte(folder))),
  88. mutex: sync.NewMutex(),
  89. }
  90. s.db.checkGlobals([]byte(folder), &s.globalSize)
  91. var deviceID protocol.DeviceID
  92. s.db.withAllFolderTruncated([]byte(folder), func(device []byte, f FileInfoTruncated) bool {
  93. copy(deviceID[:], device)
  94. if f.LocalVersion > s.localVersion[deviceID] {
  95. s.localVersion[deviceID] = f.LocalVersion
  96. }
  97. if deviceID == protocol.LocalDeviceID {
  98. s.localSize.addFile(f)
  99. }
  100. return true
  101. })
  102. l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
  103. clock(s.localVersion[protocol.LocalDeviceID])
  104. return &s
  105. }
  106. func (s *FileSet) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
  107. l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
  108. normalizeFilenames(fs)
  109. s.mutex.Lock()
  110. defer s.mutex.Unlock()
  111. s.localVersion[device] = s.db.replace([]byte(s.folder), device[:], fs, &s.localSize, &s.globalSize)
  112. if len(fs) == 0 {
  113. // Reset the local version if all files were removed.
  114. s.localVersion[device] = 0
  115. }
  116. if device == protocol.LocalDeviceID {
  117. s.blockmap.Drop()
  118. s.blockmap.Add(fs)
  119. }
  120. }
  121. func (s *FileSet) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
  122. l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
  123. normalizeFilenames(fs)
  124. s.mutex.Lock()
  125. defer s.mutex.Unlock()
  126. if device == protocol.LocalDeviceID {
  127. discards := make([]protocol.FileInfo, 0, len(fs))
  128. updates := make([]protocol.FileInfo, 0, len(fs))
  129. for _, newFile := range fs {
  130. existingFile, ok := s.db.getFile([]byte(s.folder), device[:], []byte(newFile.Name))
  131. if !ok || !existingFile.Version.Equal(newFile.Version) {
  132. discards = append(discards, existingFile)
  133. updates = append(updates, newFile)
  134. }
  135. }
  136. s.blockmap.Discard(discards)
  137. s.blockmap.Update(updates)
  138. }
  139. if lv := s.db.updateFiles([]byte(s.folder), device[:], fs, &s.localSize, &s.globalSize); lv > s.localVersion[device] {
  140. s.localVersion[device] = lv
  141. }
  142. }
  143. func (s *FileSet) WithNeed(device protocol.DeviceID, fn Iterator) {
  144. l.Debugf("%s WithNeed(%v)", s.folder, device)
  145. s.db.withNeed([]byte(s.folder), device[:], false, nativeFileIterator(fn))
  146. }
  147. func (s *FileSet) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
  148. l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
  149. s.db.withNeed([]byte(s.folder), device[:], true, nativeFileIterator(fn))
  150. }
  151. func (s *FileSet) WithHave(device protocol.DeviceID, fn Iterator) {
  152. l.Debugf("%s WithHave(%v)", s.folder, device)
  153. s.db.withHave([]byte(s.folder), device[:], false, nativeFileIterator(fn))
  154. }
  155. func (s *FileSet) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
  156. l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
  157. s.db.withHave([]byte(s.folder), device[:], true, nativeFileIterator(fn))
  158. }
  159. func (s *FileSet) WithGlobal(fn Iterator) {
  160. l.Debugf("%s WithGlobal()", s.folder)
  161. s.db.withGlobal([]byte(s.folder), nil, false, nativeFileIterator(fn))
  162. }
  163. func (s *FileSet) WithGlobalTruncated(fn Iterator) {
  164. l.Debugf("%s WithGlobalTruncated()", s.folder)
  165. s.db.withGlobal([]byte(s.folder), nil, true, nativeFileIterator(fn))
  166. }
  167. func (s *FileSet) WithPrefixedGlobalTruncated(prefix string, fn Iterator) {
  168. l.Debugf("%s WithPrefixedGlobalTruncated()", s.folder, prefix)
  169. s.db.withGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(prefix)), true, nativeFileIterator(fn))
  170. }
  171. func (s *FileSet) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
  172. f, ok := s.db.getFile([]byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
  173. f.Name = osutil.NativeFilename(f.Name)
  174. return f, ok
  175. }
  176. func (s *FileSet) GetGlobal(file string) (protocol.FileInfo, bool) {
  177. fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), false)
  178. if !ok {
  179. return protocol.FileInfo{}, false
  180. }
  181. f := fi.(protocol.FileInfo)
  182. f.Name = osutil.NativeFilename(f.Name)
  183. return f, true
  184. }
  185. func (s *FileSet) GetGlobalTruncated(file string) (FileInfoTruncated, bool) {
  186. fi, ok := s.db.getGlobal([]byte(s.folder), []byte(osutil.NormalizedFilename(file)), true)
  187. if !ok {
  188. return FileInfoTruncated{}, false
  189. }
  190. f := fi.(FileInfoTruncated)
  191. f.Name = osutil.NativeFilename(f.Name)
  192. return f, true
  193. }
  194. func (s *FileSet) Availability(file string) []protocol.DeviceID {
  195. return s.db.availability([]byte(s.folder), []byte(osutil.NormalizedFilename(file)))
  196. }
  197. func (s *FileSet) LocalVersion(device protocol.DeviceID) int64 {
  198. s.mutex.Lock()
  199. defer s.mutex.Unlock()
  200. return s.localVersion[device]
  201. }
  202. func (s *FileSet) LocalSize() (files, deleted int, bytes int64) {
  203. return s.localSize.Size()
  204. }
  205. func (s *FileSet) GlobalSize() (files, deleted int, bytes int64) {
  206. return s.globalSize.Size()
  207. }
  208. // DropFolder clears out all information related to the given folder from the
  209. // database.
  210. func DropFolder(db *Instance, folder string) {
  211. db.dropFolder([]byte(folder))
  212. bm := &BlockMap{
  213. db: db,
  214. folder: db.folderIdx.ID([]byte(folder)),
  215. }
  216. bm.Drop()
  217. NewVirtualMtimeRepo(db, folder).Drop()
  218. }
  219. func normalizeFilenames(fs []protocol.FileInfo) {
  220. for i := range fs {
  221. fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
  222. }
  223. }
  224. func nativeFileIterator(fn Iterator) Iterator {
  225. return func(fi FileIntf) bool {
  226. switch f := fi.(type) {
  227. case protocol.FileInfo:
  228. f.Name = osutil.NativeFilename(f.Name)
  229. return fn(f)
  230. case FileInfoTruncated:
  231. f.Name = osutil.NativeFilename(f.Name)
  232. return fn(f)
  233. default:
  234. panic("unknown interface type")
  235. }
  236. }
  237. }