atomic writes

This commit is contained in:
Alexander Wang 2024-10-05 16:33:48 -06:00
parent fba0a3e150
commit 645f4bc5ca
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
3 changed files with 15 additions and 6 deletions

View file

@ -537,7 +537,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
if err != nil {
return nil, false, err
}
err = ms.WritePath(outputPath, out)
err = Write(ms, outputPath, out)
if err != nil {
return nil, false, err
}
@ -621,7 +621,7 @@ func compile(ctx context.Context, ms *xmain.State, plugins []d2plugin.Plugin, fs
if err != nil {
return nil, false, err
}
err = ms.WritePath(outputPath, out)
err = Write(ms, outputPath, out)
if err != nil {
return nil, false, err
}
@ -906,7 +906,7 @@ func _render(ctx context.Context, ms *xmain.State, plugin d2plugin.Plugin, opts
if err != nil {
return svg, err
}
err = ms.WritePath(outputPath, out)
err = Write(ms, outputPath, out)
if err != nil {
return svg, err
}
@ -1369,6 +1369,15 @@ func AnimatePNGs(ms *xmain.State, pngs [][]byte, animIntervalMs int) ([]byte, er
return xgif.AnimatePNGs(pngs, animIntervalMs)
}
func Write(ms *xmain.State, path string, out []byte) error {
err := ms.AtomicWritePath(path, out)
if err == nil {
return nil
}
ms.Log.Debug.Printf("atomic write failed: %s, trying non-atomic write", err.Error())
return ms.WritePath(path, out)
}
func init() {
log.Init()
}

2
go.mod generated
View file

@ -29,7 +29,7 @@ require (
golang.org/x/xerrors v0.0.0-20240903120638-7835f813f4da
gonum.org/v1/plot v0.14.0
nhooyr.io/websocket v1.8.17
oss.terrastruct.com/util-go v0.0.0-20241001213509-2c8848a930fb
oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896
)
require (

4
go.sum generated
View file

@ -205,7 +205,7 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.17 h1:KEVeLJkUywCKVsnLIDlD/5gtayKp8VoCkksHCGGfT9Y=
nhooyr.io/websocket v1.8.17/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
oss.terrastruct.com/util-go v0.0.0-20241001213509-2c8848a930fb h1:3bvpnAM5gbaqmQYi1kWzNyfEH8p0UM4uCwAWV+/Ph7k=
oss.terrastruct.com/util-go v0.0.0-20241001213509-2c8848a930fb/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896 h1:g752s1ECv9FD8GunFOZRGWzjeR0cr/TZdSsjAnFEmL8=
oss.terrastruct.com/util-go v0.0.0-20241005222610-44c011a04896/go.mod h1:eMWv0sOtD9T2RUl90DLWfuShZCYp4NrsqNpI8eqO6U4=
rsc.io/pdf v0.1.1 h1:k1MczvYDUvJBe93bYd7wrZLLUEcLZAuF824/I4e5Xr4=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=