New Expandable file formats ()

This commit is contained in:
Antonio Navarro Perez 2021-11-29 11:07:54 +01:00 committed by GitHub
parent 15c72452de
commit 8d9a9281c9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
26 changed files with 808 additions and 346 deletions

View file

@ -9,29 +9,34 @@ import (
"github.com/billziss-gh/cgofuse/fuse"
"github.com/distribyted/distribyted/fs"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
type FS struct {
fuse.FileSystemBase
fh *fileHandler
log zerolog.Logger
}
func NewFS(fs fs.Filesystem) fuse.FileSystemInterface {
l := log.Logger.With().Str("component", "fuse").Logger()
return &FS{
fh: &fileHandler{fs: fs},
fh: &fileHandler{fs: fs},
log: l,
}
}
func (fs *FS) Open(path string, flags int) (errc int, fh uint64) {
fh, err := fs.fh.OpenHolder(path)
if err == os.ErrNotExist {
log.Debug().Str("path", path).Msg("file does not exists")
if os.IsNotExist(err) {
fs.log.Debug().Str("path", path).Msg("file does not exists")
return -fuse.ENOENT, fhNone
}
if err != nil {
log.Error().Err(err).Str("path", path).Msg("error opening file")
fs.log.Error().Err(err).Str("path", path).Msg("error opening file")
return -fuse.EIO, fhNone
}
@ -42,20 +47,20 @@ func (fs *FS) Opendir(path string) (errc int, fh uint64) {
return fs.Open(path, 0)
}
func (cfs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) {
func (fs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) {
if path == "/" {
stat.Mode = fuse.S_IFDIR | 0555
return 0
}
file, err := cfs.fh.GetFile(path, fh)
if err == os.ErrNotExist {
log.Debug().Str("path", path).Msg("file does not exists")
file, err := fs.fh.GetFile(path, fh)
if os.IsNotExist(err) {
fs.log.Debug().Str("path", path).Msg("file does not exists")
return -fuse.ENOENT
}
if err != nil {
log.Error().Err(err).Str("path", path).Msg("error getting holder when reading file attributes")
fs.log.Error().Err(err).Str("path", path).Msg("error getting holder when reading file attributes")
return -fuse.EIO
}
@ -71,13 +76,13 @@ func (cfs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) {
func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int {
file, err := fs.fh.GetFile(path, fh)
if err == os.ErrNotExist {
log.Error().Str("path", path).Msg("file not found on READ operation")
if os.IsNotExist(err) {
fs.log.Error().Err(err).Str("path", path).Msg("file not found on READ operation")
return -fuse.ENOENT
}
if err != nil {
log.Error().Err(err).Str("path", path).Msg("error getting holder reading data from file")
fs.log.Error().Err(err).Str("path", path).Msg("error getting holder reading data from file")
return -fuse.EIO
}
@ -100,7 +105,7 @@ func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int {
func (fs *FS) Release(path string, fh uint64) int {
if err := fs.fh.Remove(fh); err != nil {
log.Error().Err(err).Str("path", path).Msg("error getting holder when releasing file")
fs.log.Error().Err(err).Str("path", path).Msg("error getting holder when releasing file")
return -fuse.EIO
}
@ -121,13 +126,13 @@ func (fs *FS) Readdir(path string,
//TODO improve this function to make use of fh index if possible
paths, err := fs.fh.ListDir(path)
if err != nil {
log.Error().Str("path", path).Msg("error reading directory")
return -fuse.EIO
fs.log.Error().Err(err).Str("path", path).Msg("error reading directory")
return -fuse.ENOSYS
}
for _, p := range paths {
if !fill(p, nil, 0) {
log.Error().Str("path", path).Msg("error adding directory")
fs.log.Error().Str("path", path).Msg("error adding directory")
break
}
}
@ -141,25 +146,24 @@ var ErrHolderEmpty = errors.New("file holder is empty")
var ErrBadHolderIndex = errors.New("holder index too big")
type fileHandler struct {
mu sync.Mutex
mu sync.RWMutex
opened []fs.File
fs fs.Filesystem
}
func (fh *fileHandler) GetFile(path string, fhi uint64) (fs.File, error) {
fh.mu.Lock()
defer fh.mu.Unlock()
fh.mu.RLock()
defer fh.mu.RUnlock()
if fhi == fhNone {
return fh.lookupFile(path)
}
return fh.get(fhi)
}
func (fh *fileHandler) ListDir(path string) ([]string, error) {
fh.mu.Lock()
defer fh.mu.Unlock()
fh.mu.RLock()
defer fh.mu.RUnlock()
var out []string
files, err := fh.fs.ReadDir(path)
@ -174,14 +178,14 @@ func (fh *fileHandler) ListDir(path string) ([]string, error) {
}
func (fh *fileHandler) OpenHolder(path string) (uint64, error) {
fh.mu.Lock()
defer fh.mu.Unlock()
file, err := fh.lookupFile(path)
if err != nil {
return fhNone, err
}
fh.mu.Lock()
defer fh.mu.Unlock()
for i, old := range fh.opened {
if old == nil {
fh.opened[i] = file