Files
kopia/repo/content/content_cache.go
Jarek Kowalski e03971fc59 Upgraded linter to v1.33.0 (#734)
* linter: upgraded to 1.33, disabled some linters

* lint: fixed 'errorlint' errors

This ensures that all error comparisons use errors.Is() or errors.As().
We will be wrapping more errors going forward so it's important that
error checks are not strict everywhere.

Verified that there are no exceptions for errorlint linter which
guarantees that.

* lint: fixed or suppressed wrapcheck errors

* lint: nolintlint and misc cleanups

Co-authored-by: Julio López <julio+gh@kasten.io>
2020-12-21 22:39:22 -08:00

47 lines
1.1 KiB
Go

package content
import (
"context"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/kopia/kopia/internal/ctxutil"
"github.com/kopia/kopia/repo/blob"
"github.com/kopia/kopia/repo/blob/filesystem"
)
type cacheKey string
type contentCache interface {
close()
getContent(ctx context.Context, cacheKey cacheKey, blobID blob.ID, offset, length int64) ([]byte, error)
}
func newCacheStorageOrNil(ctx context.Context, cacheDir string, maxBytes int64, subdir string) (blob.Storage, error) {
var cacheStorage blob.Storage
var err error
if maxBytes > 0 && cacheDir != "" {
contentCacheDir := filepath.Join(cacheDir, subdir)
if _, err = os.Stat(contentCacheDir); os.IsNotExist(err) {
if mkdirerr := os.MkdirAll(contentCacheDir, 0o700); mkdirerr != nil {
return nil, errors.Wrap(mkdirerr, "error creating cache directory")
}
}
cacheStorage, err = filesystem.New(ctxutil.Detach(ctx), &filesystem.Options{
Path: contentCacheDir,
DirectoryShards: []int{2},
})
if err != nil {
return nil, errors.Wrap(err, "error initializing filesystem cache")
}
}
return cacheStorage, nil
}