2014-05-15 03:26:55 +00:00
|
|
|
package model
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/calmh/syncthing/buffers"
|
|
|
|
"github.com/calmh/syncthing/cid"
|
2014-05-15 00:18:09 +00:00
|
|
|
"github.com/calmh/syncthing/config"
|
2014-03-28 13:36:57 +00:00
|
|
|
"github.com/calmh/syncthing/protocol"
|
|
|
|
"github.com/calmh/syncthing/scanner"
|
|
|
|
)
|
|
|
|
|
|
|
|
type requestResult struct {
|
|
|
|
node string
|
|
|
|
file scanner.File
|
|
|
|
filepath string // full filepath name
|
|
|
|
offset int64
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
type openFile struct {
|
|
|
|
filepath string // full filepath name
|
|
|
|
temp string // temporary filename
|
|
|
|
availability uint64 // availability bitset
|
|
|
|
file *os.File
|
|
|
|
err error // error when opening or writing to file, all following operations are cancelled
|
|
|
|
outstanding int // number of requests we still have outstanding
|
|
|
|
done bool // we have sent all requests for this file
|
|
|
|
}
|
|
|
|
|
|
|
|
type activityMap map[string]int
|
|
|
|
|
|
|
|
func (m activityMap) leastBusyNode(availability uint64, cm *cid.Map) string {
|
|
|
|
var low int = 2<<30 - 1
|
|
|
|
var selected string
|
|
|
|
for _, node := range cm.Names() {
|
|
|
|
id := cm.Get(node)
|
|
|
|
if id == cid.LocalID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
usage := m[node]
|
|
|
|
if availability&(1<<id) != 0 {
|
|
|
|
if usage < low {
|
|
|
|
low = usage
|
|
|
|
selected = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m[selected]++
|
|
|
|
return selected
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m activityMap) decrease(node string) {
|
|
|
|
m[node]--
|
|
|
|
}
|
|
|
|
|
|
|
|
var errNoNode = errors.New("no available source node")
|
|
|
|
|
|
|
|
type puller struct {
|
2014-05-15 03:26:55 +00:00
|
|
|
cfg *config.Configuration
|
2014-03-28 13:36:57 +00:00
|
|
|
repo string
|
|
|
|
dir string
|
|
|
|
bq *blockQueue
|
|
|
|
model *Model
|
|
|
|
oustandingPerNode activityMap
|
|
|
|
openFiles map[string]openFile
|
|
|
|
requestSlots chan bool
|
|
|
|
blocks chan bqBlock
|
|
|
|
requestResults chan requestResult
|
|
|
|
}
|
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
func newPuller(repo, dir string, model *Model, slots int, cfg *config.Configuration) *puller {
|
2014-03-28 13:36:57 +00:00
|
|
|
p := &puller{
|
2014-05-15 03:26:55 +00:00
|
|
|
cfg: cfg,
|
2014-03-28 13:36:57 +00:00
|
|
|
repo: repo,
|
|
|
|
dir: dir,
|
|
|
|
bq: newBlockQueue(),
|
|
|
|
model: model,
|
|
|
|
oustandingPerNode: make(activityMap),
|
|
|
|
openFiles: make(map[string]openFile),
|
|
|
|
requestSlots: make(chan bool, slots),
|
|
|
|
blocks: make(chan bqBlock),
|
|
|
|
requestResults: make(chan requestResult),
|
|
|
|
}
|
|
|
|
|
|
|
|
if slots > 0 {
|
|
|
|
// Read/write
|
|
|
|
for i := 0; i < slots; i++ {
|
|
|
|
p.requestSlots <- true
|
|
|
|
}
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("starting puller; repo %q dir %q slots %d", repo, dir, slots)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
go p.run()
|
|
|
|
} else {
|
|
|
|
// Read only
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("starting puller; repo %q dir %q (read only)", repo, dir)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
go p.runRO()
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) run() {
|
|
|
|
go func() {
|
|
|
|
// fill blocks queue when there are free slots
|
|
|
|
for {
|
|
|
|
<-p.requestSlots
|
|
|
|
b := p.bq.get()
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("filler: queueing %q / %q offset %d copy %d", p.repo, b.file.Name, b.block.Offset, len(b.copy))
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
p.blocks <- b
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
walkTicker := time.Tick(time.Duration(p.cfg.Options.RescanIntervalS) * time.Second)
|
2014-03-28 13:36:57 +00:00
|
|
|
timeout := time.Tick(5 * time.Second)
|
2014-04-01 21:18:32 +00:00
|
|
|
changed := true
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
// Run the pulling loop as long as there are blocks to fetch
|
|
|
|
pull:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case res := <-p.requestResults:
|
2014-04-14 07:58:17 +00:00
|
|
|
p.model.setState(p.repo, RepoSyncing)
|
2014-04-01 21:18:32 +00:00
|
|
|
changed = true
|
2014-03-28 13:36:57 +00:00
|
|
|
p.requestSlots <- true
|
|
|
|
p.handleRequestResult(res)
|
|
|
|
|
|
|
|
case b := <-p.blocks:
|
2014-04-14 07:58:17 +00:00
|
|
|
p.model.setState(p.repo, RepoSyncing)
|
2014-04-01 21:18:32 +00:00
|
|
|
changed = true
|
2014-04-27 10:06:11 +00:00
|
|
|
if p.handleBlock(b) {
|
|
|
|
// Block was fully handled, free up the slot
|
|
|
|
p.requestSlots <- true
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
case <-timeout:
|
|
|
|
if len(p.openFiles) == 0 && p.bq.empty() {
|
|
|
|
// Nothing more to do for the moment
|
|
|
|
break pull
|
|
|
|
}
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("%q: idle but have %d open files", p.repo, len(p.openFiles))
|
2014-03-28 13:36:57 +00:00
|
|
|
i := 5
|
|
|
|
for _, f := range p.openFiles {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf(" %v", f)
|
2014-03-28 13:36:57 +00:00
|
|
|
i--
|
|
|
|
if i == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:18:32 +00:00
|
|
|
if changed {
|
2014-04-14 07:58:17 +00:00
|
|
|
p.model.setState(p.repo, RepoCleaning)
|
2014-04-01 21:18:32 +00:00
|
|
|
p.fixupDirectories()
|
|
|
|
changed = false
|
|
|
|
}
|
|
|
|
|
2014-04-14 07:58:17 +00:00
|
|
|
p.model.setState(p.repo, RepoIdle)
|
|
|
|
|
2014-03-28 13:36:57 +00:00
|
|
|
// Do a rescan if it's time for it
|
|
|
|
select {
|
|
|
|
case <-walkTicker:
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("%q: time for rescan", p.repo)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-05-04 16:20:25 +00:00
|
|
|
err := p.model.ScanRepo(p.repo)
|
|
|
|
if err != nil {
|
2014-05-15 03:26:55 +00:00
|
|
|
invalidateRepo(p.cfg, p.repo, err)
|
2014-05-04 16:20:25 +00:00
|
|
|
return
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
// Queue more blocks to fetch, if any
|
|
|
|
p.queueNeededBlocks()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) runRO() {
|
2014-05-15 03:26:55 +00:00
|
|
|
walkTicker := time.Tick(time.Duration(p.cfg.Options.RescanIntervalS) * time.Second)
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
for _ = range walkTicker {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("%q: time for rescan", p.repo)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-05-04 16:20:25 +00:00
|
|
|
err := p.model.ScanRepo(p.repo)
|
|
|
|
if err != nil {
|
2014-05-15 03:26:55 +00:00
|
|
|
invalidateRepo(p.cfg, p.repo, err)
|
2014-05-04 16:20:25 +00:00
|
|
|
return
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 21:18:32 +00:00
|
|
|
func (p *puller) fixupDirectories() {
|
|
|
|
var deleteDirs []string
|
2014-05-19 20:31:28 +00:00
|
|
|
var changed = 0
|
|
|
|
|
|
|
|
var walkFn = func(path string, info os.FileInfo, err error) error {
|
2014-04-01 21:18:32 +00:00
|
|
|
if !info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rn, err := filepath.Rel(p.dir, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if rn == "." {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-05-19 20:31:28 +00:00
|
|
|
cur := p.model.CurrentRepoFile(p.repo, rn)
|
2014-04-01 21:18:32 +00:00
|
|
|
if cur.Name != rn {
|
|
|
|
// No matching dir in current list; weird
|
2014-05-19 20:31:28 +00:00
|
|
|
if debug {
|
|
|
|
l.Debugf("missing dir: %s; %v", rn, cur)
|
|
|
|
}
|
2014-04-01 21:18:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if cur.Flags&protocol.FlagDeleted != 0 {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("queue delete dir: %v", cur)
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We queue the directories to delete since we walk the
|
|
|
|
// tree in depth first order and need to remove the
|
|
|
|
// directories in the opposite order.
|
|
|
|
|
|
|
|
deleteDirs = append(deleteDirs, path)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if cur.Flags&uint32(os.ModePerm) != uint32(info.Mode()&os.ModePerm) {
|
2014-05-19 20:31:28 +00:00
|
|
|
err := os.Chmod(path, os.FileMode(cur.Flags)&os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("Restoring folder flags: %q: %v", path, err)
|
|
|
|
} else {
|
|
|
|
changed++
|
|
|
|
if debug {
|
|
|
|
l.Debugf("restored dir flags: %o -> %v", info.Mode()&os.ModePerm, cur)
|
|
|
|
}
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if cur.Modified != info.ModTime().Unix() {
|
|
|
|
t := time.Unix(cur.Modified, 0)
|
2014-05-19 20:31:28 +00:00
|
|
|
err := os.Chtimes(path, t, t)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln("Restoring folder modtime: %q: %v", path, err)
|
|
|
|
} else {
|
|
|
|
changed++
|
|
|
|
if debug {
|
|
|
|
l.Debugf("restored dir modtime: %d -> %v", info.ModTime().Unix(), cur)
|
|
|
|
}
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2014-05-19 20:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
deleteDirs = nil
|
|
|
|
changed = 0
|
|
|
|
filepath.Walk(p.dir, walkFn)
|
|
|
|
|
|
|
|
var deleted = 0
|
|
|
|
// Delete any queued directories
|
|
|
|
for i := len(deleteDirs) - 1; i >= 0; i-- {
|
|
|
|
dir := deleteDirs[i]
|
|
|
|
if debug {
|
|
|
|
l.Debugln("delete dir:", dir)
|
|
|
|
}
|
|
|
|
err := os.Remove(dir)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnln(err)
|
|
|
|
} else {
|
|
|
|
deleted++
|
|
|
|
}
|
|
|
|
}
|
2014-04-01 21:18:32 +00:00
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-19 20:31:28 +00:00
|
|
|
l.Debugf("changed %d, deleted %d dirs", changed, deleted)
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
2014-05-19 20:31:28 +00:00
|
|
|
|
|
|
|
if changed+deleted == 0 {
|
|
|
|
return
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-28 13:36:57 +00:00
|
|
|
func (p *puller) handleRequestResult(res requestResult) {
|
|
|
|
p.oustandingPerNode.decrease(res.node)
|
|
|
|
f := res.file
|
|
|
|
|
|
|
|
of, ok := p.openFiles[f.Name]
|
|
|
|
if !ok || of.err != nil {
|
|
|
|
// no entry in openFiles means there was an error and we've cancelled the operation
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, of.err = of.file.WriteAt(res.data, res.offset)
|
|
|
|
buffers.Put(res.data)
|
|
|
|
|
|
|
|
of.outstanding--
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: wrote %q / %q offset %d outstanding %d done %v", p.repo, f.Name, res.offset, of.outstanding, of.done)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if of.done && of.outstanding == 0 {
|
2014-04-27 10:14:53 +00:00
|
|
|
p.closeFile(f)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 10:06:11 +00:00
|
|
|
// handleBlock fulfills the block request by copying, ignoring or fetching
|
|
|
|
// from the network. Returns true if the block was fully handled
|
|
|
|
// synchronously, i.e. if the slot can be reused.
|
|
|
|
func (p *puller) handleBlock(b bqBlock) bool {
|
2014-03-28 13:36:57 +00:00
|
|
|
f := b.file
|
|
|
|
|
2014-05-19 20:31:28 +00:00
|
|
|
// For directories, making sure they exist is enough.
|
|
|
|
// Deleted directories we mark as handled and delete later.
|
2014-04-01 21:18:32 +00:00
|
|
|
if f.Flags&protocol.FlagDirectory != 0 {
|
2014-05-19 20:31:28 +00:00
|
|
|
if f.Flags&protocol.FlagDeleted == 0 {
|
|
|
|
path := filepath.Join(p.dir, f.Name)
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
if err != nil && os.IsNotExist(err) {
|
|
|
|
if debug {
|
|
|
|
l.Debugf("create dir: %v", f)
|
|
|
|
}
|
|
|
|
err = os.MkdirAll(path, 0777)
|
|
|
|
if err != nil {
|
|
|
|
l.Warnf("Create folder: %q: %v", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if debug {
|
|
|
|
l.Debugf("ignore delete dir: %v", f)
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
|
|
|
p.model.updateLocal(p.repo, f)
|
2014-04-27 10:06:11 +00:00
|
|
|
return true
|
2014-04-01 21:18:32 +00:00
|
|
|
}
|
|
|
|
|
2014-03-28 13:36:57 +00:00
|
|
|
of, ok := p.openFiles[f.Name]
|
|
|
|
of.done = b.last
|
|
|
|
|
|
|
|
if !ok {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: %q: opening file %q", p.repo, f.Name)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 17:53:48 +00:00
|
|
|
of.availability = uint64(p.model.repoFiles[p.repo].Availability(f.Name))
|
2014-03-28 13:36:57 +00:00
|
|
|
of.filepath = filepath.Join(p.dir, f.Name)
|
|
|
|
of.temp = filepath.Join(p.dir, defTempNamer.TempName(f.Name))
|
|
|
|
|
|
|
|
dirName := filepath.Dir(of.filepath)
|
|
|
|
_, err := os.Stat(dirName)
|
|
|
|
if err != nil {
|
|
|
|
err = os.MkdirAll(dirName, 0777)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, err)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
of.file, of.err = os.Create(of.temp)
|
|
|
|
if of.err != nil {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, of.err)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
if !b.last {
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
}
|
2014-04-27 10:06:11 +00:00
|
|
|
return true
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
2014-04-22 12:26:51 +00:00
|
|
|
defTempNamer.Hide(of.temp)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if of.err != nil {
|
|
|
|
// We have already failed this file.
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q has already failed: %v", p.repo, f.Name, of.err)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
if b.last {
|
|
|
|
delete(p.openFiles, f.Name)
|
|
|
|
}
|
|
|
|
|
2014-04-27 10:06:11 +00:00
|
|
|
return true
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case len(b.copy) > 0:
|
|
|
|
p.handleCopyBlock(b)
|
2014-04-27 10:06:11 +00:00
|
|
|
return true
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
case b.block.Size > 0:
|
2014-04-27 10:06:11 +00:00
|
|
|
return p.handleRequestBlock(b)
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
p.handleEmptyBlock(b)
|
2014-04-27 10:06:11 +00:00
|
|
|
return true
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) handleCopyBlock(b bqBlock) {
|
|
|
|
// We have blocks to copy from the existing file
|
|
|
|
f := b.file
|
|
|
|
of := p.openFiles[f.Name]
|
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: copying %d blocks for %q / %q", len(b.copy), p.repo, f.Name)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var exfd *os.File
|
|
|
|
exfd, of.err = os.Open(of.filepath)
|
|
|
|
if of.err != nil {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, of.err)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
of.file.Close()
|
|
|
|
of.file = nil
|
|
|
|
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer exfd.Close()
|
|
|
|
|
|
|
|
for _, b := range b.copy {
|
|
|
|
bs := buffers.Get(int(b.Size))
|
|
|
|
_, of.err = exfd.ReadAt(bs, b.Offset)
|
|
|
|
if of.err == nil {
|
|
|
|
_, of.err = of.file.WriteAt(bs, b.Offset)
|
|
|
|
}
|
|
|
|
buffers.Put(bs)
|
|
|
|
if of.err != nil {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, of.err)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
exfd.Close()
|
|
|
|
of.file.Close()
|
|
|
|
of.file = nil
|
|
|
|
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 10:06:11 +00:00
|
|
|
// handleRequestBlock tries to pull a block from the network. Returns true if
|
|
|
|
// the block could _not_ be fetched (i.e. it was fully handled, matching the
|
|
|
|
// return criteria of handleBlock)
|
|
|
|
func (p *puller) handleRequestBlock(b bqBlock) bool {
|
2014-03-28 13:36:57 +00:00
|
|
|
f := b.file
|
2014-04-27 10:14:53 +00:00
|
|
|
of, ok := p.openFiles[f.Name]
|
|
|
|
if !ok {
|
|
|
|
panic("bug: request for non-open file")
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
|
|
|
|
node := p.oustandingPerNode.leastBusyNode(of.availability, p.model.cm)
|
|
|
|
if len(node) == 0 {
|
|
|
|
of.err = errNoNode
|
|
|
|
if of.file != nil {
|
|
|
|
of.file.Close()
|
|
|
|
of.file = nil
|
|
|
|
os.Remove(of.temp)
|
|
|
|
}
|
|
|
|
if b.last {
|
|
|
|
delete(p.openFiles, f.Name)
|
|
|
|
} else {
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
}
|
2014-04-27 10:06:11 +00:00
|
|
|
return true
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
of.outstanding++
|
|
|
|
p.openFiles[f.Name] = of
|
|
|
|
|
|
|
|
go func(node string, b bqBlock) {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: requesting %q / %q offset %d size %d from %q outstanding %d", p.repo, f.Name, b.block.Offset, b.block.Size, node, of.outstanding)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-29 17:53:48 +00:00
|
|
|
bs, err := p.model.requestGlobal(node, p.repo, f.Name, b.block.Offset, int(b.block.Size), nil)
|
2014-03-28 13:36:57 +00:00
|
|
|
p.requestResults <- requestResult{
|
|
|
|
node: node,
|
|
|
|
file: f,
|
|
|
|
filepath: of.filepath,
|
|
|
|
offset: b.block.Offset,
|
|
|
|
data: bs,
|
|
|
|
err: err,
|
|
|
|
}
|
|
|
|
}(node, b)
|
2014-04-27 10:06:11 +00:00
|
|
|
|
|
|
|
return false
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) handleEmptyBlock(b bqBlock) {
|
|
|
|
f := b.file
|
|
|
|
of := p.openFiles[f.Name]
|
|
|
|
|
|
|
|
if b.last {
|
|
|
|
if of.err == nil {
|
|
|
|
of.file.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Flags&protocol.FlagDeleted != 0 {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: delete %q", f.Name)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
os.Remove(of.temp)
|
2014-05-19 21:42:08 +00:00
|
|
|
os.Chmod(of.filepath, 0666)
|
2014-05-20 18:27:21 +00:00
|
|
|
if err := os.Remove(of.filepath); err == nil || os.IsNotExist(err) {
|
2014-05-19 21:42:08 +00:00
|
|
|
p.model.updateLocal(p.repo, f)
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
} else {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: no blocks to fetch and nothing to copy for %q / %q", p.repo, f.Name)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
t := time.Unix(f.Modified, 0)
|
2014-05-19 21:42:08 +00:00
|
|
|
if os.Chtimes(of.temp, t, t) != nil {
|
|
|
|
delete(p.openFiles, f.Name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if os.Chmod(of.temp, os.FileMode(f.Flags&0777)) != nil {
|
|
|
|
delete(p.openFiles, f.Name)
|
|
|
|
return
|
|
|
|
}
|
2014-04-22 12:26:51 +00:00
|
|
|
defTempNamer.Show(of.temp)
|
2014-05-19 21:42:08 +00:00
|
|
|
if Rename(of.temp, of.filepath) == nil {
|
|
|
|
p.model.updateLocal(p.repo, f)
|
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
delete(p.openFiles, f.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *puller) queueNeededBlocks() {
|
|
|
|
queued := 0
|
2014-04-01 21:18:32 +00:00
|
|
|
for _, f := range p.model.NeedFilesRepo(p.repo) {
|
|
|
|
lf := p.model.CurrentRepoFile(p.repo, f.Name)
|
2014-03-28 13:36:57 +00:00
|
|
|
have, need := scanner.BlockDiff(lf.Blocks, f.Blocks)
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("need:\n local: %v\n global: %v\n haveBlocks: %v\n needBlocks: %v", lf, f, have, need)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
queued++
|
|
|
|
p.bq.put(bqAdd{
|
|
|
|
file: f,
|
|
|
|
have: have,
|
|
|
|
need: need,
|
|
|
|
})
|
|
|
|
}
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug && queued > 0 {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("%q: queued %d blocks", p.repo, queued)
|
2014-03-28 13:36:57 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-27 10:14:53 +00:00
|
|
|
|
|
|
|
func (p *puller) closeFile(f scanner.File) {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: closing %q / %q", p.repo, f.Name)
|
2014-04-27 10:14:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
of := p.openFiles[f.Name]
|
|
|
|
of.file.Close()
|
|
|
|
defer os.Remove(of.temp)
|
|
|
|
|
|
|
|
delete(p.openFiles, f.Name)
|
|
|
|
|
|
|
|
fd, err := os.Open(of.temp)
|
|
|
|
if err != nil {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, err)
|
2014-04-27 10:14:53 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2014-05-15 03:26:55 +00:00
|
|
|
hb, _ := scanner.Blocks(fd, scanner.StandardBlockSize)
|
2014-04-27 10:14:53 +00:00
|
|
|
fd.Close()
|
|
|
|
|
|
|
|
if l0, l1 := len(hb), len(f.Blocks); l0 != l1 {
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: %q / %q: nblocks %d != %d", p.repo, f.Name, l0, l1)
|
2014-04-27 10:14:53 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range hb {
|
|
|
|
if bytes.Compare(hb[i].Hash, f.Blocks[i].Hash) != 0 {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: %q / %q: block %d hash mismatch", p.repo, f.Name, i)
|
2014-04-27 10:14:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t := time.Unix(f.Modified, 0)
|
|
|
|
os.Chtimes(of.temp, t, t)
|
|
|
|
os.Chmod(of.temp, os.FileMode(f.Flags&0777))
|
|
|
|
defTempNamer.Show(of.temp)
|
2014-05-15 03:26:55 +00:00
|
|
|
if debug {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: rename %q / %q: %q", p.repo, f.Name, of.filepath)
|
2014-04-27 10:14:53 +00:00
|
|
|
}
|
|
|
|
if err := Rename(of.temp, of.filepath); err == nil {
|
|
|
|
p.model.updateLocal(p.repo, f)
|
|
|
|
} else {
|
2014-05-15 00:08:56 +00:00
|
|
|
l.Debugf("pull: error: %q / %q: %v", p.repo, f.Name, err)
|
2014-04-27 10:14:53 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-15 00:18:09 +00:00
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
func invalidateRepo(cfg *config.Configuration, repoID string, err error) {
|
2014-05-15 00:18:09 +00:00
|
|
|
for i := range cfg.Repositories {
|
|
|
|
repo := &cfg.Repositories[i]
|
|
|
|
if repo.ID == repoID {
|
|
|
|
repo.Invalid = err.Error()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|