Skip to content

Commit

Permalink
path filter (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
soerenkoehler committed Feb 7, 2024
1 parent a0519a7 commit 4a9d5f9
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
34 changes: 27 additions & 7 deletions digest/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"io"
"log"
"os"
"path"
"path/filepath"
"sync"
"time"

"github.com/soerenkoehler/go-chdiff/util"
)

type Calculator func(rootPath string, algorithm HashType) Digest
type Calculator func(
rootPath string,
exclude util.PathFilter,
algorithm HashType) Digest

type digestEntry struct {
file string
Expand All @@ -25,23 +27,35 @@ type digestEntry struct {

type digestContext struct {
rootPath string
exclude util.PathFilter
algorithm HashType
waitGroup *sync.WaitGroup
digest chan digestEntry
}

func Calculate(rootPath string, algorithm HashType) Digest {
func Calculate(
rootPath string,
exclude util.PathFilter,
algorithm HashType) Digest {

context := digestContext{
rootPath: rootPath,
exclude: exclude,
algorithm: algorithm,
waitGroup: &sync.WaitGroup{},
digest: make(chan digestEntry),
}

go func() {
context.processPath(context.rootPath)
defer close(context.digest)

absPath, err := filepath.Abs(context.rootPath)
if err == nil {
return
}

context.processPath(absPath)
context.waitGroup.Wait()
close(context.digest)
}()

result := NewDigest(rootPath, time.Now())
Expand All @@ -55,6 +69,12 @@ func Calculate(rootPath string, algorithm HashType) Digest {
func (context digestContext) processPath(path string) {
context.waitGroup.Add(1)
go func() {
defer context.waitGroup.Done()

if context.exclude.Matches(path) {
return
}

switch info := util.Stat(path); {
case info.IsSymlink:
log.Printf("[W] skipping symlink: %s => %s", path, info.Target)
Expand All @@ -63,7 +83,7 @@ func (context digestContext) processPath(path string) {
default:
context.processFile(path)
}
context.waitGroup.Done()

}()
}

Expand All @@ -75,7 +95,7 @@ func (context digestContext) processDir(dir string) {
}

for _, entry := range entries {
context.processPath(path.Join(dir, entry.Name()))
context.processPath(filepath.Join(dir, entry.Name()))
}
}

Expand Down
9 changes: 5 additions & 4 deletions digest/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"io"
"math/rand"
"os"
"path"
"path/filepath"
"testing"

"github.com/soerenkoehler/go-chdiff/digest"
"github.com/soerenkoehler/go-chdiff/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -73,7 +74,7 @@ func (s *TestSuiteCalculator) verifyDigest(
testdata []testCase,
algorithm digest.HashType) {

digest := digest.Calculate(createData(s, testdata), algorithm)
digest := digest.Calculate(createData(s, testdata), util.PathFilter{}, algorithm)

require.Equal(s.T(), len(testdata), len(*digest.Entries))

Expand All @@ -89,15 +90,15 @@ func createData(
root := s.T().TempDir()

for _, dataPoint := range testdata {
file := path.Join(root, dataPoint.path)
file := filepath.Join(root, dataPoint.path)
createRandomFile(file, dataPoint.size, dataPoint.seed)
}

return root
}

func createRandomFile(file string, size, seed int64) {
err := os.MkdirAll(path.Dir(file), 0700)
err := os.MkdirAll(filepath.Dir(file), 0700)
if err != nil {
panic(err)
}
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/soerenkoehler/go-chdiff/chdiff"
"github.com/soerenkoehler/go-chdiff/diff"
"github.com/soerenkoehler/go-chdiff/digest"
"github.com/soerenkoehler/go-chdiff/util"
)

var _Version = "DEV"
Expand All @@ -23,7 +24,7 @@ func (*DefaultDependencies) DigestWrite(d digest.Digest, df string) error {
}

func (*DefaultDependencies) DigestCalculate(rp string, ht digest.HashType) digest.Digest {
return digest.Calculate(rp, ht)
return digest.Calculate(rp, util.PathFilter{}, ht)
}

func (*DefaultDependencies) DigestCompare(old, new digest.Digest) diff.Diff {
Expand Down
21 changes: 21 additions & 0 deletions util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,15 @@ import (
"log"
"os"
"path/filepath"
"strings"
)

// PathFilter defines patterns to match against paths
type PathFilter struct {
Prefix []string
Suffix []string
}

// PathInfo distilles information from FileInfo and Readlink
type PathInfo struct {
IsDir bool
Expand All @@ -31,3 +38,17 @@ func Stat(path string) PathInfo {
IsSymlink: false,
Target: path}
}

func (filter *PathFilter) Matches(path string) bool {
for _, pattern := range filter.Prefix {
if strings.HasPrefix(path, pattern) {
return true
}
}
for _, pattern := range filter.Suffix {
if strings.HasSuffix(path, pattern) {
return true
}
}
return false
}

0 comments on commit 4a9d5f9

Please sign in to comment.