Skip to content

Commit 186daa8

Browse files
nh2sgotti
authored andcommitted
keeper: write files atomically
fixes sorintlab#247
1 parent c97847a commit 186daa8

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

cmd/keeper/keeper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,7 @@ func (p *PostgresKeeper) saveKeeperLocalState() error {
13591359
if err != nil {
13601360
return err
13611361
}
1362-
return ioutil.WriteFile(p.keeperLocalStateFilePath(), sj, 0600)
1362+
return common.WriteFileAtomic(p.keeperLocalStateFilePath(), sj, 0600)
13631363
}
13641364

13651365
func (p *PostgresKeeper) dbLocalStateFilePath() string {
@@ -1384,7 +1384,7 @@ func (p *PostgresKeeper) saveDBLocalState() error {
13841384
if err != nil {
13851385
return err
13861386
}
1387-
return ioutil.WriteFile(p.dbLocalStateFilePath(), sj, 0600)
1387+
return common.WriteFileAtomic(p.dbLocalStateFilePath(), sj, 0600)
13881388
}
13891389

13901390
func sigHandler(sigs chan os.Signal, stop chan bool) {

common/common.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ package common
1616

1717
import (
1818
"fmt"
19+
"io/ioutil"
20+
"os"
21+
"path"
1922
"reflect"
2023
"strings"
2124

@@ -66,3 +69,34 @@ type Parameters map[string]string
6669
func (s Parameters) Equals(is Parameters) bool {
6770
return reflect.DeepEqual(s, is)
6871
}
72+
73+
// WriteFileAtomic atoically writes a file, it achieves this by creating a
74+
// temporary file and then moving it.
75+
// This function is taken from
76+
// https://github.com/youtube/vitess/blob/master/go/ioutil2/ioutil.go
77+
// Copyright 2012, Google Inc. BSD-license, see licenses/LICENSE-BSD-3-Clause
78+
func WriteFileAtomic(filename string, data []byte, perm os.FileMode) error {
79+
dir, name := path.Split(filename)
80+
f, err := ioutil.TempFile(dir, name)
81+
if err != nil {
82+
return err
83+
}
84+
_, err = f.Write(data)
85+
if err == nil {
86+
err = f.Sync()
87+
}
88+
if closeErr := f.Close(); err == nil {
89+
err = closeErr
90+
}
91+
if permErr := os.Chmod(f.Name(), perm); err == nil {
92+
err = permErr
93+
}
94+
if err == nil {
95+
err = os.Rename(f.Name(), filename)
96+
}
97+
// Any err should result in full cleanup.
98+
if err != nil {
99+
os.Remove(f.Name())
100+
}
101+
return err
102+
}

licenses/LICENSE-BSD-3-Clause

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) [year], [fullname]
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
16+
* Neither the name of the copyright holder nor the names of its
17+
contributors may be used to endorse or promote products derived from
18+
this software without specific prior written permission.
19+
20+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

0 commit comments

Comments
 (0)