This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-9p/store/root.go

57 lines
1.4 KiB
Go

package store
import (
"context"
"fmt"
"os"
"strings"
"ollie/pkg/backend"
"ollie/pkg/paths"
)
// NewRootStore returns a read-only Store for synthetic root-level files.
func NewRootStore() *DirStore {
helpPath := paths.CfgDir() + "/help.md"
entries := map[string]func() ([]byte, error){
"backends": func() ([]byte, error) {
return []byte(strings.Join(backend.Backends(), "\n") + "\n"), nil
},
"help": func() ([]byte, error) {
return os.ReadFile(helpPath)
},
}
notBlocking := func(context.Context, string) ([]byte, string, error) {
return nil, "", fmt.Errorf("blocking read not supported")
}
readOnly := func([]byte) error { return fmt.Errorf("read-only") }
return NewDirStore(nil, 0444,
WithReadOnly(),
WithResolver(func(_ []string, name string) (string, error) {
if _, ok := entries[name]; ok {
return name, nil
}
return "", fmt.Errorf("%s: not found", name)
}),
WithLister(func(_ []string) ([]os.DirEntry, error) {
return []os.DirEntry{
FileEntry("backends", 0444),
FileEntry("help", 0444),
}, nil
}),
WithOpener(func(_ []string, name string) (StoreEntry, error) {
readFn, ok := entries[name]
if !ok {
return nil, fmt.Errorf("%s: not found", name)
}
return &EntryConfig{
StatFn: func() (os.FileInfo, error) { return &SyntheticFileInfo{Name_: name, Mode_: 0444}, nil },
ReadFn: readFn,
WriteFn: readOnly,
BlockingReadFn: notBlocking,
}, nil
}),
)
}