github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/snapstate/policy/base.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package policy
    21  
    22  import (
    23  	"sort"
    24  
    25  	"github.com/snapcore/snapd/boot"
    26  	"github.com/snapcore/snapd/overlord/snapstate"
    27  	"github.com/snapcore/snapd/overlord/state"
    28  	"github.com/snapcore/snapd/snap"
    29  )
    30  
    31  type basePolicy struct {
    32  	modelBase string
    33  }
    34  
    35  func (p *basePolicy) CanRemove(st *state.State, snapst *snapstate.SnapState, rev snap.Revision, dev boot.Device) error {
    36  	name := snapst.InstanceName()
    37  	if name == "" {
    38  		// not installed, or something. What are you even trying to do.
    39  		return errNoName
    40  	}
    41  
    42  	if ephemeral(dev) {
    43  		return errEphemeralSnapsNotRemovalable
    44  	}
    45  
    46  	if p.modelBase == name {
    47  		if !rev.Unset() {
    48  			// TODO: tweak boot.InUse so that it DTRT when rev.Unset, call
    49  			// it unconditionally as an extra precaution
    50  			if err := inUse(name, rev, snap.TypeBase, dev); err != nil {
    51  				return err
    52  			}
    53  			return nil
    54  		}
    55  		return errIsModel
    56  	}
    57  
    58  	if !rev.Unset() {
    59  		return nil
    60  	}
    61  
    62  	// a core system could have core18 required in the model due to dependencies for ex
    63  	if snapst.Required {
    64  		return errRequired
    65  	}
    66  
    67  	// here we use that bases can't be instantiated (InstanceName == SnapName always)
    68  	usedBy, err := baseUsedBy(st, name)
    69  	if len(usedBy) == 0 || err != nil {
    70  		return err
    71  	}
    72  	return inUseByErr(usedBy)
    73  }
    74  
    75  func baseUsedBy(st *state.State, baseName string) ([]string, error) {
    76  	snapStates, err := snapstate.All(st)
    77  	if err != nil {
    78  		// note snapstate.All doesn't currently return ErrNoState
    79  		return nil, err
    80  	}
    81  	alsoCore16 := false
    82  	if baseName == "" {
    83  		// if core is installed, a snap having base: core16 will not
    84  		// pull in core16 itself but use core instead. So if we are
    85  		// looking at core (a base of ""), and core16 is not installed,
    86  		// then we need to look out for things having base: core16 as
    87  		// well as "".
    88  		//
    89  		// TODO: if we ever do the converse, using core16 for snaps
    90  		//       having a base of "", then this needs a tweak.
    91  		if _, ok := snapStates["core16"]; !ok {
    92  			alsoCore16 = true
    93  		}
    94  	}
    95  
    96  	var usedBy []string
    97  	for name, snapst := range snapStates {
    98  		if typ, err := snapst.Type(); err == nil && typ != snap.TypeApp && typ != snap.TypeGadget {
    99  			continue
   100  		}
   101  
   102  		for _, si := range snapst.Sequence {
   103  			snapInfo, err := snap.ReadInfo(name, si)
   104  			if err == nil {
   105  				if typ := snapInfo.Type(); typ != snap.TypeApp && typ != snap.TypeGadget {
   106  					continue
   107  				}
   108  				if !(baseName == snapInfo.Base || (alsoCore16 && snapInfo.Base == "core16")) {
   109  					continue
   110  				}
   111  				usedBy = append(usedBy, snapInfo.InstanceName())
   112  				break
   113  			}
   114  		}
   115  	}
   116  	sort.Strings(usedBy)
   117  	return usedBy, nil
   118  }