2017-06-10 11:10:08 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
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".
2017-09-28 04:16:22 +00:00
|
|
|
"runtime"
|
2017-06-10 11:10:08 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/restic/restic/internal/debug"
|
|
|
|
"github.com/restic/restic/internal/fs"
|
|
|
|
)
|
|
|
|
|
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".
2017-09-28 04:16:22 +00:00
|
|
|
// xdgCacheDir returns the cache directory according to XDG basedir spec, see
|
2017-06-10 11:10:08 +00:00
|
|
|
// http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
|
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".
2017-09-28 04:16:22 +00:00
|
|
|
func xdgCacheDir() (string, error) {
|
2017-06-10 11:10:08 +00:00
|
|
|
xdgcache := os.Getenv("XDG_CACHE_HOME")
|
|
|
|
home := os.Getenv("HOME")
|
|
|
|
|
|
|
|
if xdgcache != "" {
|
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".
2017-09-28 04:16:22 +00:00
|
|
|
return filepath.Join(xdgcache, "restic"), nil
|
2017-06-10 11:10:08 +00:00
|
|
|
} else if home != "" {
|
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".
2017-09-28 04:16:22 +00:00
|
|
|
return filepath.Join(home, ".cache", "restic"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)")
|
|
|
|
}
|
|
|
|
|
|
|
|
// windowsCacheDir returns the cache directory for Windows.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2017-11-20 20:58:38 +00:00
|
|
|
// DefaultDir returns the default cache directory for the current OS.
|
|
|
|
func DefaultDir() (cachedir string, err error) {
|
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".
2017-09-28 04:16:22 +00:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "darwin":
|
|
|
|
cachedir, err = darwinCacheDir()
|
|
|
|
case "windows":
|
|
|
|
cachedir, err = windowsCacheDir()
|
2017-11-20 05:11:18 +00:00
|
|
|
default:
|
|
|
|
// Default to XDG for Linux and any other OSes.
|
|
|
|
cachedir, err = xdgCacheDir()
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 05:11:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cachedir, nil
|
2017-11-20 20:58:38 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 20:03:47 +00:00
|
|
|
// mkdirCacheDir ensures that the cache directory exists. It it didn't, created
|
|
|
|
// is set to true.
|
|
|
|
func mkdirCacheDir(cachedir string) (created bool, err error) {
|
|
|
|
var newCacheDir bool
|
|
|
|
|
2017-06-10 11:10:08 +00:00
|
|
|
fi, err := fs.Stat(cachedir)
|
|
|
|
if os.IsNotExist(errors.Cause(err)) {
|
|
|
|
err = fs.MkdirAll(cachedir, 0700)
|
|
|
|
if err != nil {
|
2018-08-28 20:03:47 +00:00
|
|
|
return true, errors.Wrap(err, "MkdirAll")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fi, err = fs.Stat(cachedir)
|
|
|
|
debug.Log("create cache dir %v", cachedir)
|
2018-08-28 20:03:47 +00:00
|
|
|
|
|
|
|
newCacheDir = true
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-08-28 20:03:47 +00:00
|
|
|
return newCacheDir, errors.Wrap(err, "Stat")
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !fi.IsDir() {
|
2018-08-28 20:03:47 +00:00
|
|
|
return newCacheDir, errors.Errorf("cache dir %v is not a directory", cachedir)
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 20:03:47 +00:00
|
|
|
return newCacheDir, nil
|
2017-06-10 11:10:08 +00:00
|
|
|
}
|