aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/pkg.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/pkg/pkg.go')
-rw-r--r--internal/pkg/pkg.go26
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
}