Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 4e3a563

Browse files
committed
Handle errors on close in config file write.
I'm not sure if this fixes anything, however I have seen some weird behavior on Windows where temp config files are left around and there doesn't seem to be any errors reported. Signed-off-by: Brian Goff <cpuguy83@gmail.com> (cherry picked from commit d02173090ff15ffb3ec4a5427ba35dafeb7fa92b) Signed-off-by: Brian Goff <cpuguy83@gmail.com> Upstream-commit: aaf1170520727772a5cbfa3982192055e17ff537 Component: cli
1 parent 19d2ee3 commit 4e3a563

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

  • components/cli/cli/config/configfile

components/cli/cli/config/configfile/file.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/docker/cli/cli/config/credentials"
1414
"github.com/docker/cli/cli/config/types"
1515
"github.com/pkg/errors"
16+
"github.com/sirupsen/logrus"
1617
)
1718

1819
const (
@@ -177,7 +178,7 @@ func (configFile *ConfigFile) SaveToWriter(writer io.Writer) error {
177178
}
178179

179180
// Save encodes and writes out all the authorization information
180-
func (configFile *ConfigFile) Save() error {
181+
func (configFile *ConfigFile) Save() (retErr error) {
181182
if configFile.Filename == "" {
182183
return errors.Errorf("Can't save config with empty filename")
183184
}
@@ -190,15 +191,26 @@ func (configFile *ConfigFile) Save() error {
190191
if err != nil {
191192
return err
192193
}
194+
defer func() {
195+
temp.Close()
196+
if retErr != nil {
197+
if err := os.Remove(temp.Name()); err != nil {
198+
logrus.WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file")
199+
}
200+
}
201+
}()
202+
193203
err = configFile.SaveToWriter(temp)
194-
temp.Close()
195204
if err != nil {
196-
os.Remove(temp.Name())
197205
return err
198206
}
207+
208+
if err := temp.Close(); err != nil {
209+
return errors.Wrap(err, "error closing temp file")
210+
}
211+
199212
// Try copying the current config file (if any) ownership and permissions
200213
copyFilePermissions(configFile.Filename, temp.Name())
201-
202214
return os.Rename(temp.Name(), configFile.Filename)
203215
}
204216

0 commit comments

Comments
 (0)