2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00: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 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-09-27 12:44:15 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2020-11-09 14:33:32 +00:00
|
|
|
"encoding/binary"
|
2022-08-16 08:01:49 +00:00
|
|
|
"fmt"
|
2021-10-05 09:23:27 +00:00
|
|
|
"io"
|
2016-04-15 10:59:41 +00:00
|
|
|
"time"
|
2014-09-27 12:44:15 +00:00
|
|
|
|
2017-08-19 14:36:56 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/fs"
|
2021-03-21 09:34:08 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/osutil"
|
2015-09-22 17:38:46 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/protocol"
|
2015-08-06 09:29:25 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/sync"
|
2014-09-27 12:44:15 +00: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 14:16:36 +00:00
|
|
|
file protocol.FileInfo // The new file (desired end state)
|
2017-08-19 14:36:56 +00:00
|
|
|
fs fs.Filesystem
|
2015-03-05 21:13:31 +00:00
|
|
|
folder string
|
|
|
|
tempName string
|
|
|
|
realName string
|
|
|
|
reused int // Number of blocks reused from temporary file
|
|
|
|
ignorePerms bool
|
2017-08-22 06:42:09 +00:00
|
|
|
hasCurFile bool // Whether curFile is set
|
|
|
|
curFile protocol.FileInfo // The file as it exists now in our database
|
2015-11-21 15:30:53 +00:00
|
|
|
sparse bool
|
2016-05-22 10:16:09 +00:00
|
|
|
created time.Time
|
2020-05-01 07:36:46 +00:00
|
|
|
fsync bool
|
2014-09-27 12:44:15 +00:00
|
|
|
|
|
|
|
// Mutable, must be locked for access
|
2019-08-16 07:35:19 +00: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 06:07:05 +00:00
|
|
|
available []int // Indexes of the blocks that are available in the temporary file
|
2019-08-16 07:35:19 +00:00
|
|
|
availableUpdated time.Time // Time when list of available blocks was last updated
|
|
|
|
mut sync.RWMutex // Protects the above
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 06:07:05 +00: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 17:55:14 +00: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 07:36:46 +00:00
|
|
|
fsync: fsync,
|
2020-04-21 17:55:14 +00:00
|
|
|
created: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-16 23:18:59 +00:00
|
|
|
// A momentary state representing the progress of the puller
|
2024-07-31 05:31:14 +00:00
|
|
|
type PullerProgress struct {
|
2016-12-14 23:30:29 +00: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-16 23:18:59 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 07:35:19 +00:00
|
|
|
// lockedWriterAt adds a lock to protect from closing the fd at the same time as writing.
|
2014-11-29 22:51:53 +00:00
|
|
|
// WriteAt() is goroutine safe by itself, but not against for example Close().
|
|
|
|
type lockedWriterAt struct {
|
2019-08-16 07:35:19 +00:00
|
|
|
mut sync.RWMutex
|
|
|
|
fd fs.File
|
2014-11-29 22:51:53 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 07:35:19 +00: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 07:36:46 +00:00
|
|
|
func (w *lockedWriterAt) SyncClose(fsync bool) error {
|
2019-08-16 07:35:19 +00:00
|
|
|
w.mut.Lock()
|
|
|
|
defer w.mut.Unlock()
|
2020-05-01 07:36:46 +00: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 07:35:19 +00:00
|
|
|
}
|
|
|
|
return w.fd.Close()
|
2014-11-29 22:51:53 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 12:44:15 +00:00
|
|
|
// tempFile returns the fd for the temporary file, reusing an open fd
|
|
|
|
// or creating the file as necessary.
|
2020-06-18 06:15:47 +00:00
|
|
|
func (s *sharedPullerState) tempFile() (*lockedWriterAt, error) {
|
2014-09-27 12:44:15 +00: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 07:35:19 +00:00
|
|
|
if s.writer != nil {
|
|
|
|
return s.writer, nil
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
if err := s.addWriterLocked(); err != nil {
|
2019-07-31 08:53:35 +00:00
|
|
|
s.failLocked(err)
|
2017-12-07 08:42:03 +00:00
|
|
|
return nil, err
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 07:35:19 +00:00
|
|
|
return s.writer, nil
|
2019-07-31 08:53:35 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 14:33:32 +00:00
|
|
|
func (s *sharedPullerState) addWriterLocked() error {
|
|
|
|
return inWritableDir(s.tempFileInWritableDir, s.fs, s.tempName, s.ignorePerms)
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:53:35 +00:00
|
|
|
// tempFileInWritableDir should only be called from tempFile.
|
|
|
|
func (s *sharedPullerState) tempFileInWritableDir(_ string) error {
|
2016-07-25 10:18:05 +00: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.
|
2023-08-04 17:57:30 +00:00
|
|
|
mode := fs.FileMode(s.file.Permissions) | 0o600
|
2016-07-25 10:18:05 +00:00
|
|
|
if s.ignorePerms {
|
|
|
|
// When ignorePerms is set we use a very permissive mode and let the
|
|
|
|
// system umask filter it.
|
2023-08-04 17:57:30 +00:00
|
|
|
mode = 0o666
|
2016-07-25 10:18:05 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 12:44:15 +00:00
|
|
|
// Attempt to create the temp file
|
2016-04-22 08:12:10 +00:00
|
|
|
// RDWR because of issue #2994.
|
2017-08-19 14:36:56 +00:00
|
|
|
flags := fs.OptReadWrite
|
2014-10-12 20:38:22 +00:00
|
|
|
if s.reused == 0 {
|
2017-08-19 14:36:56 +00:00
|
|
|
flags |= fs.OptCreate | fs.OptExclusive
|
2016-07-25 10:18:05 +00:00
|
|
|
} else if !s.ignorePerms {
|
2014-12-30 13:37:06 +00: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 07:20:11 +00:00
|
|
|
// moved it to its final name. This leaves us with a read only temp
|
2014-12-30 13:37:06 +00: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 10:18:05 +00: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 14:36:56 +00:00
|
|
|
if err := s.fs.Chmod(s.tempName, mode); err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("setting perms on temp file: %w", err)
|
2014-12-30 13:37:06 +00:00
|
|
|
}
|
2014-10-03 22:15:54 +00:00
|
|
|
}
|
2017-08-19 14:36:56 +00:00
|
|
|
fd, err := s.fs.OpenFile(s.tempName, flags, mode)
|
2014-09-27 12:44:15 +00:00
|
|
|
if err != nil {
|
2022-08-16 08:01:49 +00:00
|
|
|
return fmt.Errorf("opening temp file: %w", err)
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 13:47:51 +00:00
|
|
|
// Hide the temporary file
|
|
|
|
s.fs.Hide(s.tempName)
|
|
|
|
|
2015-11-28 20:23:08 +00: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 14:33:32 +00:00
|
|
|
size := s.file.Size
|
|
|
|
// Trailer added to encrypted files
|
|
|
|
if len(s.file.Encrypted) > 0 {
|
2020-11-25 21:57:25 +00:00
|
|
|
size += encryptionTrailerSize(s.file)
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
2015-11-21 15:30:53 +00: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 14:33:32 +00:00
|
|
|
if err := fd.Truncate(size); err != nil {
|
2017-12-14 10:42:40 +00: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 08:53:35 +00:00
|
|
|
return err
|
2017-12-14 10:42:40 +00:00
|
|
|
}
|
2015-11-21 15:30:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 12:44:15 +00:00
|
|
|
// Same fd will be used by all writers
|
2019-08-16 07:35:19 +00:00
|
|
|
s.writer = &lockedWriterAt{sync.NewRWMutex(), fd}
|
2019-07-31 08:53:35 +00:00
|
|
|
return nil
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:22:26 +00: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 13:01:52 +00:00
|
|
|
func (s *sharedPullerState) fail(err error) {
|
2014-09-27 12:44:15 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2019-03-04 13:01:52 +00:00
|
|
|
s.failLocked(err)
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 13:01:52 +00:00
|
|
|
func (s *sharedPullerState) failLocked(err error) {
|
2017-09-23 14:22:26 +00:00
|
|
|
if s.err != nil || err == nil {
|
2014-09-27 12:44:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-04 13:01:52 +00:00
|
|
|
s.err = err
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedPullerState) failed() error {
|
2016-04-15 10:59:41 +00:00
|
|
|
s.mut.RLock()
|
|
|
|
err := s.err
|
|
|
|
s.mut.RUnlock()
|
2014-09-27 12:44:15 +00:00
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
return err
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
func (s *sharedPullerState) copyDone(block protocol.BlockInfo) {
|
2014-09-27 12:44:15 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
s.copyNeeded--
|
2016-04-15 10:59:41 +00:00
|
|
|
s.updated = time.Now()
|
2020-10-02 06:07:05 +00:00
|
|
|
s.available = append(s.available, int(block.Offset/int64(s.file.BlockSize())))
|
2016-04-15 10:59:41 +00:00
|
|
|
s.availableUpdated = time.Now()
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("sharedPullerState", s.folder, s.file.Name, "copyNeeded ->", s.copyNeeded)
|
2014-10-08 22:41:23 +00:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
func (s *sharedPullerState) copiedFromOrigin(bytes int) {
|
2014-10-12 20:38:22 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
s.copyOrigin++
|
2016-04-15 10:59:41 +00:00
|
|
|
s.updated = time.Now()
|
2014-10-12 20:38:22 +00:00
|
|
|
s.mut.Unlock()
|
2023-08-04 17:57:30 +00:00
|
|
|
metricFolderProcessedBytesTotal.WithLabelValues(s.folder, metricSourceLocalOrigin).Add(float64(bytes))
|
2014-10-12 20:38:22 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 17:57:30 +00:00
|
|
|
func (s *sharedPullerState) copiedFromElsewhere(bytes int) {
|
|
|
|
metricFolderProcessedBytesTotal.WithLabelValues(s.folder, metricSourceLocalOther).Add(float64(bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedPullerState) skippedSparseBlock(bytes int) {
|
|
|
|
// pretend we copied it, historical
|
|
|
|
s.mut.Lock()
|
|
|
|
s.copyOrigin++
|
|
|
|
s.updated = time.Now()
|
|
|
|
s.mut.Unlock()
|
|
|
|
metricFolderProcessedBytesTotal.WithLabelValues(s.folder, metricSourceSkipped).Add(float64(bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sharedPullerState) copiedFromOriginShifted(bytes int) {
|
2016-12-14 23:30:29 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
s.copyOrigin++
|
|
|
|
s.copyOriginShifted++
|
|
|
|
s.updated = time.Now()
|
|
|
|
s.mut.Unlock()
|
2023-08-04 17:57:30 +00:00
|
|
|
metricFolderProcessedBytesTotal.WithLabelValues(s.folder, metricSourceLocalShifted).Add(float64(bytes))
|
2016-12-14 23:30:29 +00:00
|
|
|
}
|
|
|
|
|
2014-10-08 22:41:23 +00:00
|
|
|
func (s *sharedPullerState) pullStarted() {
|
|
|
|
s.mut.Lock()
|
2014-10-12 20:38:22 +00:00
|
|
|
s.copyTotal--
|
|
|
|
s.copyNeeded--
|
|
|
|
s.pullTotal++
|
2014-10-08 22:41:23 +00:00
|
|
|
s.pullNeeded++
|
2016-04-15 10:59:41 +00:00
|
|
|
s.updated = time.Now()
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("sharedPullerState", s.folder, s.file.Name, "pullNeeded start ->", s.pullNeeded)
|
2014-09-27 12:44:15 +00:00
|
|
|
s.mut.Unlock()
|
|
|
|
}
|
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
func (s *sharedPullerState) pullDone(block protocol.BlockInfo) {
|
2014-09-27 12:44:15 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
s.pullNeeded--
|
2016-04-15 10:59:41 +00:00
|
|
|
s.updated = time.Now()
|
2020-10-02 06:07:05 +00:00
|
|
|
s.available = append(s.available, int(block.Offset/int64(s.file.BlockSize())))
|
2016-04-15 10:59:41 +00:00
|
|
|
s.availableUpdated = time.Now()
|
2015-10-03 15:25:21 +00:00
|
|
|
l.Debugln("sharedPullerState", s.folder, s.file.Name, "pullNeeded done ->", s.pullNeeded)
|
2014-09-27 12:44:15 +00:00
|
|
|
s.mut.Unlock()
|
2023-08-04 17:57:30 +00:00
|
|
|
metricFolderProcessedBytesTotal.WithLabelValues(s.folder, metricSourceNetwork).Add(float64(block.Size))
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 09:14:39 +00:00
|
|
|
if s.closed {
|
|
|
|
// Already closed
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2015-01-07 23:12:12 +00:00
|
|
|
if s.pullNeeded+s.copyNeeded != 0 && s.err == nil {
|
2015-05-27 09:14:39 +00:00
|
|
|
// Not done yet, and not errored
|
2014-09-27 12:44:15 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2020-11-09 14:33:32 +00: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 07:35:19 +00:00
|
|
|
if s.writer != nil {
|
2020-05-01 07:36:46 +00:00
|
|
|
if err := s.writer.SyncClose(s.fsync); err != nil && s.err == nil {
|
2019-05-09 20:20:29 +00:00
|
|
|
// This is our error as we weren't errored before.
|
|
|
|
s.err = err
|
2015-05-27 09:14:39 +00:00
|
|
|
}
|
2019-08-16 07:35:19 +00:00
|
|
|
s.writer = nil
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
2015-05-27 09:14:39 +00:00
|
|
|
|
|
|
|
s.closed = true
|
|
|
|
|
2017-09-23 13:47:51 +00: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 09:14:39 +00:00
|
|
|
return true, s.err
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
2014-11-16 23:18:59 +00:00
|
|
|
|
2020-11-09 14:33:32 +00: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 {
|
2021-10-05 09:23:27 +00:00
|
|
|
if s.writer == nil {
|
|
|
|
if err := s.addWriterLocked(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 20:02:59 +00:00
|
|
|
trailerSize, err := writeEncryptionTrailer(s.file, s.writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.file.Size += trailerSize
|
2023-03-28 20:47:24 +00:00
|
|
|
s.file.EncryptionTrailerSize = int(trailerSize)
|
2023-03-28 20:02:59 +00:00
|
|
|
return nil
|
2021-10-05 09:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the size of the written trailer.
|
|
|
|
func writeEncryptionTrailer(file protocol.FileInfo, writer io.WriterAt) (int64, error) {
|
2021-03-21 09:34:08 +00:00
|
|
|
// Here the file is in native format, while encryption happens in
|
|
|
|
// wire format (always slashes).
|
2021-10-05 09:23:27 +00:00
|
|
|
wireFile := file
|
2021-03-21 09:34:08 +00:00
|
|
|
wireFile.Name = osutil.NormalizedFilename(wireFile.Name)
|
|
|
|
|
2021-10-05 09:23:27 +00:00
|
|
|
trailerSize := encryptionTrailerSize(wireFile)
|
|
|
|
bs := make([]byte, trailerSize)
|
2021-03-21 09:34:08 +00:00
|
|
|
n, err := wireFile.MarshalTo(bs)
|
2020-11-09 14:33:32 +00:00
|
|
|
if err != nil {
|
2021-10-05 09:23:27 +00:00
|
|
|
return 0, err
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
binary.BigEndian.PutUint32(bs[n:], uint32(n))
|
|
|
|
bs = bs[:n+4]
|
|
|
|
|
2021-10-05 09:23:27 +00:00
|
|
|
if _, err := writer.WriteAt(bs, wireFile.Size); err != nil {
|
|
|
|
return 0, err
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 09:23:27 +00:00
|
|
|
return trailerSize, nil
|
2020-11-09 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 21:57:25 +00:00
|
|
|
func encryptionTrailerSize(file protocol.FileInfo) int64 {
|
|
|
|
return int64(file.ProtoSize()) + 4
|
|
|
|
}
|
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
// Progress returns the momentarily progress for the puller
|
2024-07-31 05:31:14 +00:00
|
|
|
func (s *sharedPullerState) Progress() *PullerProgress {
|
2016-04-15 10:59:41 +00:00
|
|
|
s.mut.RLock()
|
|
|
|
defer s.mut.RUnlock()
|
2014-11-16 23:18:59 +00:00
|
|
|
total := s.reused + s.copyTotal + s.pullTotal
|
|
|
|
done := total - s.copyNeeded - s.pullNeeded
|
2021-01-31 22:40:15 +00:00
|
|
|
file := len(s.file.Blocks)
|
2024-07-31 05:31:14 +00:00
|
|
|
return &PullerProgress{
|
2014-11-16 23:18:59 +00:00
|
|
|
Total: total,
|
|
|
|
Reused: s.reused,
|
|
|
|
CopiedFromOrigin: s.copyOrigin,
|
|
|
|
CopiedFromElsewhere: s.copyTotal - s.copyNeeded - s.copyOrigin,
|
|
|
|
Pulled: s.pullTotal - s.pullNeeded,
|
|
|
|
Pulling: s.pullNeeded,
|
2021-01-31 22:40:15 +00:00
|
|
|
BytesTotal: blocksToSize(total, file, s.file.BlockSize(), s.file.Size),
|
|
|
|
BytesDone: blocksToSize(done, file, s.file.BlockSize(), s.file.Size),
|
2014-11-16 23:18:59 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-15 10:59:41 +00: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 06:07:05 +00:00
|
|
|
func (s *sharedPullerState) Available() []int {
|
2016-04-15 10:59:41 +00:00
|
|
|
s.mut.RLock()
|
|
|
|
blocks := s.available
|
|
|
|
s.mut.RUnlock()
|
|
|
|
return blocks
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
2021-01-31 22:40:15 +00:00
|
|
|
func blocksToSize(blocks, blocksInFile, blockSize int, fileSize int64) int64 {
|
|
|
|
// The last/only block has somewhere between 1 and blockSize bytes. We do
|
|
|
|
// not know whether the smaller block is part of the blocks and use an
|
|
|
|
// estimate assuming a random chance that the small block is contained.
|
|
|
|
if blocksInFile == 0 {
|
|
|
|
return 0
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|
2021-01-31 22:40:15 +00:00
|
|
|
return int64(blocks)*int64(blockSize) - (int64(blockSize)-fileSize%int64(blockSize))*int64(blocks)/int64(blocksInFile)
|
2016-07-04 10:40:29 +00:00
|
|
|
}
|