tstor/src/host/vfs/memory.go

105 lines
2.1 KiB
Go
Raw Normal View History

2023-10-16 09:18:40 +00:00
package vfs
2021-11-23 12:05:49 +00:00
import (
"bytes"
2024-03-20 21:47:51 +00:00
"context"
2023-12-21 23:15:39 +00:00
"io/fs"
"path"
2021-11-23 12:05:49 +00:00
)
2023-10-16 09:18:40 +00:00
var _ Filesystem = &MemoryFs{}
2021-11-23 12:05:49 +00:00
2023-10-16 09:18:40 +00:00
type MemoryFs struct {
2024-03-20 10:49:19 +00:00
name string
2023-10-16 09:18:40 +00:00
files map[string]*MemoryFile
2021-11-23 12:05:49 +00:00
}
2024-03-20 10:49:19 +00:00
// Info implements Filesystem.
func (fs *MemoryFs) Info() (fs.FileInfo, error) {
return newDirInfo(fs.name), nil
}
// IsDir implements Filesystem.
func (fs *MemoryFs) IsDir() bool {
return true
}
// Name implements Filesystem.
func (fs *MemoryFs) Name() string {
return fs.name
}
// Type implements Filesystem.
func (mfs *MemoryFs) Type() fs.FileMode {
return fs.ModeDir
}
func NewMemoryFS(name string, files map[string]*MemoryFile) *MemoryFs {
2023-10-16 09:18:40 +00:00
return &MemoryFs{
2024-03-20 10:49:19 +00:00
name: name,
2023-10-16 09:18:40 +00:00
files: files,
2021-11-23 12:05:49 +00:00
}
}
2024-03-20 21:47:51 +00:00
func (m *MemoryFs) Open(ctx context.Context, filename string) (File, error) {
2023-10-16 09:18:40 +00:00
return getFile(m.files, filename)
2021-11-23 12:05:49 +00:00
}
2024-03-20 21:47:51 +00:00
func (fs *MemoryFs) ReadDir(ctx context.Context, path string) ([]fs.DirEntry, error) {
2023-12-21 23:15:39 +00:00
return listDirFromFiles(fs.files, path)
}
// Stat implements Filesystem.
2024-03-20 21:47:51 +00:00
func (mfs *MemoryFs) Stat(ctx context.Context, filename string) (fs.FileInfo, error) {
2023-12-21 23:15:39 +00:00
file, ok := mfs.files[filename]
if !ok {
return nil, ErrNotExist
}
return newFileInfo(path.Base(filename), file.Size()), nil
2021-11-23 12:05:49 +00:00
}
2024-03-20 21:47:51 +00:00
// Unlink implements Filesystem.
func (fs *MemoryFs) Unlink(ctx context.Context, filename string) error {
return ErrNotImplemented
}
var _ File = (*MemoryFile)(nil)
2021-11-23 12:05:49 +00:00
type MemoryFile struct {
2023-12-21 23:15:39 +00:00
name string
2024-03-20 21:47:51 +00:00
data *bytes.Reader
2021-11-23 12:05:49 +00:00
}
2023-12-21 23:15:39 +00:00
func NewMemoryFile(name string, data []byte) *MemoryFile {
2021-11-23 12:05:49 +00:00
return &MemoryFile{
2024-03-20 21:47:51 +00:00
name: name,
data: bytes.NewReader(data),
2021-11-23 12:05:49 +00:00
}
}
2023-12-21 23:15:39 +00:00
func (d *MemoryFile) Stat() (fs.FileInfo, error) {
2024-03-20 21:47:51 +00:00
return newFileInfo(d.name, int64(d.data.Len())), nil
2023-12-21 23:15:39 +00:00
}
2021-11-23 12:05:49 +00:00
func (d *MemoryFile) Size() int64 {
2024-03-20 21:47:51 +00:00
return int64(d.data.Len())
2021-11-23 12:05:49 +00:00
}
func (d *MemoryFile) IsDir() bool {
return false
}
2024-03-20 21:47:51 +00:00
func (d *MemoryFile) Close(ctx context.Context) (err error) {
2021-11-23 12:05:49 +00:00
return
}
2024-03-20 21:47:51 +00:00
// Read implements File.
func (d *MemoryFile) Read(ctx context.Context, p []byte) (n int, err error) {
return d.data.Read(p)
}
// ReadAt implements File.
func (d *MemoryFile) ReadAt(ctx context.Context, p []byte, off int64) (n int, err error) {
return d.data.ReadAt(p, off)
}