tstor/fuse/handler.go

79 lines
1.4 KiB
Go
Raw Normal View History

package fuse
import (
"os"
2021-11-18 19:36:07 +00:00
"path/filepath"
"runtime"
"github.com/billziss-gh/cgofuse/fuse"
"github.com/distribyted/distribyted/fs"
"github.com/rs/zerolog/log"
)
type Handler struct {
fuseAllowOther bool
2021-04-04 17:24:58 +00:00
path string
2021-04-04 17:24:58 +00:00
host *fuse.FileSystemHost
}
2021-04-04 17:24:58 +00:00
func NewHandler(fuseAllowOther bool, path string) *Handler {
return &Handler{
fuseAllowOther: fuseAllowOther,
2021-04-04 17:24:58 +00:00
path: path,
}
}
func (s *Handler) Mount(fss map[string]fs.Filesystem) error {
2021-04-04 17:24:58 +00:00
folder := s.path
// On windows, the folder must don't exist
if runtime.GOOS == "windows" {
2021-11-18 19:36:07 +00:00
folder = filepath.Dir(s.path)
2021-04-04 17:24:58 +00:00
}
if filepath.VolumeName(folder) == "" {
if err := os.MkdirAll(folder, 0744); err != nil && !os.IsExist(err) {
return err
}
2021-04-04 17:24:58 +00:00
}
2021-04-04 17:24:58 +00:00
cfs, err := fs.NewContainerFs(fss)
if err != nil {
return err
}
2021-04-04 17:24:58 +00:00
host := fuse.NewFileSystemHost(NewFS(cfs))
2021-04-04 17:24:58 +00:00
// TODO improve error handling here
go func() {
var config []string
2021-04-04 17:24:58 +00:00
if s.fuseAllowOther {
config = append(config, "-o", "allow_other")
}
2021-04-04 17:24:58 +00:00
ok := host.Mount(s.path, config)
if !ok {
log.Error().Str("path", s.path).Msg("error trying to mount filesystem")
}
}()
s.host = host
log.Info().Str("path", s.path).Msg("starting FUSE mount")
return nil
}
2021-04-04 17:24:58 +00:00
func (s *Handler) Unmount() {
if s.host == nil {
return
}
2021-03-01 18:04:59 +00:00
2021-04-04 17:24:58 +00:00
ok := s.host.Unmount()
if !ok {
//TODO try to force unmount if possible
log.Error().Str("path", s.path).Msg("unmount failed")
}
}