2020-09-27 19:23:47 +00:00
|
|
|
package fuse
|
2020-04-27 16:46:23 +00:00
|
|
|
|
|
|
|
import (
|
2020-11-08 17:19:25 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"runtime"
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
"github.com/billziss-gh/cgofuse/fuse"
|
2021-03-01 16:43:28 +00:00
|
|
|
"github.com/distribyted/distribyted/fs"
|
2021-03-10 09:54:56 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2020-04-27 16:46:23 +00:00
|
|
|
)
|
|
|
|
|
2020-05-18 17:42:23 +00:00
|
|
|
type Handler struct {
|
2020-11-13 11:06:33 +00:00
|
|
|
fuseAllowOther bool
|
2021-04-04 17:24:58 +00:00
|
|
|
path string
|
2020-11-13 11:06:33 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
host *fuse.FileSystemHost
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
func NewHandler(fuseAllowOther bool, path string) *Handler {
|
2020-05-18 17:42:23 +00:00
|
|
|
return &Handler{
|
2020-11-13 11:06:33 +00:00
|
|
|
fuseAllowOther: fuseAllowOther,
|
2021-04-04 17:24:58 +00:00
|
|
|
path: path,
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
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" {
|
|
|
|
folder = path.Dir(s.path)
|
|
|
|
}
|
|
|
|
if err := os.MkdirAll(folder, 0744); err != nil && !os.IsExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-18 17:42:23 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
cfs, err := fs.NewContainerFs(fss)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-02 12:06:18 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
host := fuse.NewFileSystemHost(NewFS(cfs))
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
// TODO improve error handling here
|
|
|
|
go func() {
|
|
|
|
var config []string
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
if s.fuseAllowOther {
|
|
|
|
config = append(config, "-o", "allow_other")
|
|
|
|
}
|
2020-11-13 11:06:33 +00:00
|
|
|
|
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
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2021-11-16 12:13:58 +00:00
|
|
|
log.Info().Str("path", folder).Msg("starting FUSE mount")
|
|
|
|
|
2020-04-27 16:46:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-04 17:24:58 +00:00
|
|
|
func (s *Handler) Unmount() {
|
|
|
|
if s.host == nil {
|
|
|
|
return
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
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")
|
|
|
|
}
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|