Skip to content

Commit

Permalink
Merge pull request #646 from alfrunes/do-not-replace-output
Browse files Browse the repository at this point in the history
Do not replace output file on write
  • Loading branch information
lluiscampos authored Nov 5, 2024
2 parents b355e97 + 2df9f93 commit 6c1b643
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 63 deletions.
Binary file added cli/artifact.mender
Binary file not shown.
6 changes: 3 additions & 3 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func getCliContext() *cli.App {
artifactName,
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output artifact file.",
Usage: "Full path to output artifact file, '-' for stdout.",
},
cli.IntFlag{
Name: "version, v",
Expand Down Expand Up @@ -342,7 +342,7 @@ func getCliContext() *cli.App {
},
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output artifact file.",
Usage: "Full path to output artifact file, '-' for stdout.",
},
cli.IntFlag{
Name: "version, v",
Expand Down Expand Up @@ -434,7 +434,7 @@ func getCliContext() *cli.App {
artifactName,
cli.StringFlag{
Name: "output-path, o",
Usage: "Full path to output artifact file.",
Usage: "Full path to output artifact file, '-' for standard output.",
},
cli.IntFlag{
Name: "version, v",
Expand Down
105 changes: 45 additions & 60 deletions cli/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,22 @@ func writeBootstrapArtifact(c *cli.Context) error {

Log.Debugf("creating bootstrap artifact [%s], version: %d", name, version)

f, err := os.Create(name + ".tmp")
if err != nil {
return cli.NewExitError(
"can not create bootstrap artifact file: "+err.Error(),
errArtifactCreate,
)
var w io.Writer
if name == "-" {
w = os.Stdout
} else {
f, err := os.Create(name)
if err != nil {
return cli.NewExitError(
"can not create bootstrap artifact file: "+err.Error(),
errArtifactCreate,
)
}
defer f.Close()
w = f
}
defer func() {
f.Close()
// in case of success `.tmp` suffix will be removed and below
// will not remove valid artifact
os.Remove(name + ".tmp")
}()

aw, err := artifactWriter(c, comp, f, version)
aw, err := artifactWriter(c, comp, w, version)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
Expand Down Expand Up @@ -241,12 +242,6 @@ func writeBootstrapArtifact(c *cli.Context) error {
if err != nil {
return cli.NewExitError(err.Error(), 1)
}

f.Close()
err = os.Rename(name+".tmp", name)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}

Expand Down Expand Up @@ -298,21 +293,22 @@ func writeRootfs(c *cli.Context) error {
Updates: []handlers.Composer{h},
}

f, err := os.Create(name + ".tmp")
if err != nil {
return cli.NewExitError(
"can not create artifact file: "+err.Error(),
errArtifactCreate,
)
var w io.Writer
if name == "-" {
w = os.Stdout
} else {
f, err := os.Create(name)
if err != nil {
return cli.NewExitError(
"can not create artifact file: "+err.Error(),
errArtifactCreate,
)
}
defer f.Close()
w = f
}
defer func() {
f.Close()
// in case of success `.tmp` suffix will be removed and below
// will not remove valid artifact
os.Remove(name + ".tmp")
}()

aw, err := artifactWriter(c, comp, f, version)
aw, err := artifactWriter(c, comp, w, version)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
Expand Down Expand Up @@ -370,12 +366,6 @@ func writeRootfs(c *cli.Context) error {
if err != nil {
return cli.NewExitError(err.Error(), 1)
}

f.Close()
err = os.Rename(name+".tmp", name)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}

Expand All @@ -400,7 +390,7 @@ func reportProgress(c context.Context, state chan string) {
}
}

func artifactWriter(c *cli.Context, comp artifact.Compressor, f *os.File,
func artifactWriter(c *cli.Context, comp artifact.Compressor, w io.Writer,
ver int) (*awriter.Writer, error) {
privateKey, err := getKey(c)
if err != nil {
Expand All @@ -411,9 +401,9 @@ func artifactWriter(c *cli.Context, comp artifact.Compressor, f *os.File,
// check if we are having correct version
return nil, errors.New("can not use signed artifact with version 0")
}
return awriter.NewWriterSigned(f, comp, privateKey), nil
return awriter.NewWriterSigned(w, comp, privateKey), nil
}
return awriter.NewWriter(f, comp), nil
return awriter.NewWriter(w, comp), nil
}

func makeUpdates(ctx *cli.Context) (*awriter.Updates, error) {
Expand Down Expand Up @@ -765,21 +755,22 @@ func writeModuleImage(ctx *cli.Context) error {
return err
}

f, err := os.Create(name + ".tmp")
if err != nil {
return cli.NewExitError(
"can not create artifact file: "+err.Error(),
errArtifactCreate,
)
var w io.Writer
if name == "-" {
w = os.Stdout
} else {
f, err := os.Create(name)
if err != nil {
return cli.NewExitError(
"can not create artifact file: "+err.Error(),
errArtifactCreate,
)
}
defer f.Close()
w = f
}
defer func() {
f.Close()
// in case of success `.tmp` suffix will be removed and below
// will not remove valid artifact
os.Remove(name + ".tmp")
}()

aw, err := artifactWriter(ctx, comp, f, version)
aw, err := artifactWriter(ctx, comp, w, version)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
Expand Down Expand Up @@ -828,12 +819,6 @@ func writeModuleImage(ctx *cli.Context) error {
if err != nil {
return cli.NewExitError(err.Error(), 1)
}

f.Close()
err = os.Rename(name+".tmp", name)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}

Expand Down

0 comments on commit 6c1b643

Please sign in to comment.