set.go 7.8 KB

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