vendor/github.com/spf13/afero/basepath.go
changeset 260 445e01aede7e
parent 256 6d9efbef00a9
equal deleted inserted replaced
259:db4911b0c721 260:445e01aede7e
     1 package afero
     1 package afero
     2 
     2 
     3 import (
     3 import (
       
     4 	"io/fs"
     4 	"os"
     5 	"os"
     5 	"path/filepath"
     6 	"path/filepath"
     6 	"runtime"
     7 	"runtime"
     7 	"strings"
     8 	"strings"
     8 	"time"
     9 	"time"
     9 )
    10 )
    10 
    11 
    11 var _ Lstater = (*BasePathFs)(nil)
    12 var (
       
    13 	_ Lstater        = (*BasePathFs)(nil)
       
    14 	_ fs.ReadDirFile = (*BasePathFile)(nil)
       
    15 )
    12 
    16 
    13 // The BasePathFs restricts all operations to a given path within an Fs.
    17 // The BasePathFs restricts all operations to a given path within an Fs.
    14 // The given file name to the operations on this Fs will be prepended with
    18 // The given file name to the operations on this Fs will be prepended with
    15 // the base path before calling the base Fs.
    19 // the base path before calling the base Fs.
    16 // Any file name (after filepath.Clean()) outside this base path will be
    20 // Any file name (after filepath.Clean()) outside this base path will be
    31 func (f *BasePathFile) Name() string {
    35 func (f *BasePathFile) Name() string {
    32 	sourcename := f.File.Name()
    36 	sourcename := f.File.Name()
    33 	return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
    37 	return strings.TrimPrefix(sourcename, filepath.Clean(f.path))
    34 }
    38 }
    35 
    39 
       
    40 func (f *BasePathFile) ReadDir(n int) ([]fs.DirEntry, error) {
       
    41 	if rdf, ok := f.File.(fs.ReadDirFile); ok {
       
    42 		return rdf.ReadDir(n)
       
    43 
       
    44 	}
       
    45 	return readDirFile{f.File}.ReadDir(n)
       
    46 }
       
    47 
    36 func NewBasePathFs(source Fs, path string) Fs {
    48 func NewBasePathFs(source Fs, path string) Fs {
    37 	return &BasePathFs{source: source, path: path}
    49 	return &BasePathFs{source: source, path: path}
    38 }
    50 }
    39 
    51 
    40 // on a file outside the base path it returns the given file name and an error,
    52 // on a file outside the base path it returns the given file name and an error,