github.com/caddyserver/caddy/v2/internal/filesystems
No package summary is available.
Package
Files: 2. Third party imports: 0. Imports from organisation: 0. Tests: 0. Benchmarks: 0.
Constants
const (
DefaultFilesystemKey = "default"
)
Vars
var DefaultFilesystem = &wrapperFs{key: DefaultFilesystemKey, FS: OsFS{}}
var (
_ fs.StatFS = (*OsFS)(nil)
_ fs.GlobFS = (*OsFS)(nil)
_ fs.ReadDirFS = (*OsFS)(nil)
_ fs.ReadFileFS = (*OsFS)(nil)
)
Types
FilesystemMap
FilesystemMap stores a map of filesystems the empty key will be overwritten to be the default key it includes a default filesystem, based off the os fs
type FilesystemMap struct {
m sync.Map
}
OsFS
OsFS is a simple fs.FS implementation that uses the local file system. (We do not use os.DirFS because we do our own rooting or path prefixing without being constrained to a single root folder. The standard os.DirFS implementation is problematic since roots can be dynamic in our application.)
OsFS also implements fs.StatFS, fs.GlobFS, fs.ReadDirFS, and fs.ReadFileFS.
type OsFS struct{}
wrapperFs
wrapperFs exists so can easily add to wrapperFs down the line
type wrapperFs struct {
key string
fs.FS
}
Functions
func (*FilesystemMap) Default
Default will get the default filesystem in the filesystem map
func (f *FilesystemMap) Default() fs.FS {
val, _ := f.Get(DefaultFilesystemKey)
return val
}
Cognitive complexity: 0
, Cyclomatic complexity: 1
func (*FilesystemMap) Get
Get will get a filesystem with a given key
func (f *FilesystemMap) Get(k string) (v fs.FS, ok bool) {
k = f.key(k)
c, ok := f.m.Load(strings.TrimSpace(k))
if !ok {
if k == DefaultFilesystemKey {
f.m.Store(k, DefaultFilesystem)
return DefaultFilesystem, true
}
return nil, ok
}
return c.(fs.FS), true
}
Cognitive complexity: 4
, Cyclomatic complexity: 3
func (*FilesystemMap) Register
Register will add the filesystem with key to later be retrieved A call with a nil fs will call unregister, ensuring that a call to Default() will never be nil
func (f *FilesystemMap) Register(k string, v fs.FS) {
k = f.key(k)
if v == nil {
f.Unregister(k)
return
}
f.m.Store(k, &wrapperFs{key: k, FS: v})
}
Cognitive complexity: 3
, Cyclomatic complexity: 2
func (*FilesystemMap) Unregister
Unregister will remove the filesystem with key from the filesystem map if the key is the default key, it will set the default to the osFS instead of deleting it modules should call this on cleanup to be safe
func (f *FilesystemMap) Unregister(k string) {
k = f.key(k)
if k == DefaultFilesystemKey {
f.m.Store(k, DefaultFilesystem)
} else {
f.m.Delete(k)
}
}
Cognitive complexity: 4
, Cyclomatic complexity: 2
func (OsFS) Glob
func (OsFS) Glob(pattern string) ([]string, error) { return filepath.Glob(pattern) }
Cognitive complexity: 0
, Cyclomatic complexity: 1
func (OsFS) Open
func (OsFS) Open(name string) (fs.File, error) { return os.Open(name) }
Cognitive complexity: 0
, Cyclomatic complexity: 1
func (OsFS) ReadDir
func (OsFS) ReadDir(name string) ([]fs.DirEntry, error) { return os.ReadDir(name) }
Cognitive complexity: 0
, Cyclomatic complexity: 1
func (OsFS) ReadFile
func (OsFS) ReadFile(name string) ([]byte, error) { return os.ReadFile(name) }
Cognitive complexity: 0
, Cyclomatic complexity: 1
func (OsFS) Stat
func (OsFS) Stat(name string) (fs.FileInfo, error) { return os.Stat(name) }
Cognitive complexity: 0
, Cyclomatic complexity: 1
Private functions
func key
note that the first invocation of key cannot be called in a racy context.
key (k string) string