From f8526b1556d1fff8ca4f51af6c28d8c1ec4126cc Mon Sep 17 00:00:00 2001 From: Scott Rigby Date: Wed, 28 May 2025 14:26:52 -0400 Subject: [PATCH] Fix file write Signed-off-by: Scott Rigby --- pkg/registry/client.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/registry/client.go b/pkg/registry/client.go index dd17c5d99..286852807 100644 --- a/pkg/registry/client.go +++ b/pkg/registry/client.go @@ -105,13 +105,19 @@ func handleEmptyConfgFile(credentialsFile string, client *Client) { // handle only if credentials file exists // ORAS already handles if file does not exist if err == nil { - var configData map[string]json.RawMessage + fileInfo, err := os.Stat(credentialsFile) + if err != nil { + client.err = err + } // handle only if credentials file is empty - if err := json.NewDecoder(f).Decode(&configData); err != nil && errors.Is(err, io.EOF) { + if fileInfo.Size() == 0 { + emptyMap := make(map[string]interface{}) + jsonData, err := json.Marshal(emptyMap) + if err != nil { + client.err = err + } // Attempt to write empty JSON map to config file - client.credentialsFile = "" - encoder := json.NewEncoder(f) - err = encoder.Encode(configData) + err = os.WriteFile(credentialsFile, jsonData, 0644) // Note that <3.18.0 Helm would fail if the config file was not // writable, so we can continue to error if that is the case if err != nil {