set.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. // Package files provides a set type to track local/remote files with newness
  16. // checks. We must do a certain amount of normalization in here. We will get
  17. // fed paths with either native or wire-format separators and encodings
  18. // depending on who calls us. We transform paths to wire-format (NFC and
  19. // slashes) on the way to the database, and transform to native format
  20. // (varying separator and encoding) on the way back out.
  21. package files
  22. import (
  23. "sync"
  24. "github.com/syncthing/syncthing/internal/lamport"
  25. "github.com/syncthing/syncthing/internal/osutil"
  26. "github.com/syncthing/syncthing/internal/protocol"
  27. "github.com/syndtr/goleveldb/leveldb"
  28. )
  29. type Set struct {
  30. localVersion map[protocol.DeviceID]uint64
  31. mutex sync.Mutex
  32. folder string
  33. db *leveldb.DB
  34. blockmap *BlockMap
  35. }
  36. // FileIntf is the set of methods implemented by both protocol.FileInfo and
  37. // protocol.FileInfoTruncated.
  38. type FileIntf interface {
  39. Size() int64
  40. IsDeleted() bool
  41. IsInvalid() bool
  42. IsDirectory() bool
  43. IsSymlink() bool
  44. HasPermissionBits() bool
  45. }
  46. // The Iterator is called with either a protocol.FileInfo or a
  47. // protocol.FileInfoTruncated (depending on the method) and returns true to
  48. // continue iteration, false to stop.
  49. type Iterator func(f FileIntf) bool
  50. func NewSet(folder string, db *leveldb.DB) *Set {
  51. var s = Set{
  52. localVersion: make(map[protocol.DeviceID]uint64),
  53. folder: folder,
  54. db: db,
  55. blockmap: NewBlockMap(db, folder),
  56. }
  57. ldbCheckGlobals(db, []byte(folder))
  58. var deviceID protocol.DeviceID
  59. ldbWithAllFolderTruncated(db, []byte(folder), func(device []byte, f protocol.FileInfoTruncated) bool {
  60. copy(deviceID[:], device)
  61. if f.LocalVersion > s.localVersion[deviceID] {
  62. s.localVersion[deviceID] = f.LocalVersion
  63. }
  64. lamport.Default.Tick(f.Version)
  65. return true
  66. })
  67. if debug {
  68. l.Debugf("loaded localVersion for %q: %#v", folder, s.localVersion)
  69. }
  70. clock(s.localVersion[protocol.LocalDeviceID])
  71. return &s
  72. }
  73. func (s *Set) Replace(device protocol.DeviceID, fs []protocol.FileInfo) {
  74. if debug {
  75. l.Debugf("%s Replace(%v, [%d])", s.folder, device, len(fs))
  76. }
  77. normalizeFilenames(fs)
  78. s.mutex.Lock()
  79. defer s.mutex.Unlock()
  80. s.localVersion[device] = ldbReplace(s.db, []byte(s.folder), device[:], fs)
  81. if len(fs) == 0 {
  82. // Reset the local version if all files were removed.
  83. s.localVersion[device] = 0
  84. }
  85. if device == protocol.LocalDeviceID {
  86. s.blockmap.Drop()
  87. s.blockmap.Add(fs)
  88. }
  89. }
  90. func (s *Set) ReplaceWithDelete(device protocol.DeviceID, fs []protocol.FileInfo) {
  91. if debug {
  92. l.Debugf("%s ReplaceWithDelete(%v, [%d])", s.folder, device, len(fs))
  93. }
  94. normalizeFilenames(fs)
  95. s.mutex.Lock()
  96. defer s.mutex.Unlock()
  97. if lv := ldbReplaceWithDelete(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  98. s.localVersion[device] = lv
  99. }
  100. if device == protocol.LocalDeviceID {
  101. s.blockmap.Drop()
  102. s.blockmap.Add(fs)
  103. }
  104. }
  105. func (s *Set) Update(device protocol.DeviceID, fs []protocol.FileInfo) {
  106. if debug {
  107. l.Debugf("%s Update(%v, [%d])", s.folder, device, len(fs))
  108. }
  109. normalizeFilenames(fs)
  110. s.mutex.Lock()
  111. defer s.mutex.Unlock()
  112. if device == protocol.LocalDeviceID {
  113. discards := make([]protocol.FileInfo, 0, len(fs))
  114. updates := make([]protocol.FileInfo, 0, len(fs))
  115. for _, newFile := range fs {
  116. existingFile, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(newFile.Name))
  117. if !ok || existingFile.Version <= newFile.Version {
  118. discards = append(discards, existingFile)
  119. updates = append(updates, newFile)
  120. }
  121. }
  122. s.blockmap.Discard(discards)
  123. s.blockmap.Update(updates)
  124. }
  125. if lv := ldbUpdate(s.db, []byte(s.folder), device[:], fs); lv > s.localVersion[device] {
  126. s.localVersion[device] = lv
  127. }
  128. }
  129. func (s *Set) WithNeed(device protocol.DeviceID, fn Iterator) {
  130. if debug {
  131. l.Debugf("%s WithNeed(%v)", s.folder, device)
  132. }
  133. ldbWithNeed(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  134. }
  135. func (s *Set) WithNeedTruncated(device protocol.DeviceID, fn Iterator) {
  136. if debug {
  137. l.Debugf("%s WithNeedTruncated(%v)", s.folder, device)
  138. }
  139. ldbWithNeed(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  140. }
  141. func (s *Set) WithHave(device protocol.DeviceID, fn Iterator) {
  142. if debug {
  143. l.Debugf("%s WithHave(%v)", s.folder, device)
  144. }
  145. ldbWithHave(s.db, []byte(s.folder), device[:], false, nativeFileIterator(fn))
  146. }
  147. func (s *Set) WithHaveTruncated(device protocol.DeviceID, fn Iterator) {
  148. if debug {
  149. l.Debugf("%s WithHaveTruncated(%v)", s.folder, device)
  150. }
  151. ldbWithHave(s.db, []byte(s.folder), device[:], true, nativeFileIterator(fn))
  152. }
  153. func (s *Set) WithGlobal(fn Iterator) {
  154. if debug {
  155. l.Debugf("%s WithGlobal()", s.folder)
  156. }
  157. ldbWithGlobal(s.db, []byte(s.folder), false, nativeFileIterator(fn))
  158. }
  159. func (s *Set) WithGlobalTruncated(fn Iterator) {
  160. if debug {
  161. l.Debugf("%s WithGlobalTruncated()", s.folder)
  162. }
  163. ldbWithGlobal(s.db, []byte(s.folder), true, nativeFileIterator(fn))
  164. }
  165. func (s *Set) Get(device protocol.DeviceID, file string) (protocol.FileInfo, bool) {
  166. f, ok := ldbGet(s.db, []byte(s.folder), device[:], []byte(osutil.NormalizedFilename(file)))
  167. f.Name = osutil.NativeFilename(f.Name)
  168. return f, ok
  169. }
  170. func (s *Set) GetGlobal(file string) (protocol.FileInfo, bool) {
  171. f, ok := ldbGetGlobal(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
  172. f.Name = osutil.NativeFilename(f.Name)
  173. return f, ok
  174. }
  175. func (s *Set) Availability(file string) []protocol.DeviceID {
  176. return ldbAvailability(s.db, []byte(s.folder), []byte(osutil.NormalizedFilename(file)))
  177. }
  178. func (s *Set) LocalVersion(device protocol.DeviceID) uint64 {
  179. s.mutex.Lock()
  180. defer s.mutex.Unlock()
  181. return s.localVersion[device]
  182. }
  183. // ListFolders returns the folder IDs seen in the database.
  184. func ListFolders(db *leveldb.DB) []string {
  185. return ldbListFolders(db)
  186. }
  187. // DropFolder clears out all information related to the given folder from the
  188. // database.
  189. func DropFolder(db *leveldb.DB, folder string) {
  190. ldbDropFolder(db, []byte(folder))
  191. bm := &BlockMap{
  192. db: db,
  193. folder: folder,
  194. }
  195. bm.Drop()
  196. }
  197. func normalizeFilenames(fs []protocol.FileInfo) {
  198. for i := range fs {
  199. fs[i].Name = osutil.NormalizedFilename(fs[i].Name)
  200. }
  201. }
  202. func nativeFileIterator(fn Iterator) Iterator {
  203. return func(fi FileIntf) bool {
  204. switch f := fi.(type) {
  205. case protocol.FileInfo:
  206. f.Name = osutil.NativeFilename(f.Name)
  207. return fn(f)
  208. case protocol.FileInfoTruncated:
  209. f.Name = osutil.NativeFilename(f.Name)
  210. return fn(f)
  211. default:
  212. panic("unknown interface type")
  213. }
  214. }
  215. }