Use unique name and O_EXCL for temporary indexes (fixes #332)

This commit is contained in:
Jakob Borg 2014-06-04 13:43:59 +02:00
parent d87051ca99
commit bec5c76631

View File

@ -716,11 +716,12 @@ func (m *Model) saveIndex(repo string, dir string, fs []protocol.FileInfo) error
id := fmt.Sprintf("%x", sha1.Sum([]byte(m.repoCfgs[repo].Directory)))
name := id + ".idx.gz"
name = filepath.Join(dir, name)
idxf, err := os.Create(name + ".tmp")
tmp := fmt.Sprintf("%s.tmp.%d", name, time.Now().UnixNano())
idxf, err := os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer os.Remove(tmp)
gzw := gzip.NewWriter(idxf)
@ -748,7 +749,7 @@ func (m *Model) saveIndex(repo string, dir string, fs []protocol.FileInfo) error
l.Debugln("wrote index,", n, "bytes uncompressed")
}
return osutil.Rename(name+".tmp", name)
return osutil.Rename(tmp, name)
}
func (m *Model) loadIndex(repo string, dir string) []protocol.FileInfo {