Skip to content

Commit e34cd16

Browse files
🐹 🐛 fix cachedirectory.go to reuse empty dir
For the cases of cache directories that need to exist before, the code fails with the error message that it can't create the directory. For example, cache dir from github workflows. This code change makes sure to verify the cases if it's empty or not.
1 parent 5bc50ff commit e34cd16

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

internal/cachedirectory/cachedirectory.go

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,37 @@ func NewCacheDirectory(path string) CacheDirectory {
2727
}
2828
}
2929

30-
func isEmptyOrNonExistentDirectory(path string) (bool, error) {
31-
f, err := os.Open(path)
30+
func isAccessibleDirectory(path string) (bool, error) {
31+
_, err := os.Stat(path)
32+
3233
if err != nil {
3334
if os.IsNotExist(err) {
34-
return true, nil
35+
return false, nil
3536
}
3637
return false, errors.Wrapf(err, "Could not access directory %s.", path)
3738
}
38-
defer f.Close()
39+
40+
return true, nil
41+
}
3942

40-
_, err = f.Readdirnames(1)
43+
func isEmptyDirectory(path string) (bool, error) {
44+
files, err := ioutil.ReadDir(path)
4145
if err != nil {
42-
if err == io.EOF {
43-
return true, nil
44-
}
4546
return false, errors.Wrapf(err, "Could not read contents of directory %s.", path)
4647
}
47-
return false, nil
48+
49+
return len(files) == 0, nil
50+
}
51+
52+
func existsDirectory(path string) (bool, error) {
53+
_, err := os.Stat(path)
54+
if os.IsNotExist(err) {
55+
return false, nil
56+
}
57+
if err != nil {
58+
return false, errors.Wrapf(err, "Could not access directory %s.", path)
59+
}
60+
return true, nil
4861
}
4962

5063
func (cacheDirectory *CacheDirectory) CheckOrCreateVersionFile(pull bool, version string) error {
@@ -77,20 +90,37 @@ func (cacheDirectory *CacheDirectory) CheckOrCreateVersionFile(pull bool, versio
7790
}
7891
}
7992

80-
isEmptyOrNonExistent, err := isEmptyOrNonExistentDirectory(cacheDirectory.path)
93+
existsDirectory, err := existsDirectory(cacheDirectory.path)
8194
if err != nil {
8295
return err
8396
}
84-
if isEmptyOrNonExistent {
97+
if !existsDirectory {
8598
err := os.Mkdir(cacheDirectory.path, 0755)
8699
if err != nil {
87100
return errors.Wrap(err, "Could not create cache directory.")
88101
}
89-
err = ioutil.WriteFile(cacheVersionFilePath, []byte(version), 0644)
102+
103+
} else {
104+
isAccessible, err := isAccessibleDirectory(cacheDirectory.path)
105+
if err != nil {
106+
return err
107+
}
108+
if !isAccessible {
109+
return errors.Wrap(err, "Cache dir exists, but the current user can't write to it.")
110+
}
111+
112+
isEmpty, err := isEmptyDirectory(cacheDirectory.path)
90113
if err != nil {
91-
return errors.Wrap(err, "Could not create cache version file.")
114+
return err
115+
}
116+
if isEmpty {
117+
err = ioutil.WriteFile(cacheVersionFilePath, []byte(version), 0644)
118+
if err != nil {
119+
return errors.Wrap(err, "Could not create cache version file.")
120+
}
92121
}
93122
return nil
123+
94124
}
95125
return usererrors.New(errorNotACacheOrEmpty)
96126
}

0 commit comments

Comments
 (0)