Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
486c7bb6
Unverified
Commit
486c7bb6
authored
Sep 12, 2023
by
Adrian Sutton
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
cannon: Use atomic write pattern to avoid leaving partially written files.
parent
5053698b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
1 deletion
+12
-1
json.go
cannon/cmd/json.go
+12
-1
No files found.
cannon/cmd/json.go
View file @
486c7bb6
...
@@ -6,6 +6,7 @@ import (
...
@@ -6,6 +6,7 @@ import (
"fmt"
"fmt"
"io"
"io"
"os"
"os"
"path"
"github.com/ethereum-optimism/optimism/op-service/ioutil"
"github.com/ethereum-optimism/optimism/op-service/ioutil"
)
)
...
@@ -29,13 +30,20 @@ func loadJSON[X any](inputPath string) (*X, error) {
...
@@ -29,13 +30,20 @@ func loadJSON[X any](inputPath string) (*X, error) {
func
writeJSON
[
X
any
](
outputPath
string
,
value
X
,
outIfEmpty
bool
)
error
{
func
writeJSON
[
X
any
](
outputPath
string
,
value
X
,
outIfEmpty
bool
)
error
{
var
out
io
.
Writer
var
out
io
.
Writer
finish
:=
func
()
error
{
return
nil
}
if
outputPath
!=
""
{
if
outputPath
!=
""
{
f
,
err
:=
ioutil
.
OpenCompressed
(
outputPath
,
os
.
O_WRONLY
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0755
)
// Write to a tmp file but reserve the file extension if present
tmpPath
:=
outputPath
+
"-tmp"
+
path
.
Ext
(
outputPath
)
f
,
err
:=
ioutil
.
OpenCompressed
(
tmpPath
,
os
.
O_WRONLY
|
os
.
O_CREATE
|
os
.
O_TRUNC
,
0755
)
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to open output file: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to open output file: %w"
,
err
)
}
}
defer
f
.
Close
()
defer
f
.
Close
()
out
=
f
out
=
f
finish
=
func
()
error
{
// Rename the file into place as atomically as the OS will allow
return
os
.
Rename
(
tmpPath
,
outputPath
)
}
}
else
if
outIfEmpty
{
}
else
if
outIfEmpty
{
out
=
os
.
Stdout
out
=
os
.
Stdout
}
else
{
}
else
{
...
@@ -49,5 +57,8 @@ func writeJSON[X any](outputPath string, value X, outIfEmpty bool) error {
...
@@ -49,5 +57,8 @@ func writeJSON[X any](outputPath string, value X, outIfEmpty bool) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to append new-line: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to append new-line: %w"
,
err
)
}
}
if
err
:=
finish
();
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to finish write: %w"
,
err
)
}
return
nil
return
nil
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment