Skip to content

Commit c90705a

Browse files
committed
fix bug with config file being a relative symlink
Signed-off-by: Will Wang <willww64@gmail.com>
1 parent a69c036 commit c90705a

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

cli/config/configfile/file.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ func (configFile *ConfigFile) Save() (retErr error) {
171171
cfgFile := configFile.Filename
172172
if f, err := os.Readlink(cfgFile); err == nil {
173173
cfgFile = f
174+
// The target of a symlink can be a relative path, ensure the final path is absolute
175+
if !filepath.IsAbs(f) {
176+
cfgFile = filepath.Join(dir, f)
177+
}
174178
}
175179

176180
// Try copying the current config file (if any) ownership and permissions

cli/config/configfile/file_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,34 @@ func TestSaveWithSymlink(t *testing.T) {
538538
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
539539
}
540540

541+
func TestSaveWithRelativeSymlink(t *testing.T) {
542+
dir := fs.NewDir(t, t.Name(), fs.WithFile("real-config.json", `{}`))
543+
defer dir.Remove()
544+
545+
symLink := dir.Join("config.json")
546+
relativeRealFile := "real-config.json"
547+
realFile := dir.Join(relativeRealFile)
548+
err := os.Symlink(relativeRealFile, symLink)
549+
assert.NilError(t, err)
550+
551+
configFile := New(symLink)
552+
553+
err = configFile.Save()
554+
assert.NilError(t, err)
555+
556+
fi, err := os.Lstat(symLink)
557+
assert.NilError(t, err)
558+
assert.Assert(t, fi.Mode()&os.ModeSymlink != 0, "expected %s to be a symlink", symLink)
559+
560+
cfg, err := os.ReadFile(symLink)
561+
assert.NilError(t, err)
562+
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
563+
564+
cfg, err = os.ReadFile(realFile)
565+
assert.NilError(t, err)
566+
assert.Check(t, is.Equal(string(cfg), "{\n \"auths\": {}\n}"))
567+
}
568+
541569
func TestPluginConfig(t *testing.T) {
542570
configFile := New("test-plugin")
543571
defer os.Remove("test-plugin")

0 commit comments

Comments
 (0)