Created
January 13, 2015 16:46
-
-
Save jessfraz/728994b0cc0923eddf5e to your computer and use it in GitHub Desktop.
btrfs-test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// +build linux | |
package main | |
/* | |
#include <stdlib.h> | |
#include <dirent.h> | |
#include <btrfs/ioctl.h> | |
*/ | |
import "C" | |
import ( | |
"fmt" | |
"syscall" | |
"unsafe" | |
) | |
const ( | |
BTRFS_ROOT_REF_KEY = 156 | |
BTRFS_ROOT_ITEM_KEY = 132 | |
) | |
func free(p *C.char) { | |
C.free(unsafe.Pointer(p)) | |
} | |
func openDir(path string) (*C.DIR, error) { | |
Cpath := C.CString(path) | |
defer free(Cpath) | |
dir := C.opendir(Cpath) | |
if dir == nil { | |
return nil, fmt.Errorf("Can't open dir") | |
} | |
return dir, nil | |
} | |
func closeDir(dir *C.DIR) { | |
if dir != nil { | |
C.closedir(dir) | |
} | |
} | |
func getDirFd(dir *C.DIR) uintptr { | |
return uintptr(C.dirfd(dir)) | |
} | |
func main() { | |
//var name string | |
var path string = "/var/lib/docker/" | |
//var id string = "8596" | |
var args C.struct_btrfs_ioctl_search_args | |
//for i, c := range []byte(name) { | |
// args[i] = C.char(c) | |
//} | |
var u64 uint64 | |
sk := args.key | |
sk.max_type = BTRFS_ROOT_REF_KEY | |
sk.min_type = BTRFS_ROOT_ITEM_KEY | |
sk.min_objectid = 0 | |
sk.max_objectid = C.__u64(u64 - 1) | |
sk.max_offset = C.__u64(u64 - 1) | |
sk.min_offset = 0 | |
sk.max_transid = C.__u64(u64 - 1) | |
sk.nr_items = 4096 | |
dir, err := openDir(path) | |
if err != nil { | |
fmt.Errorf("Can't open dir %s: %v", path, err) | |
} | |
defer closeDir(dir) | |
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_TREE_SEARCH, uintptr(unsafe.Pointer(&args))) | |
if errno < 0 { | |
fmt.Errorf("Can't perform the search under %s", path) | |
} | |
if sk.nr_items == 0 { | |
fmt.Println("nr_items was 0") | |
return | |
} | |
//off := 0 | |
//for i = 0; i < sk.nr_items; i++ { | |
//} | |
fmt.Printf("%#v", sk) | |
fmt.Printf("%#v", args.buf) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment