Add GetDevInfo exposing BTRFS_IOC_DEV_INFO (#3)

* Add GetDevInfo exposing BTRFS_IOC_DEV_INFO

Co-authored-by: Marcus Cobden <leth@users.noreply.github.com>
This commit is contained in:
Marcus Cobden 2022-04-03 09:03:56 +01:00 committed by GitHub
parent 90a0320aae
commit b3db0b2ded
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 2 deletions

View File

@ -2,12 +2,13 @@ package btrfs
import (
"fmt"
"github.com/dennwc/ioctl"
"io"
"os"
"path/filepath"
"strconv"
"syscall"
"github.com/dennwc/ioctl"
)
const SuperMagic = 0x9123683E
@ -84,6 +85,28 @@ func (f *FS) Info() (out Info, err error) {
return
}
type DevInfo struct {
UUID UUID
BytesUsed uint64
TotalBytes uint64
Path string
}
func (f *FS) GetDevInfo(id uint64) (out DevInfo, err error) {
var arg btrfs_ioctl_dev_info_args
arg.devid = id
if err = ioctl.Do(f.f, _BTRFS_IOC_DEV_INFO, &arg); err != nil {
return
}
out.UUID = arg.uuid
out.BytesUsed = arg.bytes_used
out.TotalBytes = arg.total_bytes
out.Path = stringFromBytes(arg.path[:])
return
}
type DevStats struct {
WriteErrs uint64
ReadErrs uint64

View File

@ -1,13 +1,15 @@
package btrfs
import (
"bytes"
"fmt"
"github.com/dennwc/btrfs/mtab"
"os"
"path/filepath"
"strings"
"syscall"
"unsafe"
"github.com/dennwc/btrfs/mtab"
)
func isBtrfs(path string) (bool, error) {
@ -95,3 +97,10 @@ func treeSearchRaw(mnt *os.File, key btrfs_ioctl_search_key) (out []searchResult
}
return out, nil
}
func stringFromBytes(input []byte) string {
if i := bytes.IndexByte(input, 0); i >= 0 {
input = input[:i]
}
return string(input)
}