2023-10-16 09:18:40 +00:00
|
|
|
package vfs
|
2021-11-23 12:05:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
files map[string]*MemoryFile
|
2021-11-23 12:05:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
func NewMemoryFS(files map[string]*MemoryFile) *MemoryFs {
|
|
|
|
return &MemoryFs{
|
|
|
|
files: files,
|
2021-11-23 12:05:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
func (m *MemoryFs) Open(filename string) (File, error) {
|
|
|
|
return getFile(m.files, filename)
|
2021-11-23 12:05:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-16 09:18:40 +00:00
|
|
|
func (fs *MemoryFs) ReadDir(path string) (map[string]File, error) {
|
|
|
|
return listFilesInDir(fs.files, path)
|
2021-11-23 12:05:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ File = &MemoryFile{}
|
|
|
|
|
|
|
|
type MemoryFile struct {
|
|
|
|
*bytes.Reader
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMemoryFile(data []byte) *MemoryFile {
|
|
|
|
return &MemoryFile{
|
|
|
|
Reader: bytes.NewReader(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MemoryFile) Size() int64 {
|
|
|
|
return int64(d.Reader.Len())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MemoryFile) IsDir() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *MemoryFile) Close() (err error) {
|
|
|
|
return
|
|
|
|
}
|