Add support for finding app icons on Linux (MVP)

This commit is contained in:
Daniel 2019-11-07 16:49:02 +01:00
parent af712382f8
commit c999d5559a
11 changed files with 276 additions and 17 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/safing/portbase/api" "github.com/safing/portbase/api"
"github.com/safing/portbase/formats/dsd" "github.com/safing/portbase/formats/dsd"
"github.com/safing/portbase/utils" "github.com/safing/portbase/utils"
"github.com/safing/portmaster/profile/icons"
) )
func registerAPIEndpoints() error { func registerAPIEndpoints() error {
@ -98,7 +99,7 @@ func handleGetProfileIcon(ar *api.Request) (data []byte, err error) {
ext := filepath.Ext(name) ext := filepath.Ext(name)
// Get profile icon. // Get profile icon.
data, err = GetProfileIcon(name) data, err = icons.GetProfileIcon(name)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -152,7 +153,7 @@ func handleUpdateProfileIcon(ar *api.Request) (any, error) {
} }
// Update profile icon. // Update profile icon.
filename, err := UpdateProfileIcon(ar.InputData, ext) filename, err := icons.UpdateProfileIcon(ar.InputData, ext)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -1,6 +1,7 @@
package profile package profile
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"path" "path"
@ -146,7 +147,9 @@ func GetLocalProfile(id string, md MatchingData, createProfileCallback func() *P
// Trigger further metadata fetching from system if profile was created. // Trigger further metadata fetching from system if profile was created.
if created && profile.UsePresentationPath && !special { if created && profile.UsePresentationPath && !special {
module.StartWorker("get profile metadata", profile.updateMetadataFromSystem) module.StartWorker("get profile metadata", func(ctx context.Context) error {
return profile.updateMetadataFromSystem(ctx, md)
})
} }
// Prepare profile for first use. // Prepare profile for first use.

View file

@ -0,0 +1,10 @@
//go:build !linux
package icons
import "github.com/safing/portmaster/profile"
// FindIcon returns nil, nil for unsupported platforms.
func FindIcon(binName string, homeDir string) (*profile.Icon, error) {
return nil, nil
}

112
profile/icons/find_linux.go Normal file
View file

@ -0,0 +1,112 @@
package icons
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
// FindIcon finds an icon for the given binary name.
// Providing the home directory of the user running the process of that binary can help find an icon.
func FindIcon(ctx context.Context, binName string, homeDir string) (*Icon, error) {
// Search for icon.
iconPath, err := search(binName, homeDir)
if iconPath == "" {
if err != nil {
return nil, fmt.Errorf("failed to find icon for %s: %w", binName, err)
}
return nil, nil
}
// Load icon and save it.
data, err := os.ReadFile(iconPath)
if err != nil {
return nil, fmt.Errorf("failed to read icon %s: %w", iconPath, err)
}
filename, err := UpdateProfileIcon(data, filepath.Ext(iconPath))
if err != nil {
return nil, fmt.Errorf("failed to import icon %s: %w", iconPath, err)
}
return &Icon{
Type: IconTypeAPI,
Value: filename,
}, nil
}
func search(binName string, homeDir string) (iconPath string, err error) {
binName = strings.ToLower(binName)
// Search for icon path.
for _, iconLoc := range iconLocations {
basePath := iconLoc.GetPath(binName, homeDir)
if basePath == "" {
continue
}
switch iconLoc.Type {
case FlatDir:
iconPath, err = searchDirectory(basePath, binName)
case XDGIcons:
iconPath, err = searchXDGIconStructure(basePath, binName)
}
if iconPath != "" {
return
}
}
return
}
func searchXDGIconStructure(baseDirectory string, binName string) (iconPath string, err error) {
for _, xdgIconDir := range xdgIconPaths {
directory := filepath.Join(baseDirectory, xdgIconDir)
iconPath, err = searchDirectory(directory, binName)
if iconPath != "" {
return
}
}
return
}
func searchDirectory(directory string, binName string) (iconPath string, err error) {
entries, err := os.ReadDir(directory)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", nil
}
return "", fmt.Errorf("failed to read directory %s: %w", directory, err)
}
fmt.Println(directory)
var (
bestMatch string
bestMatchExcessChars int
)
for _, entry := range entries {
// Skip dirs.
if entry.IsDir() {
continue
}
iconName := strings.ToLower(entry.Name())
iconName = strings.TrimSuffix(iconName, filepath.Ext(iconName))
switch {
case len(iconName) < len(binName):
// Continue to next.
case iconName == binName:
// Exact match, return immediately.
return filepath.Join(directory, entry.Name()), nil
case strings.HasPrefix(iconName, binName):
excessChars := len(iconName) - len(binName)
if bestMatch == "" || excessChars < bestMatchExcessChars {
bestMatch = entry.Name()
bestMatchExcessChars = excessChars
}
}
}
return bestMatch, nil
}

View file

@ -0,0 +1,32 @@
package icons
import (
"os"
"testing"
)
func TestFindIcon(t *testing.T) {
if testing.Short() {
t.Skip("test depends on linux desktop environment")
}
t.Parallel()
home := os.Getenv("HOME")
testFindIcon(t, "evolution", home)
testFindIcon(t, "nextcloud", home)
}
func testFindIcon(t *testing.T, binName string, homeDir string) {
t.Helper()
iconPath, err := search(binName, homeDir)
if err != nil {
t.Error(err)
return
}
if iconPath == "" {
t.Errorf("no icon found for %s", binName)
return
}
t.Logf("icon for %s found: %s", binName, iconPath)
}

View file

@ -1,4 +1,4 @@
package profile package icons
import ( import (
"errors" "errors"
@ -42,7 +42,8 @@ func (t IconType) sortOrder() int {
} }
} }
func sortAndCompactIcons(icons []Icon) []Icon { // SortAndCompact sorts and compacts a list of icons.
func SortAndCompact(icons []Icon) []Icon {
// Sort. // Sort.
slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int { slices.SortFunc[[]Icon, Icon](icons, func(a, b Icon) int {
aOrder := a.Type.sortOrder() aOrder := a.Type.sortOrder()

View file

@ -1,4 +1,4 @@
package profile package icons
import ( import (
"crypto" "crypto"
@ -13,18 +13,21 @@ import (
"github.com/safing/portbase/api" "github.com/safing/portbase/api"
) )
var profileIconStoragePath = "" // ProfileIconStoragePath defines the location where profile icons are stored.
// Must be set before anything else from this package is called.
// Must not be changed once set.
var ProfileIconStoragePath = ""
// GetProfileIcon returns the profile icon with the given ID and extension. // GetProfileIcon returns the profile icon with the given ID and extension.
func GetProfileIcon(name string) (data []byte, err error) { func GetProfileIcon(name string) (data []byte, err error) {
// Check if enabled. // Check if enabled.
if profileIconStoragePath == "" { if ProfileIconStoragePath == "" {
return nil, errors.New("api icon storage not configured") return nil, errors.New("api icon storage not configured")
} }
// Build storage path. // Build storage path.
iconPath := filepath.Clean( iconPath := filepath.Clean(
filepath.Join(profileIconStoragePath, name), filepath.Join(ProfileIconStoragePath, name),
) )
iconPath, err = filepath.Abs(iconPath) iconPath, err = filepath.Abs(iconPath)
@ -34,7 +37,7 @@ func GetProfileIcon(name string) (data []byte, err error) {
// Do a quick check if we are still within the right directory. // Do a quick check if we are still within the right directory.
// This check is not entirely correct, but is sufficient for this use case. // This check is not entirely correct, but is sufficient for this use case.
if filepath.Dir(iconPath) != profileIconStoragePath { if filepath.Dir(iconPath) != ProfileIconStoragePath {
return nil, api.ErrorWithStatus(errors.New("invalid icon"), http.StatusBadRequest) return nil, api.ErrorWithStatus(errors.New("invalid icon"), http.StatusBadRequest)
} }
@ -72,7 +75,7 @@ func UpdateProfileIcon(data []byte, ext string) (filename string, err error) {
// Save to disk. // Save to disk.
filename = sum + "." + ext filename = sum + "." + ext
return filename, os.WriteFile(filepath.Join(profileIconStoragePath, filename), data, 0o0644) //nolint:gosec return filename, os.WriteFile(filepath.Join(ProfileIconStoragePath, filename), data, 0o0644) //nolint:gosec
} }
// TODO: Clean up icons regularly. // TODO: Clean up icons regularly.

View file

@ -0,0 +1,68 @@
package icons
import (
"fmt"
)
// IconLocation describes an icon location.
type IconLocation struct {
Directory string
Type IconLocationType
PathArg PathArg
}
// IconLocationType describes an icon location type.
type IconLocationType uint8
// Icon Location Types.
const (
FlatDir IconLocationType = iota
XDGIcons
)
// PathArg describes an icon location path argument.
type PathArg uint8
// Path Args.
const (
NoPathArg PathArg = iota
Home
BinName
)
var (
iconLocations = []IconLocation{
{Directory: "/usr/share/pixmaps", Type: FlatDir},
{Directory: "/usr/share", Type: XDGIcons},
{Directory: "%s/.local/share", Type: XDGIcons, PathArg: Home},
{Directory: "%s/.local/share/flatpak/exports/share", Type: XDGIcons, PathArg: Home},
{Directory: "/usr/share/%s", Type: XDGIcons, PathArg: BinName},
}
xdgIconPaths = []string{
// UI currently uses 48x48, so 256x256 should suffice for the future, even at 2x. (12.2023)
"icons/hicolor/256x256/apps",
"icons/hicolor/192x192/apps",
"icons/hicolor/128x128/apps",
"icons/hicolor/96x96/apps",
"icons/hicolor/72x72/apps",
"icons/hicolor/64x64/apps",
"icons/hicolor/48x48/apps",
"icons/hicolor/512x512/apps",
}
)
// GetPath returns the path of an icon.
func (il IconLocation) GetPath(binName string, homeDir string) string {
switch il.PathArg {
case NoPathArg:
return il.Directory
case Home:
if homeDir != "" {
return fmt.Sprintf(il.Directory, homeDir)
}
case BinName:
return fmt.Sprintf(il.Directory, binName)
}
return ""
}

View file

@ -7,6 +7,7 @@ import (
"time" "time"
"github.com/safing/portbase/database/record" "github.com/safing/portbase/database/record"
"github.com/safing/portmaster/profile/icons"
) )
// MergeProfiles merges multiple profiles into a new one. // MergeProfiles merges multiple profiles into a new one.
@ -52,12 +53,12 @@ func MergeProfiles(name string, primary *Profile, secondaries ...*Profile) (newP
} }
// Collect all icons. // Collect all icons.
newProfile.Icons = make([]Icon, 0, len(secondaries)+1) // Guess the needed space. newProfile.Icons = make([]icons.Icon, 0, len(secondaries)+1) // Guess the needed space.
newProfile.Icons = append(newProfile.Icons, primary.Icons...) newProfile.Icons = append(newProfile.Icons, primary.Icons...)
for _, sp := range secondaries { for _, sp := range secondaries {
newProfile.Icons = append(newProfile.Icons, sp.Icons...) newProfile.Icons = append(newProfile.Icons, sp.Icons...)
} }
newProfile.Icons = sortAndCompactIcons(newProfile.Icons) newProfile.Icons = icons.SortAndCompact(newProfile.Icons)
// Collect all fingerprints. // Collect all fingerprints.
newProfile.Fingerprints = make([]Fingerprint, 0, len(primary.Fingerprints)+len(secondaries)) // Guess the needed space. newProfile.Fingerprints = make([]Fingerprint, 0, len(primary.Fingerprints)+len(secondaries)) // Guess the needed space.

View file

@ -11,6 +11,7 @@ import (
"github.com/safing/portbase/log" "github.com/safing/portbase/log"
"github.com/safing/portbase/modules" "github.com/safing/portbase/modules"
_ "github.com/safing/portmaster/core/base" _ "github.com/safing/portmaster/core/base"
"github.com/safing/portmaster/profile/icons"
"github.com/safing/portmaster/updates" "github.com/safing/portmaster/updates"
) )
@ -52,7 +53,7 @@ func prep() error {
if err := iconsDir.Ensure(); err != nil { if err := iconsDir.Ensure(); err != nil {
return fmt.Errorf("failed to create/check icons directory: %w", err) return fmt.Errorf("failed to create/check icons directory: %w", err)
} }
profileIconStoragePath = iconsDir.Path icons.ProfileIconStoragePath = iconsDir.Path
return nil return nil
} }

View file

@ -18,6 +18,7 @@ import (
"github.com/safing/portbase/utils/osdetail" "github.com/safing/portbase/utils/osdetail"
"github.com/safing/portmaster/intel/filterlists" "github.com/safing/portmaster/intel/filterlists"
"github.com/safing/portmaster/profile/endpoints" "github.com/safing/portmaster/profile/endpoints"
"github.com/safing/portmaster/profile/icons"
) )
// ProfileSource is the source of the profile. // ProfileSource is the source of the profile.
@ -68,9 +69,9 @@ type Profile struct { //nolint:maligned // not worth the effort
// See IconType for more information. // See IconType for more information.
Icon string Icon string
// Deprecated: IconType describes the type of the Icon property. // Deprecated: IconType describes the type of the Icon property.
IconType IconType IconType icons.IconType
// Icons holds a list of icons to represent the application. // Icons holds a list of icons to represent the application.
Icons []Icon Icons []icons.Icon
// Deprecated: LinkedPath used to point to the executableis this // Deprecated: LinkedPath used to point to the executableis this
// profile was created for. // profile was created for.
@ -505,7 +506,7 @@ func (profile *Profile) updateMetadata(binaryPath string) (changed bool) {
// updateMetadataFromSystem updates the profile metadata with data from the // updateMetadataFromSystem updates the profile metadata with data from the
// operating system and saves it afterwards. // operating system and saves it afterwards.
func (profile *Profile) updateMetadataFromSystem(ctx context.Context) error { func (profile *Profile) updateMetadataFromSystem(ctx context.Context, md MatchingData) error {
var changed bool var changed bool
// This function is only valid for local profiles. // This function is only valid for local profiles.
@ -531,6 +532,22 @@ func (profile *Profile) updateMetadataFromSystem(ctx context.Context) error {
return nil return nil
} }
// Get icon if path matches presentation path.
var newIcon *icons.Icon
if profile.PresentationPath == md.Path() {
// Get home from ENV.
var home string
if env := md.Env(); env != nil {
home = env["HOME"]
}
var err error
newIcon, err = icons.FindIcon(ctx, profile.PresentationPath, home)
if err != nil {
log.Warningf("profile: failed to find icon for %s: %s", profile.PresentationPath, err)
newIcon = nil
}
}
// Apply new data to profile. // Apply new data to profile.
func() { func() {
// Lock profile for applying metadata. // Lock profile for applying metadata.
@ -542,6 +559,16 @@ func (profile *Profile) updateMetadataFromSystem(ctx context.Context) error {
profile.Name = newName profile.Name = newName
changed = true changed = true
} }
// Apply new icon if found.
if newIcon != nil {
if len(profile.Icons) == 0 {
profile.Icons = []icons.Icon{*newIcon}
} else {
profile.Icons = append(profile.Icons, *newIcon)
profile.Icons = icons.SortAndCompact(profile.Icons)
}
}
}() }()
// If anything changed, save the profile. // If anything changed, save the profile.