Skip to content

Commit

Permalink
Make hasher work with symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc-André Aßbrock committed Oct 1, 2019
1 parent 35887d9 commit 664edb1
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions pkg/launcher/hashing/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
"github.com/setlog/trivrost/pkg/launcher/config"
log "github.com/sirupsen/logrus"
)

func fopen(filePath string) (io.ReadCloser, error) {
Expand Down Expand Up @@ -61,10 +61,10 @@ func mustHashRelatively(readDir readDirFunc, readFile readFileFunc, stat statFun
func mustHashDir(readDir readDirFunc, readFile readFileFunc, stat statFunc, hashFilePath string) config.FileInfoMap {
fm := make(config.FileInfoMap)
for _, info := range mustReadDir(readDir, hashFilePath) {
if info.IsDir() {
fm.Join(mustHashDir(readDir, readFile, stat, filepath.Join(hashFilePath, info.Name())))
filePath := filepath.Join(hashFilePath, info.Name())
if isDir(filePath) {
fm.Join(mustHashDir(readDir, readFile, stat, filePath))
} else {
filePath := filepath.Join(hashFilePath, info.Name())
sha, size, err := calculateSha256(filePath, readFile)
if err != nil {
panic(fmt.Sprintf("failed hashing file \"%s\": %v", hashFilePath, err))
Expand All @@ -75,8 +75,24 @@ func mustHashDir(readDir readDirFunc, readFile readFileFunc, stat statFunc, hash
return fm
}

func evaluateSoftLink(filePath string) string {
evaluatedName, err := filepath.EvalSymlinks(filePath)
if err != nil {
panic(err)
}
return evaluatedName
}

func isDir(filePath string) bool {
fi, err := os.Stat(filePath)
if err != nil {
panic(err)
}
return fi.IsDir()
}

func mustReadDir(readDir readDirFunc, directoryPath string) []os.FileInfo {
infos, err := readDir(directoryPath)
infos, err := readDir(evaluateSoftLink(directoryPath))
if err != nil {
if os.IsNotExist(err) {
return nil
Expand Down

0 comments on commit 664edb1

Please sign in to comment.