<commit_before>package RiverFS

import (
	"bazil.org/fuse"
	"bazil.org/fuse/fs"
	//"github.com/goraft/raft"
	"os"
	"sync"
)

type RootDir struct {
	Dir *Dir
}

func NewRootDir() (r *RootDir) {
	r = &RootDir{Dir: NewDir()}
	return
}

func (nf *RootDir) Root() (fs.Node, fuse.Error) {
	return nf.Dir, nil
}

type Node struct {
	Name string
	Mode os.FileMode
}

func (n *Node) Attr() fuse.Attr {
	return fuse.Attr{Mode: n.Mode}
}

func (n *Node) Setattr(req *fuse.SetattrRequest, resp *fuse.SetattrResponse, intr fs.Intr) fuse.Error {
	n.Mode = req.Mode
	return nil
}

type Dir struct {
	Node
	rwmut    sync.RWMutex
	Children map[string]fs.Node
}

func NewDir() (d *Dir) {
	d = &Dir{}
	d.Node = Node{}
	d.Mode = os.ModeDir
	d.Children = make(map[string]fs.Node)
	return
}

func (d *Dir) Add(name string, n fs.Node) {
	//TODO: consensous
	//TODO: Check if exists and error
	d.rwmut.Lock()
	d.Children[name] = n
	d.rwmut.Unlock()

}

func (d *Dir) Rm(name string) {
	//TODO: consensous
	//TODO: Check if exists and error
	//TODO: clean up disk space
	d.rwmut.Lock()
	delete(d.Children, name)
	d.rwmut.Unlock()

}

func (d *Dir) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (n fs.Node, ferr fuse.Error) {
	ndir := NewDir()
	ndir.Name = req.Name
	ndir.Mode = req.Mode
	d.Add(req.Name, ndir)
	return ndir, nil

}

func (d *Dir) Remove(req *fuse.RemoveRequest, intr fs.Intr) fuse.Error {
	d.Rm(req.Name)
	return nil
}

func (d *Dir) Rename(req *fuse.RenameRequest, newDir fs.Node, intr fs.Intr) fuse.Error {

	d.rwmut.RLock()
	node, ok := d.Children[req.OldName]
	d.rwmut.RUnlock()
	if !ok {
		return fuse.ENOENT
	}
	d.Rm(req.OldName)

	switch node.(type) {
	case *Dir:
		node.(*Dir).Name = req.NewName
	case *File:
		node.(*File).Name = req.NewName
	}

	newDir.(*Dir).Add(req.NewName, node)

	return nil

}

func (d *Dir) Create(req *fuse.CreateRequest, resp *fuse.CreateResponse, intr fs.Intr) (fs.Node, fs.Handle, fuse.Error) {

	n := &File{}
	n.Mode = req.Mode
	n.Name = req.Name
	d.Add(req.Name, n)
	return n, n, nil
}

// func (d *Dir) Mknod(req *fuse.MknodRequest, intr fs.Intr) (n fs.Node, ferr fuse.Error) {
// 	switch {
// 	case req.Mode.IsDir():
// 		n = NewDir()
// 	case req.Mode.IsRegular():
// 		n = &File{}
// 	default:
// 		return n, fuse.ENOSYS
// 	}
// 	n.(*Node).Mode = req.Mode
// 	n.(*Node).Name = req.Name
// 	d.Add(req.Name, n)
// 	return
// }

func (d *Dir) Lookup(name string, intr fs.Intr) (fs fs.Node, err fuse.Error) {
	d.rwmut.RLock()
	fs, ok := d.Children[name]
	d.rwmut.RUnlock()
	if !ok {
		return nil, fuse.ENOENT
	}

	return
}

func (d *Dir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
	var out []fuse.Dirent
<commit_msg>all needed fs stuff except file io working via fuse<commit_after>package RiverFS

import (
	"bazil.org/fuse"
	"bazil.org/fuse/fs"
	//"github.com/goraft/raft"
	"os"
	"sync"
)

type RootDir struct {
	Dir *Dir
}

func NewRootDir() (r *RootDir) {
	r = &RootDir{Dir: NewDir()}
	return
}

func (nf *RootDir) Root() (fs.Node, fuse.Error) {
	return nf.Dir, nil
}

type Node struct {
	Name string
	Mode os.FileMode
}

func (n *Node) Attr() fuse.Attr {
	return fuse.Attr{Mode: n.Mode}
}

func (n *Node) Setattr(req *fuse.SetattrRequest, resp *fuse.SetattrResponse, intr fs.Intr) fuse.Error {
	n.Mode = req.Mode
	return nil
}

type Dir struct {
	Node
	rwmut    sync.RWMutex
	Children map[string]fs.Node
}

func NewDir() (d *Dir) {
	d = &Dir{}
	d.Node = Node{}
	d.Mode = os.ModeDir
	d.Children = make(map[string]fs.Node)
	return
}

func (d *Dir) Add(name string, n fs.Node) {
	//TODO: consensous
	//TODO: Check if exists and error
	d.rwmut.Lock()
	d.Children[name] = n
	d.rwmut.Unlock()

}

func (d *Dir) Rm(name string) {
	//TODO: consensous
	//TODO: Check if exists and error
	//TODO: clean up disk space
	d.rwmut.Lock()
	delete(d.Children, name)
	d.rwmut.Unlock()

}

func (d *Dir) Mkdir(req *fuse.MkdirRequest, intr fs.Intr) (n fs.Node, ferr fuse.Error) {
	ndir := NewDir()
	ndir.Name = req.Name
	ndir.Mode = req.Mode
	d.Add(req.Name, ndir)
	return ndir, nil

}

func (d *Dir) Remove(req *fuse.RemoveRequest, intr fs.Intr) fuse.Error {
	d.Rm(req.Name)
	return nil
}

func (d *Dir) Rename(req *fuse.RenameRequest, newDir fs.Node, intr fs.Intr) fuse.Error {

	d.rwmut.RLock()
	node, ok := d.Children[req.OldName]
	d.rwmut.RUnlock()
	if !ok {
		return fuse.ENOENT
	}
	d.Rm(req.OldName)

	switch node.(type) {
	case *Dir:
		node.(*Dir).Name = req.NewName
	case *File:
		node.(*File).Name = req.NewName
	}

	newDir.(*Dir).Add(req.NewName, node)

	return nil

}

func (d *Dir) Create(req *fuse.CreateRequest, resp *fuse.CreateResponse, intr fs.Intr) (fs.Node, fs.Handle, fuse.Error) {

	n := &File{}
	n.Mode = req.Mode
	n.Name = req.Name
	d.Add(req.Name, n)
	return n, n, nil
}

func (d *Dir) Lookup(name string, intr fs.Intr) (fs fs.Node, err fuse.Error) {
	d.rwmut.RLock()
	fs, ok := d.Children[name]
	d.rwmut.RUnlock()
	if !ok {
		return nil, fuse.ENOENT
	}

	return
}

func (d *Dir) ReadDir(intr fs.Intr) ([]fuse.Dirent, fuse.Error) {
	var out []fuse.Dirent

{'fraction_non_alphanumeric': 0.16973210715713716, 'fraction_numerical': 0.0, 'mean_word_length': 3.327854671280277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4896099', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2181, 'n_words': 528}