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

Commit

Permalink
fs/fsutil: write files atomically
Browse files Browse the repository at this point in the history
Writing files atomically is very important. Unfortunately, it seems
atomic rename is not available on Windows, so this feature is Unix-only.
  • Loading branch information
gbrlsnchs committed Apr 17, 2020
1 parent 367ce02 commit d596f15
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
6 changes: 0 additions & 6 deletions fs/fsutil/os_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ func (OSDriver) Symlink(oldname, newname string) error {
return os.Symlink(oldname, newname)
}

// WriteFile writes data to filename with permission perm.
func (OSDriver) WriteFile(filename string, data []byte, perm os.FileMode) error {
// TODO(gbrlsnchs): use package "renameio"
return ioutil.WriteFile(filename, data, perm)
}

type fileInfo struct {
name string
exists bool
Expand Down
14 changes: 14 additions & 0 deletions fs/fsutil/os_driver_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// +build !windows

package fsutil

import (
"os"

"github.com/google/renameio"
)

// WriteFile writes data to filename atomically with permission perm.
func (OSDriver) WriteFile(filename string, data []byte, perm os.FileMode) error {
return renameio.WriteFile(filename, data, perm)
}
13 changes: 13 additions & 0 deletions fs/fsutil/os_driver_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// +build windows

package fsutil

import (
"io/ioutil"
"os"
)

// WriteFile writes data to filename with permission perm.
func (OSDriver) WriteFile(filename string, data []byte, perm os.FileMode) error {
return ioutil.WriteFile(filename, data, perm)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/gbrlsnchs/cli v0.6.0
github.com/google/go-cmdtest v0.2.0
github.com/google/go-cmp v0.4.0
github.com/google/renameio v0.1.0
github.com/magefile/mage v1.9.0
github.com/sergi/go-diff v1.1.0 // indirect
golang.org/x/lint v0.0.0-20191125180803-fdd1cda4f05f
Expand Down

0 comments on commit d596f15

Please sign in to comment.