feat: Add basic implementation of File.Readdir

This commit is contained in:
Felix Pojtinger
2021-12-17 18:39:00 +01:00
parent 9fd72adf49
commit 4bb2bd17f8
2 changed files with 48 additions and 4 deletions

View File

@@ -4,23 +4,44 @@ import (
"log"
"os"
models "github.com/pojntfx/stfs/internal/db/sqlite/models/metadata"
"github.com/pojntfx/stfs/pkg/config"
"github.com/pojntfx/stfs/pkg/inventory"
"github.com/spf13/afero"
)
type File struct {
afero.File
metadata config.MetadataConfig
path string
name string
info os.FileInfo
onHeader func(hdr *models.Header)
}
func NewFile(
metadata config.MetadataConfig,
path string,
name string,
stat os.FileInfo,
onHeader func(hdr *models.Header),
) *File {
return &File{
metadata: metadata,
path: path,
name: name,
info: stat,
onHeader: onHeader,
}
}
@@ -39,7 +60,24 @@ func (f File) Stat() (os.FileInfo, error) {
func (f File) Readdir(count int) ([]os.FileInfo, error) {
log.Println("File.Readdir", f.name, count)
return nil, ErrNotImplemented
hdrs, err := inventory.List(
f.metadata,
f.path,
f.onHeader,
)
if err != nil {
return nil, err
}
fileInfos := []os.FileInfo{}
for _, hdr := range hdrs {
// TODO: Handle count; only return all if count = -1
fileInfos = append(fileInfos, NewFileInfo(hdr))
}
return fileInfos, nil
}
func (f File) Readdirnames(n int) ([]string, error) {

View File

@@ -67,15 +67,15 @@ func (S *FileSystem) MkdirAll(path string, perm os.FileMode) error {
panic(ErrNotImplemented)
}
func (s *FileSystem) Open(name string) (afero.File, error) {
func (f *FileSystem) Open(name string) (afero.File, error) {
log.Println("FileSystem.Open", name)
hdr, err := inventory.Stat(
s.metadata,
f.metadata,
name,
s.onHeader,
f.onHeader,
)
if err != nil {
if err == sql.ErrNoRows {
@@ -86,8 +86,14 @@ func (s *FileSystem) Open(name string) (afero.File, error) {
}
return NewFile(
f.metadata,
hdr.Name,
filepath.Base(hdr.Name),
NewFileInfo(hdr),
f.onHeader,
), nil
}