diff options
author | Jakob Borg <jakob@nym.se> | 2014-04-27 12:14:53 +0200 |
---|---|---|
committer | Jakob Borg <jakob@nym.se> | 2014-04-27 12:14:53 +0200 |
commit | ef81a366542d1d0fc46a1567f9b8881cf9c457e0 (patch) | |
tree | 918ce0e111fb1bc016bcb174607b55acd77af826 /cmd | |
parent | 9fd2724d73db5fd33f22d0e06e43c41c4e951c71 (diff) |
Extract method closeFile
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/syncthing/puller.go | 104 |
1 files changed, 56 insertions, 48 deletions
diff --git a/cmd/syncthing/puller.go b/cmd/syncthing/puller.go index 7c5e69b993..b95961d4bf 100644 --- a/cmd/syncthing/puller.go +++ b/cmd/syncthing/puller.go @@ -196,7 +196,7 @@ func (p *puller) runRO() { func (p *puller) fixupDirectories() { var deleteDirs []string - fn := func(path string, info os.FileInfo, err error) error { + filepath.Walk(p.dir, func(path string, info os.FileInfo, err error) error { if !info.IsDir() { return nil } @@ -245,8 +245,7 @@ func (p *puller) fixupDirectories() { } return nil - } - filepath.Walk(p.dir, fn) + }) // Delete any queued directories for i := len(deleteDirs) - 1; i >= 0; i-- { @@ -281,50 +280,7 @@ func (p *puller) handleRequestResult(res requestResult) { } if of.done && of.outstanding == 0 { - if debugPull { - dlog.Printf("pull: closing %q / %q", p.repo, f.Name) - } - of.file.Close() - defer os.Remove(of.temp) - - delete(p.openFiles, f.Name) - - fd, err := os.Open(of.temp) - if err != nil { - if debugPull { - dlog.Printf("pull: error: %q / %q: %v", p.repo, f.Name, err) - } - return - } - hb, _ := scanner.Blocks(fd, BlockSize) - fd.Close() - - if l0, l1 := len(hb), len(f.Blocks); l0 != l1 { - if debugPull { - dlog.Printf("pull: %q / %q: nblocks %d != %d", p.repo, f.Name, l0, l1) - } - return - } - - for i := range hb { - if bytes.Compare(hb[i].Hash, f.Blocks[i].Hash) != 0 { - dlog.Printf("pull: %q / %q: block %d hash mismatch", p.repo, f.Name, i) - 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) - if debugPull { - dlog.Printf("pull: rename %q / %q: %q", p.repo, f.Name, of.filepath) - } - if err := Rename(of.temp, of.filepath); err == nil { - p.model.updateLocal(p.repo, f) - } else { - dlog.Printf("pull: error: %q / %q: %v", p.repo, f.Name, err) - } + p.closeFile(f) } } @@ -457,7 +413,10 @@ func (p *puller) handleCopyBlock(b bqBlock) { // return criteria of handleBlock) func (p *puller) handleRequestBlock(b bqBlock) bool { f := b.file - of := p.openFiles[f.Name] + of, ok := p.openFiles[f.Name] + if !ok { + panic("bug: request for non-open file") + } node := p.oustandingPerNode.leastBusyNode(of.availability, p.model.cm) if len(node) == 0 { @@ -546,3 +505,52 @@ func (p *puller) queueNeededBlocks() { dlog.Printf("%q: queued %d blocks", p.repo, queued) } } + +func (p *puller) closeFile(f scanner.File) { + if debugPull { + dlog.Printf("pull: closing %q / %q", p.repo, f.Name) + } + + 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 { + if debugPull { + dlog.Printf("pull: error: %q / %q: %v", p.repo, f.Name, err) + } + return + } + hb, _ := scanner.Blocks(fd, BlockSize) + fd.Close() + + if l0, l1 := len(hb), len(f.Blocks); l0 != l1 { + if debugPull { + dlog.Printf("pull: %q / %q: nblocks %d != %d", p.repo, f.Name, l0, l1) + } + return + } + + for i := range hb { + if bytes.Compare(hb[i].Hash, f.Blocks[i].Hash) != 0 { + dlog.Printf("pull: %q / %q: block %d hash mismatch", p.repo, f.Name, i) + 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) + if debugPull { + dlog.Printf("pull: rename %q / %q: %q", p.repo, f.Name, of.filepath) + } + if err := Rename(of.temp, of.filepath); err == nil { + p.model.updateLocal(p.repo, f) + } else { + dlog.Printf("pull: error: %q / %q: %v", p.repo, f.Name, err) + } +} |