| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/rootparticipation" |
| 6 | "os" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/rootstate" |
| 9 | ) |
| 10 | |
| 11 | const rootReleasePrivate = "/var/lib/carlos-release/private" |
| 12 | |
| 13 | func withClaimRootRelease(ctx context.Context, _ bool, open func() (rootstate.Store, error), effect func() error, census ...func() (rootparticipation.Ledger, bool, error)) error { |
| 14 | if open == nil || effect == nil { |
| 15 | return os.ErrPermission |
| 16 | } |
| 17 | probe := rootparticipation.OpenForUpdate |
| 18 | if len(census) > 1 { |
| 19 | return os.ErrPermission |
| 20 | } |
| 21 | if len(census) == 1 { |
| 22 | if census[0] == nil { |
| 23 | return os.ErrPermission |
| 24 | } |
| 25 | probe = census[0] |
| 26 | } |
| 27 | return withClaimReleaseCensus(ctx, probe, open, effect) |
| 28 | } |
| 29 | |
| 30 | func withClaimReleaseCensus(ctx context.Context, census func() (rootparticipation.Ledger, bool, error), open func() (rootstate.Store, error), effect func() error) error { |
| 31 | l, required, err := census() |
| 32 | if l.State != nil { |
| 33 | defer l.State.Close() |
| 34 | } |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | if !required { |
| 39 | return effect() |
| 40 | } |
| 41 | return withRootReleaseStore(ctx, open, effect) |
| 42 | } |
| 43 | |
| 44 | func withRootReleaseStore(ctx context.Context, open func() (rootstate.Store, error), effect func() error) error { |
| 45 | s, err := open() |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | defer s.Close() |
| 50 | return rootstate.WithRelease(ctx, s, effect) |
| 51 | } |
| 52 | |
| 53 | type adoptRootSource struct { |
| 54 | OpenRelease func() (rootstate.Store, error) |
| 55 | Census func() (rootparticipation.Ledger, bool, error) |
| 56 | } |
| 57 | |