Skip to content

Commit a096f3a

Browse files
authored
internal/plugintest: Switch from (os.File).Readdir() to os.ReadDir() (#1056)
Reference: https://pkg.go.dev/os#File.Readdir Reference: https://pkg.go.dev/os#ReadDir While attempting to troubleshoot macOS kernel panic behaviors while running acceptance testing, I was able to capture a log whose last entry was `Symlinking source directories to work directory`. Since that operation tends to occur in temporary directory space, there is at least some potential there for strange macOS and Go behaviors. The `(os.File).Readdir()` method does make this mention in particular: > Most clients are better served by the more efficient ReadDir method. Making this a branch so others potentially affected by macOS kernel panics can try this out. This may not be the root cause, as there is a lot more process-oriented logic that occurs to start and stop providers in the testing framework, but if nothing else it could help performance slightly.
1 parent ef65fde commit a096f3a

File tree

1 file changed

+18
-57
lines changed

1 file changed

+18
-57
lines changed

internal/plugintest/util.go

Lines changed: 18 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -28,79 +28,40 @@ func symlinkFile(src string, dest string) error {
2828
return nil
2929
}
3030

31-
// symlinkDir is a simplistic function for recursively symlinking all files in a directory to a new path.
32-
// It is intended only for limited internal use and does not cover all edge cases.
33-
func symlinkDir(srcDir string, destDir string) (err error) {
34-
srcInfo, err := os.Stat(srcDir)
35-
if err != nil {
36-
return err
37-
}
38-
39-
err = os.MkdirAll(destDir, srcInfo.Mode())
40-
if err != nil {
41-
return err
42-
}
43-
44-
directory, _ := os.Open(srcDir)
45-
defer directory.Close()
46-
objects, err := directory.Readdir(-1)
47-
48-
for _, obj := range objects {
49-
srcPath := filepath.Join(srcDir, obj.Name())
50-
destPath := filepath.Join(destDir, obj.Name())
51-
52-
if obj.IsDir() {
53-
err = symlinkDir(srcPath, destPath)
54-
if err != nil {
55-
return err
56-
}
57-
} else {
58-
err = symlinkFile(srcPath, destPath)
59-
if err != nil {
60-
return err
61-
}
62-
}
63-
64-
}
65-
return
66-
}
67-
6831
// symlinkDirectoriesOnly finds only the first-level child directories in srcDir
6932
// and symlinks them into destDir.
7033
// Unlike symlinkDir, this is done non-recursively in order to limit the number
7134
// of file descriptors used.
72-
func symlinkDirectoriesOnly(srcDir string, destDir string) (err error) {
35+
func symlinkDirectoriesOnly(srcDir string, destDir string) error {
7336
srcInfo, err := os.Stat(srcDir)
7437
if err != nil {
75-
return err
38+
return fmt.Errorf("unable to stat source directory %q: %w", srcDir, err)
7639
}
7740

7841
err = os.MkdirAll(destDir, srcInfo.Mode())
7942
if err != nil {
80-
return err
43+
return fmt.Errorf("unable to make destination directory %q: %w", destDir, err)
8144
}
8245

83-
directory, err := os.Open(srcDir)
84-
if err != nil {
85-
return err
86-
}
87-
defer directory.Close()
88-
objects, err := directory.Readdir(-1)
46+
dirEntries, err := os.ReadDir(srcDir)
47+
8948
if err != nil {
90-
return err
49+
return fmt.Errorf("unable to read source directory %q: %w", srcDir, err)
9150
}
9251

93-
for _, obj := range objects {
94-
srcPath := filepath.Join(srcDir, obj.Name())
95-
destPath := filepath.Join(destDir, obj.Name())
96-
97-
if obj.IsDir() {
98-
err = symlinkFile(srcPath, destPath)
99-
if err != nil {
100-
return err
101-
}
52+
for _, dirEntry := range dirEntries {
53+
if !dirEntry.IsDir() {
54+
continue
10255
}
10356

57+
srcPath := filepath.Join(srcDir, dirEntry.Name())
58+
destPath := filepath.Join(destDir, dirEntry.Name())
59+
err := symlinkFile(srcPath, destPath)
60+
61+
if err != nil {
62+
return fmt.Errorf("unable to symlink directory %q to %q: %w", srcPath, destPath, err)
63+
}
10464
}
105-
return
65+
66+
return nil
10667
}

0 commit comments

Comments
 (0)