Commit 595afc5c authored by Adam Reese's avatar Adam Reese Committed by GitHub

Merge pull request #2449 from rvbaz/write-index-atomic

Write repo file using atomicfile
parents dd7357df 965cb7fd
...@@ -102,6 +102,8 @@ imports: ...@@ -102,6 +102,8 @@ imports:
version: ba18e35c5c1b36ef6334cad706eb681153d2d379 version: ba18e35c5c1b36ef6334cad706eb681153d2d379
- name: github.com/exponent-io/jsonpath - name: github.com/exponent-io/jsonpath
version: d6023ce2651d8eafb5c75bb0c7167536102ec9f5 version: d6023ce2651d8eafb5c75bb0c7167536102ec9f5
- name: github.com/facebookgo/atomicfile
version: 2de1f203e7d5e386a6833233882782932729f27e
- name: github.com/facebookgo/symwalk - name: github.com/facebookgo/symwalk
version: 42004b9f322246749dd73ad71008b1f3160c0052 version: 42004b9f322246749dd73ad71008b1f3160c0052
- name: github.com/ghodss/yaml - name: github.com/ghodss/yaml
......
...@@ -41,6 +41,7 @@ import: ...@@ -41,6 +41,7 @@ import:
- package: github.com/gobwas/glob - package: github.com/gobwas/glob
version: ^0.2.1 version: ^0.2.1
- package: github.com/evanphx/json-patch - package: github.com/evanphx/json-patch
- package: github.com/facebookgo/atomicfile
- package: github.com/facebookgo/symwalk - package: github.com/facebookgo/symwalk
- package: github.com/BurntSushi/toml - package: github.com/BurntSushi/toml
version: ~0.3.0 version: ~0.3.0
......
...@@ -23,6 +23,7 @@ import ( ...@@ -23,6 +23,7 @@ import (
"os" "os"
"time" "time"
"github.com/facebookgo/atomicfile"
"github.com/ghodss/yaml" "github.com/ghodss/yaml"
) )
...@@ -134,9 +135,20 @@ func (r *RepoFile) Remove(name string) bool { ...@@ -134,9 +135,20 @@ func (r *RepoFile) Remove(name string) bool {
// WriteFile writes a repositories file to the given path. // WriteFile writes a repositories file to the given path.
func (r *RepoFile) WriteFile(path string, perm os.FileMode) error { func (r *RepoFile) WriteFile(path string, perm os.FileMode) error {
f, err := atomicfile.New(path, perm)
if err != nil {
return err
}
data, err := yaml.Marshal(r) data, err := yaml.Marshal(r)
if err != nil { if err != nil {
return err return err
} }
return ioutil.WriteFile(path, data, perm)
_, err = f.File.Write(data)
if err != nil {
return err
}
return f.Close()
} }
...@@ -201,10 +201,18 @@ func TestWriteFile(t *testing.T) { ...@@ -201,10 +201,18 @@ func TestWriteFile(t *testing.T) {
t.Errorf("failed to create test-file (%v)", err) t.Errorf("failed to create test-file (%v)", err)
} }
defer os.Remove(repoFile.Name()) defer os.Remove(repoFile.Name())
if err := sampleRepository.WriteFile(repoFile.Name(), 744); err != nil {
fileMode := os.FileMode(0744)
if err := sampleRepository.WriteFile(repoFile.Name(), fileMode); err != nil {
t.Errorf("failed to write file (%v)", err) t.Errorf("failed to write file (%v)", err)
} }
info, _ := os.Stat(repoFile.Name())
mode := info.Mode()
if mode != fileMode {
t.Errorf("incorrect file mode: %s (expected %s)", mode, fileMode)
}
repos, err := LoadRepositoriesFile(repoFile.Name()) repos, err := LoadRepositoriesFile(repoFile.Name())
if err != nil { if err != nil {
t.Errorf("failed to load file (%v)", err) t.Errorf("failed to load file (%v)", err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment