mirror of
https://github.com/octoleo/restic.git
synced 2024-11-22 21:05:10 +00:00
cache: OS-specific cache directories
Windows, and to a lesser extent OS X, don't conform to XDG and have their own preferred locations for caches. On Windows, use %LOCALAPPDATA%/restic (i.e., ~/AppData/Local/restic). I can't find authoritative documentation from Microsoft recommending specifically which of %APPDATA%, %LOCALAPPDATA%, and %TEMP% should be used for caches, but %LOCALAPPDATA% is where browsers store their caches, so it seems like a good fit. On OS X, use ~/Library/Caches/restic, which is recommended by the Apple documentation. They do suggest using the application "bundle identifier" as the base folder name, but restic doesn't have one, so I just used "restic".
This commit is contained in:
parent
1ee1559506
commit
2133869127
2
internal/cache/cache.go
vendored
2
internal/cache/cache.go
vendored
@ -84,7 +84,7 @@ func writeCachedirTag(dir string) error {
|
|||||||
// performReadahead returns true.
|
// performReadahead returns true.
|
||||||
func New(id string, basedir string) (c *Cache, err error) {
|
func New(id string, basedir string) (c *Cache, err error) {
|
||||||
if basedir == "" {
|
if basedir == "" {
|
||||||
basedir, err = getXDGCacheDir()
|
basedir, err = defaultCacheDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
59
internal/cache/dir.go
vendored
59
internal/cache/dir.go
vendored
@ -3,27 +3,68 @@ package cache
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/fs"
|
"github.com/restic/restic/internal/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// getXDGCacheDir returns the cache directory according to XDG basedir spec, see
|
// xdgCacheDir returns the cache directory according to XDG basedir spec, see
|
||||||
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
||||||
func getXDGCacheDir() (string, error) {
|
func xdgCacheDir() (string, error) {
|
||||||
xdgcache := os.Getenv("XDG_CACHE_HOME")
|
xdgcache := os.Getenv("XDG_CACHE_HOME")
|
||||||
home := os.Getenv("HOME")
|
home := os.Getenv("HOME")
|
||||||
|
|
||||||
if xdgcache == "" && home == "" {
|
if xdgcache != "" {
|
||||||
return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)")
|
return filepath.Join(xdgcache, "restic"), nil
|
||||||
|
} else if home != "" {
|
||||||
|
return filepath.Join(home, ".cache", "restic"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
cachedir := ""
|
return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)")
|
||||||
if xdgcache != "" {
|
}
|
||||||
cachedir = filepath.Join(xdgcache, "restic")
|
|
||||||
} else if home != "" {
|
// windowsCacheDir returns the cache directory for Windows.
|
||||||
cachedir = filepath.Join(home, ".cache", "restic")
|
//
|
||||||
|
// Uses LOCALAPPDATA, where application data not synchronized between machines
|
||||||
|
// is stored. (Browser caches stored here).
|
||||||
|
func windowsCacheDir() (string, error) {
|
||||||
|
appdata := os.Getenv("LOCALAPPDATA")
|
||||||
|
if appdata == "" {
|
||||||
|
return "", errors.New("unable to locate cache directory (APPDATA unset)")
|
||||||
|
}
|
||||||
|
return filepath.Join(appdata, "restic"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// darwinCacheDir returns the cache directory for darwin.
|
||||||
|
//
|
||||||
|
// Uses ~/Library/Caches/, which is recommended by Apple, see
|
||||||
|
// https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
|
||||||
|
func darwinCacheDir() (string, error) {
|
||||||
|
home := os.Getenv("HOME")
|
||||||
|
if home == "" {
|
||||||
|
return "", errors.New("unable to locate cache directory (HOME unset)")
|
||||||
|
}
|
||||||
|
return filepath.Join(home, "Library", "Caches", "restic"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultCacheDir determines and creates the default cache directory for this
|
||||||
|
// system.
|
||||||
|
func defaultCacheDir() (string, error) {
|
||||||
|
var cachedir string
|
||||||
|
var err error
|
||||||
|
switch runtime.GOOS {
|
||||||
|
case "darwin":
|
||||||
|
cachedir, err = darwinCacheDir()
|
||||||
|
case "windows":
|
||||||
|
cachedir, err = windowsCacheDir()
|
||||||
|
default:
|
||||||
|
// Default to XDG for Linux and any other OSes.
|
||||||
|
cachedir, err = xdgCacheDir()
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
fi, err := fs.Stat(cachedir)
|
fi, err := fs.Stat(cachedir)
|
||||||
|
Loading…
Reference in New Issue
Block a user