Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 39 additions & 12 deletions internal/cachedirectory/cachedirectory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cachedirectory

import (
usererrors "errors"
"io"
"io/ioutil"
"os"
"path"
Expand All @@ -27,24 +26,37 @@ func NewCacheDirectory(path string) CacheDirectory {
}
}

func isEmptyOrNonExistentDirectory(path string) (bool, error) {
f, err := os.Open(path)
func isAccessibleDirectory(path string) (bool, error) {
_, err := os.Stat(path)

if err != nil {
if os.IsNotExist(err) {
return true, nil
return false, nil
}
return false, errors.Wrapf(err, "Could not access directory %s.", path)
}
defer f.Close()

_, err = f.Readdirnames(1)
return true, nil
}

func isEmptyDirectory(path string) (bool, error) {
files, err := ioutil.ReadDir(path)
if err != nil {
if err == io.EOF {
return true, nil
}
return false, errors.Wrapf(err, "Could not read contents of directory %s.", path)
}
return false, nil

return len(files) == 0, nil
}

func existsDirectory(path string) (bool, error) {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, errors.Wrapf(err, "Could not access directory %s.", path)
}
return true, nil
}

func (cacheDirectory *CacheDirectory) CheckOrCreateVersionFile(pull bool, version string) error {
Expand Down Expand Up @@ -77,15 +89,30 @@ func (cacheDirectory *CacheDirectory) CheckOrCreateVersionFile(pull bool, versio
}
}

isEmptyOrNonExistent, err := isEmptyOrNonExistentDirectory(cacheDirectory.path)
existsDirectory, err := existsDirectory(cacheDirectory.path)
if err != nil {
return err
}
if isEmptyOrNonExistent {
if !existsDirectory {
err := os.Mkdir(cacheDirectory.path, 0755)
if err != nil {
return errors.Wrap(err, "Could not create cache directory.")
}

} else {
isAccessible, err := isAccessibleDirectory(cacheDirectory.path)
if err != nil {
return err
}
if !isAccessible {
return errors.Wrap(err, "Cache dir exists, but the current user can't write to it.")
}
}
isEmpty, err := isEmptyDirectory(cacheDirectory.path)
if err != nil {
return err
}
if isEmpty {
err = ioutil.WriteFile(cacheVersionFilePath, []byte(version), 0644)
if err != nil {
return errors.Wrap(err, "Could not create cache version file.")
Expand Down
12 changes: 12 additions & 0 deletions internal/cachedirectory/cachedirectory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ func TestCreateCacheDirectoryWithTrailingSlash(t *testing.T) {
require.NoError(t, err)
}

func TestUseProvidedEmptyCacheDirectory(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
cacheDirectoryPath := path.Join(temporaryDirectory, "cache")
err := os.MkdirAll(cacheDirectoryPath, 0755)
require.NoError(t, err)
cacheDirectory := NewCacheDirectory(cacheDirectoryPath)
err = cacheDirectory.CheckOrCreateVersionFile(true, aVersion)
require.NoError(t, err)
cacheVersionFilePath := cacheDirectory.versionFilePath()
require.FileExists(t, cacheVersionFilePath)
}

func TestLocking(t *testing.T) {
temporaryDirectory := test.CreateTemporaryDirectory(t)
cacheDirectory := NewCacheDirectory(path.Join(temporaryDirectory, "cache"))
Expand Down
Loading