Skip to content

Commit

Permalink
atomic writes
Browse files Browse the repository at this point in the history
  • Loading branch information
alixander committed Oct 5, 2024
1 parent fba0a3e commit 645f4bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
15 changes: 12 additions & 3 deletions d2cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 changes: 1 addition & 1 deletion go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 645f4bc

Please sign in to comment.