github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/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 const ( 65 // DevMode switches confinement to non-enforcing mode. 66 patch1DevMode = 1 << iota 67 // TryMode is set for snaps installed to try directly from a local directory. 68 patch1TryMode 69 ) 70 71 type patch1SnapSetup struct { 72 Name string `json:"name,omitempty"` 73 Revision snap.Revision `json:"revision,omitempty"` 74 Channel string `json:"channel,omitempty"` 75 UserID int `json:"user-id,omitempty"` 76 77 Flags patch1Flags `json:"flags,omitempty"` 78 79 SnapPath string `json:"snap-path,omitempty"` 80 } 81 82 type patch1SnapState struct { 83 SnapType string `json:"type"` 84 Sequence []*patch1SideInfo `json:"sequence"` 85 Current snap.Revision `json:"current"` 86 Candidate *patch1SideInfo `json:"candidate,omitempty"` 87 Active bool `json:"active,omitempty"` 88 Channel string `json:"channel,omitempty"` 89 Flags patch1Flags `json:"flags,omitempty"` 90 // incremented revision used for local installs 91 LocalRevision snap.Revision `json:"local-revision,omitempty"` 92 } 93 94 // patch1 adds the snap type and the current revision to the snap state. 95 func patch1(s *state.State) error { 96 var stateMap map[string]*patch1SnapState 97 98 err := s.Get("snaps", &stateMap) 99 if err == state.ErrNoState { 100 return nil 101 } 102 if err != nil { 103 return err 104 } 105 106 for snapName, snapst := range stateMap { 107 seq := snapst.Sequence 108 if len(seq) == 0 { 109 continue 110 } 111 snapst.Current = seq[len(seq)-1].Revision 112 typ, err := patch1ReadType(snapName, snapst.Current) 113 if err != nil { 114 logger.Noticef("Recording type for snap %q: cannot retrieve info, assuming it's a app: %v", snapName, err) 115 } else { 116 logger.Noticef("Recording type for snap %q: setting to %q", snapName, typ) 117 } 118 snapst.SnapType = string(typ) 119 } 120 121 s.Set("snaps", stateMap) 122 return nil 123 }