2020-09-27 19:23:47 +00:00
|
|
|
package fuse
|
|
|
|
|
|
|
|
import (
|
2020-11-09 18:37:15 +00:00
|
|
|
"errors"
|
2020-09-27 19:23:47 +00:00
|
|
|
"io"
|
|
|
|
"math"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/billziss-gh/cgofuse/fuse"
|
2021-03-01 16:43:28 +00:00
|
|
|
"github.com/distribyted/distribyted/fs"
|
2020-09-27 19:23:47 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FS struct {
|
|
|
|
fuse.FileSystemBase
|
2020-11-09 18:37:15 +00:00
|
|
|
fh *fileHandler
|
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2021-03-06 22:08:15 +00:00
|
|
|
func NewFS(fs fs.Filesystem) fuse.FileSystemInterface {
|
2020-11-09 18:37:15 +00:00
|
|
|
return &FS{
|
2021-03-06 22:08:15 +00:00
|
|
|
fh: &fileHandler{fs: fs},
|
2020-11-09 18:37:15 +00:00
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Open(path string, flags int) (errc int, fh uint64) {
|
2020-11-09 18:37:15 +00:00
|
|
|
fh, err := fs.fh.OpenHolder(path)
|
|
|
|
if err == os.ErrNotExist {
|
2020-11-14 15:28:50 +00:00
|
|
|
logrus.WithField("path", path).Debug("file does not exists")
|
2020-11-09 18:37:15 +00:00
|
|
|
return -fuse.ENOENT, fhNone
|
|
|
|
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithError(err).WithField("path", path).Error("error opening file")
|
|
|
|
return -fuse.EIO, fhNone
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0, fh
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
func (fs *FS) Opendir(path string) (errc int, fh uint64) {
|
|
|
|
return fs.Open(path, 0)
|
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
func (cfs *FS) Getattr(path string, stat *fuse.Stat_t, fh uint64) (errc int) {
|
2020-09-27 19:23:47 +00:00
|
|
|
if path == "/" {
|
|
|
|
stat.Mode = fuse.S_IFDIR | 0555
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
file, err := cfs.fh.GetFile(path, fh)
|
2020-09-27 19:23:47 +00:00
|
|
|
if err == os.ErrNotExist {
|
2020-11-14 15:28:50 +00:00
|
|
|
logrus.WithField("path", path).Debug("file does not exists")
|
2020-09-27 19:23:47 +00:00
|
|
|
return -fuse.ENOENT
|
|
|
|
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-09 18:37:15 +00:00
|
|
|
logrus.WithError(err).WithField("path", path).Error("error getting holder when reading file attributes")
|
2020-09-27 19:23:47 +00:00
|
|
|
return -fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
if file.IsDir() {
|
|
|
|
stat.Mode = fuse.S_IFDIR | 0555
|
|
|
|
} else {
|
|
|
|
stat.Mode = fuse.S_IFREG | 0444
|
|
|
|
stat.Size = file.Size()
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Read(path string, dest []byte, off int64, fh uint64) int {
|
2020-11-09 18:37:15 +00:00
|
|
|
file, err := fs.fh.GetFile(path, fh)
|
2020-09-27 19:23:47 +00:00
|
|
|
if err == os.ErrNotExist {
|
2020-11-09 18:37:15 +00:00
|
|
|
logrus.WithField("path", path).Error("file not found on READ operation")
|
2020-09-27 19:23:47 +00:00
|
|
|
return -fuse.ENOENT
|
|
|
|
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-11-09 18:37:15 +00:00
|
|
|
logrus.WithError(err).WithField("path", path).Error("error getting holder reading data from file")
|
2020-09-27 19:23:47 +00:00
|
|
|
return -fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
end := int(math.Min(float64(len(dest)), float64(int64(file.Size())-off)))
|
|
|
|
if end < 0 {
|
|
|
|
end = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := dest[:end]
|
|
|
|
|
|
|
|
n, err := file.ReadAt(buf, off)
|
|
|
|
if err != nil && err != io.EOF {
|
2020-11-09 18:37:15 +00:00
|
|
|
logrus.WithError(err).WithField("path", path).Error("error reading data")
|
2020-09-27 19:23:47 +00:00
|
|
|
return -fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
dest = buf[:n]
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fs *FS) Release(path string, fh uint64) (errc int) {
|
2020-11-09 18:37:15 +00:00
|
|
|
if err := fs.fh.Remove(fh); err != nil {
|
|
|
|
logrus.WithError(err).WithField("path", path).Error("error getting holder when releasing file")
|
|
|
|
return -fuse.EIO
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
func (fs *FS) Releasedir(path string, fh uint64) int {
|
|
|
|
return fs.Release(path, fh)
|
|
|
|
}
|
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
func (fs *FS) Readdir(path string,
|
|
|
|
fill func(name string, stat *fuse.Stat_t, ofst int64) bool,
|
|
|
|
ofst int64,
|
|
|
|
fh uint64) (errc int) {
|
|
|
|
fill(".", nil, 0)
|
|
|
|
fill("..", nil, 0)
|
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
//TODO improve this function to make use of fh index if possible
|
|
|
|
paths, err := fs.fh.ListDir(path)
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithField("path", path).Error("error reading directory")
|
|
|
|
return -fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range paths {
|
|
|
|
if !fill(p, nil, 0) {
|
|
|
|
logrus.WithField("path", p).Error("error adding directory")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const fhNone = ^uint64(0)
|
|
|
|
|
|
|
|
var ErrHolderEmpty = errors.New("file holder is empty")
|
|
|
|
var ErrBadHolderIndex = errors.New("holder index too big")
|
|
|
|
|
|
|
|
type fileHandler struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
opened []fs.File
|
2021-03-06 22:08:15 +00:00
|
|
|
fs fs.Filesystem
|
2020-11-09 18:37:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *fileHandler) GetFile(path string, fhi uint64) (fs.File, error) {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
var out []string
|
2021-03-06 22:08:15 +00:00
|
|
|
files, err := fh.fs.ReadDir(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for p := range files {
|
|
|
|
out = append(out, p)
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, old := range fh.opened {
|
|
|
|
if old == nil {
|
|
|
|
fh.opened[i] = file
|
|
|
|
return uint64(i), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fh.opened = append(fh.opened, file)
|
|
|
|
|
|
|
|
return uint64(len(fh.opened) - 1), nil
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 18:37:15 +00:00
|
|
|
func (fh *fileHandler) get(fhi uint64) (fs.File, error) {
|
|
|
|
if int(fhi) >= len(fh.opened) {
|
|
|
|
return nil, ErrBadHolderIndex
|
|
|
|
}
|
|
|
|
// TODO check opened slice to avoid panics
|
|
|
|
h := fh.opened[int(fhi)]
|
|
|
|
if h == nil {
|
|
|
|
return nil, ErrHolderEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
return h, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *fileHandler) Remove(fhi uint64) error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
|
|
|
|
if fhi == fhNone {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO check opened slice to avoid panics
|
|
|
|
f := fh.opened[int(fhi)]
|
|
|
|
if f == nil {
|
|
|
|
return ErrHolderEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fh.opened[int(fhi)] = nil
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *fileHandler) lookupFile(path string) (fs.File, error) {
|
2021-03-06 22:08:15 +00:00
|
|
|
file, err := fh.fs.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-27 19:23:47 +00:00
|
|
|
|
2021-03-06 22:08:15 +00:00
|
|
|
if file != nil {
|
|
|
|
return file, nil
|
2020-09-27 19:23:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, os.ErrNotExist
|
|
|
|
}
|