aboutsummaryrefslogtreecommitdiffstats
path: root/utils/ioutil/common.go
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2021-05-12 21:42:07 +0100
committerGitHub <noreply@github.com>2021-05-12 22:42:07 +0200
commit720c192831a890d0a36b4c6720b60411fa4a0159 (patch)
tree9f3020c074fe9b113e22d6f2c601e8a36bf0ac49 /utils/ioutil/common.go
parente6e23391e4d044cc85e09b4420a2533715e7312d (diff)
downloadgo-git-720c192831a890d0a36b4c6720b60411fa4a0159.tar.gz
plumbing: format/packfile, prevent large objects from being read into memory completely (#303)v5.4.0
This PR adds code to prevent large objects from being read into memory from packfiles or the filesystem. Objects greater than 1Mb are now no longer directly stored in the cache or read completely into memory. Signed-off-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'utils/ioutil/common.go')
-rw-r--r--utils/ioutil/common.go22
1 files changed, 22 insertions, 0 deletions
diff --git a/utils/ioutil/common.go b/utils/ioutil/common.go
index b52e85a..a0e79a2 100644
--- a/utils/ioutil/common.go
+++ b/utils/ioutil/common.go
@@ -55,6 +55,28 @@ func NewReadCloser(r io.Reader, c io.Closer) io.ReadCloser {
return &readCloser{Reader: r, closer: c}
}
+type readCloserCloser struct {
+ io.ReadCloser
+ closer func() error
+}
+
+func (r *readCloserCloser) Close() (err error) {
+ defer func() {
+ if err == nil {
+ err = r.closer()
+ return
+ }
+ _ = r.closer()
+ }()
+ return r.ReadCloser.Close()
+}
+
+// NewReadCloserWithCloser creates an `io.ReadCloser` with the given `io.ReaderCloser` and
+// `io.Closer` that ensures that the closer is closed on close
+func NewReadCloserWithCloser(r io.ReadCloser, c func() error) io.ReadCloser {
+ return &readCloserCloser{ReadCloser: r, closer: c}
+}
+
type writeCloser struct {
io.Writer
closer io.Closer