github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/overlord/patch/patch6.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  	"github.com/snapcore/snapd/overlord/state"
    24  	"github.com/snapcore/snapd/snap"
    25  )
    26  
    27  func init() {
    28  	patches[6] = []PatchFunc{patch6, patch6_1, patch6_2, patch6_3}
    29  }
    30  
    31  type patch6Flags struct {
    32  	DevMode  bool `json:"devmode,omitempty"`
    33  	JailMode bool `json:"jailmode,omitempty"`
    34  	TryMode  bool `json:"trymode,omitempty"`
    35  	Revert   bool `json:"revert,omitempty"`
    36  }
    37  
    38  type patch6SnapSetup struct {
    39  	Channel string `json:"channel,omitempty"`
    40  	UserID  int    `json:"user-id,omitempty"`
    41  
    42  	patch6Flags
    43  
    44  	SnapPath string `json:"snap-path,omitempty"`
    45  
    46  	DownloadInfo *patch4DownloadInfo `json:"download-info,omitempty"`
    47  	SideInfo     *patch4SideInfo     `json:"side-info,omitempty"`
    48  }
    49  
    50  type patch6SnapState struct {
    51  	SnapType string            `json:"type"` // Use Type and SetType
    52  	Sequence []*patch4SideInfo `json:"sequence"`
    53  	Active   bool              `json:"active,omitempty"`
    54  	Current  snap.Revision     `json:"current"`
    55  	Channel  string            `json:"channel,omitempty"`
    56  	patch6Flags
    57  }
    58  
    59  func patch6FlagsFromPatch4(old patch4Flags) patch6Flags {
    60  	return patch6Flags{
    61  		DevMode:  old.DevMode(),
    62  		TryMode:  old.TryMode(),
    63  		JailMode: old.JailMode(),
    64  		Revert:   old.Revert(),
    65  	}
    66  }
    67  
    68  // patch6:
    69  //  - move from a flags-are-ints world to a flags-are-struct-of-bools world
    70  func patch6(st *state.State) error {
    71  	var oldStateMap map[string]*patch4SnapState
    72  	err := st.Get("snaps", &oldStateMap)
    73  	if err == state.ErrNoState {
    74  		return nil
    75  	}
    76  	if err != nil {
    77  		return err
    78  	}
    79  	newStateMap := make(map[string]*patch6SnapState, len(oldStateMap))
    80  
    81  	for key, old := range oldStateMap {
    82  		newStateMap[key] = &patch6SnapState{
    83  			SnapType:    old.SnapType,
    84  			Sequence:    old.Sequence,
    85  			Active:      old.Active,
    86  			Current:     old.Current,
    87  			Channel:     old.Channel,
    88  			patch6Flags: patch6FlagsFromPatch4(old.Flags),
    89  		}
    90  	}
    91  
    92  	for _, task := range st.Tasks() {
    93  		var old patch4SnapSetup
    94  		err := task.Get("snap-setup", &old)
    95  		if err == state.ErrNoState {
    96  			continue
    97  		}
    98  		if err != nil && err != state.ErrNoState {
    99  			return err
   100  		}
   101  
   102  		task.Set("snap-setup", &patch6SnapSetup{
   103  			Channel:      old.Channel,
   104  			UserID:       old.UserID,
   105  			SnapPath:     old.SnapPath,
   106  			DownloadInfo: old.DownloadInfo,
   107  			SideInfo:     old.SideInfo,
   108  			patch6Flags:  patch6FlagsFromPatch4(old.Flags),
   109  		})
   110  	}
   111  
   112  	st.Set("snaps", newStateMap)
   113  
   114  	return nil
   115  }