forked from Silverfish/proton-bridge
GODT-1916: Use XDG_DATA_HOME to store persistent data on linux
This change ensures persistent data is stored in XDG_DATA_HOME instead of XDG_CACHE_HOME on linux. It adds the UserData() method on locations.Provider to return a path suitable for storing persistent data. On linux, this returns $XDG_DATA_HOME/protonmail (likely ~/.local/share/protonmail), and on non-linux this returns os.UserConfigDir() because that is assumed to be a more persistent location than os.UserCacheDir(). locations.Locations has been modified to use this new data directory; gluon and logs are now stored here.
This commit is contained in:
@ -18,19 +18,22 @@
|
||||
package locations
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// Provider provides standard locations.
|
||||
type Provider interface {
|
||||
UserConfig() string
|
||||
UserData() string
|
||||
UserCache() string
|
||||
}
|
||||
|
||||
// DefaultProvider is a locations provider using the system-default storage locations.
|
||||
type DefaultProvider struct {
|
||||
config, cache string
|
||||
config, data, cache string
|
||||
}
|
||||
|
||||
func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
||||
@ -39,6 +42,11 @@ func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := userDataDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cache, err := os.UserCacheDir()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@ -46,14 +54,44 @@ func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
||||
|
||||
return &DefaultProvider{
|
||||
config: filepath.Join(config, name),
|
||||
data: filepath.Join(data, name),
|
||||
cache: filepath.Join(cache, name),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UserConfig returns a directory that can be used to store user-specific configuration.
|
||||
// $XDG_CONFIG_HOME/protonmail is used on Unix systems; similar on others.
|
||||
func (p *DefaultProvider) UserConfig() string {
|
||||
return p.config
|
||||
}
|
||||
|
||||
// UserData returns a directory that can be used to store user-specific data.
|
||||
// $XDG_DATA_HOME/protonmail is used on Unix systems; similar on others.
|
||||
func (p *DefaultProvider) UserData() string {
|
||||
return p.data
|
||||
}
|
||||
|
||||
// UserCache returns a directory that can be used to store user-specific non-essential data.
|
||||
// $XDG_CACHE_HOME/protonmail is used on Unix systems; similar on others.
|
||||
func (p *DefaultProvider) UserCache() string {
|
||||
return p.cache
|
||||
}
|
||||
|
||||
// userDataDir returns a directory that can be used to store user-specific data.
|
||||
// This is necessary because os.UserDataDir() is not implemented by the Go standard library, sadly.
|
||||
// On non-linux systems, it is the same as os.UserConfigDir().
|
||||
func userDataDir() (string, error) {
|
||||
if runtime.GOOS != "linux" {
|
||||
return os.UserConfigDir()
|
||||
}
|
||||
|
||||
if dir := os.Getenv("XDG_DATA_HOME"); dir != "" {
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
if dir := os.Getenv("HOME"); dir != "" {
|
||||
return filepath.Join(dir, ".local", "share"), nil
|
||||
}
|
||||
|
||||
return "", errors.New("neither $XDG_DATA_HOME nor $HOME are defined")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user