add statfsmore call to get filesystem mount ids

This commit is contained in:
Ben McClelland
2019-09-30 10:28:04 -07:00
parent 1b826bc454
commit 5128942779
4 changed files with 82 additions and 1 deletions

View File

@@ -9,7 +9,7 @@
package scoutfs
// use this to generate the types for scoutfs:
// go tool cgo -godefs c_defs.go >scoutfsdefs.go
// go tool cgo -godefs c_defs_linux.go >scoutfsdefs.go
// #include <unistd.h>
// #include <stdio.h>
@@ -46,6 +46,7 @@ package scoutfs
// typedef struct scoutfs_ioctl_setattr_more scoutfs_ioctl_setattr_more_t;
// typedef struct scoutfs_ioctl_listxattr_hidden scoutfs_ioctl_listxattr_hidden_t;
// typedef struct scoutfs_ioctl_find_xattrs scoutfs_ioctl_find_xattrs_t;
// typedef struct scoutfs_ioctl_statfs_more scoutfs_ioctl_statfs_more_t;
import "C"
const IOCQUERYINODES = C.SCOUTFS_IOC_WALK_INODES
@@ -57,6 +58,7 @@ const IOCDATAWAITING = C.SCOUTFS_IOC_DATA_WAITING
const IOCSETATTRMORE = C.SCOUTFS_IOC_SETATTR_MORE
const IOCLISTXATTRHIDDEN = C.SCOUTFS_IOC_LISTXATTR_HIDDEN
const IOCFINDXATTRS = C.SCOUTFS_IOC_FIND_XATTRS
const IOCSTATFSMORE = C.SCOUTFS_IOC_STATFS_MORE
const QUERYINODESMETASEQ = C.SCOUTFS_IOC_WALK_INODES_META_SEQ
const QUERYINODESDATASEQ = C.SCOUTFS_IOC_WALK_INODES_DATA_SEQ
@@ -76,3 +78,6 @@ type dataWaiting C.scoutfs_ioctl_data_waiting_t
type setattrMore C.scoutfs_ioctl_setattr_more_t
type listXattrHidden C.scoutfs_ioctl_listxattr_hidden_t
type findXattrs C.scoutfs_ioctl_find_xattrs_t
type statfsMore C.scoutfs_ioctl_statfs_more_t
const sizeofstatfsMore = C.sizeof_scoutfs_ioctl_statfs_more_t

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2018 Versity Software, Inc.
//
// Use of this source code is governed by a BSD-3-Clause license
// that can be found in the LICENSE file in the root of the source
// tree.
package main
import (
"fmt"
"log"
"os"
scoutfs "github.com/versity/scoutfs-go"
)
func main() {
if len(os.Args) != 2 || os.Args[1] == "-h" {
fmt.Fprintln(os.Stderr, "usage:", os.Args[0], "<filepath>")
os.Exit(1)
}
f, err := os.Open(os.Args[1])
if err != nil {
log.Fatalf("open %q: %v", os.Args[1], err)
}
defer f.Close()
id, err := scoutfs.GetIDs(f)
if err != nil {
log.Fatalf("error GetIDs: %v", err)
}
fmt.Printf("fsid %016x\n", id.FSID)
fmt.Printf("rid %016x\n", id.RandomID)
fmt.Printf("%v\n", id.ShortID)
}

View File

@@ -9,6 +9,7 @@ package scoutfs
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"os"
"time"
@@ -480,3 +481,33 @@ func bufToStrings(b []byte) []string {
}
return s
}
// FSID contains the statfs more info for mounted scoutfs filesystem
type FSID struct {
FSID uint64
RandomID uint64
ShortID string
}
// GetIDs gets the statfs more filesystem and random id from file handle within
// scoutfs filesystem
func GetIDs(f *os.File) (FSID, error) {
stfs := statfsMore{Bytes: sizeofstatfsMore}
_, err := scoutfsctl(f.Fd(), IOCSTATFSMORE, uintptr(unsafe.Pointer(&stfs)))
if err != nil {
return FSID{}, err
}
if stfs.Bytes != sizeofstatfsMore {
return FSID{}, fmt.Errorf("unexpected return size: %v", stfs.Bytes)
}
short := fmt.Sprintf("f.%v.r.%v",
fmt.Sprintf("%016x", stfs.Fsid)[:][:6], fmt.Sprintf("%016x", stfs.Rid)[:][:6])
return FSID{
FSID: stfs.Fsid,
RandomID: stfs.Rid,
ShortID: short,
}, nil
}

View File

@@ -12,6 +12,7 @@ const IOCDATAWAITING = 0x80287307
const IOCSETATTRMORE = 0x40287308
const IOCLISTXATTRHIDDEN = 0x80187309
const IOCFINDXATTRS = 0x8020730a
const IOCSTATFSMORE = 0x8018730b
const QUERYINODESMETASEQ = 0x0
const QUERYINODESDATASEQ = 0x1
@@ -98,3 +99,10 @@ type findXattrs struct {
Nr_inodes uint16
X_pad [4]uint8
}
type statfsMore struct {
Bytes uint64
Fsid uint64
Rid uint64
}
const sizeofstatfsMore = 0x18