Skip to content

Instantly share code, notes, and snippets.

@csabahenk
Created September 15, 2012 08:39
Show Gist options
  • Save csabahenk/3727018 to your computer and use it in GitHub Desktop.
Save csabahenk/3727018 to your computer and use it in GitHub Desktop.
Quick printer (for glusterfs and github.com/csabahenk/strace-fusedump produced) FUSE dumps
package main
import (
"fmt"
"io"
"log"
"os"
"unsafe"
)
type FuseOpcode int32
const (
LOOKUP FuseOpcode = 1
FORGET FuseOpcode = 2
GETATTR FuseOpcode = 3
SETATTR FuseOpcode = 4
READLINK FuseOpcode = 5
SYMLINK FuseOpcode = 6
MKNOD FuseOpcode = 8
MKDIR FuseOpcode = 9
UNLINK FuseOpcode = 10
RMDIR FuseOpcode = 11
RENAME FuseOpcode = 12
LINK FuseOpcode = 13
OPEN FuseOpcode = 14
READ FuseOpcode = 15
WRITE FuseOpcode = 16
STATFS FuseOpcode = 17
RELEASE FuseOpcode = 18
FSYNC FuseOpcode = 20
SETXATTR FuseOpcode = 21
GETXATTR FuseOpcode = 22
LISTXATTR FuseOpcode = 23
REMOVEXATTR FuseOpcode = 24
FLUSH FuseOpcode = 25
INIT FuseOpcode = 26
OPENDIR FuseOpcode = 27
READDIR FuseOpcode = 28
RELEASEDIR FuseOpcode = 29
FSYNCDIR FuseOpcode = 30
GETLK FuseOpcode = 31
SETLK FuseOpcode = 32
SETLKW FuseOpcode = 33
ACCESS FuseOpcode = 34
CREATE FuseOpcode = 35
INTERRUPT FuseOpcode = 36
BMAP FuseOpcode = 37
DESTROY FuseOpcode = 38
IOCTL FuseOpcode = 39
POLL FuseOpcode = 40
)
var FuseOpnames = [...]string{
LOOKUP: "LOOKUP",
FORGET: "FORGET",
GETATTR: "GETATTR",
SETATTR: "SETATTR",
READLINK: "READLINK",
SYMLINK: "SYMLINK",
MKNOD: "MKNOD",
MKDIR: "MKDIR",
UNLINK: "UNLINK",
RMDIR: "RMDIR",
RENAME: "RENAME",
LINK: "LINK",
OPEN: "OPEN",
READ: "READ",
WRITE: "WRITE",
STATFS: "STATFS",
RELEASE: "RELEASE",
FSYNC: "FSYNC",
SETXATTR: "SETXATTR",
GETXATTR: "GETXATTR",
LISTXATTR: "LISTXATTR",
REMOVEXATTR: "REMOVEXATTR",
FLUSH: "FLUSH",
INIT: "INIT",
OPENDIR: "OPENDIR",
READDIR: "READDIR",
RELEASEDIR: "RELEASEDIR",
FSYNCDIR: "FSYNCDIR",
GETLK: "GETLK",
SETLK: "SETLK",
SETLKW: "SETLKW",
ACCESS: "ACCESS",
CREATE: "CREATE",
INTERRUPT: "INTERRUPT",
BMAP: "BMAP",
DESTROY: "DESTROY",
IOCTL: "IOCTL",
POLL: "POLL",
}
type InHeader struct {
Length uint32
Opcode FuseOpcode
Unique uint64
NodeId uint64
Uid uint32
Gid uint32
Pid uint32
Padding uint32
}
type OutHeader struct {
Length uint32
Status int32
Unique uint64
}
func main() {
var inh *InHeader
var ouh *OutHeader
insize := int(unsafe.Sizeof(*inh))
outsize := int(unsafe.Sizeof(*ouh))
if insize <= outsize {
panic("header size assertion fails")
}
buf := make([]byte, insize+1)
pos := int64(0)
readgen := func(m, n int, shortcb func(r int)) {
r, err := os.Stdin.ReadAt(buf[m:n], pos)
if err == io.EOF {
err = nil
r = 0
}
if err != nil {
log.Fatal("Read: ", err)
}
if r < n-m {
shortcb(r)
}
pos += int64(r)
}
read := func(n int) {
readgen(0, n, func(r int) {
if r == 0 {
os.Exit(0)
}
log.Fatal("short read")
})
}
readfrom := func(n int) {
readgen(n, len(buf), func(r int) { log.Fatal("short read") })
}
for {
ppos := pos
read(outsize + 1)
switch buf[0] {
case 'R':
readfrom(outsize + 1)
inh = (*InHeader)(unsafe.Pointer(&buf[1]))
fmt.Printf("%d %s %+v\n", ppos, FuseOpnames[inh.Opcode], *inh)
pos = ppos + int64(1) + int64(inh.Length)
case 'W':
ouh = (*OutHeader)(unsafe.Pointer(&buf[1]))
fmt.Printf("%d %+v\n", ppos, *ouh)
pos = ppos + int64(1) + int64(ouh.Length)
default:
log.Fatalf("unknown direction %c", buf[0])
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment