Skip to content

Commit

Permalink
interfaces/builtin: always use /snap for the content interface
Browse files Browse the repository at this point in the history
  • Loading branch information
morphis committed May 17, 2017
1 parent 8f9dd95 commit 2c626ed
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
2 changes: 2 additions & 0 deletions dirs/dirs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (

SnapMountDir string
BaseSnapMountDir string
CoreSnapMountDir string
SnapBlobDir string
SnapDataDir string
SnapDataHomeGlob string
Expand Down Expand Up @@ -185,6 +186,7 @@ func SetRootDir(rootdir string) {
}

CoreLibExecDir = "/usr/lib/snapd"
CoreSnapMountDir = "/snap"

XdgRuntimeDirBase = filepath.Join(rootdir, "/run/user")
XdgRuntimeDirGlob = filepath.Join(rootdir, XdgRuntimeDirBase, "*/")
Expand Down
13 changes: 12 additions & 1 deletion interfaces/builtin/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"path/filepath"
"strings"

"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/mount"
Expand Down Expand Up @@ -116,13 +117,23 @@ func (iface *contentInterface) path(slot *interfaces.Slot, name string) []string
return out
}

const (
SnapMountDir = iota
SnapDataDir
SnapCommonDir
)

// resolveSpecialVariable resolves one of the three $SNAP* variables at the
// beginning of a given path. The variables are $SNAP, $SNAP_DATA and
// $SNAP_COMMON. If there are no variables then $SNAP is implicitly assumed
// (this is the behavior that was used before the variables were supporter).
func resolveSpecialVariable(path string, snapInfo *snap.Info) string {
if strings.HasPrefix(path, "$SNAP/") || path == "$SNAP" {
return strings.Replace(path, "$SNAP", snapInfo.MountDir(), 1)
// NOTE: We use dirs.CoreSnapMountDir here as the path used will be always
// inside the mount namespace snap-confine creates and there we will
// always have a /snap directory available regardless if the system
// we're running on supports this or not.
return strings.Replace(path, "$SNAP", snap.MountDirWithBasePath(dirs.CoreSnapMountDir, snapInfo.Name(), snapInfo.Revision), 1)
}
if strings.HasPrefix(path, "$SNAP_DATA/") || path == "$SNAP_DATA" {
return strings.Replace(path, "$SNAP_DATA", snapInfo.DataDir(), 1)
Expand Down
16 changes: 8 additions & 8 deletions interfaces/builtin/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ apps:

func (s *ContentSuite) TestResolveSpecialVariable(c *C) {
info := snaptest.MockInfo(c, "name: name", &snap.SideInfo{Revision: snap.R(42)})
c.Check(builtin.ResolveSpecialVariable("foo", info), Equals, filepath.Join(dirs.BaseSnapMountDir, "name/42/foo"))
c.Check(builtin.ResolveSpecialVariable("$SNAP/foo", info), Equals, filepath.Join(dirs.BaseSnapMountDir, "name/42/foo"))
c.Check(builtin.ResolveSpecialVariable("foo", info), Equals, filepath.Join(dirs.CoreSnapMountDir, "name/42/foo"))
c.Check(builtin.ResolveSpecialVariable("$SNAP/foo", info), Equals, filepath.Join(dirs.CoreSnapMountDir, "name/42/foo"))
c.Check(builtin.ResolveSpecialVariable("$SNAP_DATA/foo", info), Equals, "/var/snap/name/42/foo")
c.Check(builtin.ResolveSpecialVariable("$SNAP_COMMON/foo", info), Equals, "/var/snap/name/common/foo")
c.Check(builtin.ResolveSpecialVariable("$SNAP", info), Equals, filepath.Join(dirs.BaseSnapMountDir, "name/42"))
c.Check(builtin.ResolveSpecialVariable("$SNAP", info), Equals, filepath.Join(dirs.CoreSnapMountDir, "name/42"))
c.Check(builtin.ResolveSpecialVariable("$SNAP_DATA", info), Equals, "/var/snap/name/42")
c.Check(builtin.ResolveSpecialVariable("$SNAP_COMMON", info), Equals, "/var/snap/name/common")
}
Expand All @@ -230,8 +230,8 @@ slots:
spec := &mount.Specification{}
c.Assert(spec.AddConnectedPlug(s.iface, plug, nil, slot, nil), IsNil)
expectedMnt := []mount.Entry{{
Name: filepath.Join(dirs.BaseSnapMountDir, "producer/5/export"),
Dir: filepath.Join(dirs.BaseSnapMountDir, "consumer/7/import"),
Name: filepath.Join(dirs.CoreSnapMountDir, "producer/5/export"),
Dir: filepath.Join(dirs.CoreSnapMountDir, "consumer/7/import"),
Options: []string{"bind", "ro"},
}}
c.Assert(spec.MountEntries(), DeepEquals, expectedMnt)
Expand Down Expand Up @@ -261,8 +261,8 @@ slots:
spec := &mount.Specification{}
c.Assert(spec.AddConnectedPlug(s.iface, plug, nil, slot, nil), IsNil)
expectedMnt := []mount.Entry{{
Name: filepath.Join(dirs.BaseSnapMountDir, "producer/5/export"),
Dir: filepath.Join(dirs.BaseSnapMountDir, "consumer/7/import"),
Name: filepath.Join(dirs.CoreSnapMountDir, "producer/5/export"),
Dir: filepath.Join(dirs.CoreSnapMountDir, "consumer/7/import"),
Options: []string{"bind", "ro"},
}}
c.Assert(spec.MountEntries(), DeepEquals, expectedMnt)
Expand All @@ -276,7 +276,7 @@ slots:
# snaps may directly access the slot implementation's files
# read-only.
%s/producer/5/export/** mrkix,
`, dirs.BaseSnapMountDir)
`, dirs.CoreSnapMountDir)
c.Assert(apparmorSpec.SnippetForTag("snap.consumer.app"), Equals, expected)
}

Expand Down
7 changes: 6 additions & 1 deletion snap/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func MinimalPlaceInfo(name string, revision Revision) PlaceInfo {

// MountDir returns the base directory where it gets mounted of the snap with the given name and revision.
func MountDir(name string, revision Revision) string {
return filepath.Join(dirs.SnapMountDir, name, revision.String())
return MountDirWithBasePath(dirs.SnapMountDir, name, revision)
}

// MountDirWithBasePath returns the base directory where it gets mounted of the snap with the given name and revision.
func MountDirWithBasePath(basePath, name string, revision Revision) string {
return filepath.Join(basePath, name, revision.String())
}

// MountFile returns the path where the snap file that is mounted is installed.
Expand Down

0 comments on commit 2c626ed

Please sign in to comment.