gitforgefs/fstree/group.go

116 lines
2.9 KiB
Go
Raw Normal View History

package fstree
import (
"context"
"syscall"
"github.com/hanwen/go-fuse/v2/fs"
"github.com/hanwen/go-fuse/v2/fuse"
)
2024-08-09 20:34:45 +00:00
const (
groupBaseInode = 1_000_000_000
)
type groupNode struct {
fs.Inode
param *FSParam
2020-12-31 20:00:10 +00:00
source GroupSource
2020-12-31 20:00:10 +00:00
staticNodes map[string]staticNode
}
type GroupSource interface {
GetGroupID() uint64
InvalidateContentCache()
}
// Ensure we are implementing the NodeReaddirer interface
var _ = (fs.NodeReaddirer)((*groupNode)(nil))
// Ensure we are implementing the NodeLookuper interface
var _ = (fs.NodeLookuper)((*groupNode)(nil))
func newGroupNodeFromSource(source GroupSource, param *FSParam) (*groupNode, error) {
node := &groupNode{
param: param,
source: source,
2020-12-31 20:00:10 +00:00
staticNodes: map[string]staticNode{
".refresh": newRefreshNode(source, param),
2020-12-31 20:00:10 +00:00
},
}
return node, nil
}
func (n *groupNode) Readdir(ctx context.Context) (fs.DirStream, syscall.Errno) {
2024-08-05 01:45:51 +00:00
groups, repositories, err := n.param.GitForge.FetchGroupContent(n.source.GetGroupID())
if err != nil {
2024-06-06 05:51:34 +00:00
n.param.logger.Error(err.Error())
}
entries := make([]fuse.DirEntry, 0, len(groups)+len(repositories)+len(n.staticNodes))
for groupName, group := range groups {
entries = append(entries, fuse.DirEntry{
Name: groupName,
2024-08-09 20:34:45 +00:00
Ino: group.GetGroupID() + groupBaseInode,
Mode: fuse.S_IFDIR,
})
}
for repositoryName, repository := range repositories {
entries = append(entries, fuse.DirEntry{
Name: repositoryName,
2024-08-09 20:34:45 +00:00
Ino: repository.GetRepositoryID() + repositoryBaseInode,
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) {
2024-08-05 01:45:51 +00:00
groups, repositories, err := n.param.GitForge.FetchGroupContent(n.source.GetGroupID())
2024-08-04 22:59:57 +00:00
if err != nil {
n.param.logger.Error(err.Error())
} else {
// Check if the map of groups contains it
group, found := groups[name]
if found {
attrs := fs.StableAttr{
2024-08-09 20:34:45 +00:00
Ino: group.GetGroupID() + groupBaseInode,
2024-08-04 22:59:57 +00:00
Mode: fuse.S_IFDIR,
}
groupNode, _ := newGroupNodeFromSource(group, n.param)
return n.NewInode(ctx, groupNode, attrs), 0
}
2024-08-04 22:59:57 +00:00
// Check if the map of projects contains it
repository, found := repositories[name]
if found {
attrs := fs.StableAttr{
2024-08-09 20:34:45 +00:00
Ino: repository.GetRepositoryID() + repositoryBaseInode,
2024-08-04 22:59:57 +00:00
Mode: fuse.S_IFLNK,
}
repositoryNode, _ := newRepositoryNodeFromSource(repository, n.param)
return n.NewInode(ctx, repositoryNode, attrs), 0
}
2020-12-31 20:00:10 +00:00
2024-08-04 22:59:57 +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
2020-12-31 20:00:10 +00:00
}
}
return nil, syscall.ENOENT
}