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:
@ -63,7 +63,7 @@ func (j *TestCookieJar) Cookies(u *url.URL) []*http.Cookie {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TestLocationsProvider struct {
|
type TestLocationsProvider struct {
|
||||||
config, cache string
|
config, data, cache string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTestLocationsProvider(dir string) *TestLocationsProvider {
|
func NewTestLocationsProvider(dir string) *TestLocationsProvider {
|
||||||
@ -72,6 +72,11 @@ func NewTestLocationsProvider(dir string) *TestLocationsProvider {
|
|||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data, err := os.MkdirTemp(dir, "data")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
cache, err := os.MkdirTemp(dir, "cache")
|
cache, err := os.MkdirTemp(dir, "cache")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
@ -79,6 +84,7 @@ func NewTestLocationsProvider(dir string) *TestLocationsProvider {
|
|||||||
|
|
||||||
return &TestLocationsProvider{
|
return &TestLocationsProvider{
|
||||||
config: config,
|
config: config,
|
||||||
|
data: data,
|
||||||
cache: cache,
|
cache: cache,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -87,6 +93,10 @@ func (provider *TestLocationsProvider) UserConfig() string {
|
|||||||
return provider.config
|
return provider.config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (provider *TestLocationsProvider) UserData() string {
|
||||||
|
return provider.data
|
||||||
|
}
|
||||||
|
|
||||||
func (provider *TestLocationsProvider) UserCache() string {
|
func (provider *TestLocationsProvider) UserCache() string {
|
||||||
return provider.cache
|
return provider.cache
|
||||||
}
|
}
|
||||||
|
|||||||
@ -36,16 +36,26 @@ import (
|
|||||||
// - updates: ~/.config/protonmail/<app>/updates
|
// - updates: ~/.config/protonmail/<app>/updates
|
||||||
// - lockfile: ~/.cache/protonmail/<app>/<app>.lock .
|
// - lockfile: ~/.cache/protonmail/<app>/<app>.lock .
|
||||||
type Locations struct {
|
type Locations struct {
|
||||||
userConfig, userCache string
|
// userConfig is the path to the user config directory, for storing persistent config data.
|
||||||
configName string
|
userConfig string
|
||||||
configGuiName string
|
|
||||||
|
// userData is the path to the user data directory, for storing persistent data.
|
||||||
|
userData string
|
||||||
|
|
||||||
|
// userCache is the path to the user cache directory, for storing non-essential data.
|
||||||
|
userCache string
|
||||||
|
|
||||||
|
configName string
|
||||||
|
configGuiName string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new locations object.
|
// New returns a new locations object.
|
||||||
func New(provider Provider, configName string) *Locations {
|
func New(provider Provider, configName string) *Locations {
|
||||||
return &Locations{
|
return &Locations{
|
||||||
userConfig: provider.UserConfig(),
|
userConfig: provider.UserConfig(),
|
||||||
userCache: provider.UserCache(),
|
userData: provider.UserData(),
|
||||||
|
userCache: provider.UserCache(),
|
||||||
|
|
||||||
configName: configName,
|
configName: configName,
|
||||||
configGuiName: configName + "-gui",
|
configGuiName: configName + "-gui",
|
||||||
}
|
}
|
||||||
@ -178,7 +188,7 @@ func (l *Locations) GetOldUpdatesPath() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Locations) getGluonPath() string {
|
func (l *Locations) getGluonPath() string {
|
||||||
return filepath.Join(l.userCache, "gluon")
|
return filepath.Join(l.userData, "gluon")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Locations) getGUICertPath() string {
|
func (l *Locations) getGUICertPath() string {
|
||||||
@ -190,7 +200,7 @@ func (l *Locations) getSettingsPath() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (l *Locations) getLogsPath() string {
|
func (l *Locations) getLogsPath() string {
|
||||||
return filepath.Join(l.userCache, "logs")
|
return filepath.Join(l.userData, "logs")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Locations) getGoIMAPCachePath() string {
|
func (l *Locations) getGoIMAPCachePath() string {
|
||||||
@ -215,6 +225,7 @@ func (l *Locations) getUpdatesPath() string {
|
|||||||
func (l *Locations) Clear() error {
|
func (l *Locations) Clear() error {
|
||||||
return files.Remove(
|
return files.Remove(
|
||||||
l.userConfig,
|
l.userConfig,
|
||||||
|
l.userData,
|
||||||
l.userCache,
|
l.userCache,
|
||||||
).Except(
|
).Except(
|
||||||
l.GetGuiLockFile(),
|
l.GetGuiLockFile(),
|
||||||
@ -232,7 +243,10 @@ func (l *Locations) ClearUpdates() error {
|
|||||||
// Clean removes any unexpected files from the app cache folder
|
// Clean removes any unexpected files from the app cache folder
|
||||||
// while leaving files in the standard locations untouched.
|
// while leaving files in the standard locations untouched.
|
||||||
func (l *Locations) Clean() error {
|
func (l *Locations) Clean() error {
|
||||||
return files.Remove(l.userCache).Except(
|
return files.Remove(
|
||||||
|
l.userCache,
|
||||||
|
l.userData,
|
||||||
|
).Except(
|
||||||
l.GetGuiLockFile(),
|
l.GetGuiLockFile(),
|
||||||
l.getLogsPath(),
|
l.getLogsPath(),
|
||||||
l.getUpdatesPath(),
|
l.getUpdatesPath(),
|
||||||
|
|||||||
@ -27,13 +27,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type fakeAppDirs struct {
|
type fakeAppDirs struct {
|
||||||
configDir, cacheDir string
|
configDir, dataDir, cacheDir string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dirs *fakeAppDirs) UserConfig() string {
|
func (dirs *fakeAppDirs) UserConfig() string {
|
||||||
return dirs.configDir
|
return dirs.configDir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (dirs *fakeAppDirs) UserData() string {
|
||||||
|
return dirs.dataDir
|
||||||
|
}
|
||||||
|
|
||||||
func (dirs *fakeAppDirs) UserCache() string {
|
func (dirs *fakeAppDirs) UserCache() string {
|
||||||
return dirs.cacheDir
|
return dirs.cacheDir
|
||||||
}
|
}
|
||||||
@ -134,6 +138,7 @@ func TestRemoveOldGoIMAPCacheFolders(t *testing.T) {
|
|||||||
func newFakeAppDirs(t *testing.T) *fakeAppDirs {
|
func newFakeAppDirs(t *testing.T) *fakeAppDirs {
|
||||||
return &fakeAppDirs{
|
return &fakeAppDirs{
|
||||||
configDir: t.TempDir(),
|
configDir: t.TempDir(),
|
||||||
|
dataDir: t.TempDir(),
|
||||||
cacheDir: t.TempDir(),
|
cacheDir: t.TempDir(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,19 +18,22 @@
|
|||||||
package locations
|
package locations
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Provider provides standard locations.
|
// Provider provides standard locations.
|
||||||
type Provider interface {
|
type Provider interface {
|
||||||
UserConfig() string
|
UserConfig() string
|
||||||
|
UserData() string
|
||||||
UserCache() string
|
UserCache() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// DefaultProvider is a locations provider using the system-default storage locations.
|
// DefaultProvider is a locations provider using the system-default storage locations.
|
||||||
type DefaultProvider struct {
|
type DefaultProvider struct {
|
||||||
config, cache string
|
config, data, cache string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
||||||
@ -39,6 +42,11 @@ func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data, err := userDataDir()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
cache, err := os.UserCacheDir()
|
cache, err := os.UserCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -46,14 +54,44 @@ func NewDefaultProvider(name string) (*DefaultProvider, error) {
|
|||||||
|
|
||||||
return &DefaultProvider{
|
return &DefaultProvider{
|
||||||
config: filepath.Join(config, name),
|
config: filepath.Join(config, name),
|
||||||
|
data: filepath.Join(data, name),
|
||||||
cache: filepath.Join(cache, name),
|
cache: filepath.Join(cache, name),
|
||||||
}, nil
|
}, 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 {
|
func (p *DefaultProvider) UserConfig() string {
|
||||||
return p.config
|
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 {
|
func (p *DefaultProvider) UserCache() string {
|
||||||
return p.cache
|
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