Commit 698633cd authored by siddharth0a's avatar siddharth0a Committed by GitHub

feat: ensure proper closing of gzip and file writers/readers to prevent resource leaks (#11475)

* add gzipCloser struct

* add Close method

* fix OpenDecompressed func

* fix CompressByFileType func

* knit

* Apply suggestions from code review

close both even if one fails
Co-authored-by: default avatarAdrian Sutton <adrian@symphonious.net>

* fix WriteCloser Close method

* fix name for more general

* fix writercloser name for more geneeral

* add construction function for WrappedCloser

* using construction func

* seperate wrapped closer struct to wrapped_closer.go

---------
Co-authored-by: default avatarAdrian Sutton <adrian@symphonious.net>
parent f243ad0d
...@@ -12,23 +12,23 @@ import ( ...@@ -12,23 +12,23 @@ import (
// OpenDecompressed opens a reader for the specified file and automatically gzip decompresses the content // OpenDecompressed opens a reader for the specified file and automatically gzip decompresses the content
// if the filename ends with .gz // if the filename ends with .gz
func OpenDecompressed(path string) (io.ReadCloser, error) { func OpenDecompressed(path string) (io.ReadCloser, error) {
var r io.ReadCloser
r, err := os.Open(path) r, err := os.Open(path)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if IsGzip(path) { if IsGzip(path) {
r, err = gzip.NewReader(r) gr, err := gzip.NewReader(r)
if err != nil { if err != nil {
r.Close()
return nil, fmt.Errorf("failed to create gzip reader: %w", err) return nil, fmt.Errorf("failed to create gzip reader: %w", err)
} }
return NewWrappedReadCloser(gr, r), nil
} }
return r, nil return r, nil
} }
// OpenCompressed opens a file for writing and automatically compresses the content if the filename ends with .gz // OpenCompressed opens a file for writing and automatically compresses the content if the filename ends with .gz
func OpenCompressed(file string, flag int, perm os.FileMode) (io.WriteCloser, error) { func OpenCompressed(file string, flag int, perm os.FileMode) (io.WriteCloser, error) {
var out io.WriteCloser
out, err := os.OpenFile(file, flag, perm) out, err := os.OpenFile(file, flag, perm)
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -70,7 +70,7 @@ func IsGzip(path string) bool { ...@@ -70,7 +70,7 @@ func IsGzip(path string) bool {
func CompressByFileType(file string, out io.WriteCloser) io.WriteCloser { func CompressByFileType(file string, out io.WriteCloser) io.WriteCloser {
if IsGzip(file) { if IsGzip(file) {
return gzip.NewWriter(out) return NewWrappedWriteCloser(gzip.NewWriter(out), out)
} }
return out return out
} }
package ioutil
import (
"errors"
"io"
)
// WrappedReadCloser is a struct that closes both the gzip.Reader and the underlying io.Closer.
type WrappedReadCloser struct {
io.ReadCloser
closer io.Closer
}
// WrappedWriteCloser is a struct that closes both the gzip.Writer and the underlying io.Closer.
type WrappedWriteCloser struct {
io.WriteCloser
closer io.Closer
}
// Close closes both the gzip.Reader and the underlying reader.
func (g *WrappedReadCloser) Close() error {
return errors.Join(g.ReadCloser.Close(), g.closer.Close())
}
// Close closes both the gzip.Writer and the underlying writer.
func (g *WrappedWriteCloser) Close() error {
return errors.Join(g.WriteCloser.Close(), g.closer.Close())
}
// NewWrappedReadCloser is a constructor function that initializes a WrappedReadCloser structure.
func NewWrappedReadCloser(r io.ReadCloser, c io.Closer) *WrappedReadCloser {
return &WrappedReadCloser{
ReadCloser: r,
closer: c,
}
}
// NewWrappedWriteCloser is a constructor function that initializes a WrappedWriteCloser structure.
func NewWrappedWriteCloser(r io.WriteCloser, c io.Closer) *WrappedWriteCloser {
return &WrappedWriteCloser{
WriteCloser: r,
closer: c,
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment