Skip to content

Commit 9d9f632

Browse files
Merge pull request #6476 from thaJeztah/28.x_backport_ignore_broken_symlinks
[28.x backport] cli-plugins/manager: ignore broken symlinks
2 parents a2e17eb + d1122a2 commit 9d9f632

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

cli-plugins/manager/manager.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package manager
22

33
import (
44
"context"
5+
"errors"
56
"os"
67
"os/exec"
78
"path/filepath"
@@ -13,6 +14,7 @@ import (
1314
"github.com/docker/cli/cli-plugins/metadata"
1415
"github.com/docker/cli/cli/config"
1516
"github.com/docker/cli/cli/config/configfile"
17+
"github.com/docker/cli/cli/debug"
1618
"github.com/fvbommel/sortorder"
1719
"github.com/spf13/cobra"
1820
"golang.org/x/sync/errgroup"
@@ -74,9 +76,17 @@ func addPluginCandidatesFromDir(res map[string][]string, d string) {
7476
return
7577
}
7678
for _, dentry := range dentries {
77-
switch dentry.Type() & os.ModeType { //nolint:exhaustive,nolintlint // no need to include all possible file-modes in this list
78-
case 0, os.ModeSymlink:
79-
// Regular file or symlink, keep going
79+
switch mode := dentry.Type() & os.ModeType; mode { //nolint:exhaustive,nolintlint // no need to include all possible file-modes in this list
80+
case os.ModeSymlink:
81+
if !debug.IsEnabled() {
82+
// Skip broken symlinks unless debug is enabled. With debug
83+
// enabled, this will print a warning in "docker info".
84+
if _, err := os.Stat(filepath.Join(d, dentry.Name())); errors.Is(err, os.ErrNotExist) {
85+
continue
86+
}
87+
}
88+
case 0:
89+
// Regular file, keep going
8090
default:
8191
// Something else, ignore.
8292
continue

cli-plugins/manager/manager_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestListPluginCandidates(t *testing.T) {
3838
"plugins3-target", // Will be referenced as a symlink from below
3939
fs.WithFile("docker-plugin1", ""),
4040
fs.WithDir("ignored3"),
41-
fs.WithSymlink("docker-brokensymlink", "broken"), // A broken symlink is still a candidate (but would fail tests later)
41+
fs.WithSymlink("docker-brokensymlink", "broken"), // A broken symlink is ignored
4242
fs.WithFile("non-plugin-symlinked", ""), // This shouldn't appear, but ...
4343
fs.WithSymlink("docker-symlinked", "non-plugin-symlinked"), // ... this link to it should.
4444
),
@@ -72,9 +72,6 @@ func TestListPluginCandidates(t *testing.T) {
7272
"hardlink2": {
7373
dir.Join("plugins2", "docker-hardlink2"),
7474
},
75-
"brokensymlink": {
76-
dir.Join("plugins3", "docker-brokensymlink"),
77-
},
7875
"symlinked": {
7976
dir.Join("plugins3", "docker-symlinked"),
8077
},

0 commit comments

Comments
 (0)