mirror of https://github.com/docker/cli.git
fix bug with config file being a relative symlink
Signed-off-by: Will Wang <willww64@gmail.com>
This commit is contained in:
parent
a69c0365b6
commit
c90705a413
|
@ -171,6 +171,10 @@ func (configFile *ConfigFile) Save() (retErr error) {
|
||||||
cfgFile := configFile.Filename
|
cfgFile := configFile.Filename
|
||||||
if f, err := os.Readlink(cfgFile); err == nil {
|
if f, err := os.Readlink(cfgFile); err == nil {
|
||||||
cfgFile = f
|
cfgFile = f
|
||||||
|
// The target of a symlink can be a relative path, ensure the final path is absolute
|
||||||
|
if !filepath.IsAbs(f) {
|
||||||
|
cfgFile = filepath.Join(dir, f)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try copying the current config file (if any) ownership and permissions
|
// Try copying the current config file (if any) ownership and permissions
|
||||||
|
|
|
@ -538,6 +538,34 @@ func TestSaveWithSymlink(t *testing.T) {
|
||||||
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
|
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSaveWithRelativeSymlink(t *testing.T) {
|
||||||
|
dir := fs.NewDir(t, t.Name(), fs.WithFile("real-config.json", `{}`))
|
||||||
|
defer dir.Remove()
|
||||||
|
|
||||||
|
symLink := dir.Join("config.json")
|
||||||
|
relativeRealFile := "real-config.json"
|
||||||
|
realFile := dir.Join(relativeRealFile)
|
||||||
|
err := os.Symlink(relativeRealFile, symLink)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
configFile := New(symLink)
|
||||||
|
|
||||||
|
err = configFile.Save()
|
||||||
|
assert.NilError(t, err)
|
||||||
|
|
||||||
|
fi, err := os.Lstat(symLink)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Assert(t, fi.Mode()&os.ModeSymlink != 0, "expected %s to be a symlink", symLink)
|
||||||
|
|
||||||
|
cfg, err := os.ReadFile(symLink)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
|
||||||
|
|
||||||
|
cfg, err = os.ReadFile(realFile)
|
||||||
|
assert.NilError(t, err)
|
||||||
|
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestPluginConfig(t *testing.T) {
|
func TestPluginConfig(t *testing.T) {
|
||||||
configFile := New("test-plugin")
|
configFile := New("test-plugin")
|
||||||
defer os.Remove("test-plugin")
|
defer os.Remove("test-plugin")
|
||||||
|
|
Loading…
Reference in New Issue