Skip to content

Commit

Permalink
feat(storage): add core virtual file system (#347)
Browse files Browse the repository at this point in the history
  • Loading branch information
plastikfan committed Oct 28, 2023
1 parent c023453 commit 738a742
Show file tree
Hide file tree
Showing 10 changed files with 748 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ i18n/temp/*

dist/
MUSICO/
shroud.txt
__A/
14 changes: 14 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
"cSpell.words": [
"argh",
"Assisable",
"avfs",
"beezledub",
"bodyclose",
"booter",
"bootstrapper",
"chardata",
"cobrass",
"deadcode",
"deepcopy",
"depguard",
Expand All @@ -16,6 +19,7 @@
"exportloopref",
"extendio",
"Fastward",
"faydeaudeau",
"fieldalignment",
"Flacs",
"forloc",
Expand All @@ -27,7 +31,9 @@
"gofmt",
"Goid",
"goimports",
"gola",
"goleak",
"gomega",
"gomnd",
"gosec",
"gosimple",
Expand All @@ -42,21 +48,29 @@
"linters",
"logr",
"lorax",
"memfs",
"mohae",
"MUSICO",
"nakedret",
"natefinch",
"navi",
"newname",
"newpath",
"nicksnyder",
"nolint",
"nolintlint",
"oldname",
"oldpath",
"onecontext",
"onsi",
"prealloc",
"rabbitweed",
"repath",
"Resumer",
"rxgo",
"samber",
"sidewalk",
"snivilised",
"staticcheck",
"structcheck",
"stylecheck",
Expand Down
1 change: 1 addition & 0 deletions Test/data/storage/Nephilim/Mourning Sun/info.requiem.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requiem-content
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
)

require (
github.com/avfs/avfs v0.33.0 // indirect
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/BurntSushi/toml v1.0.0 h1:dtDWrepsVPfW9H/4y7dDgFc2MBUSeJhlaDtK13CxFlU=
github.com/BurntSushi/toml v1.0.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/avfs/avfs v0.33.0 h1:5WQXbUbr6VS7aani39ZN2Vrd/s3wLnyih1Sc4ExWTxs=
github.com/avfs/avfs v0.33.0/go.mod h1:Q59flcFRYe9KYkNMfrLUJney3yeKGQpcWRyxsDBW7vI=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
120 changes: 120 additions & 0 deletions xfs/storage/mem-fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package storage

import (
"fmt"
"io/fs"
"os"

"github.com/avfs/avfs/vfs/memfs"
"github.com/pkg/errors"
)

type memFS struct {
backend VirtualBackend
mfs *memfs.MemFS
}

func UseMemFS() VirtualFS {
return &memFS{
backend: "mem",
mfs: memfs.New(),
}
}

func (ms *memFS) Backend() VirtualBackend {
return ms.backend
}

// interface ExistsInFS

func (ms *memFS) FileExists(path string) bool {
result := false
if info, err := ms.mfs.Lstat(path); err == nil {
result = !info.IsDir()
}

return result
}

func (ms *memFS) DirectoryExists(path string) bool {
result := false
if info, err := ms.mfs.Lstat(path); err == nil {
result = info.IsDir()
}

return result
}

// end: interface ExistsInFS

// interface ReadOnlyVirtualFS

func (ms *memFS) Lstat(path string) (fs.FileInfo, error) {
return ms.mfs.Lstat(path)
}

func (ms *memFS) Stat(path string) (fs.FileInfo, error) {
return ms.mfs.Stat(path)
}

func (ms *memFS) ReadFile(name string) ([]byte, error) {
return ms.mfs.ReadFile(name)
}

func (ms *memFS) ReadDir(name string) ([]os.DirEntry, error) {
return ms.mfs.ReadDir(name)
}

// end: interface ReadOnlyVirtualFS

// interface WriteToFS

func (ms *memFS) Chmod(name string, mode os.FileMode) error {
return ms.mfs.Chmod(name, mode)
}

func (ms *memFS) Chown(name string, uid, gid int) error {
return ms.mfs.Chown(name, uid, gid)
}

func (ms *memFS) Create(name string) (*os.File, error) {
f, err := ms.mfs.Create(name)

if file, ok := f.(*os.File); ok {
return file, err
}

return nil, errors.Wrap(err,
fmt.Sprintf("file '%v' creation in '%v' failed", name, ms.backend),
)
}

func (ms *memFS) Link(oldname, newname string) error {
return ms.mfs.Link(oldname, newname)
}

func (ms *memFS) Mkdir(name string, perm fs.FileMode) error {
return ms.mfs.Mkdir(name, perm)
}

func (ms *memFS) MkdirAll(path string, perm os.FileMode) error {
return ms.mfs.MkdirAll(path, perm)
}

func (ms *memFS) Remove(name string) error {
return ms.mfs.Remove(name)
}

func (ms *memFS) RemoveAll(path string) error {
return ms.mfs.RemoveAll(path)
}

func (ms *memFS) Rename(oldpath, newpath string) error {
return ms.mfs.Rename(oldpath, newpath)
}

func (ms *memFS) WriteFile(name string, data []byte, perm os.FileMode) error {
return ms.mfs.WriteFile(name, data, perm)
}

// end: interface WriteToFS
106 changes: 106 additions & 0 deletions xfs/storage/native-fs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package storage

import (
"io/fs"
"os"
)

type nativeFS struct {
backend VirtualBackend
}

func UseNativeFS() VirtualFS {
return &nativeFS{
backend: "native",
}
}

func (ns *nativeFS) Backend() VirtualBackend {
return ns.backend
}

// interface ExistsInFS

func (ns *nativeFS) FileExists(path string) bool {
result := false
if info, err := os.Lstat(path); err == nil {
result = !info.IsDir()
}

return result
}

func (ns *nativeFS) DirectoryExists(path string) bool {
result := false
if info, err := os.Lstat(path); err == nil {
result = info.IsDir()
}

return result
}

// end: interface ExistsInFS

// interface ReadOnlyVirtualFS

func (ns *nativeFS) Lstat(path string) (fs.FileInfo, error) {
return os.Lstat(path)
}

func (ns *nativeFS) Stat(path string) (fs.FileInfo, error) {
return os.Stat(path)
}

func (ns *nativeFS) ReadFile(name string) ([]byte, error) {
return os.ReadFile(name)
}

func (ns *nativeFS) ReadDir(name string) ([]os.DirEntry, error) {
return os.ReadDir(name)
}

// end: interface ReadOnlyVirtualFS

func (ns *nativeFS) Chmod(name string, mode os.FileMode) error {
return os.Chmod(name, mode)
}

func (ns *nativeFS) Chown(name string, uid, gid int) error {
return os.Chown(name, uid, gid)
}

func (ns *nativeFS) Create(name string) (*os.File, error) {
return os.Create(name)
}

// interface WriteToFS

func (ns *nativeFS) Link(oldname, newname string) error {
return os.Link(oldname, newname)
}

func (ns *nativeFS) Mkdir(name string, perm fs.FileMode) error {
return os.Mkdir(name, perm)
}

func (ns *nativeFS) MkdirAll(path string, perm os.FileMode) error {
return os.MkdirAll(path, perm)
}

func (ns *nativeFS) Remove(name string) error {
return os.Remove(name)
}

func (ns *nativeFS) RemoveAll(path string) error {
return os.RemoveAll(path)
}

func (ns *nativeFS) Rename(oldpath, newpath string) error {
return os.Rename(oldpath, newpath)
}

func (ns *nativeFS) WriteFile(name string, data []byte, perm os.FileMode) error {
return os.WriteFile(name, data, perm)
}

// end: interface WriteToFS
Loading

0 comments on commit 738a742

Please sign in to comment.