remove unused code

This commit is contained in:
Ahrav Dutta
2024-04-23 19:01:30 -07:00
parent d85c966437
commit 683a077043
4 changed files with 4 additions and 135 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
bufferwriter "github.com/trufflesecurity/trufflehog/v3/pkg/writers/buffer_writer"
"github.com/trufflesecurity/trufflehog/v3/pkg/readers"
)
func TestHandleARFile(t *testing.T) {
@@ -19,7 +19,7 @@ func TestHandleARFile(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
rdr, err := bufferwriter.NewBufferReadSeekCloser(ctx, file)
rdr, err := readers.NewBufferedFileReader(ctx, file)
assert.NoError(t, err)
defer rdr.Close()
+2 -2
View File
@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/trufflesecurity/trufflehog/v3/pkg/context"
bufferwriter "github.com/trufflesecurity/trufflehog/v3/pkg/writers/buffer_writer"
"github.com/trufflesecurity/trufflehog/v3/pkg/readers"
)
func TestHandleRPMFile(t *testing.T) {
@@ -19,7 +19,7 @@ func TestHandleRPMFile(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
rdr, err := bufferwriter.NewBufferReadSeekCloser(ctx, file)
rdr, err := readers.NewBufferedFileReader(ctx, file)
assert.NoError(t, err)
defer rdr.Close()
-65
View File
@@ -2,7 +2,6 @@
package bufferwriter
import (
"bytes"
"fmt"
"io"
"time"
@@ -105,67 +104,3 @@ func (b *BufferWriter) String() (string, error) {
// Len returns the length of the buffer's content.
func (b *BufferWriter) Len() int { return b.size }
// bufferReadSeekCloser provides random access read, seek, and close capabilities on top of the BufferWriter.
// It combines the functionality of BufferWriter for buffered writing, bytes.Reader for
// random access reading and seeking, and adds a close method to return the buffer to the pool.
type bufferReadSeekCloser struct {
bufWriter *BufferWriter
reader *bytes.Reader
}
// NewBufferReadSeekCloser initializes a bufferReadSeekCloser from an io.Reader by using
// the BufferWriter's functionality to read and store data, then setting up a bytes.Reader for random access.
// It returns the initialized bufferReadSeekCloser and any error encountered during the process.
func NewBufferReadSeekCloser(ctx context.Context, r io.Reader) (*bufferReadSeekCloser, error) {
rdr, err := NewFromReader(ctx, r)
if err != nil {
return nil, fmt.Errorf("error creating bufferReadSeekCloser: %w", err)
}
// Ensure that the BufferWriter is not in write mode anymore.
if err := rdr.CloseForWriting(); err != nil {
return nil, err
}
return &bufferReadSeekCloser{rdr, bytes.NewReader(rdr.buf.Bytes())}, nil
}
// Close releases the buffer back to the buffer pool.
// It should be called when the bufferReadSeekCloser is no longer needed.
// Note that closing the bufferReadSeekCloser does not affect the underlying bytes.Reader,
// which can still be used for reading, seeking, and reading at specific positions.
// Close is a no-op for the bytes.Reader.
func (b *bufferReadSeekCloser) Close() error {
b.bufWriter.bufPool.Put(b.bufWriter.buf)
return nil
}
// Read reads up to len(p) bytes into p from the underlying bytes.Reader.
// It returns the number of bytes read and any error encountered.
// If the bytes.Reader reaches the end of the available data, Read returns 0, io.EOF.
// It implements the io.Reader interface.
func (b *bufferReadSeekCloser) Read(p []byte) (int, error) {
return b.reader.Read(p)
}
// Seek sets the offset for the next Read or Write operation on the underlying bytes.Reader.
// The offset is interpreted according to the whence parameter:
// - io.SeekStart means relative to the start of the file
// - io.SeekCurrent means relative to the current offset
// - io.SeekEnd means relative to the end of the file
//
// Seek returns the new offset and any error encountered.
// It implements the io.Seeker interface.
func (b *bufferReadSeekCloser) Seek(offset int64, whence int) (int64, error) {
return b.reader.Seek(offset, whence)
}
// ReadAt reads len(p) bytes from the underlying bytes.Reader starting at byte offset off.
// It returns the number of bytes read and any error encountered.
// If the bytes.Reader reaches the end of the available data before len(p) bytes are read,
// ReadAt returns the number of bytes read and io.EOF.
// It implements the io.ReaderAt interface.
func (b *bufferReadSeekCloser) ReadAt(p []byte, off int64) (n int, err error) {
return b.reader.ReadAt(p, off)
}
@@ -217,69 +217,3 @@ func TestNewFromReader(t *testing.T) {
})
}
}
func TestBufferReadSeekCloser(t *testing.T) {
t.Parallel()
data := []byte("Hello, World!")
bufferReadSeekCloser, err := NewBufferReadSeekCloser(context.Background(), bytes.NewReader(data))
assert.NoError(t, err)
defer bufferReadSeekCloser.Close()
// Test Read.
buffer := make([]byte, len(data))
n, err := bufferReadSeekCloser.Read(buffer)
assert.NoError(t, err)
assert.Equal(t, len(data), n)
assert.Equal(t, data, buffer)
// Test Seek.
offset := 7
seekPos, err := bufferReadSeekCloser.Seek(int64(offset), io.SeekStart)
assert.NoError(t, err)
assert.Equal(t, int64(offset), seekPos)
// Test ReadAt.
buffer = make([]byte, len(data)-offset)
n, err = bufferReadSeekCloser.ReadAt(buffer, int64(offset))
assert.NoError(t, err)
assert.Equal(t, len(data)-offset, n)
assert.Equal(t, data[offset:], buffer)
// Test Close.
err = bufferReadSeekCloser.Close()
assert.NoError(t, err)
}
func TestBufferReadSeekCloserClose(t *testing.T) {
t.Parallel()
data := []byte("Hello, World!")
bufferReadSeekCloser, err := NewBufferReadSeekCloser(context.Background(), bytes.NewReader(data))
assert.NoError(t, err)
err = bufferReadSeekCloser.Close()
assert.NoError(t, err)
// Read after closing.
buffer := make([]byte, len(data))
n, err := bufferReadSeekCloser.Read(buffer)
assert.NoError(t, err)
assert.Equal(t, len(data), n)
assert.Equal(t, data, buffer)
// Seek after closing.
offset := 7
seekPos, err := bufferReadSeekCloser.Seek(int64(offset), io.SeekStart)
assert.NoError(t, err)
assert.Equal(t, int64(offset), seekPos)
// ReadAt after closing.
buffer = make([]byte, len(data)-offset)
n, err = bufferReadSeekCloser.ReadAt(buffer, int64(offset))
assert.NoError(t, err)
assert.Equal(t, len(data)-offset, n)
assert.Equal(t, data[offset:], buffer)
}