Use unique name and O_EXCL for temporary indexes (fixes #332)
This commit is contained in:
parent
d87051ca99
commit
bec5c76631
|
@ -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 {
|
||||||
|
|
Loading…
Reference in New Issue