Skip to content
This repository was archived by the owner on Feb 21, 2022. It is now read-only.

Commit d596f15

Browse files
committed
fs/fsutil: write files atomically
Writing files atomically is very important. Unfortunately, it seems atomic rename is not available on Windows, so this feature is Unix-only.
1 parent 367ce02 commit d596f15

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

fs/fsutil/os_driver.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,6 @@ func (OSDriver) Symlink(oldname, newname string) error {
9090
return os.Symlink(oldname, newname)
9191
}
9292

93-
// WriteFile writes data to filename with permission perm.
94-
func (OSDriver) WriteFile(filename string, data []byte, perm os.FileMode) error {
95-
// TODO(gbrlsnchs): use package "renameio"
96-
return ioutil.WriteFile(filename, data, perm)
97-
}
98-
9993
type fileInfo struct {
10094
name string
10195
exists bool

fs/fsutil/os_driver_unix.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// +build !windows
2+
3+
package fsutil
4+
5+
import (
6+
"os"
7+
8+
"github.com/google/renameio"
9+
)
10+
11+
// WriteFile writes data to filename atomically with permission perm.
12+
func (OSDriver) WriteFile(filename string, data []byte, perm os.FileMode) error {
13+
return renameio.WriteFile(filename, data, perm)
14+
}

fs/fsutil/os_driver_windows.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// +build windows
2+
3+
package fsutil
4+
5+
import (
6+
"io/ioutil"
7+
"os"
8+
)
9+
10+
// WriteFile writes data to filename with permission perm.
11+
func (OSDriver) WriteFile(filename string, data []byte, perm os.FileMode) error {
12+
return ioutil.WriteFile(filename, data, perm)
13+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/gbrlsnchs/cli v0.6.0
88
github.com/google/go-cmdtest v0.2.0
99
github.com/google/go-cmp v0.4.0
10+
github.com/google/renameio v0.1.0
1011
github.com/magefile/mage v1.9.0
1112
github.com/sergi/go-diff v1.1.0 // indirect
1213
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f

0 commit comments

Comments
 (0)