Merge pull request #1194 from syncthing/fix-1186

Use comma-ok idiom to signal files missing in database (fixes #1186)
This commit is contained in:
Audrius Butkevicius
2015-01-06 21:54:13 +00:00
6 changed files with 75 additions and 45 deletions

View File

@@ -440,13 +440,17 @@ func (m *Model) NeedFolderFiles(folder string, max int) ([]protocol.FileInfoTrun
seen = make(map[string]bool, len(progressNames)+len(queuedNames))
for i, name := range progressNames {
progress[i] = rf.GetGlobal(name).ToTruncated() /// XXX: Should implement GetGlobalTruncated directly
seen[name] = true
if f, ok := rf.GetGlobal(name); ok {
progress[i] = f.ToTruncated() /// XXX: Should implement GetGlobalTruncated directly
seen[name] = true
}
}
for i, name := range queuedNames {
queued[i] = rf.GetGlobal(name).ToTruncated() /// XXX: Should implement GetGlobalTruncated directly
seen[name] = true
if f, ok := rf.GetGlobal(name); ok {
queued[i] = f.ToTruncated() /// XXX: Should implement GetGlobalTruncated directly
seen[name] = true
}
}
}
left := max - len(progress) - len(queued)
@@ -704,7 +708,11 @@ func (m *Model) Request(deviceID protocol.DeviceID, folder, name string, offset
return nil, ErrNoSuchFile
}
lf := r.Get(protocol.LocalDeviceID, name)
lf, ok := r.Get(protocol.LocalDeviceID, name)
if !ok {
return nil, ErrNoSuchFile
}
if lf.IsInvalid() || lf.IsDeleted() {
if debug {
l.Debugf("%v REQ(in): %s: %q / %q o=%d s=%d; invalid: %v", m, deviceID, folder, name, offset, size, lf)
@@ -759,18 +767,18 @@ func (m *Model) ReplaceLocal(folder string, fs []protocol.FileInfo) {
m.fmut.RUnlock()
}
func (m *Model) CurrentFolderFile(folder string, file string) protocol.FileInfo {
func (m *Model) CurrentFolderFile(folder string, file string) (protocol.FileInfo, bool) {
m.fmut.RLock()
f := m.folderFiles[folder].Get(protocol.LocalDeviceID, file)
f, ok := m.folderFiles[folder].Get(protocol.LocalDeviceID, file)
m.fmut.RUnlock()
return f
return f, ok
}
func (m *Model) CurrentGlobalFile(folder string, file string) protocol.FileInfo {
func (m *Model) CurrentGlobalFile(folder string, file string) (protocol.FileInfo, bool) {
m.fmut.RLock()
f := m.folderFiles[folder].GetGlobal(file)
f, ok := m.folderFiles[folder].GetGlobal(file)
m.fmut.RUnlock()
return f
return f, ok
}
type cFiler struct {
@@ -779,7 +787,7 @@ type cFiler struct {
}
// Implements scanner.CurrentFiler
func (cf cFiler) CurrentFile(file string) protocol.FileInfo {
func (cf cFiler) CurrentFile(file string) (protocol.FileInfo, bool) {
return cf.m.CurrentFolderFile(cf.r, file)
}
@@ -1309,8 +1317,8 @@ func (m *Model) Override(folder string) {
batch = batch[:0]
}
have := fs.Get(protocol.LocalDeviceID, need.Name)
if have.Name != need.Name {
have, ok := fs.Get(protocol.LocalDeviceID, need.Name)
if !ok || have.Name != need.Name {
// We are missing the file
need.Flags |= protocol.FlagDeleted
need.Blocks = nil

View File

@@ -348,8 +348,12 @@ func (p *Puller) pullerIteration(ignores *ignore.Matcher) int {
if !ok {
break
}
f := p.model.CurrentGlobalFile(p.folder, fileName)
p.handleFile(f, copyChan, finisherChan)
if f, ok := p.model.CurrentGlobalFile(p.folder, fileName); ok {
p.handleFile(f, copyChan, finisherChan)
} else {
// File is no longer in the index. Mark it as done and drop it.
p.queue.Done(fileName)
}
}
// Signal copy and puller routines that we are done with the in data for
@@ -386,7 +390,7 @@ func (p *Puller) handleDir(file protocol.FileInfo) {
}
if debug {
curFile := p.model.CurrentFolderFile(p.folder, file.Name)
curFile, _ := p.model.CurrentFolderFile(p.folder, file.Name)
l.Debugf("need dir\n\t%v\n\t%v", file, curFile)
}
@@ -480,9 +484,9 @@ func (p *Puller) deleteFile(file protocol.FileInfo) {
// 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, finisherChan chan<- *sharedPullerState) {
curFile := p.model.CurrentFolderFile(p.folder, file.Name)
curFile, ok := p.model.CurrentFolderFile(p.folder, file.Name)
if len(curFile.Blocks) == len(file.Blocks) && scanner.BlocksEqual(curFile.Blocks, file.Blocks) {
if ok && len(curFile.Blocks) == len(file.Blocks) && scanner.BlocksEqual(curFile.Blocks, file.Blocks) {
// 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.