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
1 changed files with 4 additions and 3 deletions

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))) id := fmt.Sprintf("%x", sha1.Sum([]byte(m.repoCfgs[repo].Directory)))
name := id + ".idx.gz" name := id + ".idx.gz"
name = filepath.Join(dir, name) name = filepath.Join(dir, name)
tmp := fmt.Sprintf("%s.tmp.%d", name, time.Now().UnixNano())
idxf, err := os.Create(name + ".tmp") idxf, err := os.OpenFile(tmp, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
if err != nil { if err != nil {
return err return err
} }
defer os.Remove(tmp)
gzw := gzip.NewWriter(idxf) 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") 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 { func (m *Model) loadIndex(repo string, dir string) []protocol.FileInfo {