* Remove specialized handler and archive struct and restructure handlers pkg. * Refactor RPM archive handlers to use a library instead of shelling out * make rpm handling context aware * update test * Refactor AR/deb archive handler to use an existing library instead of shelling out * Update tests * Handle non-archive data within the DefaultHandler * make structs and methods private * Remove non-archive data handling within sources * add max size check * add filename and size to context kvp * move skip file check and is binary check before opening file * fix test * preserve existing funcitonality of not handling non-archive files in HandleFile * Handle non-archive data within the DefaultHandler * rebase * Remove non-archive data handling within sources * Adjust check for rpm/deb archive type * add additional deb mime type * add gzip * move diskbuffered rereader setup into handler pkg * remove DiskBuffereReader creation logic within sources * update comment * move rewind closer * reduce log verbosity * add metrics for file handling * add metrics for errors * make defaultBufferSize a const * add metrics for file handling * add metrics for errors * fix tests * add metrics for max archive depth and skipped files * update error * skip symlinks and dirs * update err * Address incompatible reader to openArchive * remove nil check * fix err assignment * Allow git cat-file blob to complete before trying to handle the file * wrap compReader with DiskbufferReader * Allow git cat-file blob to complete before trying to handle the file * updates * use buffer writer * update * refactor * update context pkg * revert stuff * update test * fix test * remove * use correct reader * add metrics for file handling * add metrics for errors * fix tests * rebase * add metrics for errors * add metrics for max archive depth and skipped files * update error * skip symlinks and dirs * update err * fix err assignment * rebase * remove * Update write method in contentWriter interface * Add bufferReadSeekCloser * update name * update comment * fix lint * Remove specialized handler and archive struct and restructure handlers pkg. * Refactor RPM archive handlers to use a library instead of shelling out * make rpm handling context aware * update test * Refactor AR/deb archive handler to use an existing library instead of shelling out * Update tests * add max size check * add filename and size to context kvp * move skip file check and is binary check before opening file * fix test * preserve existing funcitonality of not handling non-archive files in HandleFile * Handle non-archive data within the DefaultHandler * rebase * Remove non-archive data handling within sources * Handle non-archive data within the DefaultHandler * add gzip * move diskbuffered rereader setup into handler pkg * remove DiskBuffereReader creation logic within sources * update comment * move rewind closer * reduce log verbosity * make defaultBufferSize a const * add metrics for file handling * add metrics for errors * fix tests * add metrics for max archive depth and skipped files * update error * skip symlinks and dirs * update err * Address incompatible reader to openArchive * remove nil check * fix err assignment * wrap compReader with DiskbufferReader * Allow git cat-file blob to complete before trying to handle the file * updates * use buffer writer * update * refactor * update context pkg * revert stuff * update test * remove * rebase * go mod tidy * lint check * update metric to ms * update metric * update comments * dont use ptr * update * fix * Remove specialized handler and archive struct and restructure handlers pkg. * Refactor RPM archive handlers to use a library instead of shelling out * make rpm handling context aware * update test * Refactor AR/deb archive handler to use an existing library instead of shelling out * Update tests * add max size check * add filename and size to context kvp * move skip file check and is binary check before opening file * fix test * preserve existing funcitonality of not handling non-archive files in HandleFile * Adjust check for rpm/deb archive type * add additional deb mime type * update comment * go mod tidy * update go mod * Add a buffered file reader * update comments * use Buffered File Readder * return buffer * update * fix * return * go mod tidy * merge * use a shared pool * use sync.Once * reorganzie * remove unused code * fix double init * fix stuff * nil check * reduce allocations * updates * update metrics * updates * reset buffer instead of putting it back * skip binaries * skip * concurrently process diffs * close chan * concurrently enumerate orgs * increase workers * ignore pbix and vsdx files * add metrics for gitparse's Diffchan * fix metric * update metrics * update * fix checks * fix * inc * update * reduce * Create workers to handle binary files * modify workers * updates * add check * delete code * use custom reader * rename struct * add nonarchive handler * fix break * add comments * add tests * refactor * remove log * do not scan rpm links * simplify * rename var * rename * fix benchmark * add buffer * buffer * buffer * handle panic * merge main * merge main * add recover * revert stuff * revert * revert to using reader * fixes * remove * update * fixes * linter * fix test * move buffers pkg out of writers pkg * rename * [refactor] - move buffer pool logic into own pkg (#2828) * move buffer pool logic into own pkg * fix test * fix test * whoops * [feat] - additional buffer pool (#2829) * move buffer pool logic into own pkg * move * fix test * fix test * fix test * remove * fix test * whoops * revert * fix
104 lines
3.0 KiB
Go
104 lines
3.0 KiB
Go
// Package bufferwriter provides a contentWriter implementation using a shared buffer pool for memory management.
|
|
package bufferwriter
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/buffers/buffer"
|
|
"github.com/trufflesecurity/trufflehog/v3/pkg/buffers/pool"
|
|
)
|
|
|
|
type metrics struct{}
|
|
|
|
func (metrics) recordDataProcessed(size int64, dur time.Duration) {
|
|
writeSize.Observe(float64(size))
|
|
totalWriteDuration.Add(float64(dur.Microseconds()))
|
|
}
|
|
|
|
const defaultBufferSize = 1 << 12 // 4KB
|
|
func init() { bufferPool = pool.NewBufferPool(defaultBufferSize) }
|
|
|
|
// bufferPool is the shared Buffer pool used by all BufferedFileWriters.
|
|
// This allows for efficient reuse of buffers across multiple writers.
|
|
var bufferPool *pool.Pool
|
|
|
|
// state represents the current mode of buffer.
|
|
type state uint8
|
|
|
|
const (
|
|
// writeOnly indicates the buffer is in write-only mode.
|
|
writeOnly state = iota
|
|
// readOnly indicates the buffer has been closed and is in read-only mode.
|
|
readOnly
|
|
)
|
|
|
|
// BufferWriter implements contentWriter, using a shared buffer pool for memory management.
|
|
type BufferWriter struct {
|
|
buf *buffer.Buffer // The current buffer in use.
|
|
bufPool *pool.Pool // The buffer pool used to manage the buffer.
|
|
size int // The total size of the content written to the buffer.
|
|
state state // The current state of the buffer.
|
|
|
|
metrics metrics
|
|
}
|
|
|
|
// New creates a new instance of BufferWriter.
|
|
func New() *BufferWriter {
|
|
return &BufferWriter{state: writeOnly, bufPool: bufferPool}
|
|
}
|
|
|
|
// Write delegates the writing operation to the underlying bytes.Buffer.
|
|
func (b *BufferWriter) Write(data []byte) (int, error) {
|
|
if b.state != writeOnly {
|
|
return 0, fmt.Errorf("buffer must be in write-only mode to write data; current state: %d", b.state)
|
|
}
|
|
if b.buf == nil {
|
|
b.buf = b.bufPool.Get()
|
|
if b.buf == nil {
|
|
b.buf = buffer.NewBuffer()
|
|
}
|
|
}
|
|
|
|
size := len(data)
|
|
b.size += size
|
|
start := time.Now()
|
|
defer func(start time.Time) {
|
|
b.metrics.recordDataProcessed(int64(size), time.Since(start))
|
|
}(start)
|
|
|
|
return b.buf.Write(data)
|
|
}
|
|
|
|
// ReadCloser provides a read-closer for the buffer's content.
|
|
// It wraps the buffer's content in a NopCloser to provide a ReadCloser without additional closing behavior,
|
|
// as closing a bytes.Buffer is a no-op.
|
|
func (b *BufferWriter) ReadCloser() (io.ReadCloser, error) {
|
|
if b.state != readOnly {
|
|
return nil, fmt.Errorf("buffer is in read-only mode")
|
|
}
|
|
if b.buf == nil {
|
|
return nil, fmt.Errorf("writer buffer is nil")
|
|
}
|
|
|
|
return buffer.ReadCloser(b.buf.Bytes(), func() { b.bufPool.Put(b.buf) }), nil
|
|
}
|
|
|
|
// CloseForWriting is a no-op for buffer, as there is no resource cleanup needed for bytes.Buffer.
|
|
func (b *BufferWriter) CloseForWriting() error {
|
|
b.state = readOnly
|
|
return nil
|
|
}
|
|
|
|
// String returns the buffer's content as a string.
|
|
func (b *BufferWriter) String() (string, error) {
|
|
if b.buf == nil {
|
|
return "", fmt.Errorf("buffer is nil")
|
|
}
|
|
return b.buf.String(), nil
|
|
}
|
|
|
|
// Len returns the length of the buffer's content.
|
|
func (b *BufferWriter) Len() int { return b.size }
|