github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/wrappers/icons.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 wrappers 21 22 import ( 23 "fmt" 24 "os" 25 "path/filepath" 26 "strings" 27 28 "github.com/snapcore/snapd/dirs" 29 "github.com/snapcore/snapd/osutil" 30 "github.com/snapcore/snapd/snap" 31 ) 32 33 func findIconFiles(snapName string, rootDir string) (icons []string, err error) { 34 if !osutil.IsDirectory(rootDir) { 35 return nil, nil 36 } 37 iconGlob := fmt.Sprintf("snap.%s.*", snapName) 38 forbiddenDirGlob := "snap.*" 39 err = filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error { 40 if err != nil { 41 return err 42 } 43 rel, err := filepath.Rel(rootDir, path) 44 if err != nil { 45 return err 46 } 47 base := filepath.Base(path) 48 if info.IsDir() { 49 // Ignore directories that could match an icon glob 50 if ok, err := filepath.Match(forbiddenDirGlob, base); ok || err != nil { 51 return filepath.SkipDir 52 } 53 } else { 54 if ok, err := filepath.Match(iconGlob, base); err != nil { 55 return err 56 } else if ok { 57 ext := filepath.Ext(base) 58 if ext == ".png" || ext == ".svg" { 59 icons = append(icons, rel) 60 } 61 } 62 } 63 return nil 64 }) 65 return icons, err 66 } 67 68 func deriveIconContent(instanceName string, rootDir string, icons []string) (content map[string]map[string]osutil.FileState, err error) { 69 content = make(map[string]map[string]osutil.FileState) 70 snapPrefix := fmt.Sprintf("snap.%s.", snap.InstanceSnap(instanceName)) 71 instancePrefix := fmt.Sprintf("snap.%s.", instanceName) 72 73 for _, iconFile := range icons { 74 base := filepath.Base(iconFile) 75 if !strings.HasPrefix(base, snapPrefix) { 76 return nil, fmt.Errorf("cannot use icon file %q: must start with snap prefix %q", iconFile, snapPrefix) 77 } 78 dir := filepath.Dir(iconFile) 79 dirContent := content[dir] 80 if dirContent == nil { 81 dirContent = make(map[string]osutil.FileState) 82 content[dir] = dirContent 83 } 84 // rename icons to match snap instance name 85 base = instancePrefix + base[len(snapPrefix):] 86 dirContent[base] = &osutil.FileReferencePlusMode{ 87 FileReference: osutil.FileReference{Path: filepath.Join(rootDir, iconFile)}, 88 Mode: 0644, 89 } 90 } 91 return content, nil 92 } 93 94 func AddSnapIcons(s *snap.Info) error { 95 if err := os.MkdirAll(dirs.SnapDesktopIconsDir, 0755); err != nil { 96 return err 97 } 98 99 rootDir := filepath.Join(s.MountDir(), "meta", "gui", "icons") 100 icons, err := findIconFiles(s.SnapName(), rootDir) 101 if err != nil { 102 return err 103 } 104 105 content, err := deriveIconContent(s.InstanceName(), rootDir, icons) 106 if err != nil { 107 return err 108 } 109 iconGlob := fmt.Sprintf("snap.%s.*", s.InstanceName()) 110 _, _, err = osutil.EnsureTreeState(dirs.SnapDesktopIconsDir, []string{iconGlob}, content) 111 return err 112 } 113 114 func RemoveSnapIcons(s *snap.Info) error { 115 if !osutil.IsDirectory(dirs.SnapDesktopIconsDir) { 116 return nil 117 } 118 iconGlob := fmt.Sprintf("snap.%s.*", s.InstanceName()) 119 _, _, err := osutil.EnsureTreeState(dirs.SnapDesktopIconsDir, []string{iconGlob}, nil) 120 return err 121 }