gitforgefs/fs/group.go

116 lines
2.8 KiB
Go
Raw Permalink Normal View History

package fs
import (
"context"
"syscall"
"github.com/badjware/gitlabfs/gitlab"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
type groupNode struct {
fs.Inode
param *FSParam
2020-12-31 20:00:10 +00:00
group *gitlab.Group
staticNodes map[string]staticNode
}
// Ensure we are implementing the NodeReaddirer interface
var _ = (fs.NodeReaddirer)((*groupNode)(nil))
// Ensure we are implementing the NodeLookuper interface
var _ = (fs.NodeLookuper)((*groupNode)(nil))
2020-12-30 00:49:11 +00:00
func newGroupNodeByID(gid int, param *FSParam) (*groupNode, error) {
group, err := param.Gitlab.FetchGroup(gid)
if err != nil {
return nil, err
}
node := &groupNode{
param: param,
group: group,
2020-12-31 20:00:10 +00:00
staticNodes: map[string]staticNode{
".refresh": newRefreshNode(group, param),
},
}
return node, nil
}
func newGroupNode(group *gitlab.Group, param *FSParam) (*groupNode, error) {
node := &groupNode{
param: param,
group: group,
2020-12-31 20:00:10 +00:00
staticNodes: map[string]staticNode{
".refresh": newRefreshNode(group, param),
},
}
return node, nil
}
func (n *groupNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
2020-12-30 00:49:11 +00:00
groupContent, _ := n.param.Gitlab.FetchGroupContent(n.group)
2020-12-31 20:00:10 +00:00
entries := make([]fuse.DirEntry, 0, len(groupContent.Groups)+len(groupContent.Projects)+len(n.staticNodes))
for _, group := range groupContent.Groups {
entries = append(entries, fuse.DirEntry{
2020-12-30 00:49:11 +00:00
Name: group.Name,
Ino: uint64(group.ID),
Mode: fuse.S_IFDIR,
})
}
2020-12-30 00:49:11 +00:00
for _, project := range groupContent.Projects {
entries = append(entries, fuse.DirEntry{
2020-12-30 00:49:11 +00:00
Name: project.Name,
Ino: uint64(project.ID),
Mode: fuse.S_IFLNK,
})
}
2020-12-31 20:00:10 +00:00
for name, staticNode := range n.staticNodes {
entries = append(entries, fuse.DirEntry{
Name: name,
Ino: staticNode.Ino(),
Mode: staticNode.Mode(),
})
}
return fs.NewListDirStream(entries), 0
}
func (n *groupNode) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fs.Inode, syscall.Errno) {
2020-12-30 00:49:11 +00:00
groupContent, _ := n.param.Gitlab.FetchGroupContent(n.group)
// Check if the map of groups contains it
group, ok := groupContent.Groups[name]
if ok {
attrs := fs.StableAttr{
Ino: uint64(group.ID),
Mode: fuse.S_IFDIR,
}
groupNode, _ := newGroupNode(group, n.param)
return n.NewInode(ctx, groupNode, attrs), 0
}
2020-12-30 00:49:11 +00:00
// Check if the map of projects contains it
project, ok := groupContent.Projects[name]
if ok {
attrs := fs.StableAttr{
2020-12-30 00:49:11 +00:00
Ino: uint64(project.ID),
Mode: fuse.S_IFLNK,
}
2020-12-30 00:49:11 +00:00
repositoryNode, _ := newRepositoryNode(project, n.param)
return n.NewInode(ctx, repositoryNode, attrs), 0
}
2020-12-31 20:00:10 +00:00
// Check if the map of static nodes contains it
staticNode, ok := n.staticNodes[name]
if ok {
attrs := fs.StableAttr{
Ino: staticNode.Ino(),
Mode: staticNode.Mode(),
}
return n.NewInode(ctx, staticNode, attrs), 0
}
return nil, syscall.ENOENT
}