Skip to content

Commit

Permalink
snap, s/snapdir, c/snap: fix import cycle issue with hook from snapdi…
Browse files Browse the repository at this point in the history
…r into snap
  • Loading branch information
andrewphelpsj committed Jun 18, 2024
1 parent a06dffb commit b099df9
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 28 deletions.
7 changes: 1 addition & 6 deletions cmd/snap-exec/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/snapcore/snapd/logger"
"github.com/snapcore/snapd/osutil"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapdir"
"github.com/snapcore/snapd/snap/snapenv"
)

Expand Down Expand Up @@ -259,11 +258,7 @@ func execApp(snapTarget, revision, command string, args []string) error {
}

func getComponentInfo(name string, snapInfo *snap.Info) (*snap.ComponentInfo, error) {
container := func(p string) (snap.Container, error) {
return snapdir.New(p), nil
}

return snap.ReadCurrentComponentInfo(name, snapInfo, container)
return snap.ReadCurrentComponentInfo(name, snapInfo)
}

func execHook(snapTarget string, revision, hookName string) error {
Expand Down
11 changes: 1 addition & 10 deletions cmd/snap/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import (
"github.com/snapcore/snapd/sandbox/cgroup"
"github.com/snapcore/snapd/sandbox/selinux"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapdir"
"github.com/snapcore/snapd/snap/snapenv"
"github.com/snapcore/snapd/strutil/shlex"
"github.com/snapcore/snapd/timeutil"
Expand Down Expand Up @@ -330,14 +329,6 @@ func getSnapInfo(snapName string, revision snap.Revision) (info *snap.Info, err
return info, err
}

func getComponentInfo(name string, snapInfo *snap.Info) (*snap.ComponentInfo, error) {
container := func(p string) (snap.Container, error) {
return snapdir.New(p), nil
}

return snap.ReadCurrentComponentInfo(name, snapInfo, container)
}

func createOrUpdateUserDataSymlink(info *snap.Info, usr *user.User, opts *dirs.SnapDirOptions) error {
// 'current' symlink for user data (SNAP_USER_DATA)
userData := info.UserDataDir(usr.HomeDir, opts)
Expand Down Expand Up @@ -615,7 +606,7 @@ func (x *cmdRun) snapRunHook(snapTarget string) error {
componentRevision := snap.Revision{}
_ = componentRevision

component, err = getComponentInfo(componentName, info)
component, err = snap.ReadCurrentComponentInfo(componentName, info)
if err != nil {
return err
}
Expand Down
11 changes: 5 additions & 6 deletions snap/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,9 @@ func ReadCurrentInfo(snapName string) (*Info, error) {
return ReadInfo(snapName, &SideInfo{Revision: revision})
}

func ReadCurrentComponentInfo(component string, info *Info, container func(string) (Container, error)) (*ComponentInfo, error) {
var NewContainerFromDir func(snapName string) Container

func ReadCurrentComponentInfo(component string, info *Info) (*ComponentInfo, error) {
link := filepath.Join(ComponentsBaseDir(info.InstanceName()), info.Revision.String(), component)

linkSource, err := os.Readlink(link)
Expand All @@ -1600,12 +1602,9 @@ func ReadCurrentComponentInfo(component string, info *Info, container func(strin
return nil, fmt.Errorf("cannot parse current revision for component %q: %s", component, err)
}

cont, err := container(link)
if err != nil {
return nil, err
}
container := NewContainerFromDir(link)

return ReadComponentInfoFromContainer(cont, info, &ComponentSideInfo{
return ReadComponentInfoFromContainer(container, info, &ComponentSideInfo{
Revision: revision,
Component: naming.NewComponentRef(info.SnapName(), component),
})
Expand Down
8 changes: 2 additions & 6 deletions snap/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var _ = Suite(&infoSimpleSuite{})

func (s *infoSimpleSuite) SetUpTest(c *C) {
dirs.SetRootDir(c.MkDir())
snap.NewContainerFromDir = snapdir.NewContainerFromDir
}

func (s *infoSimpleSuite) TearDownTest(c *C) {
Expand Down Expand Up @@ -404,12 +405,7 @@ type: test
Revision: snap.R(21),
})

// TODO: fix this annoying import cycle issue
container := func(p string) (snap.Container, error) {
return snapdir.New(p), nil
}

currentCompInfo, err := snap.ReadCurrentComponentInfo("comp", info, container)
currentCompInfo, err := snap.ReadCurrentComponentInfo("comp", info)
c.Assert(err, IsNil)

c.Assert(currentCompInfo.Revision, Equals, snap.R(21))
Expand Down
8 changes: 8 additions & 0 deletions snap/snapdir/snapdir.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,11 @@ func (s *SnapDir) ListDir(path string) ([]string, error) {
func (s *SnapDir) Unpack(src, dstDir string) error {
return fmt.Errorf("unpack is not supported with snaps of type snapdir")
}

func NewContainerFromDir(path string) snap.Container {
return New(path)
}

func init() {
snap.NewContainerFromDir = NewContainerFromDir
}

0 comments on commit b099df9

Please sign in to comment.