filehash random read
Some checks failed
docker / build-docker (push) Failing after 1m22s

This commit is contained in:
royalcat 2024-12-10 00:29:59 +03:00
parent 2d9dcd87fa
commit 62a25d3dd7

View file

@ -5,7 +5,6 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"io"
) )
const chunkSize int64 = 64 * 1024 const chunkSize int64 = 64 * 1024
@ -18,11 +17,6 @@ func FileHash(ctx context.Context, f File) (Hash, error) {
ctx, span := tracer.Start(ctx, "FileHash") ctx, span := tracer.Start(ctx, "FileHash")
defer span.End() 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() fileSize := f.Size()
if fileSize <= 8 { if fileSize <= 8 {
return "", fmt.Errorf("cannot calculate oshash where size < 8 (%d)", fileSize) 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) head := make([]byte, fileChunkSize)
tail := make([]byte, fileChunkSize) tail := make([]byte, fileChunkSize)
// read the head of the file into the start of the buffer _, err := f.ReadAt(ctx, head, 0)
_, err = f.Read(ctx, head)
if err != nil { if err != nil {
return "", err return "", err
} }
// seek to the end of the file - the chunk size _, err = f.ReadAt(ctx, tail, fileSize-fileChunkSize)
_, err = f.Seek(-fileChunkSize, io.SeekEnd)
if err != nil {
return "", err
}
// read the tail of the file
_, err = f.Read(ctx, tail)
if err != nil { if err != nil {
return "", err return "", err
} }