sharedpullerstate.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 https://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "encoding/binary"
  9. "time"
  10. "github.com/pkg/errors"
  11. "github.com/syncthing/syncthing/lib/fs"
  12. "github.com/syncthing/syncthing/lib/osutil"
  13. "github.com/syncthing/syncthing/lib/protocol"
  14. "github.com/syncthing/syncthing/lib/sync"
  15. )
  16. // A sharedPullerState is kept for each file that is being synced and is kept
  17. // updated along the way.
  18. type sharedPullerState struct {
  19. // Immutable, does not require locking
  20. file protocol.FileInfo // The new file (desired end state)
  21. fs fs.Filesystem
  22. folder string
  23. tempName string
  24. realName string
  25. reused int // Number of blocks reused from temporary file
  26. ignorePerms bool
  27. hasCurFile bool // Whether curFile is set
  28. curFile protocol.FileInfo // The file as it exists now in our database
  29. sparse bool
  30. created time.Time
  31. fsync bool
  32. // Mutable, must be locked for access
  33. err error // The first error we hit
  34. writer *lockedWriterAt // Wraps fd to prevent fd closing at the same time as writing
  35. copyTotal int // Total number of copy actions for the whole job
  36. pullTotal int // Total number of pull actions for the whole job
  37. copyOrigin int // Number of blocks copied from the original file
  38. copyOriginShifted int // Number of blocks copied from the original file but shifted
  39. copyNeeded int // Number of copy actions still pending
  40. pullNeeded int // Number of block pulls still pending
  41. updated time.Time // Time when any of the counters above were last updated
  42. closed bool // True if the file has been finalClosed.
  43. available []int // Indexes of the blocks that are available in the temporary file
  44. availableUpdated time.Time // Time when list of available blocks was last updated
  45. mut sync.RWMutex // Protects the above
  46. }
  47. func newSharedPullerState(file protocol.FileInfo, fs fs.Filesystem, folderID, tempName string, blocks []protocol.BlockInfo, reused []int, ignorePerms, hasCurFile bool, curFile protocol.FileInfo, sparse bool, fsync bool) *sharedPullerState {
  48. return &sharedPullerState{
  49. file: file,
  50. fs: fs,
  51. folder: folderID,
  52. tempName: tempName,
  53. realName: file.Name,
  54. copyTotal: len(blocks),
  55. copyNeeded: len(blocks),
  56. reused: len(reused),
  57. updated: time.Now(),
  58. available: reused,
  59. availableUpdated: time.Now(),
  60. ignorePerms: ignorePerms,
  61. hasCurFile: hasCurFile,
  62. curFile: curFile,
  63. mut: sync.NewRWMutex(),
  64. sparse: sparse,
  65. fsync: fsync,
  66. created: time.Now(),
  67. }
  68. }
  69. // A momentary state representing the progress of the puller
  70. type pullerProgress struct {
  71. Total int `json:"total"`
  72. Reused int `json:"reused"`
  73. CopiedFromOrigin int `json:"copiedFromOrigin"`
  74. CopiedFromOriginShifted int `json:"copiedFromOriginShifted"`
  75. CopiedFromElsewhere int `json:"copiedFromElsewhere"`
  76. Pulled int `json:"pulled"`
  77. Pulling int `json:"pulling"`
  78. BytesDone int64 `json:"bytesDone"`
  79. BytesTotal int64 `json:"bytesTotal"`
  80. }
  81. // lockedWriterAt adds a lock to protect from closing the fd at the same time as writing.
  82. // WriteAt() is goroutine safe by itself, but not against for example Close().
  83. type lockedWriterAt struct {
  84. mut sync.RWMutex
  85. fd fs.File
  86. }
  87. // WriteAt itself is goroutine safe, thus just needs to acquire a read-lock to
  88. // prevent closing concurrently (see SyncClose).
  89. func (w *lockedWriterAt) WriteAt(p []byte, off int64) (n int, err error) {
  90. w.mut.RLock()
  91. defer w.mut.RUnlock()
  92. return w.fd.WriteAt(p, off)
  93. }
  94. // SyncClose ensures that no more writes are happening before going ahead and
  95. // syncing and closing the fd, thus needs to acquire a write-lock.
  96. func (w *lockedWriterAt) SyncClose(fsync bool) error {
  97. w.mut.Lock()
  98. defer w.mut.Unlock()
  99. if fsync {
  100. if err := w.fd.Sync(); err != nil {
  101. // Sync() is nice if it works but not worth failing the
  102. // operation over if it fails.
  103. l.Debugf("fsync failed: %v", err)
  104. }
  105. }
  106. return w.fd.Close()
  107. }
  108. // tempFile returns the fd for the temporary file, reusing an open fd
  109. // or creating the file as necessary.
  110. func (s *sharedPullerState) tempFile() (*lockedWriterAt, error) {
  111. s.mut.Lock()
  112. defer s.mut.Unlock()
  113. // If we've already hit an error, return early
  114. if s.err != nil {
  115. return nil, s.err
  116. }
  117. // If the temp file is already open, return the file descriptor
  118. if s.writer != nil {
  119. return s.writer, nil
  120. }
  121. if err := s.addWriterLocked(); err != nil {
  122. s.failLocked(err)
  123. return nil, err
  124. }
  125. return s.writer, nil
  126. }
  127. func (s *sharedPullerState) addWriterLocked() error {
  128. return inWritableDir(s.tempFileInWritableDir, s.fs, s.tempName, s.ignorePerms)
  129. }
  130. // tempFileInWritableDir should only be called from tempFile.
  131. func (s *sharedPullerState) tempFileInWritableDir(_ string) error {
  132. // The permissions to use for the temporary file should be those of the
  133. // final file, except we need user read & write at minimum. The
  134. // permissions will be set to the final value later, but in the meantime
  135. // we don't want to have a temporary file with looser permissions than
  136. // the final outcome.
  137. mode := fs.FileMode(s.file.Permissions) | 0600
  138. if s.ignorePerms {
  139. // When ignorePerms is set we use a very permissive mode and let the
  140. // system umask filter it.
  141. mode = 0666
  142. }
  143. // Attempt to create the temp file
  144. // RDWR because of issue #2994.
  145. flags := fs.OptReadWrite
  146. if s.reused == 0 {
  147. flags |= fs.OptCreate | fs.OptExclusive
  148. } else if !s.ignorePerms {
  149. // With sufficiently bad luck when exiting or crashing, we may have
  150. // had time to chmod the temp file to read only state but not yet
  151. // moved it to its final name. This leaves us with a read only temp
  152. // file that we're going to try to reuse. To handle that, we need to
  153. // make sure we have write permissions on the file before opening it.
  154. //
  155. // When ignorePerms is set we trust that the permissions are fine
  156. // already and make no modification, as we would otherwise override
  157. // what the umask dictates.
  158. if err := s.fs.Chmod(s.tempName, mode); err != nil {
  159. return errors.Wrap(err, "setting perms on temp file")
  160. }
  161. }
  162. fd, err := s.fs.OpenFile(s.tempName, flags, mode)
  163. if err != nil {
  164. return errors.Wrap(err, "opening temp file")
  165. }
  166. // Hide the temporary file
  167. s.fs.Hide(s.tempName)
  168. // Don't truncate symlink files, as that will mean that the path will
  169. // contain a bunch of nulls.
  170. if s.sparse && !s.file.IsSymlink() {
  171. size := s.file.Size
  172. // Trailer added to encrypted files
  173. if len(s.file.Encrypted) > 0 {
  174. size += encryptionTrailerSize(s.file)
  175. }
  176. // Truncate sets the size of the file. This creates a sparse file or a
  177. // space reservation, depending on the underlying filesystem.
  178. if err := fd.Truncate(size); err != nil {
  179. // The truncate call failed. That can happen in some cases when
  180. // space reservation isn't possible or over some network
  181. // filesystems... This generally doesn't matter.
  182. if s.reused > 0 {
  183. // ... but if we are attempting to reuse a file we have a
  184. // corner case when the old file is larger than the new one
  185. // and we can't just overwrite blocks and let the old data
  186. // linger at the end. In this case we attempt a delete of
  187. // the file and hope for better luck next time, when we
  188. // should come around with s.reused == 0.
  189. fd.Close()
  190. if remErr := s.fs.Remove(s.tempName); remErr != nil {
  191. l.Debugln("failed to remove temporary file:", remErr)
  192. }
  193. return err
  194. }
  195. }
  196. }
  197. // Same fd will be used by all writers
  198. s.writer = &lockedWriterAt{sync.NewRWMutex(), fd}
  199. return nil
  200. }
  201. // fail sets the error on the puller state compose of error, and marks the
  202. // sharedPullerState as failed. Is a no-op when called on an already failed state.
  203. func (s *sharedPullerState) fail(err error) {
  204. s.mut.Lock()
  205. defer s.mut.Unlock()
  206. s.failLocked(err)
  207. }
  208. func (s *sharedPullerState) failLocked(err error) {
  209. if s.err != nil || err == nil {
  210. return
  211. }
  212. s.err = err
  213. }
  214. func (s *sharedPullerState) failed() error {
  215. s.mut.RLock()
  216. err := s.err
  217. s.mut.RUnlock()
  218. return err
  219. }
  220. func (s *sharedPullerState) copyDone(block protocol.BlockInfo) {
  221. s.mut.Lock()
  222. s.copyNeeded--
  223. s.updated = time.Now()
  224. s.available = append(s.available, int(block.Offset/int64(s.file.BlockSize())))
  225. s.availableUpdated = time.Now()
  226. l.Debugln("sharedPullerState", s.folder, s.file.Name, "copyNeeded ->", s.copyNeeded)
  227. s.mut.Unlock()
  228. }
  229. func (s *sharedPullerState) copiedFromOrigin() {
  230. s.mut.Lock()
  231. s.copyOrigin++
  232. s.updated = time.Now()
  233. s.mut.Unlock()
  234. }
  235. func (s *sharedPullerState) copiedFromOriginShifted() {
  236. s.mut.Lock()
  237. s.copyOrigin++
  238. s.copyOriginShifted++
  239. s.updated = time.Now()
  240. s.mut.Unlock()
  241. }
  242. func (s *sharedPullerState) pullStarted() {
  243. s.mut.Lock()
  244. s.copyTotal--
  245. s.copyNeeded--
  246. s.pullTotal++
  247. s.pullNeeded++
  248. s.updated = time.Now()
  249. l.Debugln("sharedPullerState", s.folder, s.file.Name, "pullNeeded start ->", s.pullNeeded)
  250. s.mut.Unlock()
  251. }
  252. func (s *sharedPullerState) pullDone(block protocol.BlockInfo) {
  253. s.mut.Lock()
  254. s.pullNeeded--
  255. s.updated = time.Now()
  256. s.available = append(s.available, int(block.Offset/int64(s.file.BlockSize())))
  257. s.availableUpdated = time.Now()
  258. l.Debugln("sharedPullerState", s.folder, s.file.Name, "pullNeeded done ->", s.pullNeeded)
  259. s.mut.Unlock()
  260. }
  261. // finalClose atomically closes and returns closed status of a file. A true
  262. // first return value means the file was closed and should be finished, with
  263. // the error indicating the success or failure of the close. A false first
  264. // return value indicates the file is not ready to be closed, or is already
  265. // closed and should in either case not be finished off now.
  266. func (s *sharedPullerState) finalClose() (bool, error) {
  267. s.mut.Lock()
  268. defer s.mut.Unlock()
  269. if s.closed {
  270. // Already closed
  271. return false, nil
  272. }
  273. if s.pullNeeded+s.copyNeeded != 0 && s.err == nil {
  274. // Not done yet, and not errored
  275. return false, nil
  276. }
  277. if len(s.file.Encrypted) > 0 {
  278. if err := s.finalizeEncrypted(); err != nil && s.err == nil {
  279. // This is our error as we weren't errored before.
  280. s.err = err
  281. }
  282. }
  283. if s.writer != nil {
  284. if err := s.writer.SyncClose(s.fsync); err != nil && s.err == nil {
  285. // This is our error as we weren't errored before.
  286. s.err = err
  287. }
  288. s.writer = nil
  289. }
  290. s.closed = true
  291. // Unhide the temporary file when we close it, as it's likely to
  292. // immediately be renamed to the final name. If this is a failed temp
  293. // file we will also unhide it, but I'm fine with that as we're now
  294. // leaving it around for potentially quite a while.
  295. s.fs.Unhide(s.tempName)
  296. return true, s.err
  297. }
  298. // finalizeEncrypted adds a trailer to the encrypted file containing the
  299. // serialized FileInfo and the length of that FileInfo. When initializing a
  300. // folder from encrypted data we can extract this FileInfo from the end of
  301. // the file and regain the original metadata.
  302. func (s *sharedPullerState) finalizeEncrypted() error {
  303. // Here the file is in native format, while encryption happens in
  304. // wire format (always slashes).
  305. wireFile := s.file
  306. wireFile.Name = osutil.NormalizedFilename(wireFile.Name)
  307. bs := make([]byte, encryptionTrailerSize(wireFile))
  308. n, err := wireFile.MarshalTo(bs)
  309. if err != nil {
  310. return err
  311. }
  312. binary.BigEndian.PutUint32(bs[n:], uint32(n))
  313. bs = bs[:n+4]
  314. if s.writer == nil {
  315. if err := s.addWriterLocked(); err != nil {
  316. return err
  317. }
  318. }
  319. if _, err := s.writer.WriteAt(bs, wireFile.Size); err != nil {
  320. return err
  321. }
  322. return nil
  323. }
  324. func encryptionTrailerSize(file protocol.FileInfo) int64 {
  325. return int64(file.ProtoSize()) + 4
  326. }
  327. // Progress returns the momentarily progress for the puller
  328. func (s *sharedPullerState) Progress() *pullerProgress {
  329. s.mut.RLock()
  330. defer s.mut.RUnlock()
  331. total := s.reused + s.copyTotal + s.pullTotal
  332. done := total - s.copyNeeded - s.pullNeeded
  333. file := len(s.file.Blocks)
  334. return &pullerProgress{
  335. Total: total,
  336. Reused: s.reused,
  337. CopiedFromOrigin: s.copyOrigin,
  338. CopiedFromElsewhere: s.copyTotal - s.copyNeeded - s.copyOrigin,
  339. Pulled: s.pullTotal - s.pullNeeded,
  340. Pulling: s.pullNeeded,
  341. BytesTotal: blocksToSize(total, file, s.file.BlockSize(), s.file.Size),
  342. BytesDone: blocksToSize(done, file, s.file.BlockSize(), s.file.Size),
  343. }
  344. }
  345. // Updated returns the time when any of the progress related counters was last updated.
  346. func (s *sharedPullerState) Updated() time.Time {
  347. s.mut.RLock()
  348. t := s.updated
  349. s.mut.RUnlock()
  350. return t
  351. }
  352. // AvailableUpdated returns the time last time list of available blocks was updated
  353. func (s *sharedPullerState) AvailableUpdated() time.Time {
  354. s.mut.RLock()
  355. t := s.availableUpdated
  356. s.mut.RUnlock()
  357. return t
  358. }
  359. // Available returns blocks available in the current temporary file
  360. func (s *sharedPullerState) Available() []int {
  361. s.mut.RLock()
  362. blocks := s.available
  363. s.mut.RUnlock()
  364. return blocks
  365. }
  366. func blocksToSize(blocks, blocksInFile, blockSize int, fileSize int64) int64 {
  367. // The last/only block has somewhere between 1 and blockSize bytes. We do
  368. // not know whether the smaller block is part of the blocks and use an
  369. // estimate assuming a random chance that the small block is contained.
  370. if blocksInFile == 0 {
  371. return 0
  372. }
  373. return int64(blocks)*int64(blockSize) - (int64(blockSize)-fileSize%int64(blockSize))*int64(blocks)/int64(blocksInFile)
  374. }