<commit_before>package fuse

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"io"
	"os"
	"path"
)

const (
	bufSize = 66000
)

type FileSystem interface {
	List(parent string) (names []string, status Status)
	GetAttr(path string) (out *Attr, status Status)
}

type Mounted interface {
	Unmount() (err os.Error)
}

func Mount(mountPoint string, fs FileSystem) (m Mounted, err os.Error, errors chan os.Error) {
	f, m, err := mount(mountPoint)
	if err != nil {
		return
	}
	errors = make(chan os.Error, 100)
	go loop(f, fs, errors)
	return
}

func loop(f *os.File, fs FileSystem, errors chan os.Error) {
	buf := make([]byte, bufSize)
	defer close(errors)
	toW := make(chan [][]byte, 100)
	defer close(toW)
	go writer(f, toW, errors)
	managerReq := make(chan *managerRequest, 100)
	startManager(fs, managerReq)
	c := &managerClient{managerReq}
	defer close(managerReq)
	for {
		n, err := f.Read(buf)
		if err == os.EOF {
			break
		}
		if err != nil {
			errors <- os.NewError(fmt.Sprintf("Failed to read from fuse conn: %v", err))
			break
		}

		go dispatch(fs, buf[0:n], c, toW, errors)
	}
}

func dispatch(fs FileSystem, in_data []byte, c *managerClient, toW chan [][]byte, errors chan os.Error) {
	fmt.Printf("in_data: %v\n", in_data)
	r := bytes.NewBuffer(in_data)
	h := new(InHeader)
	err := binary.Read(r, binary.LittleEndian, h)
	if err == os.EOF {
		err = os.NewError(fmt.Sprintf("MountPoint, handle: can't read a header, in_data: %v", in_data))
	}
	if err != nil {
		errors <- err
		return
	}
	var out [][]byte
	fmt.Printf("Opcode: %v, NodeId: %v, h: %v\n", h.Opcode, h.NodeId, h)
	switch h.Opcode {
	case FUSE_INIT:
		out = initFuse(fs, h, r, c)
	case FUSE_FORGET:
		return
	case FUSE_GETATTR:
		out = getAttr(fs, h, r, c)
	case FUSE_GETXATTR:
		out = getXAttr(h, r, c)
	case FUSE_OPENDIR:
		out = openDir(h, r, c)
	case FUSE_READDIR:
		out = readDir(h, r, c)
	case FUSE_LOOKUP:
		out = lookup(h, r, c)
	case FUSE_RELEASEDIR:
		out = releaseDir(h, r, c)
	default:
		errors <- os.NewError(fmt.Sprintf("Unsupported OpCode: %d", h.Opcode))
		out = serialize(h, ENOSYS, nil)
	}
	if err != nil {
		errors <- err
		out = serialize(h, EIO, nil)
	}
	if out == nil || len(out) == 0 {
		fmt.Printf("out is empty\n")
		return
	}

	fmt.Printf("Sending to writer: %v\n", out)
	toW <- out
}

func serialize(h *InHeader, res Status, out interface{}) (data [][]byte) {
	b := new(bytes.Buffer)
	out_data := make([]byte, 0)
	fmt.Printf("OpCode: %v result: %v\n", h.Opcode, res)
	if out != nil && res == OK {
		fmt.Printf("out = %v, out == nil: %v\n", out, out == nil)
		err := binary.Write(b, binary.LittleEndian, out)
		if err == nil {
			out_data = b.Bytes()
		} else {
			panic(fmt.Sprintf("Can't serialize out: %v, err: %v", out, err))
		}
	}
	fmt.Printf("out_data: %v, len(out_data): %d, SizeOfOutHeader: %d\n", out_data, len(out_data), SizeOfOutHeader)
	var hout OutHeader
	hout.Unique = h.Unique
	hout.Status = res
	hout.Length = uint32(len(out_data) + SizeOfOutHeader)
	b = new(bytes.Buffer)
	err := binary.Write(b, binary.LittleEndian, &hout)
	if err != nil {
		panic("Can't serialize OutHeader")
	}<commit_msg>dispatch is splitted to handle and dispatch<commit_after>package fuse

import (
	"bytes"
	"encoding/binary"
	"fmt"
	"io"
	"os"
	"path"
)

const (
	bufSize = 66000
)

type FileSystem interface {
	List(parent string) (names []string, status Status)
	GetAttr(path string) (out *Attr, status Status)
}

type Mounted interface {
	Unmount() (err os.Error)
}

func Mount(mountPoint string, fs FileSystem) (m Mounted, err os.Error, errors chan os.Error) {
	f, m, err := mount(mountPoint)
	if err != nil {
		return
	}
	errors = make(chan os.Error, 100)
	go loop(f, fs, errors)
	return
}

func loop(f *os.File, fs FileSystem, errors chan os.Error) {
	buf := make([]byte, bufSize)
	defer close(errors)
	toW := make(chan [][]byte, 100)
	defer close(toW)
	go writer(f, toW, errors)
	managerReq := make(chan *managerRequest, 100)
	startManager(fs, managerReq)
	c := &managerClient{managerReq}
	defer close(managerReq)
	for {
		n, err := f.Read(buf)
		if err == os.EOF {
			break
		}
		if err != nil {
			errors <- os.NewError(fmt.Sprintf("Failed to read from fuse conn: %v", err))
			break
		}

		go handle(fs, buf[0:n], c, toW, errors)
	}
}

func handle(fs FileSystem, in_data []byte, c *managerClient, toW chan [][]byte, errors chan os.Error) {
	fmt.Printf("in_data: %v\n", in_data)
	r := bytes.NewBuffer(in_data)
	h := new(InHeader)
	err := binary.Read(r, binary.LittleEndian, h)
	if err == os.EOF {
		err = os.NewError(fmt.Sprintf("MountPoint, handle: can't read a header, in_data: %v", in_data))
	}
	if err != nil {
		errors <- err
		return
	}
	out := dispatch(fs, h, r, c, errors)
	if out == nil {
		fmt.Printf("out is nil")
		return
	}
	fmt.Printf("Sending to writer: %v\n", out)
	toW <- out
}

func dispatch(fs FileSystem, h *InHeader, r *bytes.Buffer, c *managerClient, errors chan os.Error) (out [][]byte) {
	fmt.Printf("Opcode: %v, NodeId: %v, h: %v\n", h.Opcode, h.NodeId, h)
	switch h.Opcode {
	case FUSE_INIT:
		return initFuse(fs, h, r, c)
	case FUSE_FORGET:
		return nil
	case FUSE_GETATTR:
		return getAttr(fs, h, r, c)
	case FUSE_GETXATTR:
		return getXAttr(h, r, c)
	case FUSE_OPENDIR:
		return openDir(h, r, c)
	case FUSE_READDIR:
		return readDir(h, r, c)
	case FUSE_LOOKUP:
		return lookup(h, r, c)
	case FUSE_RELEASEDIR:
		return releaseDir(h, r, c)
	default:
		errors <- os.NewError(fmt.Sprintf("Unsupported OpCode: %d", h.Opcode))
		return serialize(h, ENOSYS, nil)
	}
	return
}

func serialize(h *InHeader, res Status, out interface{}) (data [][]byte) {
	b := new(bytes.Buffer)
	out_data := make([]byte, 0)
	fmt.Printf("OpCode: %v result: %v\n", h.Opcode, res)
	if out != nil && res == OK {
		fmt.Printf("out = %v, out == nil: %v\n", out, out == nil)
		err := binary.Write(b, binary.LittleEndian, out)
		if err == nil {
			out_data = b.Bytes()
		} else {
			panic(fmt.Sprintf("Can't serialize out: %v, err: %v", out, err))
		}
	}
	fmt.Printf("out_data: %v, len(out_data): %d, SizeOfOutHeader: %d\n", out_data, len(out_data), SizeOfOutHeader)
	var hout OutHeader
	hout.Unique = h.Unique
	hout.Status = res
	hout.Length = uint32(len(out_data) + SizeOfOutHeader)
	b = new(bytes.Buffer)
	err := binary.Write(b, binary.LittleEndian, &hout)
	if err != nil {
		panic("Can't serialize OutHeader")
	}
{'fraction_non_alphanumeric': 0.16148969889064976, 'fraction_numerical': 0.005863708399366086, 'mean_word_length': 3.8998447204968945, 'pattern_counts': {'":': 0, '<': 12, '<?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': '288010', 'n_tokens_mistral': 2673, 'n_tokens_neox': 2555, 'n_words': 700}