Skip to content

Commit

Permalink
feat(fsx): introduce file system abstraction (#16)
Browse files Browse the repository at this point in the history
This abstraction is based on github.com/spf13/afero but adds specific
changes that make the code suitable for rbmk.

We will use this new functionality for testing of the rbmk tool and
possibly also to allow to virtualise the file system, a feature that may
be useful for embedding rbmk.

This change also required to extend the mocks to mock the new filesystem
types. The actual shared model between fsx and the mocks package is a
private model in the internal folder. The fsx package exports all the
private model definitions to fsx consumers.

While there upgrade dependencies.
  • Loading branch information
bassosimone authored Dec 10, 2024
1 parent ddd54c6 commit 4ab111b
Show file tree
Hide file tree
Showing 13 changed files with 2,452 additions and 3 deletions.
119 changes: 119 additions & 0 deletions fsx/chdirfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//
// SPDX-License-Identifier: Apache-2.0
//
// Adapted from: https://github.com/spf13/afero
//

package fsx

import (
"io/fs"
"net"
"path/filepath"
"time"
)

// NewChdirFS creates a new [FS] where each file name is
// prefixed with the given directory path.
func NewChdirFS(dep FS, path string) *ChdirFS {
return &ChdirFS{basepath: path, dep: dep}
}

// ChdirFS is the [FS] type returned by [NewChdirFS].
//
// The zero value IS NOT ready to use; construct using [NewChdirFS].
type ChdirFS struct {
// basepath is the base path.
basepath string

// dep is the dependency [FS].
dep FS
}

// Ensure [basePathFS] implements [FS].
var _ FS = &ChdirFS{}

// realPath returns the real path of a given file name or an error.
func (rfs *ChdirFS) realPath(name string) string {
return filepath.Join(rfs.basepath, name)
}

// Chmod implements [FS].
func (rfs *ChdirFS) Chmod(name string, mode fs.FileMode) error {
return rfs.dep.Chmod(rfs.realPath(name), mode)
}

// Chown implements [FS].
func (rfs *ChdirFS) Chown(name string, uid, gid int) error {
return rfs.dep.Chown(rfs.realPath(name), uid, gid)
}

// Chtimes implements [FS].
func (rfs *ChdirFS) Chtimes(name string, atime, mtime time.Time) error {
return rfs.dep.Chtimes(rfs.realPath(name), atime, mtime)
}

// Create implements [FS].
func (rfs *ChdirFS) Create(name string) (File, error) {
return rfs.dep.Create(rfs.realPath(name))
}

// DialUnix implements [FS].
func (rfs *ChdirFS) DialUnix(name string) (net.Conn, error) {
return rfs.dep.DialUnix(rfs.realPath(name))
}

// ListenUnix implements [FS].
func (rfs *ChdirFS) ListenUnix(name string) (net.Listener, error) {
return rfs.dep.ListenUnix(rfs.realPath(name))
}

// Lstat implements [FS].
func (rfs *ChdirFS) Lstat(name string) (fs.FileInfo, error) {
return rfs.dep.Lstat(rfs.realPath(name))
}

// Mkdir implements [FS].
func (rfs *ChdirFS) Mkdir(name string, mode fs.FileMode) error {
return rfs.dep.Mkdir(rfs.realPath(name), mode)
}

// MkdirAll implements [FS].
func (rfs *ChdirFS) MkdirAll(name string, mode fs.FileMode) error {
return rfs.dep.MkdirAll(rfs.realPath(name), mode)
}

// Open implements [FS].
func (rfs *ChdirFS) Open(name string) (File, error) {
return rfs.dep.Open(rfs.realPath(name))
}

// OpenFile implements [FS].
func (rfs *ChdirFS) OpenFile(name string, flag int, mode fs.FileMode) (File, error) {
return rfs.dep.OpenFile(rfs.realPath(name), flag, mode)
}

// ReadDir implements [FS].
func (rfs *ChdirFS) ReadDir(name string) ([]fs.DirEntry, error) {
return rfs.dep.ReadDir(rfs.realPath(name))
}

// Remove implements [FS].
func (rfs *ChdirFS) Remove(name string) error {
return rfs.dep.Remove(rfs.realPath(name))
}

// RemoveAll implements [FS].
func (rfs *ChdirFS) RemoveAll(name string) error {
return rfs.dep.RemoveAll(rfs.realPath(name))
}

// Rename implements [FS].
func (rfs *ChdirFS) Rename(oldname, newname string) error {
return rfs.dep.Rename(rfs.realPath(oldname), rfs.realPath(newname))
}

// Stat implements [FS].
func (rfs *ChdirFS) Stat(name string) (fs.FileInfo, error) {
return rfs.dep.Stat(rfs.realPath(name))
}
Loading

0 comments on commit 4ab111b

Please sign in to comment.