Commit a6dfd5ae authored by Ivan Krasin's avatar Ivan Krasin

something corrups the memory. test raises a panic and the place seems to be random.

parent ea88e396
......@@ -15,14 +15,17 @@ import (
)
const (
bufSize = 65536 + 100 // See the link above for the details
bufSize = 66000
)
type FileSystem interface{}
type FileSystem interface{
Init(in *InitIn) (out *InitOut, code Error)
}
type MountPoint struct {
mountPoint string
f *os.File
fs FileSystem
}
// Mount create a fuse fs on the specified mount point.
......@@ -63,7 +66,7 @@ func Mount(mountPoint string, fs FileSystem) (m *MountPoint, err os.Error) {
if err != nil {
return
}
m = &MountPoint{mountPoint, f}
m = &MountPoint{mountPoint, f, fs}
go m.loop()
return
}
......@@ -88,36 +91,76 @@ func (m *MountPoint) loop() {
}
}
func (m *MountPoint) handle(in []byte, toW chan [][]byte, errors chan os.Error) {
r := bytes.NewBuffer(in)
var h InHeader
err := binary.Read(r, binary.LittleEndian, &h)
func (m *MountPoint) handle(in_data []byte, toW chan [][]byte, errors chan os.Error) {
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: %v", in))
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 interface{}
var result Error = OK
switch h.Opcode {
// case FUSE_INIT: // I too want to sleep. Will continue later.
case FUSE_INIT:
in := new(InitIn)
err = binary.Read(r, binary.LittleEndian, in)
if err != nil {
break
}
fmt.Printf("in: %v\n", in)
var init_out *InitOut
init_out, result = m.fs.Init(in)
if init_out != nil {
out = init_out
}
default:
errors <- os.NewError(fmt.Sprintf("Unsupported OpCode: %d", h.Opcode))
fmt.Printf("Unsupported OpCode: %d\n", h.Opcode)
// TODO: report an error to the kernel
os.Exit(1)
result = EIO
}
}
func (m *MountPoint) writer(f *os.File, in chan [][]byte, errors chan os.Error) {
fd := f.Fd()
for packet := range in {
_, err := Writev(fd, packet)
if err != nil {
errors <- err
continue
out = nil
result = EIO
// Add sending result msg with error
}
b := new(bytes.Buffer)
out_data := make([]byte, 0)
if out != nil && result == OK {
fmt.Printf("out = %v, out == nil: %v\n", out, out == nil)
return
err = binary.Write(b, binary.LittleEndian, out)
if err == nil {
out_data = b.Bytes()
} else {
errors <- os.NewError(fmt.Sprintf("Can serialize out: %v", err))
}
}
var hout OutHeader
hout.Unique = h.Unique
hout.Error = int32(result)
hout.Length = uint32(len(out_data) + SizeOfOutHeader)
b = new(bytes.Buffer)
err = binary.Write(b, binary.LittleEndian, &hout)
if err != nil {
errors <- err
return
}
_, _ = b.Write(out_data)
toW <- [][]byte { b.Bytes() }
}
func (m *MountPoint) writer(f *os.File, in chan [][]byte, errors chan os.Error) {
// fd := f.Fd()
for _ = range in {
// _, err := Writev(fd, packet)
// if err != nil {
// errors <- err
// continue
// }
}
}
......
package fuse
import (
"fmt"
"os"
"testing"
)
......@@ -11,6 +12,22 @@ const (
type testFuse struct {}
func (fs *testFuse) Init(in *InitIn) (out *InitOut, code Error) {
if (in.Major != FUSE_KERNEL_VERSION) {
fmt.Printf("Major versions does not match. Given %d, want %d\n", in.Major, FUSE_KERNEL_VERSION)
return nil, EIO
}
if (in.Minor < FUSE_KERNEL_MINOR_VERSION) {
fmt.Printf("Minor version is less than we support. Given %d, want at least %d\n", in.Minor, FUSE_KERNEL_MINOR_VERSION)
return nil, EIO
}
out.Major = FUSE_KERNEL_VERSION
out.Minor = FUSE_KERNEL_MINOR_VERSION
out.MaxReadahead = 65536
out.MaxWrite = 65536
return
}
func TestMount(t *testing.T) {
fs := new(testFuse)
err := os.Mkdir(tempMountDir, 0777)
......
package fuse
import (
"syscall"
)
const (
/** Version number of this interface */
......@@ -124,6 +128,14 @@ const (
CUSE_INIT_INFO_MAX = 4096
)
type Error int32
const (
OK = Error(0)
EIO = Error(syscall.EIO)
ENOSYS = Error(syscall.ENOSYS)
)
type Opcode int
const (
......@@ -402,10 +414,10 @@ type InitIn struct {
type InitOut struct {
Major uint32
Minor uint32
Max_readahead uint32
MaxReadahead uint32
Flags uint32
Max_background uint16
Congestion_threshold uint16
MaxBackground uint16
CongestionThreshold uint16
MaxWrite uint32
}
......@@ -423,8 +435,8 @@ type CuseInitOut struct {
Flags uint32
Max_read uint32
MaxWrite uint32
Dev_major uint32 /* chardev major */
Dev_minor uint32 /* chardev minor */
DevMajor uint32 /* chardev major */
DevMinor uint32 /* chardev minor */
Spare [10]uint32
}
......@@ -447,8 +459,8 @@ type IoctlIn struct {
Flags uint32
Cmd uint32
Arg uint64
In_size uint32
Out_size uint32
InSize uint32
OutSize uint32
}
type IoctlOut struct {
......@@ -485,6 +497,8 @@ type InHeader struct {
Padding uint32
}
const SizeOfOutHeader = 16
type OutHeader struct {
Length uint32
Error int32
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment