tstor/src/mounts/httpfs/httpfs.go

110 lines
1.9 KiB
Go
Raw Normal View History

2023-10-08 16:46:03 +00:00
package httpfs
2021-11-23 12:05:49 +00:00
import (
"io"
"io/fs"
"net/http"
"os"
"sync"
2023-10-16 09:18:40 +00:00
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
2023-10-08 16:46:03 +00:00
"git.kmsign.ru/royalcat/tstor/src/iio"
2021-11-23 12:05:49 +00:00
)
var _ http.FileSystem = &HTTPFS{}
type HTTPFS struct {
2023-10-16 09:18:40 +00:00
fs vfs.Filesystem
2021-11-23 12:05:49 +00:00
}
2023-10-16 09:18:40 +00:00
func NewHTTPFS(fs vfs.Filesystem) *HTTPFS {
2021-11-23 12:05:49 +00:00
return &HTTPFS{fs: fs}
}
func (fs *HTTPFS) Open(name string) (http.File, error) {
f, err := fs.fs.Open(name)
if err != nil {
return nil, err
}
2023-10-16 09:18:40 +00:00
fi := vfs.NewFileInfo(name, f.Size(), f.IsDir())
2021-11-23 12:05:49 +00:00
// TODO make this lazy
fis, err := fs.filesToFileInfo(name)
if err != nil {
return nil, err
}
return newHTTPFile(f, fis, fi), nil
}
func (fs *HTTPFS) filesToFileInfo(path string) ([]fs.FileInfo, error) {
files, err := fs.fs.ReadDir(path)
if err != nil {
return nil, err
}
var out []os.FileInfo
for n, f := range files {
2023-10-16 09:18:40 +00:00
out = append(out, vfs.NewFileInfo(n, f.Size(), f.IsDir()))
2021-11-23 12:05:49 +00:00
}
return out, nil
}
var _ http.File = &httpFile{}
type httpFile struct {
iio.ReaderSeeker
mu sync.Mutex
// dirPos is protected by mu.
dirPos int
dirContent []os.FileInfo
fi fs.FileInfo
}
2023-10-16 09:18:40 +00:00
func newHTTPFile(f vfs.File, fis []fs.FileInfo, fi fs.FileInfo) *httpFile {
2021-11-23 12:05:49 +00:00
return &httpFile{
dirContent: fis,
fi: fi,
ReaderSeeker: iio.NewSeekerWrapper(f, f.Size()),
}
}
func (f *httpFile) Readdir(count int) ([]fs.FileInfo, error) {
f.mu.Lock()
defer f.mu.Unlock()
if !f.fi.IsDir() {
return nil, os.ErrInvalid
}
old := f.dirPos
if old >= len(f.dirContent) {
// The os.File Readdir docs say that at the end of a directory,
// the error is io.EOF if count > 0 and nil if count <= 0.
if count > 0 {
return nil, io.EOF
}
return nil, nil
}
if count > 0 {
f.dirPos += count
if f.dirPos > len(f.dirContent) {
f.dirPos = len(f.dirContent)
}
} else {
f.dirPos = len(f.dirContent)
old = 0
}
return f.dirContent[old:f.dirPos], nil
}
func (f *httpFile) Stat() (fs.FileInfo, error) {
return f.fi, nil
}