-
Notifications
You must be signed in to change notification settings - Fork 4
/
differ.go
80 lines (64 loc) · 1.8 KB
/
differ.go
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
package main
import (
"fmt"
"path/filepath"
"sync"
"sync/atomic"
)
// differ lists programs that are installed but need an update/differ from the repo's b3sum of it
func differ(config *Config, programs []string, verbosityLevel Verbosity, metadata map[string]interface{}) error {
var installedPrograms []string
if programs == nil {
var err error
programs, err = listFilesInDir(config.InstallDir)
if err != nil {
return fmt.Errorf("error listing files in %s: %v", config.InstallDir, err)
}
}
// Check which programs were actually installed by us
for _, file := range programs {
if fullBinaryName := listInstalled(file); fullBinaryName != "" {
installedPrograms = append(installedPrograms, fullBinaryName)
}
}
var (
checked uint32
differsFromRepo uint32
outputMutex sync.Mutex
wg sync.WaitGroup
)
// Check installed programs for differences
for _, program := range installedPrograms {
wg.Add(1)
go func(program string) {
defer wg.Done()
installPath := filepath.Join(config.InstallDir, filepath.Base(program))
if !fileExists(installPath) { // Skip if not installed
return
}
// Get local B3sum
localB3sum, err := calculateChecksum(installPath)
if err != nil { // skip
return
}
// Fetch remote metadata
binaryInfo, err := getBinaryInfo(config, program, metadata)
if binaryInfo.Bsum == "" { // skip
return
}
// Compare checksums
if localB3sum != binaryInfo.Bsum {
atomic.AddUint32(&differsFromRepo, 1)
outputMutex.Lock()
fmt.Println(program)
outputMutex.Unlock()
}
atomic.AddUint32(&checked, 1)
}(program)
}
wg.Wait()
if verbosityLevel > normalVerbosity {
fmt.Printf("Checked: %d, Needs Update: %d\n", atomic.LoadUint32(&checked), atomic.LoadUint32(&differsFromRepo))
}
return nil
}