2014-07-12 15:45:33 -07:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 13:50:14 -07:00
|
|
|
|
2014-05-14 20:26:55 -07:00
|
|
|
package model
|
2014-03-28 06:36:57 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2014-08-25 08:45:13 -07:00
|
|
|
"fmt"
|
2014-03-28 06:36:57 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2014-09-27 05:44:15 -07:00
|
|
|
"sync"
|
2014-03-28 06:36:57 -07:00
|
|
|
"time"
|
2014-06-19 15:27:54 -07:00
|
|
|
|
2014-09-22 12:42:11 -07:00
|
|
|
"github.com/syncthing/syncthing/internal/config"
|
|
|
|
"github.com/syncthing/syncthing/internal/events"
|
|
|
|
"github.com/syncthing/syncthing/internal/osutil"
|
|
|
|
"github.com/syncthing/syncthing/internal/protocol"
|
|
|
|
"github.com/syncthing/syncthing/internal/scanner"
|
|
|
|
"github.com/syncthing/syncthing/internal/versioner"
|
2014-03-28 06:36:57 -07:00
|
|
|
)
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// TODO: Stop on errors
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
const (
|
2014-09-28 04:00:38 -07:00
|
|
|
copiersPerFolder = 1
|
|
|
|
pullersPerFolder = 16
|
|
|
|
finishersPerFolder = 2
|
2014-09-28 04:05:25 -07:00
|
|
|
pauseIntv = 60 * time.Second
|
|
|
|
nextPullIntv = 10 * time.Second
|
|
|
|
checkPullIntv = 1 * time.Second
|
2014-09-27 05:44:15 -07:00
|
|
|
)
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// A pullBlockState is passed to the puller routine for each block that needs
|
|
|
|
// to be fetched.
|
|
|
|
type pullBlockState struct {
|
|
|
|
*sharedPullerState
|
|
|
|
block protocol.BlockInfo
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// A copyBlocksState is passed to copy routine if the file has blocks to be
|
|
|
|
// copied from the original.
|
|
|
|
type copyBlocksState struct {
|
|
|
|
*sharedPullerState
|
|
|
|
blocks []protocol.BlockInfo
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
var (
|
2014-09-28 04:05:25 -07:00
|
|
|
activity = newDeviceActivity()
|
2014-09-28 04:00:38 -07:00
|
|
|
errNoDevice = errors.New("no available source device")
|
2014-09-27 05:44:15 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type Puller struct {
|
2014-09-28 04:05:25 -07:00
|
|
|
folder string
|
2014-09-27 05:44:15 -07:00
|
|
|
dir string
|
|
|
|
scanIntv time.Duration
|
|
|
|
model *Model
|
|
|
|
stop chan struct{}
|
|
|
|
versioner versioner.Versioner
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Serve will run scans and pulls. It will return when Stop()ed or on a
|
|
|
|
// critical error.
|
|
|
|
func (p *Puller) Serve() {
|
|
|
|
if debug {
|
|
|
|
l.Debugln(p, "starting")
|
|
|
|
defer l.Debugln(p, "exiting")
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
p.stop = make(chan struct{})
|
2014-05-25 11:49:08 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
pullTimer := time.NewTimer(checkPullIntv)
|
|
|
|
scanTimer := time.NewTimer(p.scanIntv)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
pullTimer.Stop()
|
|
|
|
scanTimer.Stop()
|
2014-09-28 04:00:38 -07:00
|
|
|
// TODO: Should there be an actual FolderStopped state?
|
|
|
|
p.model.setState(p.folder, FolderIdle)
|
2014-09-27 05:44:15 -07:00
|
|
|
}()
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-06-19 15:27:54 -07:00
|
|
|
var prevVer uint64
|
2014-07-24 00:38:16 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Clean out old temporaries before we start pulling
|
|
|
|
p.clean()
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
loop:
|
2014-03-28 06:36:57 -07:00
|
|
|
for {
|
2014-09-27 05:44:15 -07:00
|
|
|
select {
|
|
|
|
case <-p.stop:
|
|
|
|
return
|
2014-07-24 00:38:16 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// TODO: We could easily add a channel here for notifications from
|
|
|
|
// Index(), so that we immediately start a pull when new index
|
|
|
|
// information is available. Before that though, I'd like to build a
|
|
|
|
// repeatable benchmark of how long it takes to sync a change from
|
2014-09-28 04:00:38 -07:00
|
|
|
// device A to device B, so we have something to work against.
|
2014-09-27 05:44:15 -07:00
|
|
|
case <-pullTimer.C:
|
|
|
|
// RemoteLocalVersion() is a fast call, doesn't touch the database.
|
2014-09-28 04:00:38 -07:00
|
|
|
curVer := p.model.RemoteLocalVersion(p.folder)
|
2014-09-27 05:44:15 -07:00
|
|
|
if curVer == prevVer {
|
|
|
|
pullTimer.Reset(checkPullIntv)
|
|
|
|
continue
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-08-04 13:02:44 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(p, "pulling", prevVer, curVer)
|
2014-08-04 13:02:44 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.setState(p.folder, FolderSyncing)
|
2014-09-27 05:44:15 -07:00
|
|
|
tries := 0
|
|
|
|
for {
|
|
|
|
tries++
|
2014-09-28 04:00:38 -07:00
|
|
|
changed := p.pullerIteration(copiersPerFolder, pullersPerFolder, finishersPerFolder)
|
2014-09-27 05:44:15 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(p, "changed", changed)
|
|
|
|
}
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if changed == 0 {
|
|
|
|
// No files were changed by the puller, so we are in
|
|
|
|
// sync. Remember the local version number and
|
|
|
|
// schedule a resync a little bit into the future.
|
2014-09-27 22:56:05 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
if lv := p.model.RemoteLocalVersion(p.folder); lv < curVer {
|
|
|
|
// There's a corner case where the device we needed
|
2014-09-27 22:56:05 -07:00
|
|
|
// files from disconnected during the puller
|
|
|
|
// iteration. The files will have been removed from
|
|
|
|
// the index, so we've concluded that we don't need
|
|
|
|
// them, but at the same time we have the local
|
|
|
|
// version that includes those files in curVer. So we
|
|
|
|
// catch the case that localVersion might have
|
|
|
|
// decresed here.
|
2014-09-28 04:05:25 -07:00
|
|
|
l.Debugln(p, "adjusting curVer", lv)
|
2014-09-27 22:56:05 -07:00
|
|
|
curVer = lv
|
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
prevVer = curVer
|
|
|
|
pullTimer.Reset(nextPullIntv)
|
|
|
|
break
|
|
|
|
}
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if tries > 10 {
|
|
|
|
// We've tried a bunch of times to get in sync, but
|
|
|
|
// we're not making it. Probably there are write
|
|
|
|
// errors preventing us. Flag this with a warning and
|
|
|
|
// wait a bit longer before retrying.
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Warnf("Folder %q isn't making progress - check logs for possible root cause. Pausing puller for %v.", p.folder, pauseIntv)
|
2014-09-27 05:44:15 -07:00
|
|
|
pullTimer.Reset(pauseIntv)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.setState(p.folder, FolderIdle)
|
2014-04-14 00:58:17 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// The reason for running the scanner from within the puller is that
|
|
|
|
// this is the easiest way to make sure we are not doing both at the
|
|
|
|
// same time.
|
|
|
|
case <-scanTimer.C:
|
2014-05-14 20:26:55 -07:00
|
|
|
if debug {
|
2014-09-27 05:44:15 -07:00
|
|
|
l.Debugln(p, "rescan")
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.setState(p.folder, FolderScanning)
|
|
|
|
if err := p.model.ScanFolder(p.folder); err != nil {
|
|
|
|
invalidateFolder(p.model.cfg, p.folder, err)
|
2014-09-27 05:44:15 -07:00
|
|
|
break loop
|
2014-05-04 09:20:25 -07:00
|
|
|
}
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.setState(p.folder, FolderIdle)
|
2014-09-27 05:44:15 -07:00
|
|
|
scanTimer.Reset(p.scanIntv)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
func (p *Puller) Stop() {
|
|
|
|
close(p.stop)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
func (p *Puller) String() string {
|
2014-09-28 04:00:38 -07:00
|
|
|
return fmt.Sprintf("puller/%s@%p", p.folder, p)
|
2014-09-27 05:44:15 -07:00
|
|
|
}
|
2014-09-07 12:29:06 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// pullerIteration runs a single puller iteration for the given folder and
|
2014-09-27 05:44:15 -07:00
|
|
|
// returns the number items that should have been synced (even those that
|
|
|
|
// might have failed). One puller iteration handles all files currently
|
2014-09-28 04:00:38 -07:00
|
|
|
// flagged as needed in the folder. The specified number of copier, puller and
|
2014-09-27 05:44:15 -07:00
|
|
|
// finisher routines are used. It's seldom efficient to use more than one
|
|
|
|
// copier routine, while multiple pullers are essential and multiple finishers
|
|
|
|
// may be useful (they are primarily CPU bound due to hashing).
|
|
|
|
func (p *Puller) pullerIteration(ncopiers, npullers, nfinishers int) int {
|
|
|
|
pullChan := make(chan pullBlockState)
|
|
|
|
copyChan := make(chan copyBlocksState)
|
|
|
|
finisherChan := make(chan *sharedPullerState)
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var doneWg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < ncopiers; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
// copierRoutine finishes when copyChan is closed
|
|
|
|
p.copierRoutine(copyChan, finisherChan)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < npullers; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
// pullerRoutine finishes when pullChan is closed
|
|
|
|
p.pullerRoutine(pullChan, finisherChan)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < nfinishers; i++ {
|
|
|
|
doneWg.Add(1)
|
|
|
|
// finisherRoutine finishes when finisherChan is closed
|
|
|
|
go func() {
|
|
|
|
p.finisherRoutine(finisherChan)
|
|
|
|
doneWg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2014-09-28 04:39:39 -07:00
|
|
|
p.model.fmut.RLock()
|
2014-09-28 04:00:38 -07:00
|
|
|
files := p.model.folderFiles[p.folder]
|
2014-09-28 04:39:39 -07:00
|
|
|
p.model.fmut.RUnlock()
|
2014-09-27 05:44:15 -07:00
|
|
|
|
|
|
|
// !!!
|
|
|
|
// WithNeed takes a database snapshot (by necessity). By the time we've
|
|
|
|
// handled a bunch of files it might have become out of date and we might
|
|
|
|
// be attempting to sync with an old version of a file...
|
|
|
|
// !!!
|
|
|
|
|
|
|
|
changed := 0
|
2014-09-28 04:00:38 -07:00
|
|
|
files.WithNeed(protocol.LocalDeviceID, func(intf protocol.FileIntf) bool {
|
2014-09-27 22:56:05 -07:00
|
|
|
|
|
|
|
// Needed items are delivered sorted lexicographically. This isn't
|
|
|
|
// really optimal from a performance point of view - it would be
|
|
|
|
// better if files were handled in random order, to spread the load
|
|
|
|
// over the cluster. But it means that we can be sure that we fully
|
|
|
|
// handle directories before the files that go inside them, which is
|
|
|
|
// nice.
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
file := intf.(protocol.FileInfo)
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
events.Default.Log(events.ItemStarted, map[string]string{
|
2014-09-28 04:00:38 -07:00
|
|
|
"folder": p.folder,
|
2014-09-28 04:05:25 -07:00
|
|
|
"item": file.Name,
|
2014-09-27 05:44:15 -07:00
|
|
|
})
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if debug {
|
|
|
|
l.Debugln(p, "handling", file.Name)
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
switch {
|
|
|
|
case protocol.IsDirectory(file.Flags) && protocol.IsDeleted(file.Flags):
|
|
|
|
// A deleted directory
|
|
|
|
p.deleteDir(file)
|
|
|
|
case protocol.IsDirectory(file.Flags):
|
|
|
|
// A new or changed directory
|
|
|
|
p.handleDir(file)
|
|
|
|
case protocol.IsDeleted(file.Flags):
|
|
|
|
// A deleted file
|
|
|
|
p.deleteFile(file)
|
|
|
|
default:
|
2014-09-27 22:56:05 -07:00
|
|
|
// A new or changed file. This is the only case where we do stuff
|
|
|
|
// in the background; the other three are done synchronously.
|
2014-09-27 05:44:15 -07:00
|
|
|
p.handleFile(file, copyChan, pullChan)
|
2014-04-01 14:18:32 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
changed++
|
|
|
|
return true
|
|
|
|
})
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Signal copy and puller routines that we are done with the in data for
|
|
|
|
// this iteration
|
|
|
|
close(copyChan)
|
|
|
|
close(pullChan)
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Wait for them to finish, then signal the finisher chan that there will
|
|
|
|
// be no more input.
|
|
|
|
wg.Wait()
|
|
|
|
close(finisherChan)
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Wait for the finisherChan to finish.
|
|
|
|
doneWg.Wait()
|
2014-05-19 13:31:28 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
return changed
|
|
|
|
}
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// handleDir creates or updates the given directory
|
|
|
|
func (p *Puller) handleDir(file protocol.FileInfo) {
|
|
|
|
realName := filepath.Join(p.dir, file.Name)
|
|
|
|
mode := os.FileMode(file.Flags & 0777)
|
2014-05-19 13:31:28 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if debug {
|
2014-09-28 04:00:38 -07:00
|
|
|
curFile := p.model.CurrentFolderFile(p.folder, file.Name)
|
2014-09-27 05:44:15 -07:00
|
|
|
l.Debugf("need dir\n\t%v\n\t%v", file, curFile)
|
2014-04-01 14:18:32 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 16:54:25 -07:00
|
|
|
if info, err := os.Stat(realName); err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// The directory doesn't exist, so we create it with the right
|
|
|
|
// mode bits from the start.
|
|
|
|
|
|
|
|
mkdir := func(path string) error {
|
|
|
|
// We declare a function that acts on only the path name, so
|
|
|
|
// we can pass it to InWritableDir. We use a regular Mkdir and
|
|
|
|
// not MkdirAll because the parent should already exist.
|
|
|
|
return os.Mkdir(path, mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = osutil.InWritableDir(mkdir, realName); err == nil {
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.updateLocal(p.folder, file)
|
2014-09-27 16:54:25 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): %v", p.folder, file.Name, err)
|
2014-09-27 16:54:25 -07:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weird error when stat()'ing the dir. Probably won't work to do
|
|
|
|
// anything else with it if we can't even stat() it.
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): %v", p.folder, file.Name, err)
|
2014-09-27 16:54:25 -07:00
|
|
|
return
|
2014-09-27 05:44:15 -07:00
|
|
|
} else if !info.IsDir() {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): should be dir, but is not", p.folder, file.Name)
|
2014-03-28 06:36:57 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-27 16:54:25 -07:00
|
|
|
// The directory already exists, so we just correct the mode bits. (We
|
|
|
|
// don't handle modification times on directories, because that sucks...)
|
|
|
|
// It's OK to change mode bits on stuff within non-writable directories.
|
|
|
|
|
|
|
|
if err := os.Chmod(realName, mode); err == nil {
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.updateLocal(p.folder, file)
|
2014-09-27 16:54:25 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): %v", p.folder, file.Name, err)
|
2014-07-15 04:04:37 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
}
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// deleteDir attempts to delete the given directory
|
|
|
|
func (p *Puller) deleteDir(file protocol.FileInfo) {
|
|
|
|
realName := filepath.Join(p.dir, file.Name)
|
2014-09-27 16:54:25 -07:00
|
|
|
err := osutil.InWritableDir(os.Remove, realName)
|
2014-09-27 05:44:15 -07:00
|
|
|
if err == nil || os.IsNotExist(err) {
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.updateLocal(p.folder, file)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// deleteFile attempts to delete the given file
|
|
|
|
func (p *Puller) deleteFile(file protocol.FileInfo) {
|
|
|
|
realName := filepath.Join(p.dir, file.Name)
|
2014-04-01 14:18:32 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
var err error
|
|
|
|
if p.versioner != nil {
|
2014-09-27 16:54:25 -07:00
|
|
|
err = osutil.InWritableDir(p.versioner.Archive, realName)
|
2014-09-27 05:44:15 -07:00
|
|
|
} else {
|
2014-09-27 16:54:25 -07:00
|
|
|
err = osutil.InWritableDir(os.Remove, realName)
|
2014-09-27 05:44:15 -07:00
|
|
|
}
|
2014-07-13 12:07:24 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if err != nil {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): delete: %v", p.folder, file.Name, err)
|
2014-09-27 05:44:15 -07:00
|
|
|
} else {
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.updateLocal(p.folder, file)
|
2014-05-28 02:45:45 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
}
|
2014-05-28 02:45:45 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// handleFile queues the copies and pulls as necessary for a single new or
|
|
|
|
// changed file.
|
|
|
|
func (p *Puller) handleFile(file protocol.FileInfo, copyChan chan<- copyBlocksState, pullChan chan<- pullBlockState) {
|
2014-09-28 04:00:38 -07:00
|
|
|
curFile := p.model.CurrentFolderFile(p.folder, file.Name)
|
2014-09-27 05:44:15 -07:00
|
|
|
copyBlocks, pullBlocks := scanner.BlockDiff(curFile.Blocks, file.Blocks)
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if len(copyBlocks) == len(curFile.Blocks) && len(pullBlocks) == 0 {
|
|
|
|
// We are supposed to copy the entire file, and then fetch nothing. We
|
|
|
|
// are only updating metadata, so we don't actually *need* to make the
|
|
|
|
// copy.
|
2014-05-14 20:26:55 -07:00
|
|
|
if debug {
|
2014-09-27 05:44:15 -07:00
|
|
|
l.Debugln(p, "taking shortcut on", file.Name)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
p.shortcutFile(file)
|
|
|
|
return
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Figure out the absolute filenames we need once and for all
|
|
|
|
tempName := filepath.Join(p.dir, defTempNamer.TempName(file.Name))
|
|
|
|
realName := filepath.Join(p.dir, file.Name)
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
s := sharedPullerState{
|
|
|
|
file: file,
|
2014-09-28 04:05:25 -07:00
|
|
|
folder: p.folder,
|
2014-09-27 05:44:15 -07:00
|
|
|
tempName: tempName,
|
|
|
|
realName: realName,
|
|
|
|
pullNeeded: len(pullBlocks),
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
if len(copyBlocks) > 0 {
|
|
|
|
s.copyNeeded = 1
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-05-14 20:26:55 -07:00
|
|
|
if debug {
|
2014-09-27 05:44:15 -07:00
|
|
|
l.Debugf("%v need file %s; copy %d, pull %d", p, file.Name, len(copyBlocks), len(pullBlocks))
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if len(copyBlocks) > 0 {
|
|
|
|
cs := copyBlocksState{
|
|
|
|
sharedPullerState: &s,
|
|
|
|
blocks: copyBlocks,
|
|
|
|
}
|
|
|
|
copyChan <- cs
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if len(pullBlocks) > 0 {
|
|
|
|
for _, block := range pullBlocks {
|
|
|
|
ps := pullBlockState{
|
|
|
|
sharedPullerState: &s,
|
|
|
|
block: block,
|
|
|
|
}
|
|
|
|
pullChan <- ps
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// shortcutFile sets file mode and modification time, when that's the only
|
|
|
|
// thing that has changed.
|
|
|
|
func (p *Puller) shortcutFile(file protocol.FileInfo) {
|
|
|
|
realName := filepath.Join(p.dir, file.Name)
|
|
|
|
err := os.Chmod(realName, os.FileMode(file.Flags&0777))
|
|
|
|
if err != nil {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): shortcut: %v", p.folder, file.Name, err)
|
2014-09-27 05:44:15 -07:00
|
|
|
return
|
2014-04-27 03:14:53 -07:00
|
|
|
}
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
t := time.Unix(file.Modified, 0)
|
|
|
|
err = os.Chtimes(realName, t, t)
|
|
|
|
if err != nil {
|
2014-09-28 04:00:38 -07:00
|
|
|
l.Infof("Puller (folder %q, file %q): shortcut: %v", p.folder, file.Name, err)
|
2014-09-27 05:44:15 -07:00
|
|
|
return
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.updateLocal(p.folder, file)
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// copierRoutine reads pullerStates until the in channel closes and performs
|
|
|
|
// the relevant copy.
|
|
|
|
func (p *Puller) copierRoutine(in <-chan copyBlocksState, out chan<- *sharedPullerState) {
|
|
|
|
buf := make([]byte, scanner.StandardBlockSize)
|
2014-03-28 06:36:57 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
nextFile:
|
|
|
|
for state := range in {
|
|
|
|
dstFd, err := state.tempFile()
|
|
|
|
if err != nil {
|
|
|
|
// Nothing more to do for this failed file (the error was logged
|
|
|
|
// when it happened)
|
|
|
|
continue nextFile
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
srcFd, err := state.sourceFile()
|
|
|
|
if err != nil {
|
|
|
|
// As above
|
|
|
|
continue nextFile
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-08-07 12:38:04 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
for _, block := range state.blocks {
|
|
|
|
buf = buf[:int(block.Size)]
|
|
|
|
|
|
|
|
_, err = srcFd.ReadAt(buf, block.Offset)
|
2014-08-25 09:14:49 -07:00
|
|
|
if err != nil {
|
2014-09-27 05:44:15 -07:00
|
|
|
state.earlyClose("src read", err)
|
|
|
|
srcFd.Close()
|
|
|
|
continue nextFile
|
2014-08-26 22:00:15 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
|
|
|
|
_, err = dstFd.WriteAt(buf, block.Offset)
|
|
|
|
if err != nil {
|
|
|
|
state.earlyClose("dst write", err)
|
|
|
|
srcFd.Close()
|
|
|
|
continue nextFile
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
2014-05-19 14:42:08 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
|
|
|
|
srcFd.Close()
|
|
|
|
state.copyDone()
|
|
|
|
out <- state.sharedPullerState
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
func (p *Puller) pullerRoutine(in <-chan pullBlockState, out chan<- *sharedPullerState) {
|
|
|
|
nextBlock:
|
|
|
|
for state := range in {
|
|
|
|
if state.failed() != nil {
|
|
|
|
continue nextBlock
|
|
|
|
}
|
2014-07-24 00:38:16 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Select the least busy device to pull the block frop.model. If we found no
|
|
|
|
// feasible device at all, fail the block (and in the long run, the
|
2014-09-27 05:44:15 -07:00
|
|
|
// file).
|
2014-09-28 04:00:38 -07:00
|
|
|
potentialDevices := p.model.availability(p.folder, state.file.Name)
|
|
|
|
selected := activity.leastBusy(potentialDevices)
|
|
|
|
if selected == (protocol.DeviceID{}) {
|
|
|
|
state.earlyClose("pull", errNoDevice)
|
2014-09-27 05:44:15 -07:00
|
|
|
continue nextBlock
|
|
|
|
}
|
2014-07-24 00:38:16 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Get an fd to the temporary file. Tehcnically we don't need it until
|
|
|
|
// after fetching the block, but if we run into an error here there is
|
|
|
|
// no point in issuing the request to the network.
|
|
|
|
fd, err := state.tempFile()
|
|
|
|
if err != nil {
|
|
|
|
continue nextBlock
|
2014-07-24 00:38:16 -07:00
|
|
|
}
|
2014-08-05 00:46:11 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
// Fetch the block, while marking the selected device as in use so that
|
|
|
|
// leastBusy can select another device when someone else asks.
|
2014-09-27 05:44:15 -07:00
|
|
|
activity.using(selected)
|
2014-09-28 04:00:38 -07:00
|
|
|
buf, err := p.model.requestGlobal(selected, p.folder, state.file.Name, state.block.Offset, int(state.block.Size), state.block.Hash)
|
2014-09-27 05:44:15 -07:00
|
|
|
activity.done(selected)
|
|
|
|
if err != nil {
|
|
|
|
state.earlyClose("pull", err)
|
|
|
|
continue nextBlock
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-08-05 00:46:11 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Save the block data we got from the cluster
|
|
|
|
_, err = fd.WriteAt(buf, state.block.Offset)
|
|
|
|
if err != nil {
|
|
|
|
state.earlyClose("save", err)
|
|
|
|
continue nextBlock
|
|
|
|
}
|
2014-07-24 00:38:16 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
state.pullDone()
|
|
|
|
out <- state.sharedPullerState
|
2014-07-24 00:38:16 -07:00
|
|
|
}
|
2014-03-28 06:36:57 -07:00
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
func (p *Puller) finisherRoutine(in <-chan *sharedPullerState) {
|
|
|
|
for state := range in {
|
|
|
|
if closed, err := state.finalClose(); closed {
|
|
|
|
if debug {
|
|
|
|
l.Debugln(p, "closing", state.file.Name)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("puller: final:", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Verify the file against expected hashes
|
|
|
|
fd, err := os.Open(state.tempName)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("puller: final:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = scanner.Verify(fd, scanner.StandardBlockSize, state.file.Blocks)
|
|
|
|
fd.Close()
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("puller: final:", state.file.Name, err)
|
|
|
|
continue
|
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Set the correct permission bits on the new file
|
|
|
|
err = os.Chmod(state.tempName, os.FileMode(state.file.Flags&0777))
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(state.tempName)
|
|
|
|
l.Warnln("puller: final:", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Set the correct timestamp on the new file
|
|
|
|
t := time.Unix(state.file.Modified, 0)
|
|
|
|
err = os.Chtimes(state.tempName, t, t)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(state.tempName)
|
|
|
|
l.Warnln("puller: final:", err)
|
|
|
|
continue
|
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// If we should use versioning, let the versioner archive the old
|
|
|
|
// file before we replace it. Archiving a non-existent file is not
|
|
|
|
// an error.
|
|
|
|
if p.versioner != nil {
|
|
|
|
err = p.versioner.Archive(state.realName)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(state.tempName)
|
|
|
|
l.Warnln("puller: final:", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// Replace the original file with the new one
|
|
|
|
err = osutil.Rename(state.tempName, state.realName)
|
|
|
|
if err != nil {
|
|
|
|
os.Remove(state.tempName)
|
|
|
|
l.Warnln("puller: final:", err)
|
|
|
|
continue
|
2014-08-04 13:02:44 -07:00
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
|
|
|
|
// Record the updated file in the index
|
2014-09-28 04:00:38 -07:00
|
|
|
p.model.updateLocal(p.folder, state.file)
|
2014-04-27 03:14:53 -07:00
|
|
|
}
|
|
|
|
}
|
2014-09-27 05:44:15 -07:00
|
|
|
}
|
2014-04-27 03:14:53 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
// clean deletes orphaned temporary files
|
|
|
|
func (p *Puller) clean() {
|
|
|
|
filepath.Walk(p.dir, func(path string, info os.FileInfo, err error) error {
|
2014-08-04 13:02:44 -07:00
|
|
|
if err != nil {
|
2014-09-27 05:44:15 -07:00
|
|
|
return err
|
2014-05-23 05:31:16 -07:00
|
|
|
}
|
2014-05-25 11:49:08 -07:00
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
if info.Mode().IsRegular() && defTempNamer.IsTemporary(path) {
|
|
|
|
os.Remove(path)
|
2014-05-25 11:49:08 -07:00
|
|
|
}
|
|
|
|
|
2014-09-27 05:44:15 -07:00
|
|
|
return nil
|
|
|
|
})
|
2014-04-27 03:14:53 -07:00
|
|
|
}
|
2014-05-14 17:18:09 -07:00
|
|
|
|
2014-09-28 04:00:38 -07:00
|
|
|
func invalidateFolder(cfg *config.Configuration, folderID string, err error) {
|
|
|
|
for i := range cfg.Folders {
|
|
|
|
folder := &cfg.Folders[i]
|
|
|
|
if folder.ID == folderID {
|
|
|
|
folder.Invalid = err.Error()
|
2014-05-14 17:18:09 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|