Skip to content

Commit 5e71de4

Browse files
committed
configFile: directly write changes to configFile
In some situations, the config.json is volumed from somewhere else and the file is busy, move can't be performed on this file and configFile.Save() will be failed because it's trying to move another file to this file. This will write changes directly to the file. Signed-off-by: Seena Fallah <seenafallah@gmail.com>
1 parent 2291f61 commit 5e71de4

2 files changed

Lines changed: 12 additions & 59 deletions

File tree

cli/config/configfile/file.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ 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"
1716
)
1817

1918
const (
@@ -194,37 +193,26 @@ func (configFile *ConfigFile) Save() (retErr error) {
194193
if err := os.MkdirAll(dir, 0700); err != nil {
195194
return err
196195
}
197-
temp, err := ioutil.TempFile(dir, filepath.Base(configFile.Filename))
198-
if err != nil {
199-
return err
196+
197+
// Handle situation where the configfile is a symlink
198+
cfgFile := configFile.Filename
199+
if f, err := os.Readlink(cfgFile); err == nil {
200+
cfgFile = f
200201
}
201-
defer func() {
202-
temp.Close()
203-
if retErr != nil {
204-
if err := os.Remove(temp.Name()); err != nil {
205-
logrus.WithError(err).WithField("file", temp.Name()).Debug("Error cleaning up temp file")
206-
}
207-
}
208-
}()
209202

210-
err = configFile.SaveToWriter(temp)
203+
f, err := os.OpenFile(cfgFile, os.O_CREATE|os.O_WRONLY, 0600)
211204
if err != nil {
212205
return err
213206
}
207+
defer f.Close()
214208

215-
if err := temp.Close(); err != nil {
216-
return errors.Wrap(err, "error closing temp file")
217-
}
218-
219-
// Handle situation where the configfile is a symlink
220-
cfgFile := configFile.Filename
221-
if f, err := os.Readlink(cfgFile); err == nil {
222-
cfgFile = f
209+
// Write again to cfgFile
210+
err = f.Truncate(0)
211+
if err != nil {
212+
return err
223213
}
224214

225-
// Try copying the current config file (if any) ownership and permissions
226-
copyFilePermissions(cfgFile, temp.Name())
227-
return os.Rename(temp.Name(), cfgFile)
215+
return configFile.SaveToWriter(f)
228216
}
229217

230218
// ParseProxyConfig computes proxy configuration by retrieving the config for the provided host and

cli/config/configfile/file_unix.go

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)