feat: Add function that writes to a file without expanding the buffer and closes the file.

This commit is contained in:
Elim Tsiagbey committed 2024-01-11 09:58:32 -05:00
1 parent 366128f079
commit cae97fcd2e
12 files changed
+253 -34

No files matched your search

+39 -1
View File
@@ -29,6 +29,12 @@ type writerFile interface {
fs.File
io.WriteCloser
io.StringWriter
WriteAndClose([]byte) (int, error)
}
type underlyingFile interface {
fs.File
WriteAndClose([]byte) (int, error)
}
// File is a writable file that will compute its checksum while it is written
@@ -45,7 +51,7 @@ type File struct {
checksumWriter io.Writer
underlying fs.File
underlying underlyingFile
}
// NewFile wraps the provided writerFile in a checksumed File. When the file is
@@ -113,4 +119,36 @@ func (f *File) Close() error {
return closeErrors
}
// WriteAndClose writes to the underlying file, closes the Sha256SumWriter
// and computes the checksum and writes it to f.checksumWriter
func (f *File) WriteAndClose(b []byte) (int, error) {
const op = "checksum.(File).WriteAndClose"
var closeErrors error
// Call stat before closure; calling it after results in an err
s, err := f.Stat()
if err != nil {
closeErrors = errors.Join(closeErrors, fmt.Errorf("%s: %w", op, err))
return 0, closeErrors
}
n, err := f.underlying.WriteAndClose(b)
if err != nil {
return 0, fmt.Errorf("%s: %w", op, err)
}
sum, err := f.Sha256SumWriter.Sum(f.ctx, crypto.WithHexEncoding(true))
if err != nil {
closeErrors = errors.Join(closeErrors, fmt.Errorf("%s: %w", op, err))
return 0, closeErrors
}
if _, err := f.checksumWriter.Write([]byte(fmt.Sprintf(checksumLine, sum, s.Name()))); err != nil {
closeErrors = errors.Join(closeErrors, fmt.Errorf("%s: %w", op, err))
}
return n, closeErrors
}
var _ writerFile = (*File)(nil)
+64 -8
View File
@@ -268,6 +268,11 @@ type MemFile struct {
syncMode storage.SyncMode
accessMode storage.AccessMode
bufferSize uint64
minimumBufferSize uint64
bufferOffset int64
currentOffset int64
sync.RWMutex
}
@@ -279,14 +284,18 @@ func NewMemFile(n string, mode sfs.FileMode, options ...Option) *MemFile {
storageOpts := storage.GetOpts(opts.withStorageOptions...)
return &MemFile{
name: n,
Buf: bytes.NewBuffer([]byte{}),
src: []byte{},
mode: mode,
accessMode: storageOpts.WithFileAccessMode,
syncMode: storageOpts.WithCloseSyncMode,
statFunc: opts.withStatFunc,
closeFunc: opts.withCloseFunc,
name: n,
Buf: bytes.NewBuffer([]byte{}),
src: []byte{},
mode: mode,
accessMode: storageOpts.WithFileAccessMode,
syncMode: storageOpts.WithCloseSyncMode,
statFunc: opts.withStatFunc,
closeFunc: opts.withCloseFunc,
bufferSize: storageOpts.WithBuffer,
minimumBufferSize: storageOpts.WithMinimumBuffer,
currentOffset: 0,
bufferOffset: 0,
}
}
@@ -347,6 +356,10 @@ func (m *MemFile) Close() error {
m.Lock()
defer m.Unlock()
return m.close()
}
func (m *MemFile) close() error {
if m.Closed {
return fmt.Errorf("close on closed file")
}
@@ -367,6 +380,10 @@ func (m *MemFile) Write(p []byte) (n int, err error) {
m.Lock()
defer m.Unlock()
return m.write(p)
}
func (m *MemFile) write(p []byte) (n int, err error) {
if m.Closed {
return 0, fmt.Errorf("write on closed file")
}
@@ -385,6 +402,24 @@ func (m *MemFile) Write(p []byte) (n int, err error) {
return m.Buf.Write(p)
}
// WriteAndClose writes and closes the file.
func (m *MemFile) WriteAndClose(p []byte) (int, error) {
m.Lock()
defer m.Unlock()
n, err := m.write(p)
if err != nil {
return n, fmt.Errorf("write failed: %w", err)
}
err = m.close()
if err != nil {
return n, fmt.Errorf("close failed: %w", err)
}
return n, nil
}
// TempFile implements storage.TempFile
type TempFile struct {
*os.File
@@ -412,3 +447,24 @@ func (t *TempFile) Close() error {
}
return os.Remove(fname.Name())
}
func (t *TempFile) WriteAndClose(b []byte) (int, error) {
panic("not implemented")
}
type TempBuffer struct {
bytes.Buffer
}
// NewTempBuffer creates a TempBuffer.
func NewTempBuffer() (*TempBuffer, error) {
var testBuffer bytes.Buffer
return &TempBuffer{
testBuffer,
}, nil
}
// WriteAndClose writes and closes the file.
func (t *TempBuffer) WriteAndClose(b []byte) (int, error) {
return t.Write(b)
}
+25
View File
@@ -628,3 +628,28 @@ func TestOutOfSpace(t *testing.T) {
require.ErrorIs(t, err, fstest.ErrOutOfSpace)
assert.Nil(t, r2)
}
func TestMemFile_WriteAndClose(t *testing.T) {
mf := fstest.NewMemFile(
"test-write-and-close-file",
0o644,
fstest.WithStorageOptions([]storage.Option{
storage.WithFileAccessMode(storage.ReadWrite),
}))
require.False(t, mf.Closed, "MemFile is closed")
str1 := "Input 1"
n, err := mf.Write([]byte(str1))
require.NoError(t, err)
assert.NotNil(t, n)
str2 := "Input 2"
n, err = mf.WriteAndClose([]byte(str2))
require.NoError(t, err)
assert.NotNil(t, n)
expectedString := "Input 1Input 2"
assert.Equal(t, expectedString, mf.Buf.String())
require.True(t, mf.Closed, "MemFile is not closed")
}
+32 -2
View File
@@ -314,9 +314,15 @@ func (f *LocalFile) Read(b []byte) (int, error) {
// Close closes the file preventing reads or writes.
func (f *LocalFile) Close() error {
const op = "fstest.(LocalFile).Stat"
f.Lock()
defer f.Unlock()
return f.close()
}
func (f *LocalFile) close() error {
const op = "fstest.(LocalFile).close"
if f.closed {
return nil
}
@@ -330,9 +336,15 @@ func (f *LocalFile) Close() error {
}
func (f *LocalFile) Write(b []byte) (int, error) {
const op = "fstest.(localFile).Write"
f.Lock()
defer f.Unlock()
return f.write(b)
}
func (f *LocalFile) write(b []byte) (int, error) {
const op = "fstest.(localFile).write"
if f.closed {
return 0, fmt.Errorf("%s: file is closed", op)
}
@@ -352,3 +364,21 @@ func (f *LocalFile) WriteString(s string) (int, error) {
const op = "storage.(localFile).WriteString"
return f.Write([]byte(s))
}
// WriteAndClose writes and closes the file.
func (f *LocalFile) WriteAndClose(b []byte) (int, error) {
f.Lock()
defer f.Unlock()
n, err := f.write(b)
if err != nil {
return n, fmt.Errorf("write failed: %w", err)
}
err = f.close()
if err != nil {
return n, fmt.Errorf("close failed: %w", err)
}
return n, nil
}
+32
View File
@@ -20,6 +20,7 @@ type writerFile interface {
fs.File
io.WriteCloser
io.StringWriter
WriteAndClose([]byte) (int, error)
}
// Journal is used to record meta data about the operations that will be and
@@ -61,6 +62,11 @@ func (j *Journal) Record(op, f string) error {
return err
}
// WriteAndClose writes and closes the journal
func (j *Journal) WriteAndClose(b []byte) (int, error) {
panic("not implemented")
}
// File is a writable file that will update a Journal as it closed.
type File struct {
j *Journal
@@ -102,4 +108,30 @@ func (f *File) Close() error {
return f.j.Record("CLOSED", s.Name())
}
// WriteAndClose writes to the underlying file and closes the underlying file,
// writing to the journal prior to and after
func (f *File) WriteAndClose(b []byte) (int, error) {
const op = "journal.(File).Close"
s, err := f.Stat()
if err != nil {
return 0, fmt.Errorf("%s: %w", op, err)
}
if err := f.j.Record("CLOSING", s.Name()); err != nil {
return 0, fmt.Errorf("%s: %w", op, err)
}
n, err := f.writerFile.WriteAndClose(b)
if err != nil {
return n, fmt.Errorf("%s: %w", op, err)
}
if err := f.j.Record("CLOSED", s.Name()); err != nil {
return n, fmt.Errorf("%s: %w", op, err)
}
return n, err
}
var _ writerFile = (*File)(nil)