Fix atomic write not working when file does not exist
This commit is contained in:
parent
7223fdaa38
commit
73a3366d53
@ -3,6 +3,7 @@
|
|||||||
package utils
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
@ -12,12 +13,17 @@ import (
|
|||||||
var _ = fmt.Print
|
var _ = fmt.Print
|
||||||
|
|
||||||
func AtomicWriteFile(path string, data []byte, perm os.FileMode) (err error) {
|
func AtomicWriteFile(path string, data []byte, perm os.FileMode) (err error) {
|
||||||
path, err = filepath.EvalSymlinks(path)
|
npath, err := filepath.EvalSymlinks(path)
|
||||||
|
if errors.Is(err, fs.ErrNotExist) {
|
||||||
|
err = nil
|
||||||
|
npath = path
|
||||||
|
}
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
path = npath
|
||||||
path, err = filepath.Abs(path)
|
path, err = filepath.Abs(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var f *os.File
|
var f *os.File
|
||||||
f, err = os.CreateTemp(filepath.Dir(path), filepath.Base(path))
|
f, err = os.CreateTemp(filepath.Dir(path), filepath.Base(path)+".atomic-write-")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
removed := false
|
removed := false
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user