switch to dennwc/ioctl library

This commit is contained in:
Denys Smirnov 2018-10-21 21:02:44 +03:00
parent 75c27127c2
commit 694b569856
4 changed files with 34 additions and 76 deletions

View File

@ -2,7 +2,7 @@ package btrfs
import (
"fmt"
"github.com/dennwc/btrfs/ioctl"
"github.com/dennwc/ioctl"
"io"
"os"
"path/filepath"

View File

@ -1,82 +1,61 @@
package ioctl
import (
"fmt"
"github.com/dennwc/ioctl"
"os"
"reflect"
"syscall"
)
const (
nrBits = 8
typeBits = 8
sizeBits = 14
dirBits = 2
)
const (
nrMask = ((1 << nrBits) - 1)
typeMask = ((1 << typeBits) - 1)
sizeMask = ((1 << sizeBits) - 1)
dirMask = ((1 << dirBits) - 1)
)
const (
nrShift = 0
typeShift = (nrShift + nrBits)
sizeShift = (typeShift + typeBits)
dirShift = (sizeShift + sizeBits)
)
const (
None = 0
Write = 1
Read = 2
None = ioctl.None
Write = ioctl.Write
Read = ioctl.Read
)
// IOC
//
// Deprecated: use github/dennwc/ioctl
func IOC(dir, typ, nr, size uintptr) uintptr {
return (dir << dirShift) |
(typ << typeShift) |
(nr << nrShift) |
(size << sizeShift)
return ioctl.IOC(dir, typ, nr, size)
}
// IO
//
// Deprecated: use github/dennwc/ioctl
func IO(typ, nr uintptr) uintptr {
return IOC(None, typ, nr, 0)
return ioctl.IO(typ, nr)
}
// IOC
//
// Deprecated: use github/dennwc/ioctl
func IOR(typ, nr, size uintptr) uintptr {
return IOC(Read, typ, nr, size)
return ioctl.IOR(typ, nr, size)
}
// IOW
//
// Deprecated: use github/dennwc/ioctl
func IOW(typ, nr, size uintptr) uintptr {
return IOC(Write, typ, nr, size)
return ioctl.IOW(typ, nr, size)
}
// IOWR
//
// Deprecated: use github/dennwc/ioctl
func IOWR(typ, nr, size uintptr) uintptr {
return IOC(Read|Write, typ, nr, size)
return ioctl.IOWR(typ, nr, size)
}
// Ioctl
//
// Deprecated: use github/dennwc/ioctl
func Ioctl(f *os.File, ioc uintptr, addr uintptr) error {
_, _, e := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), ioc, addr)
if e != 0 {
return e
}
return nil
return ioctl.Ioctl(f, ioc, addr)
}
// Do
//
// Deprecated: use github/dennwc/ioctl
func Do(f *os.File, ioc uintptr, arg interface{}) error {
var addr uintptr
if arg != nil {
v := reflect.ValueOf(arg)
switch v.Kind() {
case reflect.Ptr:
addr = v.Elem().UnsafeAddr()
case reflect.Slice:
addr = v.Index(0).UnsafeAddr()
default:
return fmt.Errorf("expected ptr or slice, got %T")
}
}
return Ioctl(f, ioc, addr)
return ioctl.Do(f, ioc, arg)
}

View File

@ -1,21 +0,0 @@
package ioctl
import (
"testing"
)
var casesIOC = []struct {
got uintptr
expect uintptr
}{
{got: IOC(1, 2, 3, 4), expect: 0x40040203},
}
func TestIOC(t *testing.T) {
for i, c := range casesIOC {
if c.got != c.expect {
t.Errorf("unexpected ioc (case %d): %x(%b) vs %x(%b)",
i+1, c.got, c.got, c.expect, c.expect)
}
}
}

View File

@ -3,7 +3,7 @@ package btrfs
import (
"encoding/binary"
"encoding/hex"
"github.com/dennwc/btrfs/ioctl"
"github.com/dennwc/ioctl"
"os"
"strconv"
"strings"