github.com/advanderveer/restic@v0.8.1-0.20171209104529-42a8c19aaea6/internal/cache/dir.go (about)

     1  package cache
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"runtime"
     7  
     8  	"github.com/pkg/errors"
     9  	"github.com/restic/restic/internal/debug"
    10  	"github.com/restic/restic/internal/fs"
    11  )
    12  
    13  // xdgCacheDir returns the cache directory according to XDG basedir spec, see
    14  // http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
    15  func xdgCacheDir() (string, error) {
    16  	xdgcache := os.Getenv("XDG_CACHE_HOME")
    17  	home := os.Getenv("HOME")
    18  
    19  	if xdgcache != "" {
    20  		return filepath.Join(xdgcache, "restic"), nil
    21  	} else if home != "" {
    22  		return filepath.Join(home, ".cache", "restic"), nil
    23  	}
    24  
    25  	return "", errors.New("unable to locate cache directory (XDG_CACHE_HOME and HOME unset)")
    26  }
    27  
    28  // windowsCacheDir returns the cache directory for Windows.
    29  //
    30  // Uses LOCALAPPDATA, where application data not synchronized between machines
    31  // is stored. (Browser caches stored here).
    32  func windowsCacheDir() (string, error) {
    33  	appdata := os.Getenv("LOCALAPPDATA")
    34  	if appdata == "" {
    35  		return "", errors.New("unable to locate cache directory (APPDATA unset)")
    36  	}
    37  	return filepath.Join(appdata, "restic"), nil
    38  }
    39  
    40  // darwinCacheDir returns the cache directory for darwin.
    41  //
    42  // Uses ~/Library/Caches/, which is recommended by Apple, see
    43  // https://developer.apple.com/library/content/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
    44  func darwinCacheDir() (string, error) {
    45  	home := os.Getenv("HOME")
    46  	if home == "" {
    47  		return "", errors.New("unable to locate cache directory (HOME unset)")
    48  	}
    49  	return filepath.Join(home, "Library", "Caches", "restic"), nil
    50  }
    51  
    52  // DefaultDir returns the default cache directory for the current OS.
    53  func DefaultDir() (cachedir string, err error) {
    54  	switch runtime.GOOS {
    55  	case "darwin":
    56  		cachedir, err = darwinCacheDir()
    57  	case "windows":
    58  		cachedir, err = windowsCacheDir()
    59  	default:
    60  		// Default to XDG for Linux and any other OSes.
    61  		cachedir, err = xdgCacheDir()
    62  	}
    63  
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  
    68  	return cachedir, nil
    69  }
    70  
    71  func mkdirCacheDir(cachedir string) error {
    72  	fi, err := fs.Stat(cachedir)
    73  	if os.IsNotExist(errors.Cause(err)) {
    74  		err = fs.MkdirAll(cachedir, 0700)
    75  		if err != nil {
    76  			return errors.Wrap(err, "MkdirAll")
    77  		}
    78  
    79  		fi, err = fs.Stat(cachedir)
    80  		debug.Log("create cache dir %v", cachedir)
    81  	}
    82  
    83  	if err != nil {
    84  		return errors.Wrap(err, "Stat")
    85  	}
    86  
    87  	if !fi.IsDir() {
    88  		return errors.Errorf("cache dir %v is not a directory", cachedir)
    89  	}
    90  
    91  	return nil
    92  }