package host import ( "context" "github.com/carlosframework/platform/internal/rootparticipation" "os" "github.com/carlosframework/platform/internal/rootstate" ) const rootReleasePrivate = "/var/lib/carlos-release/private" func withClaimRootRelease(ctx context.Context, _ bool, open func() (rootstate.Store, error), effect func() error, census ...func() (rootparticipation.Ledger, bool, error)) error { if open == nil || effect == nil { return os.ErrPermission } probe := rootparticipation.OpenForUpdate if len(census) > 1 { return os.ErrPermission } if len(census) == 1 { if census[0] == nil { return os.ErrPermission } probe = census[0] } return withClaimReleaseCensus(ctx, probe, open, effect) } func withClaimReleaseCensus(ctx context.Context, census func() (rootparticipation.Ledger, bool, error), open func() (rootstate.Store, error), effect func() error) error { l, required, err := census() if l.State != nil { defer l.State.Close() } if err != nil { return err } if !required { return effect() } return withRootReleaseStore(ctx, open, effect) } func withRootReleaseStore(ctx context.Context, open func() (rootstate.Store, error), effect func() error) error { s, err := open() if err != nil { return err } defer s.Close() return rootstate.WithRelease(ctx, s, effect) } type adoptRootSource struct { OpenRelease func() (rootstate.Store, error) Census func() (rootparticipation.Ledger, bool, error) }