forked from livekit/mageutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksummer.go
More file actions
128 lines (114 loc) · 2.87 KB
/
checksummer.go
File metadata and controls
128 lines (114 loc) · 2.87 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mageutil
import (
"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"sort"
"strings"
)
// A helper checksum library that generates a fast, non-portable checksum over a directory of files
// it's designed as a quick way to bypass
type Checksummer struct {
dir string
file string
checksum string
allExts bool
extMap map[string]bool
IgnoredPaths []string
}
func NewChecksummer(dir string, checksumfile string, exts ...string) *Checksummer {
c := &Checksummer{
dir: dir,
file: checksumfile,
extMap: make(map[string]bool),
}
if len(exts) == 0 {
c.allExts = true
} else {
for _, ext := range exts {
c.extMap[ext] = true
}
}
return c
}
func (c *Checksummer) IsChanged() bool {
// default changed
if err := c.computeChecksum(); err != nil {
log.Println("could not compute checksum", err)
return true
}
// read
existing, err := c.ReadChecksum()
if err != nil {
// may not be there
return true
}
return existing != c.checksum
}
func (c *Checksummer) ReadChecksum() (string, error) {
b, err := ioutil.ReadFile(filepath.Join(c.dir, c.file))
if err != nil {
return "", err
}
return string(b), nil
}
func (c *Checksummer) WriteChecksum() error {
if err := c.computeChecksum(); err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(c.dir, c.file), []byte(c.checksum), 0644)
}
func (c *Checksummer) computeChecksum() error {
if c.checksum != "" {
return nil
}
entries := make([]string, 0)
ignoredMap := make(map[string]bool)
for _, f := range c.IgnoredPaths {
ignoredMap[f] = true
}
err := filepath.Walk(c.dir, func(path string, info os.FileInfo, err error) error {
if path == c.dir {
return nil
}
if strings.HasPrefix(info.Name(), ".") || ignoredMap[path] {
if info.IsDir() {
return filepath.SkipDir
} else {
return nil
}
}
if info.IsDir() {
entries = append(entries, fmt.Sprintf("%s %d", path, info.ModTime().Unix()))
} else if c.allExts || c.extMap[filepath.Ext(info.Name())] {
entries = append(entries, fmt.Sprintf("%s %d %d", path, info.Size(), info.ModTime().Unix()))
}
return nil
})
if err != nil {
return err
}
sort.Strings(entries)
h := sha1.New()
for _, e := range entries {
h.Write([]byte(e))
}
c.checksum = fmt.Sprintf("%x", h.Sum(nil))
return nil
}