Skip to content

Commit 4548a4c

Browse files
committed
ConfigureContainer and ScanLaunchTargetsContainer take a context, bump lake
1 parent a7fc044 commit 4548a4c

8 files changed

Lines changed: 103 additions & 28 deletions

File tree

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ statically analyze a game's files for platform classification.
2222
## Usage
2323

2424
```go
25-
verdict, err := dash.Configure("path/to/game", dash.ConfigureParams{
25+
verdict, err := dash.Configure(ctx, "path/to/game", dash.ConfigureParams{
2626
Consumer: consumer,
2727
})
2828

‎configure.go‎

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ func Configure(ctx context.Context, root string, params ConfigureParams) (*Verdi
193193
return nil, err
194194
}
195195
defer pool.Close()
196-
verdict, err := ConfigureContainer(container, pool, params)
196+
verdict, err := ConfigureContainer(ctx, container, pool, params)
197197
if err != nil {
198198
return nil, err
199199
}
@@ -221,7 +221,7 @@ func openConfigurePool(ctx context.Context, root string, params ConfigureParams)
221221
// and pool, such as uploaders analyzing archive entries. params.Filter is
222222
// ignored. The verdict has no BasePath, so FixPermissions can't use it and
223223
// Filter can't probe executables for elevation.
224-
func ConfigureContainer(container *tlc.Container, pool lake.Pool, params ConfigureParams) (*Verdict, error) {
224+
func ConfigureContainer(ctx context.Context, container *tlc.Container, pool lake.Pool, params ConfigureParams) (*Verdict, error) {
225225
if container == nil || pool == nil {
226226
return nil, errors.New("container and pool are required")
227227
}
@@ -234,14 +234,17 @@ func ConfigureContainer(container *tlc.Container, pool lake.Pool, params Configu
234234
}
235235
verdict := &Verdict{}
236236

237-
s := newScan(params, pool, container)
237+
s := newScan(ctx, params, pool, container)
238238
s.candidates = make([]*Candidate, 0)
239239

240240
// lowercased path of each .app bundle's main executable, when its
241241
// Info.plist declares one
242242
bundleExecutables := make(map[*Candidate]string)
243243

244244
for _, d := range container.Dirs {
245+
if err := ctx.Err(); err != nil {
246+
return nil, err
247+
}
245248
lowerPath := strings.ToLower(d.Path)
246249
if strings.HasSuffix(lowerPath, ".app") {
247250
plistPath := lowerPath + "/contents/info.plist"
@@ -271,6 +274,9 @@ func ConfigureContainer(container *tlc.Container, pool lake.Pool, params Configu
271274
}
272275

273276
for fileIndex, f := range container.Files {
277+
if err := ctx.Err(); err != nil {
278+
return nil, err
279+
}
274280
verdict.TotalSize += f.Size
275281
if isBlacklistedExt(f.Path) {
276282
continue
@@ -365,11 +371,17 @@ func ConfigureContainer(container *tlc.Container, pool lake.Pool, params Configu
365371
if err := d.detect(s); err != nil {
366372
return nil, errors.Wrapf(err, "engine detector %T", d)
367373
}
374+
// detectors treat a failed open as a miss, so ask after each one
375+
if err := ctx.Err(); err != nil {
376+
return nil, err
377+
}
368378
}
369379

370380
s.resolveAppRunLinks()
371381
if params.DeepProbe {
372-
s.deepProbe()
382+
if err := s.deepProbe(ctx); err != nil {
383+
return nil, err
384+
}
373385
}
374386

375387
verdict.Candidates = s.candidates
@@ -419,9 +431,13 @@ func (s *scan) resolveAppRunLinks() {
419431
}
420432

421433
// deepProbe fills the native dependency records. Failures are logged, not
422-
// fatal: a candidate without a record is still a candidate.
423-
func (s *scan) deepProbe() {
434+
// fatal: a candidate without a record is still a candidate. The only error
435+
// is ctx's.
436+
func (s *scan) deepProbe(ctx context.Context) error {
424437
for _, c := range s.candidates {
438+
if err := ctx.Err(); err != nil {
439+
return err
440+
}
425441
index, ok := s.file(strings.ToLower(c.Path))
426442
if !ok {
427443
continue
@@ -473,6 +489,8 @@ func (s *scan) deepProbe() {
473489
s.loveVersionFromResource(c)
474490
}
475491
}
492+
// a cancel during the last probe only shows up as a logged read error
493+
return ctx.Err()
476494
}
477495

478496
type FixPermissionsParams struct {

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.25.0
55
require (
66
github.com/itchio/arkive v0.0.0-20260424164943-5eabdbdce5c8
77
github.com/itchio/headway v0.0.0-20251229214354-da882c8b5dd4
8-
github.com/itchio/lake v0.0.0-20261014002703-c21855d4ccaf
8+
github.com/itchio/lake v0.0.0-20261122050844-c3a57f1b367d
99
github.com/itchio/pelican v0.0.0-20261104233745-f575d787f608
1010
github.com/itchio/spellbook v0.0.0-20260223022740-4f847d058148
1111
github.com/itchio/wizardry v0.0.0-20260222230103-ec734b409e9c

‎go.sum‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ github.com/itchio/httpkit v0.0.0-20261014001543-804157f68bf3 h1:Hr0zGbUvURlg610W
5353
github.com/itchio/httpkit v0.0.0-20261014001543-804157f68bf3/go.mod h1:/eodDULLFn8KrWObOlT7AF5LcFi8Ff1beHXcwdLMe5g=
5454
github.com/itchio/kompress v0.0.0-20200301155538-5c2eecce9e51 h1:jipXtdsZClshNO8fKK8ItiVyqE9bKA09as+qjW/yjG0=
5555
github.com/itchio/kompress v0.0.0-20200301155538-5c2eecce9e51/go.mod h1:iLS+Eq+S26jTC4pbdDH6N5XtDqrjb4IOXtjj1HCzbuk=
56-
github.com/itchio/lake v0.0.0-20261014002703-c21855d4ccaf h1:UeDwcZg6FftcbFWUqOCqCjWjB7e+pecC6wgLpZA9i4U=
57-
github.com/itchio/lake v0.0.0-20261014002703-c21855d4ccaf/go.mod h1:ABzRyUqsKcRQxAKFp7uac7C4z4Y0C1T0uDvBoDVx77g=
56+
github.com/itchio/lake v0.0.0-20261122050844-c3a57f1b367d h1:qyGau50rMXO+7jFwXkb2LMlGbloaLapOaINvGhvKLg0=
57+
github.com/itchio/lake v0.0.0-20261122050844-c3a57f1b367d/go.mod h1:JFoGOaPYTRbW2DaDKFOPG9ikZzeJoTX1Z15l9s6bqGQ=
5858
github.com/itchio/pelican v0.0.0-20261104233745-f575d787f608 h1:vbJzLwS5ifiXtrquSGGBOxfuR/4iwvT+Y0ZKjYZ4Okk=
5959
github.com/itchio/pelican v0.0.0-20261104233745-f575d787f608/go.mod h1:5myIRqGAjat0gT3qiK43Cv1puZhxn9bi17+JZFpJQMo=
6060
github.com/itchio/screw v0.0.0-20260221011136-e674b460b040 h1:2nzurbZLgBSqNyLlIuO2iClpivQ7L5DX75kvCMqZ9e0=

‎launch_targets.go‎

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,45 +41,53 @@ func ScanLaunchTargets(ctx context.Context, root string, params ConfigureParams)
4141
return nil, err
4242
}
4343
defer pool.Close()
44-
return ScanLaunchTargetsContainer(container, pool, params)
44+
return ScanLaunchTargetsContainer(ctx, container, pool, params)
4545
}
4646

4747
// ScanLaunchTargetsContainer is ScanLaunchTargets for a container and pool the
4848
// caller already has. See ConfigureContainer.
49-
func ScanLaunchTargetsContainer(container *tlc.Container, pool lake.Pool, params ConfigureParams) ([]LaunchTarget, error) {
49+
func ScanLaunchTargetsContainer(ctx context.Context, container *tlc.Container, pool lake.Pool, params ConfigureParams) ([]LaunchTarget, error) {
5050
params.DeepProbe = true
5151
if params.Consumer == nil {
5252
params.Consumer = &state.Consumer{}
5353
}
54-
verdict, err := ConfigureContainer(container, pool, params)
54+
verdict, err := ConfigureContainer(ctx, container, pool, params)
5555
if err != nil {
5656
return nil, err
5757
}
58-
return launchTargetsFromVerdict(verdict, container, pool, params.Consumer), nil
58+
return launchTargetsFromVerdict(ctx, verdict, container, pool, params.Consumer)
5959
}
6060

61-
func launchTargetsFromVerdict(verdict *Verdict, container *tlc.Container, pool lake.Pool, consumer *state.Consumer) []LaunchTarget {
61+
func launchTargetsFromVerdict(ctx context.Context, verdict *Verdict, container *tlc.Container, pool lake.Pool, consumer *state.Consumer) ([]LaunchTarget, error) {
6262
indices := make(map[string]int64, len(container.Files))
6363
for i, f := range container.Files {
6464
indices[f.Path] = int64(i)
6565
}
6666
targets := make([]LaunchTarget, 0, len(verdict.Candidates))
6767
for _, c := range verdict.Candidates {
68+
// each candidate is hashed in full
69+
if err := ctx.Err(); err != nil {
70+
return nil, err
71+
}
6872
target := LaunchTarget{
6973
Path: c.Path, Depth: c.Depth, Flavor: c.Flavor, Arch: c.Arch,
7074
Helper: c.Helper, Engine: c.Engine, LinuxInfo: c.LinuxInfo,
7175
WindowsInfo: c.WindowsInfo, MacosInfo: c.MacosInfo, AndroidInfo: c.AndroidInfo,
7276
}
7377
if index, ok := indices[c.Path]; ok {
7478
target.Size = container.Files[index].Size
75-
target.Sha256 = launchTargetHash(pool, index, c.Path, consumer)
79+
target.Sha256 = launchTargetHash(ctx, pool, index, c.Path, consumer)
7680
}
7781
targets = append(targets, target)
7882
}
79-
return targets
83+
// a cancel during the last hash only shows up as an empty Sha256
84+
if err := ctx.Err(); err != nil {
85+
return nil, err
86+
}
87+
return targets, nil
8088
}
8189

82-
func launchTargetHash(pool lake.Pool, index int64, path string, consumer *state.Consumer) string {
90+
func launchTargetHash(ctx context.Context, pool lake.Pool, index int64, path string, consumer *state.Consumer) string {
8391
r, err := pool.GetReadSeeker(index)
8492
if err != nil {
8593
consumer.Warnf("Couldn't open %s for hashing: %v", path, err)
@@ -91,9 +99,22 @@ func launchTargetHash(pool lake.Pool, index int64, path string, consumer *state.
9199
return ""
92100
}
93101
hasher := sha256.New()
94-
if _, err := io.Copy(hasher, r); err != nil {
102+
if _, err := io.Copy(hasher, &ctxReader{ctx: ctx, r: r}); err != nil {
95103
consumer.Warnf("While hashing %s: %v", path, err)
96104
return ""
97105
}
98106
return fmt.Sprintf("%x", hasher.Sum(nil))
99107
}
108+
109+
// ctxReader fails reads once ctx is cancelled.
110+
type ctxReader struct {
111+
ctx context.Context
112+
r io.Reader
113+
}
114+
115+
func (cr *ctxReader) Read(p []byte) (int, error) {
116+
if err := cr.ctx.Err(); err != nil {
117+
return 0, err
118+
}
119+
return cr.r.Read(p)
120+
}

‎launch_targets_test.go‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ func TestLaunchTargetJSONContract(t *testing.T) {
8484
Engine: &EngineInfo{Engine: Engine("test"), Version: "1", Details: map[string]any{"key": "value"}},
8585
LinuxInfo: &LinuxInfo{}, WindowsInfo: &WindowsInfo{}, MacosInfo: &MacosInfo{},
8686
}}}
87-
targets := launchTargetsFromVerdict(verdict, &tlc.Container{}, nil, &state.Consumer{})
87+
targets, err := launchTargetsFromVerdict(context.Background(), verdict, &tlc.Container{}, nil, &state.Consumer{})
88+
if err != nil {
89+
t.Fatal(err)
90+
}
8891
data, err := json.Marshal(targets)
8992
if err != nil {
9093
t.Fatal(err)
@@ -132,13 +135,13 @@ func TestLaunchTargetHashRewindsAndWarns(t *testing.T) {
132135
reader := strings.NewReader("game")
133136
_, _ = reader.Seek(3, io.SeekStart)
134137
pool := &reportPool{reader: reader}
135-
hash := launchTargetHash(pool, 0, "game", &state.Consumer{})
138+
hash := launchTargetHash(context.Background(), pool, 0, "game", &state.Consumer{})
136139
if hash != fmt.Sprintf("%x", sha256.Sum256([]byte("game"))) {
137140
t.Fatalf("hash did not read whole file: %s", hash)
138141
}
139142
pool.reader = brokenReportReader{strings.NewReader("game")}
140143
warned := false
141-
hash = launchTargetHash(pool, 0, "game", &state.Consumer{OnMessage: func(_, _ string) { warned = true }})
144+
hash = launchTargetHash(context.Background(), pool, 0, "game", &state.Consumer{OnMessage: func(_, _ string) { warned = true }})
142145
if !warned || hash != "" {
143146
t.Fatalf("failed hash: warned=%v hash=%s", warned, hash)
144147
}
@@ -166,7 +169,7 @@ func TestLaunchTargetsContainerSelectionAndOwnership(t *testing.T) {
166169
}
167170
defer pool.Close()
168171
tracked := &reportPool{Pool: pool}
169-
targets, err := ScanLaunchTargetsContainer(container, &ownershipPool{Pool: pool, tracker: tracked}, ConfigureParams{
172+
targets, err := ScanLaunchTargetsContainer(context.Background(), container, &ownershipPool{Pool: pool, tracker: tracked}, ConfigureParams{
170173
Filter: func(string) tlc.FilterResult { t.Fatal("must not re-filter container"); return tlc.FilterIgnore },
171174
})
172175
if err != nil {
@@ -267,9 +270,12 @@ func TestLaunchTargetPreservesSizeWhenOpenFails(t *testing.T) {
267270
container := &tlc.Container{Files: []*tlc.File{{Path: "game.exe", Size: 123}}}
268271
verdict := &Verdict{Candidates: []*Candidate{{Path: "game.exe", Depth: 1, Flavor: FlavorNativeWindows}}}
269272
warned := false
270-
targets := launchTargetsFromVerdict(verdict, container, unavailableReportPool{}, &state.Consumer{
273+
targets, err := launchTargetsFromVerdict(context.Background(), verdict, container, unavailableReportPool{}, &state.Consumer{
271274
OnMessage: func(_, _ string) { warned = true },
272275
})
276+
if err != nil {
277+
t.Fatal(err)
278+
}
273279
if !warned || len(targets) != 1 || targets[0].Size != 123 || targets[0].Sha256 != "" {
274280
t.Fatalf("size lost on open failure: warned=%v targets=%+v", warned, targets)
275281
}

‎scan.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dash
22

33
import (
4+
"context"
45
"io"
56
"strings"
67

@@ -20,6 +21,8 @@ type engineDetector interface {
2021

2122
// scan is the state shared by Configure and the engine detectors.
2223
type scan struct {
24+
// checked on every open, so detectors stop reading once it is cancelled
25+
ctx context.Context
2326
params ConfigureParams
2427
consumer *state.Consumer
2528
pool lake.Pool
@@ -53,8 +56,9 @@ type scan struct {
5356
// the detectors look for.
5457
const tailWindow = 65535 + 22
5558

56-
func newScan(params ConfigureParams, pool lake.Pool, container *tlc.Container) *scan {
59+
func newScan(ctx context.Context, params ConfigureParams, pool lake.Pool, container *tlc.Container) *scan {
5760
s := &scan{
61+
ctx: ctx,
5862
params: params,
5963
consumer: params.Consumer,
6064
pool: pool,
@@ -150,6 +154,9 @@ func (s *scan) hasDir(lowerPath string) bool {
150154
// budget is per file, not per open: MaxProbeBytes caps the distinct bytes
151155
// every sniffer and detector together may read from one file.
152156
func (s *scan) open(index int) (*probeReader, error) {
157+
if err := s.ctx.Err(); err != nil {
158+
return nil, err
159+
}
153160
r, err := s.pool.GetReadSeeker(int64(index))
154161
if err != nil {
155162
return nil, errors.Wrapf(err, "opening %s", s.container.Files[index].Path)
@@ -167,6 +174,9 @@ func (s *scan) open(index int) (*probeReader, error) {
167174

168175
// openRaw returns an unbudgeted reader, for probes that set their own limit.
169176
func (s *scan) openRaw(index int) (io.ReadSeeker, int64, error) {
177+
if err := s.ctx.Err(); err != nil {
178+
return nil, 0, err
179+
}
170180
r, err := s.pool.GetReadSeeker(int64(index))
171181
if err != nil {
172182
return nil, 0, errors.Wrapf(err, "opening %s", s.container.Files[index].Path)

‎scan_test.go‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dash
22

33
import (
44
"context"
5+
"errors"
56
"os"
67
"path/filepath"
78
"strings"
@@ -21,7 +22,7 @@ func TestScanDirectoryIndexes(t *testing.T) {
2122
},
2223
Dirs: []*tlc.Dir{{Path: "Game"}, {Path: "Empty/Nested"}},
2324
}
24-
s := newScan(ConfigureParams{}, nil, container)
25+
s := newScan(context.Background(), ConfigureParams{}, nil, container)
2526

2627
if got := s.originalDir("game/data"); got != "Game/Data" {
2728
t.Fatalf("originalDir = %q", got)
@@ -46,7 +47,7 @@ func TestScanCandidateIndexFollowsAppends(t *testing.T) {
4647
{Path: "App.app/Contents/MacOS/Helper"},
4748
{Path: "lib/x86_64/game"},
4849
}}
49-
s := newScan(ConfigureParams{}, nil, container)
50+
s := newScan(context.Background(), ConfigureParams{}, nil, container)
5051

5152
first := s.addFileCandidate(0, FlavorNativeMacos, nil)
5253
if s.candidateAt("app.app/contents/macos/game") != first {
@@ -83,7 +84,7 @@ func TestReadBundleExecutableSizeCap(t *testing.T) {
8384
t.Fatal(err)
8485
}
8586
}
86-
container, err := tlc.WalkDir(root, tlc.WalkOpts{Filter: tlc.KeepAllFilter})
87+
container, err := tlc.WalkDir(context.Background(), root, tlc.WalkOpts{Filter: tlc.KeepAllFilter})
8788
if err != nil {
8889
t.Fatal(err)
8990
}
@@ -107,3 +108,22 @@ func TestReadBundleExecutableSizeCap(t *testing.T) {
107108
}
108109
}
109110
}
111+
112+
func TestConfigureContainerCancelled(t *testing.T) {
113+
root := "testdata/linux-sdl"
114+
container, err := tlc.WalkDir(context.Background(), root, tlc.WalkOpts{})
115+
if err != nil {
116+
t.Fatal(err)
117+
}
118+
pool, err := pools.New(context.Background(), container, root)
119+
if err != nil {
120+
t.Fatal(err)
121+
}
122+
defer pool.Close()
123+
124+
ctx, cancel := context.WithCancel(context.Background())
125+
cancel()
126+
if _, err := ConfigureContainer(ctx, container, pool, ConfigureParams{}); !errors.Is(err, context.Canceled) {
127+
t.Fatalf("got %v, want context.Canceled", err)
128+
}
129+
}

0 commit comments

Comments
 (0)