Skip to content

Commit 110bec9

Browse files
updater.go: replace os.WriteFile with file.Write() (#669)
* Update updater.go replace os.WriteFile with file.Write() Signed-off-by: udf2457 <udf2457@users.noreply.github.com> * Update metadata/updater/updater.go File is closed on all branches in the code, removing this. Signed-off-by: Fredrik Skogman <kommendorkapten@github.com> --------- Signed-off-by: udf2457 <udf2457@users.noreply.github.com> Signed-off-by: Fredrik Skogman <kommendorkapten@github.com> Co-authored-by: Fredrik Skogman <kommendorkapten@github.com>
1 parent 830edf8 commit 110bec9

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

metadata/updater/updater.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,22 @@ func (update *Updater) persistMetadata(roleName string, data []byte) error {
596596
if err != nil {
597597
return err
598598
}
599-
defer file.Close()
599+
// change the file permissions to our desired permissions
600+
err = file.Chmod(0644)
601+
if err != nil {
602+
// close and delete the temporary file if there was an error while writing
603+
file.Close()
604+
errRemove := os.Remove(file.Name())
605+
if errRemove != nil {
606+
log.Info("Failed to delete temporary file", "name", file.Name())
607+
}
608+
return err
609+
}
600610
// write the data content to the temporary file
601-
err = os.WriteFile(file.Name(), data, 0644)
611+
_, err = file.Write(data)
602612
if err != nil {
603-
// delete the temporary file if there was an error while writing
613+
// close and delete the temporary file if there was an error while writing
614+
file.Close()
604615
errRemove := os.Remove(file.Name())
605616
if errRemove != nil {
606617
log.Info("Failed to delete temporary file", "name", file.Name())

0 commit comments

Comments
 (0)