mirror of
https://github.com/kopia/kopia.git
synced 2026-05-16 18:54:38 -04:00
* 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>
47 lines
1.1 KiB
Go
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
|
|
}
|