gopkg.in/ubuntu-core/snappy.v0@v0.0.0-20210902073436-25a8614f10a6/overlord/patch/patch1.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 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 patch 21 22 import ( 23 "io/ioutil" 24 "path/filepath" 25 26 "github.com/snapcore/snapd/logger" 27 "github.com/snapcore/snapd/overlord/state" 28 "github.com/snapcore/snapd/snap" 29 ) 30 31 func init() { 32 patches[1] = []PatchFunc{patch1} 33 } 34 35 type patch1SideInfo struct { 36 OfficialName string `yaml:"name,omitempty" json:"name,omitempty"` 37 SnapID string `yaml:"snap-id" json:"snap-id"` 38 Revision snap.Revision `yaml:"revision" json:"revision"` 39 Channel string `yaml:"channel,omitempty" json:"channel,omitempty"` 40 Developer string `yaml:"developer,omitempty" json:"developer,omitempty"` 41 EditedSummary string `yaml:"summary,omitempty" json:"summary,omitempty"` 42 EditedDescription string `yaml:"description,omitempty" json:"description,omitempty"` 43 Size int64 `yaml:"size,omitempty" json:"size,omitempty"` 44 Sha512 string `yaml:"sha512,omitempty" json:"sha512,omitempty"` 45 Private bool `yaml:"private,omitempty" json:"private,omitempty"` 46 } 47 48 var patch1ReadType = func(name string, rev snap.Revision) (snap.Type, error) { 49 snapYamlFn := filepath.Join(snap.MountDir(name, rev), "meta", "snap.yaml") 50 meta, err := ioutil.ReadFile(snapYamlFn) 51 if err != nil { 52 return snap.TypeApp, err 53 } 54 info, err := snap.InfoFromSnapYaml(meta) 55 if err != nil { 56 return snap.TypeApp, err 57 } 58 59 return info.Type(), nil 60 } 61 62 type patch1Flags int 63 64 type patch1SnapSetup struct { 65 Name string `json:"name,omitempty"` 66 Revision snap.Revision `json:"revision,omitempty"` 67 Channel string `json:"channel,omitempty"` 68 UserID int `json:"user-id,omitempty"` 69 70 Flags patch1Flags `json:"flags,omitempty"` 71 72 SnapPath string `json:"snap-path,omitempty"` 73 } 74 75 type patch1SnapState struct { 76 SnapType string `json:"type"` 77 Sequence []*patch1SideInfo `json:"sequence"` 78 Current snap.Revision `json:"current"` 79 Candidate *patch1SideInfo `json:"candidate,omitempty"` 80 Active bool `json:"active,omitempty"` 81 Channel string `json:"channel,omitempty"` 82 Flags patch1Flags `json:"flags,omitempty"` 83 // incremented revision used for local installs 84 LocalRevision snap.Revision `json:"local-revision,omitempty"` 85 } 86 87 // patch1 adds the snap type and the current revision to the snap state. 88 func patch1(s *state.State) error { 89 var stateMap map[string]*patch1SnapState 90 91 err := s.Get("snaps", &stateMap) 92 if err == state.ErrNoState { 93 return nil 94 } 95 if err != nil { 96 return err 97 } 98 99 for snapName, snapst := range stateMap { 100 seq := snapst.Sequence 101 if len(seq) == 0 { 102 continue 103 } 104 snapst.Current = seq[len(seq)-1].Revision 105 typ, err := patch1ReadType(snapName, snapst.Current) 106 if err != nil { 107 logger.Noticef("Recording type for snap %q: cannot retrieve info, assuming it's a app: %v", snapName, err) 108 } else { 109 logger.Noticef("Recording type for snap %q: setting to %q", snapName, typ) 110 } 111 snapst.SnapType = string(typ) 112 } 113 114 s.Set("snaps", stateMap) 115 return nil 116 }