backend: skip hash calculation when the hashType is None - fixes #8518

When hashType is None `local` backend still runs expensive logic that reads the entire file content to produce an empty string.
This commit is contained in:
Oleksiy Stashok 2025-04-21 22:44:23 -07:00 committed by Nick Craig-Wood
parent 0ee7cd80f2
commit badf16cc34
3 changed files with 25 additions and 0 deletions

View File

@ -1090,6 +1090,10 @@ func (o *Object) Remote() string {
// Hash returns the requested hash of a file as a lowercase hex string // Hash returns the requested hash of a file as a lowercase hex string
func (o *Object) Hash(ctx context.Context, r hash.Type) (string, error) { func (o *Object) Hash(ctx context.Context, r hash.Type) (string, error) {
if r == hash.None {
return "", nil
}
// Check that the underlying file hasn't changed // Check that the underlying file hasn't changed
o.fs.objectMetaMu.RLock() o.fs.objectMetaMu.RLock()
oldtime := o.modTime oldtime := o.modTime

View File

@ -204,6 +204,23 @@ func TestSymlinkError(t *testing.T) {
assert.Equal(t, errLinksAndCopyLinks, err) assert.Equal(t, errLinksAndCopyLinks, err)
} }
func TestHashWithTypeNone(t *testing.T) {
ctx := context.Background()
r := fstest.NewRun(t)
const filePath = "file.txt"
r.WriteFile(filePath, "content", time.Now())
f := r.Flocal.(*Fs)
// Get the object
o, err := f.NewObject(ctx, filePath)
require.NoError(t, err)
// Test the hash is as we expect
h, err := o.Hash(ctx, hash.None)
require.Empty(t, h)
require.NoError(t, err)
}
// Test hashes on updating an object // Test hashes on updating an object
func TestHashOnUpdate(t *testing.T) { func TestHashOnUpdate(t *testing.T) {
ctx := context.Background() ctx := context.Background()

View File

@ -45,6 +45,10 @@ func getFileHashByte(node any, hashType hash.Type) []byte {
} }
func getFileHash(node any, hashType hash.Type) string { func getFileHash(node any, hashType hash.Type) string {
if hashType == hash.None {
return ""
}
var o fs.Object var o fs.Object
switch b := node.(type) { switch b := node.(type) {