-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(storage): add core virtual file system (#347)
- Loading branch information
1 parent
c023453
commit 738a742
Showing
10 changed files
with
748 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ i18n/temp/* | |
|
||
dist/ | ||
MUSICO/ | ||
shroud.txt | ||
__A/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
requiem-content |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.