File: package_hashes.go

package info (click to toggle)
golang-ginkgo 1.2.0%2Bgit20161006.acfa16a-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 1,324 kB
  • ctags: 1,210
  • sloc: makefile: 12
file content (82 lines) | stat: -rw-r--r-- 1,486 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package watch

import (
	"path/filepath"
	"sync"
)

type PackageHashes struct {
	PackageHashes map[string]*PackageHash
	usedPaths     map[string]bool
	lock          *sync.Mutex
}

func NewPackageHashes() *PackageHashes {
	return &PackageHashes{
		PackageHashes: map[string]*PackageHash{},
		usedPaths:     nil,
		lock:          &sync.Mutex{},
	}
}

func (p *PackageHashes) CheckForChanges() []string {
	p.lock.Lock()
	defer p.lock.Unlock()

	modified := []string{}

	for _, packageHash := range p.PackageHashes {
		if packageHash.CheckForChanges() {
			modified = append(modified, packageHash.path)
		}
	}

	return modified
}

func (p *PackageHashes) Add(path string) *PackageHash {
	p.lock.Lock()
	defer p.lock.Unlock()

	path, _ = filepath.Abs(path)
	_, ok := p.PackageHashes[path]
	if !ok {
		p.PackageHashes[path] = NewPackageHash(path)
	}

	if p.usedPaths != nil {
		p.usedPaths[path] = true
	}
	return p.PackageHashes[path]
}

func (p *PackageHashes) Get(path string) *PackageHash {
	p.lock.Lock()
	defer p.lock.Unlock()

	path, _ = filepath.Abs(path)
	if p.usedPaths != nil {
		p.usedPaths[path] = true
	}
	return p.PackageHashes[path]
}

func (p *PackageHashes) StartTrackingUsage() {
	p.lock.Lock()
	defer p.lock.Unlock()

	p.usedPaths = map[string]bool{}
}

func (p *PackageHashes) StopTrackingUsageAndPrune() {
	p.lock.Lock()
	defer p.lock.Unlock()

	for path := range p.PackageHashes {
		if !p.usedPaths[path] {
			delete(p.PackageHashes, path)
		}
	}

	p.usedPaths = nil
}