From 58f68601e61871a72e292d1cd4e308a4324422bf Mon Sep 17 00:00:00 2001 From: Simone Gotti Date: Fri, 25 Oct 2019 10:16:21 +0200 Subject: [PATCH] objectstorage posix: use limitreader only when size is specified. Use limitreader only when size is specified (greater or equal to 0). When size is unknown (less than 0) limitreader will immediately return EOF instead of writing the whole data. --- internal/objectstorage/objectstorage.go | 5 +++++ internal/objectstorage/posix/posix.go | 8 ++++++-- internal/objectstorage/posixflat/posixflat.go | 8 ++++++-- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/internal/objectstorage/objectstorage.go b/internal/objectstorage/objectstorage.go index ddf5a4d..86c4126 100644 --- a/internal/objectstorage/objectstorage.go +++ b/internal/objectstorage/objectstorage.go @@ -23,6 +23,11 @@ import ( type Storage interface { Stat(filepath string) (*types.ObjectInfo, error) ReadObject(filepath string) (types.ReadSeekCloser, error) + // WriteObject atomically writes an object. If size is greater or equal to + // zero then only size bytes will be read from data and wrote. If size is + // less than zero data will be wrote until EOF. When persist is true the + // implementation must ensure that data is persisted to the underlying + // storage. WriteObject(filepath string, data io.Reader, size int64, persist bool) error DeleteObject(filepath string) error List(prefix, startWith, delimiter string, doneCh <-chan struct{}) <-chan types.ObjectInfo diff --git a/internal/objectstorage/posix/posix.go b/internal/objectstorage/posix/posix.go index c44a184..8a4201d 100644 --- a/internal/objectstorage/posix/posix.go +++ b/internal/objectstorage/posix/posix.go @@ -98,9 +98,13 @@ func (s *PosixStorage) WriteObject(p string, data io.Reader, size int64, persist if err := os.MkdirAll(path.Dir(fspath), 0770); err != nil { return err } - lr := io.LimitReader(data, size) + + r := data + if size >= 0 { + r = io.LimitReader(data, size) + } return common.WriteFileAtomicFunc(fspath, s.dataDir, s.tmpDir, 0660, persist, func(f io.Writer) error { - _, err := io.Copy(f, lr) + _, err := io.Copy(f, r) return err }) } diff --git a/internal/objectstorage/posixflat/posixflat.go b/internal/objectstorage/posixflat/posixflat.go index e70afe6..d3e4a7c 100644 --- a/internal/objectstorage/posixflat/posixflat.go +++ b/internal/objectstorage/posixflat/posixflat.go @@ -274,9 +274,13 @@ func (s *PosixFlatStorage) WriteObject(p string, data io.Reader, size int64, per if err := os.MkdirAll(path.Dir(fspath), 0770); err != nil { return err } - lr := io.LimitReader(data, size) + + r := data + if size >= 0 { + r = io.LimitReader(data, size) + } return common.WriteFileAtomicFunc(fspath, s.dataDir, s.tmpDir, 0660, persist, func(f io.Writer) error { - _, err := io.Copy(f, lr) + _, err := io.Copy(f, r) return err }) }