1
0
mirror of https://github.com/go-task/task.git synced 2025-02-15 14:03:30 +02:00

checksum: normalize filename

This commit is contained in:
Andrey Nering 2017-09-30 14:40:11 -03:00
parent c16f8a4d46
commit 14676dc3f8

View File

@ -7,6 +7,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
)
@ -20,7 +21,7 @@ type Checksum struct {
// IsUpToDate implements the Checker interface
func (c *Checksum) IsUpToDate() (bool, error) {
checksumFile := filepath.Join(c.Dir, ".task", c.Task)
checksumFile := filepath.Join(c.Dir, ".task", c.normalizeFilename(c.Task))
data, _ := ioutil.ReadFile(checksumFile)
oldMd5 := strings.TrimSpace(string(data))
@ -36,7 +37,7 @@ func (c *Checksum) IsUpToDate() (bool, error) {
}
_ = os.MkdirAll(filepath.Join(c.Dir, ".task"), 0755)
if err = ioutil.WriteFile(checksumFile, []byte(newMd5), 0644); err != nil {
if err = ioutil.WriteFile(checksumFile, []byte(newMd5+"\n"), 0644); err != nil {
return false, err
}
return oldMd5 == newMd5, nil
@ -65,6 +66,13 @@ func (c *Checksum) checksum(files ...string) (string, error) {
return fmt.Sprintf("%x", h.Sum(nil)), nil
}
var checksumFilenameRegexp = regexp.MustCompile("[^A-z0-9]")
// replaces invalid caracters on filenames with "-"
func (*Checksum) normalizeFilename(f string) string {
return checksumFilenameRegexp.ReplaceAllString(f, "-")
}
// OnError implements the Checker interface
func (c *Checksum) OnError() error {
return os.Remove(filepath.Join(c.Dir, ".task", c.Task))