From 14676dc3f8e81196cd7aba6d190dd74633c372a5 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 30 Sep 2017 14:40:11 -0300 Subject: [PATCH] checksum: normalize filename --- status/checksum.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/status/checksum.go b/status/checksum.go index 68623529..477f75e5 100644 --- a/status/checksum.go +++ b/status/checksum.go @@ -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))