github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/snapstate/policy.go (about)

     1  package snapstate
     2  
     3  import (
     4  	"github.com/snapcore/snapd/asserts"
     5  	"github.com/snapcore/snapd/boot"
     6  	"github.com/snapcore/snapd/overlord/state"
     7  	"github.com/snapcore/snapd/snap"
     8  )
     9  
    10  // Policy encapsulates behaviour that varies with the details of a
    11  // snap installation, like the model assertion or the type of snap
    12  // involved in an operation. Rather than have a forest of `if`s
    13  // looking at type, model, etc, we move it to Policy and look it up.
    14  type Policy interface {
    15  	// CanRemove verifies that a snap can be removed.
    16  	// If rev is not unset, check for removing only that revision.
    17  	CanRemove(st *state.State, snapst *SnapState, rev snap.Revision, dev boot.Device) error
    18  }
    19  
    20  var PolicyFor func(snap.Type, *asserts.Model) Policy = policyForUnset
    21  
    22  func policyForUnset(snap.Type, *asserts.Model) Policy {
    23  	panic("PolicyFor unset!")
    24  }
    25  
    26  func inUseFor(deviceCtx DeviceContext) func(snap.Type) (boot.InUseFunc, error) {
    27  	if deviceCtx == nil {
    28  		return nil
    29  	}
    30  	return func(typ snap.Type) (boot.InUseFunc, error) {
    31  		return boot.InUse(typ, deviceCtx)
    32  	}
    33  }