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,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-09-27 12:44:15 +00:00
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2014-11-29 22:51:53 +00:00
|
|
|
"io"
|
2014-09-27 12:44:15 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-04-15 10:59:41 +00:00
|
|
|
"time"
|
2014-09-27 12:44:15 +00:00
|
|
|
|
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)
|
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
|
2015-03-29 14:16:36 +00:00
|
|
|
version protocol.Vector // The current (old) version
|
2015-11-21 15:30:53 +00:00
|
|
|
sparse bool
|
2016-05-22 10:16:09 +00:00
|
|
|
created time.Time
|
2014-09-27 12:44:15 +00:00
|
|
|
|
|
|
|
// Mutable, must be locked for access
|
2016-04-15 10:59:41 +00:00
|
|
|
err error // The first error we hit
|
|
|
|
fd *os.File // The fd of the temp file
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
available []int32 // Indexes of the blocks that are available in the temporary file
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-16 23:18:59 +00:00
|
|
|
// A momentary state representing the progress of the puller
|
|
|
|
type pullerProgress struct {
|
2015-03-10 22:45:43 +00:00
|
|
|
Total int `json:"total"`
|
|
|
|
Reused int `json:"reused"`
|
|
|
|
CopiedFromOrigin int `json:"copiedFromOrigin"`
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-11-29 22:51:53 +00:00
|
|
|
// A lockedWriterAt synchronizes WriteAt calls with an external mutex.
|
|
|
|
// WriteAt() is goroutine safe by itself, but not against for example Close().
|
|
|
|
type lockedWriterAt struct {
|
2016-04-15 10:59:41 +00:00
|
|
|
mut *sync.RWMutex
|
2014-11-29 22:51:53 +00:00
|
|
|
wr io.WriterAt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w lockedWriterAt) WriteAt(p []byte, off int64) (n int, err error) {
|
2015-04-22 22:54:31 +00:00
|
|
|
(*w.mut).Lock()
|
|
|
|
defer (*w.mut).Unlock()
|
2014-11-29 22:51:53 +00:00
|
|
|
return w.wr.WriteAt(p, off)
|
|
|
|
}
|
|
|
|
|
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.
|
2014-11-29 22:51:53 +00:00
|
|
|
func (s *sharedPullerState) tempFile() (io.WriterAt, 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
|
|
|
|
if s.fd != nil {
|
2014-11-29 22:51:53 +00:00
|
|
|
return lockedWriterAt{&s.mut, s.fd}, nil
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2014-09-27 23:54:25 +00:00
|
|
|
// Ensure that the parent directory is writable. This is
|
|
|
|
// osutil.InWritableDir except we need to do more stuff so we duplicate it
|
|
|
|
// here.
|
2014-09-27 12:44:15 +00:00
|
|
|
dir := filepath.Dir(s.tempName)
|
2014-09-27 23:54:25 +00:00
|
|
|
if info, err := os.Stat(dir); err != nil {
|
2015-10-01 07:42:57 +00:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// XXX: This works around a bug elsewhere, a race condition when
|
|
|
|
// things are deleted while being synced. However that happens, we
|
|
|
|
// end up with a directory for "foo" with the delete bit, but a
|
|
|
|
// file "foo/bar" that we want to sync. We never create the
|
|
|
|
// directory, and hence fail to create the file and end up looping
|
|
|
|
// forever on it. This breaks that by creating the directory; on
|
|
|
|
// next scan it'll be found and the delete bit on it is removed.
|
|
|
|
// The user can then clean up as they like...
|
|
|
|
l.Infoln("Resurrecting directory", dir)
|
|
|
|
if err := os.MkdirAll(dir, 0755); err != nil {
|
|
|
|
s.failLocked("resurrect dir", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.failLocked("dst stat dir", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-25 23:54:50 +00:00
|
|
|
} else if info.Mode()&0200 == 0 {
|
2014-09-27 12:44:15 +00:00
|
|
|
err := os.Chmod(dir, 0755)
|
2015-03-05 21:13:31 +00:00
|
|
|
if !s.ignorePerms && err == nil {
|
2014-09-27 12:44:15 +00:00
|
|
|
defer func() {
|
|
|
|
err := os.Chmod(dir, info.Mode().Perm())
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to create the temp file
|
2016-04-22 08:12:10 +00:00
|
|
|
// RDWR because of issue #2994.
|
|
|
|
flags := os.O_RDWR
|
2014-10-12 20:38:22 +00:00
|
|
|
if s.reused == 0 {
|
2014-10-03 22:15:54 +00:00
|
|
|
flags |= os.O_CREATE | os.O_EXCL
|
2014-12-30 13:37:06 +00:00
|
|
|
} else {
|
|
|
|
// 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
|
|
|
|
// moved it to it's final name. This leaves us with a read only temp
|
|
|
|
// 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.
|
|
|
|
err := os.Chmod(s.tempName, 0644)
|
2015-03-05 21:13:31 +00:00
|
|
|
if !s.ignorePerms && err != nil {
|
2015-01-07 23:12:12 +00:00
|
|
|
s.failLocked("dst create chmod", err)
|
2014-12-30 13:37:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-03 22:15:54 +00:00
|
|
|
}
|
2015-05-24 22:12:51 +00:00
|
|
|
fd, err := os.OpenFile(s.tempName, flags, 0666)
|
2014-09-27 12:44:15 +00:00
|
|
|
if err != nil {
|
2015-01-07 23:12:12 +00:00
|
|
|
s.failLocked("dst create", err)
|
2014-09-27 12:44:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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() {
|
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.
|
2016-07-04 10:40:29 +00:00
|
|
|
if err := fd.Truncate(s.file.Size); err != nil {
|
2015-11-21 15:30:53 +00:00
|
|
|
s.failLocked("dst truncate", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 12:44:15 +00:00
|
|
|
// Same fd will be used by all writers
|
|
|
|
s.fd = fd
|
|
|
|
|
2014-11-29 22:51:53 +00:00
|
|
|
return lockedWriterAt{&s.mut, s.fd}, nil
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sourceFile opens the existing source file for reading
|
|
|
|
func (s *sharedPullerState) sourceFile() (*os.File, error) {
|
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
|
|
|
// If we've already hit an error, return early
|
|
|
|
if s.err != nil {
|
|
|
|
return nil, s.err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to open the existing file
|
|
|
|
fd, err := os.Open(s.realName)
|
|
|
|
if err != nil {
|
2015-01-07 23:12:12 +00:00
|
|
|
s.failLocked("src open", err)
|
2014-09-27 12:44:15 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return fd, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// earlyClose prints a warning message composed of the context and
|
|
|
|
// error, and marks the sharedPullerState as failed. Is a no-op when called on
|
|
|
|
// an already failed state.
|
2015-01-07 23:12:12 +00:00
|
|
|
func (s *sharedPullerState) fail(context string, err error) {
|
2014-09-27 12:44:15 +00:00
|
|
|
s.mut.Lock()
|
|
|
|
defer s.mut.Unlock()
|
|
|
|
|
2015-01-07 23:12:12 +00:00
|
|
|
s.failLocked(context, err)
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 23:12:12 +00:00
|
|
|
func (s *sharedPullerState) failLocked(context string, err error) {
|
2014-09-27 12:44:15 +00:00
|
|
|
if s.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
l.Infof("Puller (folder %q, file %q): %s: %v", s.folder, s.file.Name, context, err)
|
2014-09-27 12:44:15 +00:00
|
|
|
s.err = err
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
s.available = append(s.available, int32(block.Offset/protocol.BlockSize))
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2014-10-12 20:38:22 +00:00
|
|
|
func (s *sharedPullerState) copiedFromOrigin() {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
s.available = append(s.available, int32(block.Offset/protocol.BlockSize))
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-05-27 09:14:39 +00:00
|
|
|
if s.fd != nil {
|
|
|
|
if closeErr := s.fd.Close(); closeErr != nil && s.err == nil {
|
|
|
|
// This is our error if we weren't errored before. Otherwise we
|
|
|
|
// keep the earlier error.
|
|
|
|
s.err = closeErr
|
|
|
|
}
|
2014-09-27 12:44:15 +00:00
|
|
|
s.fd = nil
|
|
|
|
}
|
2015-05-27 09:14:39 +00:00
|
|
|
|
|
|
|
s.closed = true
|
|
|
|
|
|
|
|
return true, s.err
|
2014-09-27 12:44:15 +00:00
|
|
|
}
|
2014-11-16 23:18:59 +00:00
|
|
|
|
2016-04-15 10:59:41 +00:00
|
|
|
// Progress returns the momentarily progress for the puller
|
2014-11-16 23:18:59 +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
|
|
|
|
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,
|
2016-07-04 10:40:29 +00:00
|
|
|
BytesTotal: blocksToSize(total),
|
|
|
|
BytesDone: blocksToSize(done),
|
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
|
|
|
|
func (s *sharedPullerState) Available() []int32 {
|
|
|
|
s.mut.RLock()
|
|
|
|
blocks := s.available
|
|
|
|
s.mut.RUnlock()
|
|
|
|
return blocks
|
|
|
|
}
|
2016-07-04 10:40:29 +00:00
|
|
|
|
|
|
|
func blocksToSize(num int) int64 {
|
|
|
|
if num < 2 {
|
|
|
|
return protocol.BlockSize / 2
|
|
|
|
}
|
|
|
|
return int64(num-1)*protocol.BlockSize + protocol.BlockSize/2
|
|
|
|
}
|