diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-01-07 21:36:47 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-01-07 21:36:47 +0900 |
| commit | e8dda70c417a78647ae31930acc0a98ecf5ad097 (patch) | |
| tree | 1993e74d929cd9494f9abf593d7e9b07ec253b3a /internal/pkg/pkg.go | |
| parent | 7ea4e8b643dbe4931e3cb3072d180ee066a28dd1 (diff) | |
internal/pkg: return reader for files
This improves efficiency for cache hits.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/pkg.go')
| -rw-r--r-- | internal/pkg/pkg.go | 26 |
1 files changed, 12 insertions, 14 deletions
diff --git a/internal/pkg/pkg.go b/internal/pkg/pkg.go index 93677db3..ab1f3e00 100644 --- a/internal/pkg/pkg.go +++ b/internal/pkg/pkg.go @@ -87,10 +87,11 @@ func (c *CureContext) Cure(a Artifact) ( return c.cache.Cure(a) } -// LoadData tries to load [File] from [Cache], and if that fails, obtains it via -// [File.Data] instead. Notably, it does not cure [File]. -func (c *CureContext) LoadData(f File) (data []byte, err error) { - return c.cache.loadData(f) +// OpenFile tries to load [File] from [Cache], and if that fails, obtains it via +// [File.Data] instead. Notably, it does not cure [File]. If err is nil, the +// caller is responsible for closing the resulting [io.ReadCloser]. +func (c *CureContext) OpenFile(f File) (r io.ReadCloser, err error) { + return c.cache.openFile(f) } // An Artifact is a read-only reference to a piece of data that may be created @@ -555,9 +556,8 @@ func (c *Cache) finaliseIdent( close(done) } -// loadData provides [CureContext.LoadData] for [Artifact.Cure]. -func (c *Cache) loadData(f File) (data []byte, err error) { - var r *os.File +// openFile provides [CureContext.OpenFile] for [Artifact.Cure]. +func (c *Cache) openFile(f File) (r io.ReadCloser, err error) { if kc, ok := f.(KnownChecksum); ok { c.checksumMu.RLock() r, err = os.Open(c.base.Append( @@ -578,13 +578,11 @@ func (c *Cache) loadData(f File) (data []byte, err error) { if !errors.Is(err, os.ErrNotExist) { return } - return f.Data() - } - - data, err = io.ReadAll(r) - closeErr := r.Close() - if err == nil { - err = closeErr + var data []byte + if data, err = f.Data(); err != nil { + return + } + r = io.NopCloser(bytes.NewReader(data)) } return } |
