MCPcopy
hub / github.com/kopia/kopia / NewStorageOrNil

Function NewStorageOrNil

internal/cache/cache_storage.go:40–65  ·  view source on GitHub ↗

NewStorageOrNil returns cache.Storage backed by the provided directory.

(ctx context.Context, cacheDir string, maxBytes int64, subdir string)

Source from the content-addressed store, hash-verified

38
39// NewStorageOrNil returns cache.Storage backed by the provided directory.
40func NewStorageOrNil(ctx context.Context, cacheDir string, maxBytes int64, subdir string) (Storage, error) {
41 if maxBytes <= 0 || cacheDir == "" {
42 return nil, nil
43 }
44
45 if !ospath.IsAbs(cacheDir) {
46 return nil, errors.Errorf("cache dir %q was not absolute", cacheDir)
47 }
48
49 contentCacheDir := filepath.Join(cacheDir, subdir)
50
51 if _, err := os.Stat(contentCacheDir); os.IsNotExist(err) {
52 if mkdirerr := mkdirAll(contentCacheDir, DirMode); mkdirerr != nil {
53 return nil, errors.Wrap(mkdirerr, "error creating cache directory")
54 }
55 }
56
57 fs, err := filesystem.New(context.WithoutCancel(ctx), &filesystem.Options{
58 Path: contentCacheDir,
59 Options: sharded.Options{
60 DirectoryShards: []int{2},
61 },
62 }, false)
63
64 return filesystemImplWrapper{fs.(Storage)}, errors.Wrap(err, "error initializing filesystem cache") //nolint:forcetypeassert
65}

Callers 6

TestDiskContentCacheFunction · 0.92
getContentCacheOrNilFunction · 0.92
NewContentCacheFunction · 0.85
TestNewStorageOrNilFunction · 0.85

Calls 5

IsAbsFunction · 0.92
NewFunction · 0.92
ErrorfMethod · 0.80
StatMethod · 0.65
IsNotExistMethod · 0.65

Tested by 3

TestDiskContentCacheFunction · 0.74
TestNewStorageOrNilFunction · 0.68