vcom/internal/vfs/readonly.go
vrubelroman 2db51e9385 feat(vfs): define the vfs.FS contract, sentinels, PathOps and ReadOnly wrapper (2.0)
Types and interfaces from design §2.1: FS, File, FileInfo, Caps, VolumeID,
RenameMode, CreateMode, PathOps (posix and native), Location.Same, the
optional Trasher/BirthTimer/Execer interfaces and ReadOnly(fs).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-31 23:21:41 +03:00

84 lines
2.6 KiB
Go

package vfs
import (
"context"
"io/fs"
"time"
)
// ReadOnly wraps fsys so that Caps().ReadOnly is true and every mutating
// method returns ErrReadOnly without reaching fsys. Trash and Exec are hidden;
// BirthTime is passed through.
func ReadOnly(fsys FS) FS {
if ro, ok := fsys.(readOnlyFS); ok {
return ro
}
return readOnlyFS{fsys}
}
type readOnlyFS struct{ inner FS }
func (r readOnlyFS) Kind() Kind { return r.inner.Kind() }
func (r readOnlyFS) Volume() VolumeID { return r.inner.Volume() }
func (r readOnlyFS) Label() string { return r.inner.Label() }
func (r readOnlyFS) Paths() PathOps { return r.inner.Paths() }
func (r readOnlyFS) Caps() Caps {
c := r.inner.Caps()
c.ReadOnly = true
c.Trash = false
c.Exec = false
return c
}
func (r readOnlyFS) Stat(ctx context.Context, p string) (FileInfo, error) {
return r.inner.Stat(ctx, p)
}
func (r readOnlyFS) Lstat(ctx context.Context, p string) (FileInfo, error) {
return r.inner.Lstat(ctx, p)
}
func (r readOnlyFS) ReadDir(ctx context.Context, p string) ([]FileInfo, error) {
return r.inner.ReadDir(ctx, p)
}
func (r readOnlyFS) ReadLink(ctx context.Context, p string) (string, error) {
return r.inner.ReadLink(ctx, p)
}
func (r readOnlyFS) RealPath(ctx context.Context, p string) (string, error) {
return r.inner.RealPath(ctx, p)
}
// Open returns a file whose Write fails with ErrReadOnly.
func (r readOnlyFS) Open(ctx context.Context, p string) (File, error) {
f, err := r.inner.Open(ctx, p)
if err != nil {
return nil, err
}
return readOnlyFile{f}, nil
}
func (r readOnlyFS) BirthTime(ctx context.Context, p string) (time.Time, bool) {
if bt, ok := r.inner.(BirthTimer); ok {
return bt.BirthTime(ctx, p)
}
return time.Time{}, false
}
func roErr(op, p string) error { return &fs.PathError{Op: op, Path: p, Err: ErrReadOnly} }
func (readOnlyFS) Create(_ context.Context, p string, _ fs.FileMode, _ CreateMode) (File, error) {
return nil, roErr("create", p)
}
func (readOnlyFS) Mkdir(_ context.Context, p string, _ fs.FileMode) error { return roErr("mkdir", p) }
func (readOnlyFS) Remove(_ context.Context, p string) error { return roErr("remove", p) }
func (readOnlyFS) Rename(_ context.Context, from, _ string, _ RenameMode) error {
return roErr("rename", from)
}
func (readOnlyFS) Symlink(_ context.Context, _, p string) error { return roErr("symlink", p) }
func (readOnlyFS) Chmod(_ context.Context, p string, _ fs.FileMode) error { return roErr("chmod", p) }
func (readOnlyFS) Chtimes(_ context.Context, p string, _, _ time.Time) error {
return roErr("chtimes", p)
}
type readOnlyFile struct{ File }
func (readOnlyFile) Write([]byte) (int, error) { return 0, ErrReadOnly }