From d7e3dfdb0f8c5a23f3f5e39393c7bf8ca5950686 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Sun, 8 Mar 2020 17:21:11 +0100 Subject: [PATCH] Added NewOsFsBallback. - Creates a new OS file system with a fallback on an afero.Fs. Exposed the file system used as source for the CopyDir. --- vfs/copydir.go | 4 ++++ vfs/fallbackfs.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/vfs/copydir.go b/vfs/copydir.go index 02e5060..ab78ffa 100644 --- a/vfs/copydir.go +++ b/vfs/copydir.go @@ -9,7 +9,9 @@ import ( "github.com/spf13/afero" ) +// CopyDir represents a directory that is mimicked on hard drive. type CopyDir interface { + Fs() afero.Fs Retrieve(name string) (string, error) Path(name string) string Open(name string) (*os.File, error) @@ -31,6 +33,8 @@ func NewCopyDir(fs afero.Fs) (CopyDir, error) { return ©Dir{fs, dir}, nil } +func (d *copyDir) Fs() afero.Fs { return d.fs } + func (d *copyDir) Retrieve(name string) (string, error) { var path = d.Path(name) if _, err := os.Stat(path); os.IsNotExist(err) { diff --git a/vfs/fallbackfs.go b/vfs/fallbackfs.go index a17240d..48de8eb 100644 --- a/vfs/fallbackfs.go +++ b/vfs/fallbackfs.go @@ -20,6 +20,12 @@ func NewFallbackFs(source, backup afero.Fs) *FallbackFs { return &FallbackFs{source, backup} } +// NewOsFsFallback creates a new Fs based on the os package, restricts access on the supplied path and uses backup as fallback. +func NewOsFsFallback(path string, backup afero.Fs) *FallbackFs { + osFs := afero.NewOsFs() + return NewFallbackFs(afero.NewBasePathFs(osFs, path), backup) +} + func (f *FallbackFs) do(action func(afero.Fs) error) error { err := action(f.source) if err == nil {