Skip to content

Commit

Permalink
wrappers: skip snapd desktop files in {Ensure,Remove}SnapDesktopFiles (
Browse files Browse the repository at this point in the history
…canonical#14468)

Snapd desktop files on core are installed under SnapDesktopFilesDir
and since they don't have the usual X-SnapInstanceName for other snaps
errors were showing in the logs.

This just skips those files when looking up installed desktop files.

Signed-off-by: Zeyad Gouda <[email protected]>
  • Loading branch information
ZeyadYasser authored Sep 23, 2024
1 parent dc801d0 commit 4941634
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
9 changes: 9 additions & 0 deletions wrappers/core18.go
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,15 @@ var snapdDesktopFileNames = []string{
"snap-handle-link.desktop",
}

func isSnapdDesktopFile(desktopFile string) bool {
for _, df := range snapdDesktopFileNames {
if desktopFile == df {
return true
}
}
return false
}

func writeSnapdDesktopFilesOnCore(s *snap.Info) error {
// Ensure /var/lib/snapd/desktop/applications exists
if err := os.MkdirAll(dirs.SnapDesktopFilesDir, 0755); err != nil {
Expand Down
12 changes: 9 additions & 3 deletions wrappers/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,29 @@ func deriveDesktopFilesContent(s *snap.Info) (map[string]osutil.FileState, error
// forAllDesktopFiles loops over all installed desktop files under
// dirs.SnapDesktopFilesDir.
//
// Only the desktop file base and parsed instance name is passed the callback
// function.
// Only the desktop file base and parsed instance name are passed to the
// callback function.
func forAllDesktopFiles(cb func(base, instanceName string) error) error {
installedDesktopFiles, err := findDesktopFiles(dirs.SnapDesktopFilesDir)
if err != nil {
return err
}

for _, desktopFile := range installedDesktopFiles {
base := filepath.Base(desktopFile)
if isSnapdDesktopFile(base) {
// skip snapd desktop files installed on core, they don't
// have the usual X-SnapInstanceName entry.
continue
}

de, err := desktopentry.Read(desktopFile)
if err != nil || de.SnapInstanceName == "" {
// cannot read instance name from desktop file, ignore
logger.Noticef("cannot read instance name: %s", err)
continue
}

base := filepath.Base(desktopFile)
if err := cb(base, de.SnapInstanceName); err != nil {
return err
}
Expand Down
26 changes: 26 additions & 0 deletions wrappers/desktop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -835,3 +835,29 @@ func (s *desktopSuite) TestAddRemoveDesktopFiles(c *C) {
c.Assert(osutil.FileExists(expectedDesktopFilePath), Equals, false)
}
}

func (s *desktopSuite) TestForAllDesktopFilesSkipsSnapdDesktopFiles(c *C) {
c.Assert(os.MkdirAll(dirs.SnapDesktopFilesDir, 0755), IsNil)

var mockDesktopFile = []byte(`
[Desktop Entry]
Name=foo
X-SnapInstanceName=foo`)

desktopFiles := wrappers.SnapdDesktopFileNames
desktopFiles = append(desktopFiles, "foo_foo.desktop", "foo_bar.desktop")
for _, df := range desktopFiles {
c.Assert(os.WriteFile(filepath.Join(dirs.SnapDesktopFilesDir, df), mockDesktopFile, 0644), IsNil)
}

found := make(map[string]string, 2)
err := wrappers.ForAllDesktopFiles(func(base, instanceName string) error {
found[base] = instanceName
return nil
})
c.Assert(err, IsNil)

c.Check(found, HasLen, 2)
c.Check(found["foo_foo.desktop"], Equals, "foo")
c.Check(found["foo_bar.desktop"], Equals, "foo")
}
2 changes: 2 additions & 0 deletions wrappers/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (
DetectAppAndRewriteExecLine = detectAppAndRewriteExecLine
RewriteIconLine = rewriteIconLine
IsValidDesktopFileLine = isValidDesktopFileLine
ForAllDesktopFiles = forAllDesktopFiles
SnapdDesktopFileNames = snapdDesktopFileNames

// daemons
NewUserServiceClientNames = newUserServiceClientNames
Expand Down

0 comments on commit 4941634

Please sign in to comment.