context fs

This commit is contained in:
royalcat 2024-03-21 00:47:51 +03:00
parent fd3beea874
commit 7b1863109c
25 changed files with 593 additions and 349 deletions
src/host/vfs

View file

@ -1,8 +1,10 @@
package vfs
import (
"context"
"io/fs"
"log/slog"
"reflect"
)
type LogFS struct {
@ -40,8 +42,8 @@ func (fs *LogFS) Type() fs.FileMode {
}
// Open implements Filesystem.
func (fs *LogFS) Open(filename string) (File, error) {
file, err := fs.fs.Open(filename)
func (fs *LogFS) Open(ctx context.Context, filename string) (File, error) {
file, err := fs.fs.Open(ctx, filename)
if err != nil {
fs.log.With("filename", filename).Error("Failed to open file")
}
@ -50,17 +52,17 @@ func (fs *LogFS) Open(filename string) (File, error) {
}
// ReadDir implements Filesystem.
func (fs *LogFS) ReadDir(path string) ([]fs.DirEntry, error) {
file, err := fs.fs.ReadDir(path)
func (fs *LogFS) ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error) {
file, err := fs.fs.ReadDir(ctx, path)
if err != nil {
fs.log.Error("Failed to read dir", "path", path, "error", err)
fs.log.ErrorContext(ctx, "Failed to read dir", "path", path, "error", err.Error(), "fs-type", reflect.TypeOf(fs.fs).Name())
}
return file, err
}
// Stat implements Filesystem.
func (fs *LogFS) Stat(filename string) (fs.FileInfo, error) {
file, err := fs.fs.Stat(filename)
func (fs *LogFS) Stat(ctx context.Context, filename string) (fs.FileInfo, error) {
file, err := fs.fs.Stat(ctx, filename)
if err != nil {
fs.log.Error("Failed to stat", "filename", filename, "error", err)
}
@ -68,8 +70,8 @@ func (fs *LogFS) Stat(filename string) (fs.FileInfo, error) {
}
// Unlink implements Filesystem.
func (fs *LogFS) Unlink(filename string) error {
err := fs.fs.Unlink(filename)
func (fs *LogFS) Unlink(ctx context.Context, filename string) error {
err := fs.fs.Unlink(ctx, filename)
if err != nil {
fs.log.Error("Failed to stat", "filename", filename, "error", err)
}
@ -91,8 +93,8 @@ func WrapLogFile(f File, filename string, log *slog.Logger) *LogFile {
}
// Close implements File.
func (f *LogFile) Close() error {
err := f.f.Close()
func (f *LogFile) Close(ctx context.Context) error {
err := f.f.Close(ctx)
if err != nil {
f.log.Error("Failed to close", "error", err)
}
@ -105,8 +107,8 @@ func (f *LogFile) IsDir() bool {
}
// Read implements File.
func (f *LogFile) Read(p []byte) (n int, err error) {
n, err = f.f.Read(p)
func (f *LogFile) Read(ctx context.Context, p []byte) (n int, err error) {
n, err = f.f.Read(ctx, p)
if err != nil {
f.log.Error("Failed to read", "error", err)
}
@ -114,8 +116,8 @@ func (f *LogFile) Read(p []byte) (n int, err error) {
}
// ReadAt implements File.
func (f *LogFile) ReadAt(p []byte, off int64) (n int, err error) {
n, err = f.f.ReadAt(p, off)
func (f *LogFile) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
n, err = f.f.ReadAt(ctx, p, off)
if err != nil {
f.log.Error("Failed to read", "offset", off, "error", err)
}