2019-06-13 16:09:05 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/fpath"
|
2019-06-13 16:09:05 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// scope implements nesting context for foreign api.
|
|
|
|
type scope struct {
|
|
|
|
ctx context.Context
|
|
|
|
cancel func()
|
|
|
|
}
|
|
|
|
|
|
|
|
// rootScope creates a scope with the specified temp directory.
|
|
|
|
func rootScope(tempDir string) scope {
|
|
|
|
ctx := context.Background()
|
|
|
|
if tempDir == "inmemory" {
|
|
|
|
ctx = fpath.WithTempData(ctx, "", true)
|
|
|
|
} else {
|
|
|
|
ctx = fpath.WithTempData(ctx, tempDir, false)
|
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
return scope{ctx, cancel}
|
|
|
|
}
|
|
|
|
|
|
|
|
// child creates an inherited scope.
|
|
|
|
func (parent *scope) child() scope {
|
|
|
|
ctx, cancel := context.WithCancel(parent.ctx)
|
|
|
|
return scope{ctx, cancel}
|
|
|
|
}
|