feat: Support relative root directories without BasePathFs

This commit is contained in:
Felicitas Pojtinger
2021-12-19 22:40:52 +01:00
parent c25c3c98be
commit 303add8714
3 changed files with 22 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ import (
"os"
"time"
"github.com/pojntfx/stfs/internal/pathext"
"github.com/pojntfx/stfs/pkg/config"
"github.com/spf13/afero"
)
@@ -17,14 +18,26 @@ func Cache(
) (afero.Fs, error) {
switch cacheType {
case CacheTypeMemory:
if pathext.IsRoot(root) {
return afero.NewCacheOnReadFs(base, afero.NewMemMapFs(), ttl), nil
}
return afero.NewCacheOnReadFs(afero.NewBasePathFs(base, root), afero.NewMemMapFs(), ttl), nil
case CacheTypeDir:
if err := os.MkdirAll(cacheDir, os.ModePerm); err != nil {
return nil, err
}
if pathext.IsRoot(root) {
return afero.NewCacheOnReadFs(base, afero.NewBasePathFs(afero.NewOsFs(), cacheDir), ttl), nil
}
return afero.NewCacheOnReadFs(afero.NewBasePathFs(base, root), afero.NewBasePathFs(afero.NewOsFs(), cacheDir), ttl), nil
case config.NoneKey:
if pathext.IsRoot(root) {
return base, nil
}
return afero.NewBasePathFs(base, root), nil
default:
return nil, ErrCacheTypeUnsupported

5
internal/pathext/path.go Normal file
View File

@@ -0,0 +1,5 @@
package pathext
func IsRoot(path string) bool {
return path == "" || path == "." || path == "/" || path == "./"
}

View File

@@ -7,10 +7,12 @@ import (
"context"
"database/sql"
"fmt"
"path/filepath"
"strings"
"github.com/pojntfx/stfs/internal/db/sqlite/migrations/metadata"
models "github.com/pojntfx/stfs/internal/db/sqlite/models/metadata"
"github.com/pojntfx/stfs/internal/pathext"
migrate "github.com/rubenv/sql-migrate"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries"
@@ -211,7 +213,7 @@ func (p *MetadataPersister) GetHeaderDirectChildren(ctx context.Context, name st
headers := models.HeaderSlice{}
// Root node
if name == "" || name == "." || name == "/" || name == "./" {
if pathext.IsRoot(name) {
prefix = ""
depth := depth{}
@@ -402,5 +404,5 @@ func (p *MetadataPersister) PurgeAllHeaders(ctx context.Context) error {
}
func withRelativeRoot(root string) string {
return "./" + strings.TrimPrefix(root, "/")
return filepath.Clean(strings.TrimPrefix(root, "/"))
}