2014-11-16 21:13:20 +01:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 21:43:32 +02:00
|
|
|
//
|
2015-03-07 21:36:35 +01:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2017-02-09 07:52:18 +01:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-27 14:44:15 +02:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2020-11-09 15:33:32 +01:00
|
|
|
"encoding/binary"
|
2016-04-15 12:59:41 +02:00
|
|
|
"time"
|
2014-09-27 14:44:15 +02:00
|
|
|
|
2019-03-04 14:01:52 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2015-09-22 19:38:46 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 11:29:25 +02:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-09-27 14:44:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// A sharedPullerState is kept for each file that is being synced and is kept
|
|
|
|
// updated along the way.
|
|
|
|
type sharedPullerState struct {
|
|
|
|
// Immutable, does not require locking
|
2015-03-29 16:16:36 +02:00
|
|
|
file protocol.FileInfo // The new file (desired end state)
|
2017-08-19 16:36:56 +02:00
|
|
|
fs fs.Filesystem
|
2015-03-05 22:13:31 +01:00
|
|
|
folder string
|
|
|
|
tempName string
|
|
|
|
realName string
|
|
|
|
reused int // Number of blocks reused from temporary file
|
|
|
|
ignorePerms bool
|
2017-08-22 08:42:09 +02:00
|
|
|
hasCurFile bool // Whether curFile is set
|
|
|
|
curFile protocol.FileInfo // The file as it exists now in our database
|
2015-11-21 16:30:53 +01:00
|
|
|
sparse bool
|
2016-05-22 12:16:09 +02:00
|
|
|
created time.Time
|
2020-05-01 09:36:46 +02:00
|
|
|
fsync bool
|
2014-09-27 14:44:15 +02:00
|
|
|
|
|
|
|
// Mutable, must be locked for access
|
2019-08-16 09:35:19 +02:00
|
|
|
err error // The first error we hit
|
|
|
|
writer *lockedWriterAt // Wraps fd to prevent fd closing at the same time as writing
|
|
|
|
copyTotal int // Total number of copy actions for the whole job
|
|
|
|
pullTotal int // Total number of pull actions for the whole job
|
|
|
|
copyOrigin int // Number of blocks copied from the original file
|
|
|
|
copyOriginShifted int // Number of blocks copied from the original file but shifted
|
|
|
|
copyNeeded int // Number of copy actions still pending
|
|
|
|
pullNeeded int // Number of block pulls still pending
|
|
|
|
updated time.Time // Time when any of the counters above were last updated
|
|
|
|
closed bool // True if the file has been finalClosed.
|
2020-10-02 08:07:05 +02:00
|
|
|
available []int // Indexes of the blocks that are available in the temporary file
|
2019-08-16 09:35:19 +02:00
|
|
|
availableUpdated time.Time // Time when list of available blocks was last updated
|
|
|
|
mut sync.RWMutex // Protects the above
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2020-10-02 08:07:05 +02:00
|
|
|
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 {
|
2020-04-21 19:55:14 +02:00
|
|
|
return &sharedPullerState{
|
|
|
|
file: file,
|
|
|
|
fs: fs,
|
|
|
|
folder: folderID,
|
|
|
|
tempName: tempName,
|
|
|
|
realName: file.Name,
|
|
|
|
copyTotal: len(blocks),
|
|
|
|
copyNeeded: len(blocks),
|
|
|
|
reused: len(reused),
|
|
|
|
updated: time.Now(),
|
|
|
|
available: reused,
|
|
|
|
availableUpdated: time.Now(),
|
|
|
|
ignorePerms: ignorePerms,
|
|
|
|
hasCurFile: hasCurFile,
|
|
|
|
curFile: curFile,
|
|
|
|
mut: sync.NewRWMutex(),
|
|
|
|
sparse: sparse,
|
2020-05-01 09:36:46 +02:00
|
|
|
fsync: fsync,
|
2020-04-21 19:55:14 +02:00
|
|
|
created: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-17 00:18:59 +01:00
|
|
|
// A momentary state representing the progress of the puller
|
|
|
|
type pullerProgress struct {
|
2016-12-15 00:30:29 +01:00
|
|
|
Total int `json:"total"`
|
|
|
|
Reused int `json:"reused"`
|
|
|
|
CopiedFromOrigin int `json:"copiedFromOrigin"`
|
|
|
|
CopiedFromOriginShifted int `json:"copiedFromOriginShifted"`
|
|
|
|
CopiedFromElsewhere int `json:"copiedFromElsewhere"`
|
|
|
|
Pulled int `json:"pulled"`
|
|
|
|
Pulling int `json:"pulling"`
|
|
|
|
BytesDone int64 `json:"bytesDone"`
|
|
|
|
BytesTotal int64 `json:"bytesTotal"`
|
2014-11-17 00:18:59 +01:00
|
|
|
}
|
|
|
|
|
2019-08-16 09:35:19 +02:00
|
|
|
// lockedWriterAt adds a lock to protect from closing the fd at the same time as writing.
|
2014-11-29 23:51:53 +01:00
|
|
|
// WriteAt() is goroutine safe by itself, but not against for example Close().
|
|
|
|
type lockedWriterAt struct {
|
2019-08-16 09:35:19 +02:00
|
|
|
mut sync.RWMutex
|
|
|
|
fd fs.File
|
2014-11-29 23:51:53 +01:00
|
|
|
}
|
|
|
|
|
2019-08-16 09:35:19 +02:00
|
|
|
// WriteAt itself is goroutine safe, thus just needs to acquire a read-lock to
|
|
|
|
// prevent closing concurrently (see SyncClose).
|
|
|
|
func (w *lockedWriterAt) WriteAt(p []byte, off int64) (n int, err error) {
|
|
|
|
w.mut.RLock()
|
|
|
|
defer w.mut.RUnlock()
|
|
|
|
return w.fd.WriteAt(p, off)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncClose ensures that no more writes are happening before going ahead and
|
|
|
|
// syncing and closing the fd, thus needs to acquire a write-lock.
|
2020-05-01 09:36:46 +02:00
|
|
|
func (w *lockedWriterAt) SyncClose(fsync bool) error {
|
2019-08-16 09:35:19 +02:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2020-05-01 09:36:46 +02:00
|
|
|
if fsync {
|
|
|
|
if err := w.fd.Sync(); err != nil {
|
|
|
|
// Sync() is nice if it works but not worth failing the
|
|
|
|
// operation over if it fails.
|
|
|
|
l.Debugf("fsync failed: %v", err)
|
|
|
|
}
|
2019-08-16 09:35:19 +02:00
|
|
|
}
|
|
|
|
return w.fd.Close()
|
2014-11-29 23:51:53 +01:00
|
|
|
}
|
|
|
|
|
2014-09-27 14:44:15 +02:00
|
|
|
// tempFile returns the fd for the temporary file, reusing an open fd
|
|
|
|
// or creating the file as necessary.
|
2020-06-18 08:15:47 +02:00
|
|
|
func (s *sharedPullerState) tempFile() (*lockedWriterAt, error) {
|
2014-09-27 14:44:15 +02:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
// If we've already hit an error, return early
|
|
|
|
if s.err != nil {
|
|
|
|
return nil, s.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the temp file is already open, return the file descriptor
|
2019-08-16 09:35:19 +02:00
|
|
|
if s.writer != nil {
|
|
|
|
return s.writer, nil
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:33:32 +01:00
|
|
|
if err := s.addWriterLocked(); err != nil {
|
2019-07-31 10:53:35 +02:00
|
|
|
s.failLocked(err)
|
2017-12-07 09:42:03 +01:00
|
|
|
return nil, err
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2019-08-16 09:35:19 +02:00
|
|
|
return s.writer, nil
|
2019-07-31 10:53:35 +02:00
|
|
|
}
|
|
|
|
|
2020-11-09 15:33:32 +01:00
|
|
|
func (s *sharedPullerState) addWriterLocked() error {
|
|
|
|
return inWritableDir(s.tempFileInWritableDir, s.fs, s.tempName, s.ignorePerms)
|
|
|
|
}
|
|
|
|
|
2019-07-31 10:53:35 +02:00
|
|
|
// tempFileInWritableDir should only be called from tempFile.
|
|
|
|
func (s *sharedPullerState) tempFileInWritableDir(_ string) error {
|
2016-07-25 12:18:05 +02:00
|
|
|
// The permissions to use for the temporary file should be those of the
|
|
|
|
// final file, except we need user read & write at minimum. The
|
|
|
|
// permissions will be set to the final value later, but in the meantime
|
|
|
|
// we don't want to have a temporary file with looser permissions than
|
|
|
|
// the final outcome.
|
2017-08-19 16:36:56 +02:00
|
|
|
mode := fs.FileMode(s.file.Permissions) | 0600
|
2016-07-25 12:18:05 +02:00
|
|
|
if s.ignorePerms {
|
|
|
|
// When ignorePerms is set we use a very permissive mode and let the
|
|
|
|
// system umask filter it.
|
|
|
|
mode = 0666
|
|
|
|
}
|
|
|
|
|
2014-09-27 14:44:15 +02:00
|
|
|
// Attempt to create the temp file
|
2016-04-22 10:12:10 +02:00
|
|
|
// RDWR because of issue #2994.
|
2017-08-19 16:36:56 +02:00
|
|
|
flags := fs.OptReadWrite
|
2014-10-12 22:38:22 +02:00
|
|
|
if s.reused == 0 {
|
2017-08-19 16:36:56 +02:00
|
|
|
flags |= fs.OptCreate | fs.OptExclusive
|
2016-07-25 12:18:05 +02:00
|
|
|
} else if !s.ignorePerms {
|
2014-12-30 14:37:06 +01:00
|
|
|
// With sufficiently bad luck when exiting or crashing, we may have
|
|
|
|
// had time to chmod the temp file to read only state but not yet
|
2017-11-04 08:20:11 +01:00
|
|
|
// moved it to its final name. This leaves us with a read only temp
|
2014-12-30 14:37:06 +01:00
|
|
|
// file that we're going to try to reuse. To handle that, we need to
|
|
|
|
// make sure we have write permissions on the file before opening it.
|
2016-07-25 12:18:05 +02:00
|
|
|
//
|
|
|
|
// When ignorePerms is set we trust that the permissions are fine
|
|
|
|
// already and make no modification, as we would otherwise override
|
|
|
|
// what the umask dictates.
|
|
|
|
|
2017-08-19 16:36:56 +02:00
|
|
|
if err := s.fs.Chmod(s.tempName, mode); err != nil {
|
2019-07-31 10:53:35 +02:00
|
|
|
return errors.Wrap(err, "setting perms on temp file")
|
2014-12-30 14:37:06 +01:00
|
|
|
}
|
2014-10-04 00:15:54 +02:00
|
|
|
}
|
2017-08-19 16:36:56 +02:00
|
|
|
fd, err := s.fs.OpenFile(s.tempName, flags, mode)
|
2014-09-27 14:44:15 +02:00
|
|
|
if err != nil {
|
2019-07-31 10:53:35 +02:00
|
|
|
return errors.Wrap(err, "opening temp file")
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2017-09-23 15:47:51 +02:00
|
|
|
// Hide the temporary file
|
|
|
|
s.fs.Hide(s.tempName)
|
|
|
|
|
2015-11-28 21:23:08 +01:00
|
|
|
// Don't truncate symlink files, as that will mean that the path will
|
|
|
|
// contain a bunch of nulls.
|
|
|
|
if s.sparse && !s.file.IsSymlink() {
|
2020-11-09 15:33:32 +01:00
|
|
|
size := s.file.Size
|
|
|
|
// Trailer added to encrypted files
|
|
|
|
if len(s.file.Encrypted) > 0 {
|
2020-11-25 22:57:25 +01:00
|
|
|
size += encryptionTrailerSize(s.file)
|
2020-11-09 15:33:32 +01:00
|
|
|
}
|
2015-11-21 16:30:53 +01:00
|
|
|
// Truncate sets the size of the file. This creates a sparse file or a
|
|
|
|
// space reservation, depending on the underlying filesystem.
|
2020-11-09 15:33:32 +01:00
|
|
|
if err := fd.Truncate(size); err != nil {
|
2017-12-14 11:42:40 +01:00
|
|
|
// The truncate call failed. That can happen in some cases when
|
|
|
|
// space reservation isn't possible or over some network
|
|
|
|
// filesystems... This generally doesn't matter.
|
|
|
|
|
|
|
|
if s.reused > 0 {
|
|
|
|
// ... but if we are attempting to reuse a file we have a
|
|
|
|
// corner case when the old file is larger than the new one
|
|
|
|
// and we can't just overwrite blocks and let the old data
|
|
|
|
// linger at the end. In this case we attempt a delete of
|
|
|
|
// the file and hope for better luck next time, when we
|
|
|
|
// should come around with s.reused == 0.
|
|
|
|
|
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if remErr := s.fs.Remove(s.tempName); remErr != nil {
|
|
|
|
l.Debugln("failed to remove temporary file:", remErr)
|
|
|
|
}
|
|
|
|
|
2019-07-31 10:53:35 +02:00
|
|
|
return err
|
2017-12-14 11:42:40 +01:00
|
|
|
}
|
2015-11-21 16:30:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 14:44:15 +02:00
|
|
|
// Same fd will be used by all writers
|
2019-08-16 09:35:19 +02:00
|
|
|
s.writer = &lockedWriterAt{sync.NewRWMutex(), fd}
|
2019-07-31 10:53:35 +02:00
|
|
|
return nil
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2017-09-23 16:22:26 +02:00
|
|
|
// fail sets the error on the puller state compose of error, and marks the
|
|
|
|
// sharedPullerState as failed. Is a no-op when called on an already failed state.
|
2019-03-04 14:01:52 +01:00
|
|
|
func (s *sharedPullerState) fail(err error) {
|
2014-09-27 14:44:15 +02:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2019-03-04 14:01:52 +01:00
|
|
|
s.failLocked(err)
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2019-03-04 14:01:52 +01:00
|
|
|
func (s *sharedPullerState) failLocked(err error) {
|
2017-09-23 16:22:26 +02:00
|
|
|
if s.err != nil || err == nil {
|
2014-09-27 14:44:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-04 14:01:52 +01:00
|
|
|
s.err = err
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedPullerState) failed() error {
|
2016-04-15 12:59:41 +02:00
|
|
|
s.mut.RLock()
|
|
|
|
err := s.err
|
|
|
|
s.mut.RUnlock()
|
2014-09-27 14:44:15 +02:00
|
|
|
|
2016-04-15 12:59:41 +02:00
|
|
|
return err
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-15 12:59:41 +02:00
|
|
|
func (s *sharedPullerState) copyDone(block protocol.BlockInfo) {
|
2014-09-27 14:44:15 +02:00
|
|
|
s.mut.Lock()
|
|
|
|
s.copyNeeded--
|
2016-04-15 12:59:41 +02:00
|
|
|
s.updated = time.Now()
|
2020-10-02 08:07:05 +02:00
|
|
|
s.available = append(s.available, int(block.Offset/int64(s.file.BlockSize())))
|
2016-04-15 12:59:41 +02:00
|
|
|
s.availableUpdated = time.Now()
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("sharedPullerState", s.folder, s.file.Name, "copyNeeded ->", s.copyNeeded)
|
2014-10-09 00:41:23 +02:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-10-12 22:38:22 +02:00
|
|
|
func (s *sharedPullerState) copiedFromOrigin() {
|
|
|
|
s.mut.Lock()
|
|
|
|
s.copyOrigin++
|
2016-04-15 12:59:41 +02:00
|
|
|
s.updated = time.Now()
|
2014-10-12 22:38:22 +02:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-12-15 00:30:29 +01:00
|
|
|
func (s *sharedPullerState) copiedFromOriginShifted() {
|
|
|
|
s.mut.Lock()
|
|
|
|
s.copyOrigin++
|
|
|
|
s.copyOriginShifted++
|
|
|
|
s.updated = time.Now()
|
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2014-10-09 00:41:23 +02:00
|
|
|
func (s *sharedPullerState) pullStarted() {
|
|
|
|
s.mut.Lock()
|
2014-10-12 22:38:22 +02:00
|
|
|
s.copyTotal--
|
|
|
|
s.copyNeeded--
|
|
|
|
s.pullTotal++
|
2014-10-09 00:41:23 +02:00
|
|
|
s.pullNeeded++
|
2016-04-15 12:59:41 +02:00
|
|
|
s.updated = time.Now()
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("sharedPullerState", s.folder, s.file.Name, "pullNeeded start ->", s.pullNeeded)
|
2014-09-27 14:44:15 +02:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-04-15 12:59:41 +02:00
|
|
|
func (s *sharedPullerState) pullDone(block protocol.BlockInfo) {
|
2014-09-27 14:44:15 +02:00
|
|
|
s.mut.Lock()
|
|
|
|
s.pullNeeded--
|
2016-04-15 12:59:41 +02:00
|
|
|
s.updated = time.Now()
|
2020-10-02 08:07:05 +02:00
|
|
|
s.available = append(s.available, int(block.Offset/int64(s.file.BlockSize())))
|
2016-04-15 12:59:41 +02:00
|
|
|
s.availableUpdated = time.Now()
|
2015-10-03 17:25:21 +02:00
|
|
|
l.Debugln("sharedPullerState", s.folder, s.file.Name, "pullNeeded done ->", s.pullNeeded)
|
2014-09-27 14:44:15 +02:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// finalClose atomically closes and returns closed status of a file. A true
|
|
|
|
// first return value means the file was closed and should be finished, with
|
|
|
|
// the error indicating the success or failure of the close. A false first
|
|
|
|
// return value indicates the file is not ready to be closed, or is already
|
|
|
|
// closed and should in either case not be finished off now.
|
|
|
|
func (s *sharedPullerState) finalClose() (bool, error) {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2015-05-27 11:14:39 +02:00
|
|
|
if s.closed {
|
|
|
|
// Already closed
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:12:12 +01:00
|
|
|
if s.pullNeeded+s.copyNeeded != 0 && s.err == nil {
|
2015-05-27 11:14:39 +02:00
|
|
|
// Not done yet, and not errored
|
2014-09-27 14:44:15 +02:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:33:32 +01:00
|
|
|
if len(s.file.Encrypted) > 0 {
|
|
|
|
if err := s.finalizeEncrypted(); err != nil && s.err == nil {
|
|
|
|
// This is our error as we weren't errored before.
|
|
|
|
s.err = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:35:19 +02:00
|
|
|
if s.writer != nil {
|
2020-05-01 09:36:46 +02:00
|
|
|
if err := s.writer.SyncClose(s.fsync); err != nil && s.err == nil {
|
2019-05-09 22:20:29 +02:00
|
|
|
// This is our error as we weren't errored before.
|
|
|
|
s.err = err
|
2015-05-27 11:14:39 +02:00
|
|
|
}
|
2019-08-16 09:35:19 +02:00
|
|
|
s.writer = nil
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
2015-05-27 11:14:39 +02:00
|
|
|
|
|
|
|
s.closed = true
|
|
|
|
|
2017-09-23 15:47:51 +02:00
|
|
|
// Unhide the temporary file when we close it, as it's likely to
|
|
|
|
// immediately be renamed to the final name. If this is a failed temp
|
|
|
|
// file we will also unhide it, but I'm fine with that as we're now
|
|
|
|
// leaving it around for potentially quite a while.
|
|
|
|
s.fs.Unhide(s.tempName)
|
|
|
|
|
2015-05-27 11:14:39 +02:00
|
|
|
return true, s.err
|
2014-09-27 14:44:15 +02:00
|
|
|
}
|
2014-11-17 00:18:59 +01:00
|
|
|
|
2020-11-09 15:33:32 +01:00
|
|
|
// finalizeEncrypted adds a trailer to the encrypted file containing the
|
|
|
|
// serialized FileInfo and the length of that FileInfo. When initializing a
|
|
|
|
// folder from encrypted data we can extract this FileInfo from the end of
|
|
|
|
// the file and regain the original metadata.
|
|
|
|
func (s *sharedPullerState) finalizeEncrypted() error {
|
2020-11-25 22:57:25 +01:00
|
|
|
bs := make([]byte, encryptionTrailerSize(s.file))
|
2020-11-09 15:33:32 +01:00
|
|
|
n, err := s.file.MarshalTo(bs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint32(bs[n:], uint32(n))
|
|
|
|
bs = bs[:n+4]
|
|
|
|
|
|
|
|
if s.writer == nil {
|
|
|
|
if err := s.addWriterLocked(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if _, err := s.writer.WriteAt(bs, s.file.Size); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 22:57:25 +01:00
|
|
|
func encryptionTrailerSize(file protocol.FileInfo) int64 {
|
|
|
|
return int64(file.ProtoSize()) + 4
|
|
|
|
}
|
|
|
|
|
2016-04-15 12:59:41 +02:00
|
|
|
// Progress returns the momentarily progress for the puller
|
2014-11-17 00:18:59 +01:00
|
|
|
func (s *sharedPullerState) Progress() *pullerProgress {
|
2016-04-15 12:59:41 +02:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
2014-11-17 00:18:59 +01:00
|
|
|
total := s.reused + s.copyTotal + s.pullTotal
|
|
|
|
done := total - s.copyNeeded - s.pullNeeded
|
|
|
|
return &pullerProgress{
|
|
|
|
Total: total,
|
|
|
|
Reused: s.reused,
|
|
|
|
CopiedFromOrigin: s.copyOrigin,
|
|
|
|
CopiedFromElsewhere: s.copyTotal - s.copyNeeded - s.copyOrigin,
|
|
|
|
Pulled: s.pullTotal - s.pullNeeded,
|
|
|
|
Pulling: s.pullNeeded,
|
2018-04-16 20:08:50 +02:00
|
|
|
BytesTotal: blocksToSize(s.file.BlockSize(), total),
|
|
|
|
BytesDone: blocksToSize(s.file.BlockSize(), done),
|
2014-11-17 00:18:59 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 12:59:41 +02:00
|
|
|
|
|
|
|
// Updated returns the time when any of the progress related counters was last updated.
|
|
|
|
func (s *sharedPullerState) Updated() time.Time {
|
|
|
|
s.mut.RLock()
|
|
|
|
t := s.updated
|
|
|
|
s.mut.RUnlock()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// AvailableUpdated returns the time last time list of available blocks was updated
|
|
|
|
func (s *sharedPullerState) AvailableUpdated() time.Time {
|
|
|
|
s.mut.RLock()
|
|
|
|
t := s.availableUpdated
|
|
|
|
s.mut.RUnlock()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Available returns blocks available in the current temporary file
|
2020-10-02 08:07:05 +02:00
|
|
|
func (s *sharedPullerState) Available() []int {
|
2016-04-15 12:59:41 +02:00
|
|
|
s.mut.RLock()
|
|
|
|
blocks := s.available
|
|
|
|
s.mut.RUnlock()
|
|
|
|
return blocks
|
|
|
|
}
|
2016-07-04 12:40:29 +02:00
|
|
|
|
2018-04-16 20:08:50 +02:00
|
|
|
func blocksToSize(size int, num int) int64 {
|
2016-07-04 12:40:29 +02:00
|
|
|
if num < 2 {
|
2018-04-16 20:08:50 +02:00
|
|
|
return int64(size / 2)
|
2016-07-04 12:40:29 +02:00
|
|
|
}
|
2018-04-16 20:08:50 +02:00
|
|
|
return int64(num-1)*int64(size) + int64(size/2)
|
2016-07-04 12:40:29 +02:00
|
|
|
}
|