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/config"
|
|
|
|
"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
|
|
|
|
|
2020-09-27 19:23:47 +00:00
|
|
|
hosts map[string]*fuse.FileSystemHost
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
func NewHandler(fuseAllowOther bool) *Handler {
|
2020-05-18 17:42:23 +00:00
|
|
|
return &Handler{
|
2020-11-13 11:06:33 +00:00
|
|
|
fuseAllowOther: fuseAllowOther,
|
|
|
|
hosts: make(map[string]*fuse.FileSystemHost),
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-06 22:08:15 +00:00
|
|
|
func (s *Handler) MountAll(fss map[string]fs.Filesystem, ef config.EventFunc) error {
|
2021-03-01 18:04:59 +00:00
|
|
|
for p, fss := range fss {
|
|
|
|
folder := p
|
|
|
|
// On windows, the folder must don't exist
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
folder = path.Dir(folder)
|
2020-05-18 17:42:23 +00:00
|
|
|
}
|
2021-03-01 18:04:59 +00:00
|
|
|
if err := os.MkdirAll(folder, 0744); err != nil && !os.IsExist(err) {
|
2020-04-27 16:46:23 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-05-18 17:42:23 +00:00
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
host := fuse.NewFileSystemHost(NewFS(fss))
|
2020-05-02 12:06:18 +00:00
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
// TODO improve error handling here
|
|
|
|
go func() {
|
|
|
|
var config []string
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
if s.fuseAllowOther {
|
|
|
|
config = append(config, "-o", "allow_other")
|
|
|
|
}
|
2020-04-27 16:46:23 +00:00
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
ok := host.Mount(p, config)
|
|
|
|
if !ok {
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Error().Str("path", p).Msg("error trying to mount filesystem")
|
2021-03-01 18:04:59 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-11-13 11:06:33 +00:00
|
|
|
|
2021-03-01 18:04:59 +00:00
|
|
|
s.hosts[p] = host
|
|
|
|
}
|
2020-04-27 16:46:23 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
func (s *Handler) UnmountAll() {
|
2020-09-27 19:23:47 +00:00
|
|
|
for path, server := range s.hosts {
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Info().Str("path", path).Msg("unmounting")
|
2020-09-27 19:23:47 +00:00
|
|
|
ok := server.Unmount()
|
|
|
|
if !ok {
|
2020-07-14 11:55:08 +00:00
|
|
|
//TODO try to force unmount if possible
|
2021-03-10 09:54:56 +00:00
|
|
|
log.Error().Str("path", path).Msg("unmount failed")
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 18:04:59 +00:00
|
|
|
|
2020-11-08 17:19:25 +00:00
|
|
|
s.hosts = make(map[string]*fuse.FileSystemHost)
|
2020-04-27 16:46:23 +00:00
|
|
|
}
|