devicedownloadstate.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // Copyright (C) 2015 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 https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "github.com/syncthing/syncthing/lib/protocol"
  9. "github.com/syncthing/syncthing/lib/sync"
  10. )
  11. // deviceFolderFileDownloadState holds current download state of a file that
  12. // a remote device has advertised. blockIndexes represends indexes within
  13. // FileInfo.Blocks that the remote device already has, and version represents
  14. // the version of the file that the remote device is downloading.
  15. type deviceFolderFileDownloadState struct {
  16. blockIndexes []int32
  17. version protocol.Vector
  18. blockSize int
  19. }
  20. // deviceFolderDownloadState holds current download state of all files that
  21. // a remote device is currently downloading in a specific folder.
  22. type deviceFolderDownloadState struct {
  23. mut sync.RWMutex
  24. files map[string]deviceFolderFileDownloadState
  25. }
  26. // Has returns whether a block at that specific index, and that specific version of the file
  27. // is currently available on the remote device for pulling from a temporary file.
  28. func (p *deviceFolderDownloadState) Has(file string, version protocol.Vector, index int32) bool {
  29. p.mut.RLock()
  30. defer p.mut.RUnlock()
  31. local, ok := p.files[file]
  32. if !ok || !local.version.Equal(version) {
  33. return false
  34. }
  35. for _, existingIndex := range local.blockIndexes {
  36. if existingIndex == index {
  37. return true
  38. }
  39. }
  40. return false
  41. }
  42. // Update updates internal state of what has been downloaded into the temporary
  43. // files by the remote device for this specific folder.
  44. func (p *deviceFolderDownloadState) Update(updates []protocol.FileDownloadProgressUpdate) {
  45. p.mut.Lock()
  46. defer p.mut.Unlock()
  47. for _, update := range updates {
  48. local, ok := p.files[update.Name]
  49. if update.UpdateType == protocol.UpdateTypeForget && ok && local.version.Equal(update.Version) {
  50. delete(p.files, update.Name)
  51. } else if update.UpdateType == protocol.UpdateTypeAppend {
  52. if !ok {
  53. local = deviceFolderFileDownloadState{
  54. blockIndexes: update.BlockIndexes,
  55. version: update.Version,
  56. blockSize: int(update.BlockSize),
  57. }
  58. } else if !local.version.Equal(update.Version) {
  59. local.blockIndexes = append(local.blockIndexes[:0], update.BlockIndexes...)
  60. local.version = update.Version
  61. local.blockSize = int(update.BlockSize)
  62. } else {
  63. local.blockIndexes = append(local.blockIndexes, update.BlockIndexes...)
  64. }
  65. p.files[update.Name] = local
  66. }
  67. }
  68. }
  69. func (p *deviceFolderDownloadState) BytesDownloaded() int64 {
  70. var res int64
  71. for _, state := range p.files {
  72. // BlockSize is a new field introduced in 1.4.1, thus a fallback
  73. // is required (will potentially underrepresent downloaded bytes).
  74. if state.blockSize != 0 {
  75. res += int64(len(state.blockIndexes) * state.blockSize)
  76. } else {
  77. res += int64(len(state.blockIndexes) * protocol.MinBlockSize)
  78. }
  79. }
  80. return res
  81. }
  82. // GetBlockCounts returns a map filename -> number of blocks downloaded.
  83. func (p *deviceFolderDownloadState) GetBlockCounts() map[string]int {
  84. p.mut.RLock()
  85. res := make(map[string]int, len(p.files))
  86. for name, state := range p.files {
  87. res[name] = len(state.blockIndexes)
  88. }
  89. p.mut.RUnlock()
  90. return res
  91. }
  92. // deviceDownloadState represents the state of all in progress downloads
  93. // for all folders of a specific device.
  94. type deviceDownloadState struct {
  95. mut sync.RWMutex
  96. folders map[string]*deviceFolderDownloadState
  97. }
  98. // Update updates internal state of what has been downloaded into the temporary
  99. // files by the remote device for this specific folder.
  100. func (t *deviceDownloadState) Update(folder string, updates []protocol.FileDownloadProgressUpdate) {
  101. if t == nil {
  102. return
  103. }
  104. t.mut.RLock()
  105. f, ok := t.folders[folder]
  106. t.mut.RUnlock()
  107. if !ok {
  108. f = &deviceFolderDownloadState{
  109. mut: sync.NewRWMutex(),
  110. files: make(map[string]deviceFolderFileDownloadState),
  111. }
  112. t.mut.Lock()
  113. t.folders[folder] = f
  114. t.mut.Unlock()
  115. }
  116. f.Update(updates)
  117. }
  118. // Has returns whether block at that specific index, and that specific version of the file
  119. // is currently available on the remote device for pulling from a temporary file.
  120. func (t *deviceDownloadState) Has(folder, file string, version protocol.Vector, index int32) bool {
  121. if t == nil {
  122. return false
  123. }
  124. t.mut.RLock()
  125. f, ok := t.folders[folder]
  126. t.mut.RUnlock()
  127. if !ok {
  128. return false
  129. }
  130. return f.Has(file, version, index)
  131. }
  132. // GetBlockCounts returns a map filename -> number of blocks downloaded for the
  133. // given folder.
  134. func (t *deviceDownloadState) GetBlockCounts(folder string) map[string]int {
  135. if t == nil {
  136. return nil
  137. }
  138. t.mut.RLock()
  139. defer t.mut.RUnlock()
  140. for name, state := range t.folders {
  141. if name == folder {
  142. return state.GetBlockCounts()
  143. }
  144. }
  145. return nil
  146. }
  147. func (t *deviceDownloadState) BytesDownloaded(folder string) int64 {
  148. if t == nil {
  149. return 0
  150. }
  151. t.mut.RLock()
  152. defer t.mut.RUnlock()
  153. for name, state := range t.folders {
  154. if name == folder {
  155. return state.BytesDownloaded()
  156. }
  157. }
  158. return 0
  159. }
  160. func newDeviceDownloadState() *deviceDownloadState {
  161. return &deviceDownloadState{
  162. mut: sync.NewRWMutex(),
  163. folders: make(map[string]*deviceFolderDownloadState),
  164. }
  165. }