Commit 155e343e authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Simplify dirent serialization.

parent 25d1f10b
...@@ -3,7 +3,6 @@ package fuse ...@@ -3,7 +3,6 @@ package fuse
// all of the code for DirEntryList. // all of the code for DirEntryList.
import ( import (
"bytes"
"log" "log"
"unsafe" "unsafe"
...@@ -12,6 +11,7 @@ import ( ...@@ -12,6 +11,7 @@ import (
var _ = log.Print var _ = log.Print
var eightPadding [8]byte var eightPadding [8]byte
const direntSize = int(unsafe.Sizeof(raw.Dirent{}))
// DirEntry is a type for PathFileSystem and NodeFileSystem to return // DirEntry is a type for PathFileSystem and NodeFileSystem to return
// directory contents in. // directory contents in.
...@@ -21,15 +21,13 @@ type DirEntry struct { ...@@ -21,15 +21,13 @@ type DirEntry struct {
} }
type DirEntryList struct { type DirEntryList struct {
buf *bytes.Buffer buf []byte
offset uint64 offset uint64
maxSize int
} }
func NewDirEntryList(data []byte, off uint64) *DirEntryList { func NewDirEntryList(data []byte, off uint64) *DirEntryList {
return &DirEntryList{ return &DirEntryList{
buf: bytes.NewBuffer(data[:0]), buf: data[:0],
maxSize: len(data),
offset: off, offset: off,
} }
} }
...@@ -39,41 +37,34 @@ func (l *DirEntryList) AddDirEntry(e DirEntry) bool { ...@@ -39,41 +37,34 @@ func (l *DirEntryList) AddDirEntry(e DirEntry) bool {
} }
func (l *DirEntryList) Add(name string, inode uint64, mode uint32) bool { func (l *DirEntryList) Add(name string, inode uint64, mode uint32) bool {
dirent := raw.Dirent{ padding := (8 - len(name)&7)&7
Off: l.offset+1, delta := padding + direntSize + len(name)
Ino: inode, oldLen := len(l.buf)
NameLen: uint32(len(name)), newLen := delta + oldLen
Typ: ModeToType(mode),
}
padding := 8 - len(name)&7 if newLen > cap(l.buf) {
if padding == 8 {
padding = 0
}
delta := padding + int(unsafe.Sizeof(raw.Dirent{})) + len(name)
newLen := delta + l.buf.Len()
if newLen > l.maxSize {
return false return false
} }
_, err := l.buf.Write(asSlice(unsafe.Pointer(&dirent), unsafe.Sizeof(raw.Dirent{}))) l.buf = l.buf[:newLen]
if err != nil { dirent := (*raw.Dirent)(unsafe.Pointer(&l.buf[oldLen]))
panic("Serialization of Dirent failed") dirent.Off = l.offset+1
} dirent.Ino = inode
l.buf.WriteString(name) dirent.NameLen= uint32(len(name))
dirent.Typ = ModeToType(mode)
oldLen += direntSize
copy(l.buf[oldLen:], name)
oldLen += len(name)
if padding > 0 { if padding > 0 {
l.buf.Write(eightPadding[:padding]) copy(l.buf[oldLen:], eightPadding[:padding])
} }
l.offset = dirent.Off l.offset = dirent.Off
if l.buf.Len() != newLen {
log.Panicf("newLen mismatch %d %d", l.buf.Len(), newLen)
}
return true return true
} }
func (l *DirEntryList) Bytes() []byte { func (l *DirEntryList) Bytes() []byte {
return l.buf.Bytes() return l.buf
} }
//////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////
......
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