gitforgefs/fs/root.go

101 lines
1.7 KiB
Go
Raw Normal View History

package fs
import (
"context"
"fmt"
"github.com/badjware/gitlabfs/git"
"github.com/badjware/gitlabfs/gitlab"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
const (
staticInodeStart = uint64(int(^(uint(0))>>1)) + 1
)
2020-12-31 20:00:10 +00:00
type staticNode interface {
fs.InodeEmbedder
Ino() uint64
Mode() uint32
}
type FSParam struct {
2020-12-30 00:49:11 +00:00
Git git.GitClonerPuller
2020-12-30 23:00:37 +00:00
Gitlab gitlab.GitlabFetcher
staticInoChan chan uint64
}
type rootNode struct {
fs.Inode
param *FSParam
rootGroupIds []int
userIds []int
}
var _ = (fs.NodeOnAdder)((*rootNode)(nil))
func (n *rootNode) OnAdd(ctx context.Context) {
projectsInode := n.NewPersistentInode(
ctx,
2020-12-30 00:49:11 +00:00
newProjectsNode(
n.rootGroupIds,
n.param,
),
fs.StableAttr{
Ino: <-n.param.staticInoChan,
Mode: fuse.S_IFDIR,
},
)
n.AddChild("projects", projectsInode, false)
usersInode := n.NewPersistentInode(
ctx,
2020-12-30 00:49:11 +00:00
newUsersNode(
n.userIds,
n.param,
),
fs.StableAttr{
Ino: <-n.param.staticInoChan,
Mode: fuse.S_IFDIR,
},
)
n.AddChild("users", usersInode, false)
2020-12-30 23:00:37 +00:00
fmt.Println("Mounted and ready to use")
}
2020-12-30 23:00:37 +00:00
func Start(mountpoint string, rootGroupIds []int, userIds []int, param *FSParam, debug bool) error {
fmt.Printf("Mounting in %v\n", mountpoint)
opts := &fs.Options{}
2020-12-30 23:00:37 +00:00
opts.Debug = debug
param.staticInoChan = make(chan uint64)
root := &rootNode{
param: param,
rootGroupIds: rootGroupIds,
userIds: userIds,
}
go staticInoGenerator(root.param.staticInoChan)
server, err := fs.Mount(mountpoint, root, opts)
if err != nil {
return fmt.Errorf("mount failed: %v", err)
}
server.Wait()
return nil
}
func staticInoGenerator(staticInoChan chan<- uint64) {
i := staticInodeStart
for {
staticInoChan <- i
i++
}
}