folder_recvonly.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright (C) 2018 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. "sort"
  9. "time"
  10. "github.com/syncthing/syncthing/lib/config"
  11. "github.com/syncthing/syncthing/lib/db"
  12. "github.com/syncthing/syncthing/lib/events"
  13. "github.com/syncthing/syncthing/lib/fs"
  14. "github.com/syncthing/syncthing/lib/ignore"
  15. "github.com/syncthing/syncthing/lib/protocol"
  16. "github.com/syncthing/syncthing/lib/versioner"
  17. )
  18. func init() {
  19. folderFactories[config.FolderTypeReceiveOnly] = newReceiveOnlyFolder
  20. }
  21. /*
  22. receiveOnlyFolder is a folder that does not propagate local changes outward.
  23. It does this by the following general mechanism (not all of which is
  24. implemted in this file):
  25. - Local changes are scanned and versioned as usual, but get the
  26. FlagLocalReceiveOnly bit set.
  27. - When changes are sent to the cluster this bit gets converted to the
  28. Invalid bit (like all other local flags, currently) and also the Version
  29. gets set to the empty version. The reason for clearing the Version is to
  30. ensure that other devices will not consider themselves out of date due to
  31. our change.
  32. - The database layer accounts sizes per flag bit, so we can know how many
  33. files have been changed locally. We use this to trigger a "Revert" option
  34. on the folder when the amount of locally changed data is nonzero.
  35. - To revert we take the files which have changed and reset their version
  36. counter down to zero. The next pull will replace our changed version with
  37. the globally latest. As this is a user-initiated operation we do not cause
  38. conflict copies when reverting.
  39. - When pulling normally (i.e., not in the revert case) with local changes,
  40. normal conflict resolution will apply. Conflict copies will be created,
  41. but not propagated outwards (because receive only, right).
  42. Implementation wise a receiveOnlyFolder is just a sendReceiveFolder that
  43. sets an extra bit on local changes and has a Revert method.
  44. */
  45. type receiveOnlyFolder struct {
  46. *sendReceiveFolder
  47. }
  48. func newReceiveOnlyFolder(model *model, fset *db.FileSet, ignores *ignore.Matcher, cfg config.FolderConfiguration, ver versioner.Versioner, fs fs.Filesystem, evLogger events.Logger) service {
  49. sr := newSendReceiveFolder(model, fset, ignores, cfg, ver, fs, evLogger).(*sendReceiveFolder)
  50. sr.localFlags = protocol.FlagLocalReceiveOnly // gets propagated to the scanner, and set on locally changed files
  51. return &receiveOnlyFolder{sr}
  52. }
  53. func (f *receiveOnlyFolder) Revert() {
  54. f.setState(FolderScanning)
  55. defer f.setState(FolderIdle)
  56. scanChan := make(chan string)
  57. go f.pullScannerRoutine(scanChan)
  58. defer close(scanChan)
  59. delQueue := &deleteQueue{
  60. handler: f, // for the deleteItemOnDisk and deleteDirOnDisk methods
  61. ignores: f.ignores,
  62. scanChan: scanChan,
  63. }
  64. batch := make([]protocol.FileInfo, 0, maxBatchSizeFiles)
  65. batchSizeBytes := 0
  66. f.fset.WithHave(protocol.LocalDeviceID, func(intf db.FileIntf) bool {
  67. fi := intf.(protocol.FileInfo)
  68. if !fi.IsReceiveOnlyChanged() {
  69. // We're only interested in files that have changed locally in
  70. // receive only mode.
  71. return true
  72. }
  73. if len(fi.Version.Counters) == 1 && fi.Version.Counters[0].ID == f.shortID {
  74. // We are the only device mentioned in the version vector so the
  75. // file must originate here. A revert then means to delete it.
  76. // We'll delete files directly, directories get queued and
  77. // handled below.
  78. handled, err := delQueue.handle(fi)
  79. if err != nil {
  80. l.Infof("Revert: deleting %s: %v\n", fi.Name, err)
  81. return true // continue
  82. }
  83. if !handled {
  84. return true // continue
  85. }
  86. fi = protocol.FileInfo{
  87. Name: fi.Name,
  88. Type: fi.Type,
  89. ModifiedS: fi.ModifiedS,
  90. ModifiedNs: fi.ModifiedNs,
  91. ModifiedBy: f.shortID,
  92. Deleted: true,
  93. Version: protocol.Vector{}, // if this file ever resurfaces anywhere we want our delete to be strictly older
  94. }
  95. } else {
  96. // Revert means to throw away our local changes. We reset the
  97. // version to the empty vector, which is strictly older than any
  98. // other existing version. It is not in conflict with anything,
  99. // either, so we will not create a conflict copy of our local
  100. // changes.
  101. fi.Version = protocol.Vector{}
  102. fi.LocalFlags &^= protocol.FlagLocalReceiveOnly
  103. }
  104. batch = append(batch, fi)
  105. batchSizeBytes += fi.ProtoSize()
  106. if len(batch) >= maxBatchSizeFiles || batchSizeBytes >= maxBatchSizeBytes {
  107. f.updateLocalsFromScanning(batch)
  108. batch = batch[:0]
  109. batchSizeBytes = 0
  110. }
  111. return true
  112. })
  113. if len(batch) > 0 {
  114. f.updateLocalsFromScanning(batch)
  115. }
  116. batch = batch[:0]
  117. batchSizeBytes = 0
  118. // Handle any queued directories
  119. deleted, err := delQueue.flush()
  120. if err != nil {
  121. l.Infoln("Revert:", err)
  122. }
  123. now := time.Now()
  124. for _, dir := range deleted {
  125. batch = append(batch, protocol.FileInfo{
  126. Name: dir,
  127. Type: protocol.FileInfoTypeDirectory,
  128. ModifiedS: now.Unix(),
  129. ModifiedBy: f.shortID,
  130. Deleted: true,
  131. Version: protocol.Vector{},
  132. })
  133. }
  134. if len(batch) > 0 {
  135. f.updateLocalsFromScanning(batch)
  136. }
  137. // We will likely have changed our local index, but that won't trigger a
  138. // pull by itself. Make sure we schedule one so that we start
  139. // downloading files.
  140. f.SchedulePull()
  141. }
  142. // deleteQueue handles deletes by delegating to a handler and queuing
  143. // directories for last.
  144. type deleteQueue struct {
  145. handler interface {
  146. deleteItemOnDisk(item protocol.FileInfo, scanChan chan<- string) error
  147. deleteDirOnDisk(dir string, scanChan chan<- string) error
  148. }
  149. ignores *ignore.Matcher
  150. dirs []string
  151. scanChan chan<- string
  152. }
  153. func (q *deleteQueue) handle(fi protocol.FileInfo) (bool, error) {
  154. // Things that are ignored but not marked deletable are not processed.
  155. ign := q.ignores.Match(fi.Name)
  156. if ign.IsIgnored() && !ign.IsDeletable() {
  157. return false, nil
  158. }
  159. // Directories are queued for later processing.
  160. if fi.IsDirectory() {
  161. q.dirs = append(q.dirs, fi.Name)
  162. return false, nil
  163. }
  164. // Kill it.
  165. err := q.handler.deleteItemOnDisk(fi, q.scanChan)
  166. return true, err
  167. }
  168. func (q *deleteQueue) flush() ([]string, error) {
  169. // Process directories from the leaves inward.
  170. sort.Sort(sort.Reverse(sort.StringSlice(q.dirs)))
  171. var firstError error
  172. var deleted []string
  173. for _, dir := range q.dirs {
  174. if err := q.handler.deleteDirOnDisk(dir, q.scanChan); err == nil {
  175. deleted = append(deleted, dir)
  176. } else if err != nil && firstError == nil {
  177. firstError = err
  178. }
  179. }
  180. return deleted, firstError
  181. }