From 62a25d3dd754fc70af696136b6dba07016020f80 Mon Sep 17 00:00:00 2001 From: royalcat Date: Tue, 10 Dec 2024 00:29:59 +0300 Subject: [PATCH] filehash random read --- src/vfs/hash.go | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/vfs/hash.go b/src/vfs/hash.go index 092f373..ede9152 100644 --- a/src/vfs/hash.go +++ b/src/vfs/hash.go @@ -5,7 +5,6 @@ import ( "encoding/binary" "errors" "fmt" - "io" ) const chunkSize int64 = 64 * 1024 @@ -18,11 +17,6 @@ func FileHash(ctx context.Context, f File) (Hash, error) { ctx, span := tracer.Start(ctx, "FileHash") defer span.End() - _, err := f.Seek(0, io.SeekStart) - if err != nil { - return "", fmt.Errorf("error seeking file: %w", err) - } - defer f.Seek(0, io.SeekStart) fileSize := f.Size() if fileSize <= 8 { return "", fmt.Errorf("cannot calculate oshash where size < 8 (%d)", fileSize) @@ -37,20 +31,12 @@ func FileHash(ctx context.Context, f File) (Hash, error) { head := make([]byte, fileChunkSize) tail := make([]byte, fileChunkSize) - // read the head of the file into the start of the buffer - _, err = f.Read(ctx, head) + _, err := f.ReadAt(ctx, head, 0) if err != nil { return "", err } - // seek to the end of the file - the chunk size - _, err = f.Seek(-fileChunkSize, io.SeekEnd) - if err != nil { - return "", err - } - - // read the tail of the file - _, err = f.Read(ctx, tail) + _, err = f.ReadAt(ctx, tail, fileSize-fileChunkSize) if err != nil { return "", err }