From 5828631e79dc9ee788c8dc148de18d14994591ee Mon Sep 17 00:00:00 2001 From: Ophestra Date: Sun, 15 Feb 2026 00:39:24 +0900 Subject: [PATCH] internal/pkg: split off context common For making these methods available to RContext. Signed-off-by: Ophestra --- internal/pkg/pkg.go | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/internal/pkg/pkg.go b/internal/pkg/pkg.go index fdde9be..54ac571 100644 --- a/internal/pkg/pkg.go +++ b/internal/pkg/pkg.go @@ -65,18 +65,23 @@ func MustDecode(s string) (checksum Checksum) { return } +// common holds elements and receives methods shared between different contexts. +type common struct { + // Address of underlying [Cache], should be zeroed or made unusable after + // Cure returns and must not be exposed directly. + cache *Cache +} + // TContext is passed to [TrivialArtifact.Cure] and provides information and // methods required for curing the [TrivialArtifact]. // // Methods of TContext are safe for concurrent use. TContext is valid // until [TrivialArtifact.Cure] returns. type TContext struct { - // Address of underlying [Cache], should be zeroed or made unusable after - // [TrivialArtifact.Cure] returns and must not be exposed directly. - cache *Cache - // Populated during [Cache.Cure]. work, temp *check.Absolute + + common } // destroy destroys the temporary directory and joins its errors with the error @@ -104,10 +109,10 @@ func (t *TContext) destroy(errP *error) { } // Unwrap returns the underlying [context.Context]. -func (t *TContext) Unwrap() context.Context { return t.cache.ctx } +func (c *common) Unwrap() context.Context { return c.cache.ctx } // GetMessage returns [message.Msg] held by the underlying [Cache]. -func (t *TContext) GetMessage() message.Msg { return t.cache.msg } +func (c *common) GetMessage() message.Msg { return c.cache.msg } // GetWorkDir returns a pathname to a directory which [Artifact] is expected to // write its output to. This is not the final resting place of the [Artifact] @@ -126,13 +131,13 @@ func (t *TContext) GetTempDir() *check.Absolute { return t.temp } // If err is nil, the caller must close the resulting [io.ReadCloser] and return // its error, if any. Failure to read r to EOF may result in a spurious // [ChecksumMismatchError], or the underlying implementation may block on Close. -func (t *TContext) Open(a Artifact) (r io.ReadCloser, err error) { +func (c *common) Open(a Artifact) (r io.ReadCloser, err error) { if f, ok := a.(FileArtifact); ok { - return t.cache.openFile(f) + return c.cache.openFile(f) } var pathname *check.Absolute - if pathname, _, err = t.cache.Cure(a); err != nil { + if pathname, _, err = c.cache.Cure(a); err != nil { return } @@ -1526,7 +1531,11 @@ func (c *Cache) cure(a Artifact, curesExempt bool) ( return } - t := TContext{c, c.base.Append(dirWork, ids), c.base.Append(dirTemp, ids)} + t := TContext{ + c.base.Append(dirWork, ids), + c.base.Append(dirTemp, ids), + common{c}, + } switch ca := a.(type) { case TrivialArtifact: defer t.destroy(&err)