99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package cachefs
|
|
|
|
import (
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/spf13/afero"
|
|
)
|
|
|
|
// ensure CacheFs implements afero.Fs.
|
|
var _ afero.Fs = &CacheFs{}
|
|
|
|
// CacheStatus represents the status of a file in the cache.
|
|
type CacheStatus int
|
|
|
|
const (
|
|
// CacheHit indicates that the requested file was found in the cache.
|
|
CacheHit CacheStatus = iota
|
|
|
|
// CacheMiss indicates that the requested file was not found in the cache.
|
|
CacheMiss
|
|
)
|
|
|
|
type CacheFs struct {
|
|
// source filesystem
|
|
source afero.Fs
|
|
|
|
// cache filesystem
|
|
cache afero.Fs
|
|
}
|
|
|
|
func NewCacheFs(source, cache afero.Fs) *CacheFs {
|
|
return &CacheFs{
|
|
source: source,
|
|
cache: cache,
|
|
}
|
|
}
|
|
|
|
// Create implements the Create method of afero.Fs, ensuring we don't exceed storage limits.
|
|
func (cfs *CacheFs) Create(name string) (afero.File, error) {
|
|
return cfs.source.Create(name)
|
|
}
|
|
|
|
func (cfs *CacheFs) Mkdir(name string, perm os.FileMode) error {
|
|
return cfs.source.Mkdir(name, perm)
|
|
}
|
|
|
|
func (cfs *CacheFs) MkdirAll(path string, perm os.FileMode) error {
|
|
return cfs.source.MkdirAll(path, perm)
|
|
}
|
|
|
|
func (cfs *CacheFs) Open(name string) (afero.File, error) {
|
|
return cfs.source.Open(name)
|
|
}
|
|
|
|
func (cfs *CacheFs) OpenFile(name string, flag int, perm os.FileMode) (afero.File, error) {
|
|
return cfs.source.OpenFile(name, flag, perm)
|
|
}
|
|
|
|
func (cfs *CacheFs) Remove(name string) error {
|
|
return cfs.source.Remove(name)
|
|
}
|
|
|
|
func (cfs *CacheFs) RemoveAll(path string) error {
|
|
return cfs.source.RemoveAll(path)
|
|
}
|
|
|
|
func (cfs *CacheFs) Rename(oldname, newname string) error {
|
|
return cfs.source.Rename(oldname, newname)
|
|
}
|
|
|
|
func (cfs *CacheFs) Stat(name string) (os.FileInfo, error) {
|
|
return cfs.source.Stat(name)
|
|
}
|
|
|
|
func (cfs *CacheFs) Name() string {
|
|
return "CacheFS"
|
|
}
|
|
|
|
func (cfs *CacheFs) Chmod(name string, mode os.FileMode) error {
|
|
return cfs.Chmod(name, mode)
|
|
}
|
|
|
|
func (cfs *CacheFs) Chown(name string, uid, gid int) error {
|
|
return cfs.source.Chown(name, uid, gid)
|
|
}
|
|
|
|
func (cfs *CacheFs) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
return cfs.source.Chtimes(name, atime, mtime)
|
|
}
|
|
|
|
// ensure CacheFile implements afero.File.
|
|
var _ afero.File = &CacheFile{}
|
|
|
|
// CacheFile wraps an afero.File to manage space usage on write operations.
|
|
type CacheFile struct {
|
|
afero.File
|
|
}
|