Change FileProgress callback to channel-based progress
Replace callback-based progress reporting in Builder.AddFile with channel-based FileHashProgress for consistency with EnumerateStatus and ScanStatus patterns. Update scanner.go to use the new channel API.
This commit is contained in:
@@ -9,8 +9,10 @@ import (
|
||||
"github.com/multiformats/go-multihash"
|
||||
)
|
||||
|
||||
// FileProgress is called during file processing to report bytes read.
|
||||
type FileProgress func(bytesRead int64)
|
||||
// FileHashProgress reports progress during file hashing.
|
||||
type FileHashProgress struct {
|
||||
BytesRead int64 // Total bytes read so far for the current file
|
||||
}
|
||||
|
||||
// Builder constructs a manifest by adding files one at a time.
|
||||
type Builder struct {
|
||||
@@ -28,14 +30,14 @@ func NewBuilder() *Builder {
|
||||
}
|
||||
|
||||
// AddFile reads file content from reader, computes hashes, and adds to manifest.
|
||||
// The progress callback is called periodically with total bytes read so far.
|
||||
// Progress updates are sent to the progress channel (if non-nil) without blocking.
|
||||
// Returns the number of bytes read.
|
||||
func (b *Builder) AddFile(
|
||||
path string,
|
||||
size int64,
|
||||
mtime time.Time,
|
||||
reader io.Reader,
|
||||
progress FileProgress,
|
||||
progress chan<- FileHashProgress,
|
||||
) (int64, error) {
|
||||
// Create hash writer
|
||||
h := sha256.New()
|
||||
@@ -49,9 +51,7 @@ func (b *Builder) AddFile(
|
||||
if n > 0 {
|
||||
h.Write(buf[:n])
|
||||
totalRead += int64(n)
|
||||
if progress != nil {
|
||||
progress(totalRead)
|
||||
}
|
||||
sendFileHashProgress(progress, FileHashProgress{BytesRead: totalRead})
|
||||
}
|
||||
if err == io.EOF {
|
||||
break
|
||||
@@ -84,6 +84,17 @@ func (b *Builder) AddFile(
|
||||
return totalRead, nil
|
||||
}
|
||||
|
||||
// sendFileHashProgress sends a progress update without blocking.
|
||||
func sendFileHashProgress(ch chan<- FileHashProgress, p FileHashProgress) {
|
||||
if ch == nil {
|
||||
return
|
||||
}
|
||||
select {
|
||||
case ch <- p:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// FileCount returns the number of files added to the builder.
|
||||
func (b *Builder) FileCount() int {
|
||||
b.mu.Lock()
|
||||
@@ -91,6 +102,23 @@ func (b *Builder) FileCount() int {
|
||||
return len(b.files)
|
||||
}
|
||||
|
||||
// AddFileWithHash adds a file entry with a pre-computed hash.
|
||||
// This is useful when the hash is already known (e.g., from an existing manifest).
|
||||
func (b *Builder) AddFileWithHash(path string, size int64, mtime time.Time, hash []byte) {
|
||||
entry := &MFFilePath{
|
||||
Path: path,
|
||||
Size: size,
|
||||
Hashes: []*MFFileChecksum{
|
||||
{MultiHash: hash},
|
||||
},
|
||||
Mtime: newTimestampFromTime(mtime),
|
||||
}
|
||||
|
||||
b.mu.Lock()
|
||||
b.files = append(b.files, entry)
|
||||
b.mu.Unlock()
|
||||
}
|
||||
|
||||
// Build finalizes the manifest and writes it to the writer.
|
||||
func (b *Builder) Build(w io.Writer) error {
|
||||
b.mu.Lock()
|
||||
|
||||
Reference in New Issue
Block a user