117 lines
2.3 KiB
Go
117 lines
2.3 KiB
Go
package vfs
|
|
|
|
import (
|
|
"io/fs"
|
|
"log/slog"
|
|
)
|
|
|
|
type LogFS struct {
|
|
fs Filesystem
|
|
log *slog.Logger
|
|
}
|
|
|
|
var _ Filesystem = (*LogFS)(nil)
|
|
|
|
func WrapLogFS(fs Filesystem, log *slog.Logger) *LogFS {
|
|
return &LogFS{
|
|
fs: fs,
|
|
log: log.With("component", "fs"),
|
|
}
|
|
}
|
|
|
|
// Open implements Filesystem.
|
|
func (fs *LogFS) Open(filename string) (File, error) {
|
|
file, err := fs.fs.Open(filename)
|
|
if err != nil {
|
|
fs.log.With("filename", filename).Error("Failed to open file")
|
|
}
|
|
file = WrapLogFile(file, filename, fs.log)
|
|
return file, err
|
|
}
|
|
|
|
// ReadDir implements Filesystem.
|
|
func (fs *LogFS) ReadDir(path string) ([]fs.DirEntry, error) {
|
|
file, err := fs.fs.ReadDir(path)
|
|
if err != nil {
|
|
fs.log.Error("Failed to read dir", "path", path, "error", err)
|
|
}
|
|
return file, err
|
|
}
|
|
|
|
// Stat implements Filesystem.
|
|
func (fs *LogFS) Stat(filename string) (fs.FileInfo, error) {
|
|
file, err := fs.fs.Stat(filename)
|
|
if err != nil {
|
|
fs.log.Error("Failed to stat", "filename", filename, "error", err)
|
|
}
|
|
return file, err
|
|
}
|
|
|
|
// Unlink implements Filesystem.
|
|
func (fs *LogFS) Unlink(filename string) error {
|
|
err := fs.fs.Unlink(filename)
|
|
if err != nil {
|
|
fs.log.Error("Failed to stat", "filename", filename, "error", err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
type LogFile struct {
|
|
f File
|
|
log *slog.Logger
|
|
}
|
|
|
|
var _ File = (*LogFile)(nil)
|
|
|
|
func WrapLogFile(f File, filename string, log *slog.Logger) *LogFile {
|
|
return &LogFile{
|
|
f: f,
|
|
log: log.With("filename", filename),
|
|
}
|
|
}
|
|
|
|
// Close implements File.
|
|
func (f *LogFile) Close() error {
|
|
err := f.f.Close()
|
|
if err != nil {
|
|
f.log.Error("Failed to close", "error", err)
|
|
}
|
|
return err
|
|
}
|
|
|
|
// IsDir implements File.
|
|
func (f *LogFile) IsDir() bool {
|
|
return f.f.IsDir()
|
|
}
|
|
|
|
// Read implements File.
|
|
func (f *LogFile) Read(p []byte) (n int, err error) {
|
|
n, err = f.f.Read(p)
|
|
if err != nil {
|
|
f.log.Error("Failed to read", "error", err)
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
// ReadAt implements File.
|
|
func (f *LogFile) ReadAt(p []byte, off int64) (n int, err error) {
|
|
n, err = f.f.ReadAt(p, off)
|
|
if err != nil {
|
|
f.log.Error("Failed to read", "offset", off, "error", err)
|
|
}
|
|
return n, err
|
|
}
|
|
|
|
// Size implements File.
|
|
func (f *LogFile) Size() int64 {
|
|
return f.f.Size()
|
|
}
|
|
|
|
// Stat implements File.
|
|
func (f *LogFile) Stat() (fs.FileInfo, error) {
|
|
info, err := f.f.Stat()
|
|
if err != nil {
|
|
f.log.Error("Failed to read", "error", err)
|
|
}
|
|
return info, err
|
|
}
|