<commit_before>package zk

import (
	"bazil.org/fuse"
	"bazil.org/fuse/fs"
	"bazil.org/fuse/fuseutil"
	"github.com/golang/glog"
	"github.com/qorio/maestro/pkg/registry"
	"golang.org/x/net/context"
	"os"
	"sync"
	"syscall"
)

type fsNode struct {
	Path registry.Path
	conn ZK
	node *Node
	Dir  *Dir
}

type FS struct {
	*fsNode
	fuse_conn  *fuse.Conn
	mountpoint *string
}

func NewFS(zc ZK, path registry.Path) *FS {
	return &FS{
		fsNode: fs_node(zc, nil, path, nil),
	}
}

func (this *FS) Unmount() error {
	if this.mountpoint != nil {
		return fuse.Unmount(*this.mountpoint)
	}
	return nil
}

func (this *FS) Shutdown() error {
	if err := this.Unmount(); err == nil {
		return this.fuse_conn.Close()
	} else {
		return err
	}
}

func (this *FS) Mount(dir string, perm os.FileMode) error {
	if err := os.MkdirAll(dir, perm); err != nil {
		return err
	}
	fc, err := fuse.Mount(dir)
	if err != nil {
		return err
	}
	this.fuse_conn = fc
	this.mountpoint = &dir
	return fs.Serve(this.fuse_conn, this)
}

func (this *FS) Wait() error {
	if this.fuse_conn != nil {
		<-this.fuse_conn.Ready
	}
	return this.fuse_conn.MountError
}

type Dir struct {
	*fsNode
}

type File struct {
	*fsNode

	// lock
	mu sync.Mutex
	// number of write-capable handles currently open
	writers uint
}

var _ = fs.FS(&FS{})

func fs_node(zc ZK, n *Node, p registry.Path, d *Dir) *fsNode {
	return &fsNode{
		Dir:  d,
		Path: p,
		conn: zc,
		node: n,
	}
}

func (this *FS) Root() (fs.Node, error) {
	if this.node == nil {
		if n, err := Follow(this.conn, this.Path); err != nil {
			return nil, err
		} else {
			this.node = n
		}
	}
	n := &Dir{
		fsNode: fs_node(this.conn, this.node, this.Path, nil),
	}
	return n, nil
}

var _ = fs.Node(&Dir{})

func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
	a.Mode = os.ModeDir | 0755
	return nil
}

var _ = fs.HandleReadDirAller(&Dir{})

func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
	var result []fuse.Dirent

	if children, err := d.node.Children(); err != nil {
		return nil, err
	} else {
		for _, c := range children {
			de := fuse.Dirent{
				Name: c.GetBasename(),
			}
			if c.CountChildren() == 0 {
				de.Type = fuse.DT_File
			} else {
				de.Type = fuse.DT_Dir
			}
			result = append(result, de)
		}
		return result, nil
	}<commit_msg>Logging<commit_after>package zk

import (
	"bazil.org/fuse"
	"bazil.org/fuse/fs"
	"bazil.org/fuse/fuseutil"
	"github.com/golang/glog"
	"github.com/qorio/maestro/pkg/registry"
	"golang.org/x/net/context"
	"os"
	"sync"
	"syscall"
)

type fsNode struct {
	Path registry.Path
	conn ZK
	node *Node
	Dir  *Dir
}

type FS struct {
	*fsNode
	fuse_conn  *fuse.Conn
	mountpoint *string
}

func NewFS(zc ZK, path registry.Path) *FS {
	return &FS{
		fsNode: fs_node(zc, nil, path, nil),
	}
}

func (this *FS) Unmount() error {
	if this.mountpoint != nil {
		err := fuse.Unmount(*this.mountpoint)
		glog.V(100).Infoln("Unmount dir=", this.mountpoint, "Err=", err)
		return err
	}
	return nil
}

func (this *FS) Shutdown() error {
	if err := this.Unmount(); err == nil {
		return this.fuse_conn.Close()
	} else {
		return err
	}
}

func (this *FS) Mount(dir string, perm os.FileMode) error {
	if err := os.MkdirAll(dir, perm); err != nil {
		return err
	}
	fc, err := fuse.Mount(dir)
	glog.V(100).Infoln("Mounting directory", dir, "Err=", err)
	if err != nil {
		return err
	}
	this.fuse_conn = fc
	this.mountpoint = &dir
	return fs.Serve(this.fuse_conn, this)
}

func (this *FS) Wait() error {
	if this.fuse_conn != nil {
		<-this.fuse_conn.Ready
	}
	return this.fuse_conn.MountError
}

type Dir struct {
	*fsNode
}

type File struct {
	*fsNode

	// lock
	mu sync.Mutex
	// number of write-capable handles currently open
	writers uint
}

var _ = fs.FS(&FS{})

func fs_node(zc ZK, n *Node, p registry.Path, d *Dir) *fsNode {
	return &fsNode{
		Dir:  d,
		Path: p,
		conn: zc,
		node: n,
	}
}

func (this *FS) Root() (fs.Node, error) {
	if this.node == nil {
		if n, err := Follow(this.conn, this.Path); err != nil {
			return nil, err
		} else {
			this.node = n
		}
	}
	n := &Dir{
		fsNode: fs_node(this.conn, this.node, this.Path, nil),
	}
	glog.V(100).Infoln("Root=", this.Path, "Node=", this.node, "Dir=", n)
	return n, nil
}

var _ = fs.Node(&Dir{})

func (d *Dir) Attr(ctx context.Context, a *fuse.Attr) error {
	a.Mode = os.ModeDir | 0755
	return nil
}

var _ = fs.HandleReadDirAller(&Dir{})

func (d *Dir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) {
	var result []fuse.Dirent

	if children, err := d.node.Children(); err != nil {
		glog.Warningln("Error reading children of", d.node)
		return nil, err
	} else {
		for _, c := range children {
			de := fuse.Dirent{
				Name: c.GetBasename(),
			}
			if c.CountChildren() == 0 {
				de.Type = fuse.DT_File
			} else {
				de.Type = fuse.DT_Dir
			}
			result = append(result, de)
			glog.V(100).Infoln("Dirent=", de, "node=", c)
		}
		return result, nil
	}
{'fraction_non_alphanumeric': 0.1598860862489829, 'fraction_numerical': 0.004475183075671277, 'mean_word_length': 3.1704834605597965, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1014513', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2051, 'n_words': 517}