operations: normalize paths in SUM files

Paths like "./dir/file", as from find, and "/dir/file" were not matched,
and on Windows backslash was not accepted as path separator, as written
by Windows tools.
This commit is contained in:
albertony committed 2026-11-03 13:49:33 +01:00
1 parent d34ecab549
commit 1775ffb04e
2 files changed
+28 -2

No files matched your search

+8
View File
@@ -8,6 +8,8 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"regexp"
"strings"
"sync"
@@ -611,6 +613,12 @@ func ParseSumFile(ctx context.Context, sumFile fs.Object, hashType hash.Type) (H
}
sum, algo, file := fields[1], fields[2], fields[3]
// Treat the path like a local path, where backslash is a separator
// on Windows, unless the escaping says it is a literal backslash
if !escaped {
file = filepath.ToSlash(file)
}
file = strings.TrimPrefix(path.Clean(file), "/")
if algo != "" && !strings.EqualFold(algo, hashType.String()) {
continue
}
+20 -2
View File
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"runtime"
"sort"
"strings"
"sync"
@@ -362,14 +363,31 @@ func TestParseSumFile(t *testing.T) {
_ = r.WriteObject(ctx, sumFile, strings.Join([]string{
`\1 dir\\file1`,
`\2 *a\nb\rc`,
`3 dir\file3`,
`\4 invalid\x`,
`\5 invalid\`,
}, "\n"), t1)
file = fstest.NewObject(ctx, t, r.Fremote, sumFile)
sums, err = operations.ParseSumFile(ctx, file, hash.SHA1)
require.NoError(t, err)
assert.Equal(t, operations.HashSums{`dir\file1`: "1", "a\nb\rc": "2", `dir\file3`: "3"}, sums)
assert.Equal(t, operations.HashSums{`dir\file1`: "1", "a\nb\rc": "2"}, sums)
// Paths are cleaned, and an unescaped backslash is a path separator on Windows
_ = r.WriteObject(ctx, sumFile, strings.Join([]string{
`1 ./file1`,
`2 /dir/file2`,
`3 dir//file3`,
`4 *dir\file4`,
`\5 dir\\file5`,
}, "\n"), t1)
file = fstest.NewObject(ctx, t, r.Fremote, sumFile)
sums, err = operations.ParseSumFile(ctx, file, hash.SHA1)
require.NoError(t, err)
want := operations.HashSums{"file1": "1", "dir/file2": "2", "dir/file3": "3", `dir\file4`: "4", `dir\file5`: "5"}
if runtime.GOOS == "windows" {
delete(want, `dir\file4`)
want["dir/file4"] = "4"
}
assert.Equal(t, want, sums)
}
func testCheckSum(t *testing.T, download bool) {