From bfc4f0e80bac3b92532e77a9bbe995711836df13 Mon Sep 17 00:00:00 2001 From: Felix Pojtinger Date: Sun, 14 Nov 2021 12:04:28 +0100 Subject: [PATCH] feat: Add cmd to seek to specific block --- cmd/stfs-seek/main.go | 14 ++++++-------- cmd/stfs-tell/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) create mode 100644 cmd/stfs-tell/main.go diff --git a/cmd/stfs-seek/main.go b/cmd/stfs-seek/main.go index 6d1bf50..de37706 100644 --- a/cmd/stfs-seek/main.go +++ b/cmd/stfs-seek/main.go @@ -7,7 +7,7 @@ import ( "unsafe" ) -// See https://github.com/benmcclelland/mtio/blob/f929531fb4fe6433f7198ccd89d1c1414ef8fa3f/mtst.go#L46 +// See https://github.com/benmcclelland/mtio const ( MTIOCTOP = 0x40086d01 // Do magnetic tape operation MTSEEK = 22 // Seek to block @@ -15,11 +15,9 @@ const ( // Operation is struct for MTIOCTOP type Operation struct { - op int16 // Operation ID - - pad int16 // Padding to match C structures - - count int32 // Operation count + Op int16 // Operation ID + Pad int16 // Padding to match C structures + Count int32 // Operation count } func main() { @@ -40,8 +38,8 @@ func main() { MTIOCTOP, uintptr(unsafe.Pointer( &Operation{ - op: MTSEEK, - count: int32(*record), + Op: MTSEEK, + Count: int32(*record), }, )), ) diff --git a/cmd/stfs-tell/main.go b/cmd/stfs-tell/main.go new file mode 100644 index 0000000..4e0d479 --- /dev/null +++ b/cmd/stfs-tell/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "flag" + "fmt" + "os" + "syscall" + "unsafe" +) + +// See https://github.com/benmcclelland/mtio +const ( + MTIOCPOS = 0x80086d03 // Get tape position +) + +// Position is struct for MTIOCPOS +type Position struct { + BlkNo int64 // Current block number +} + +func main() { + file := flag.String("file", "/dev/nst0", "File of tape drive to open") + + flag.Parse() + + f, err := os.OpenFile(*file, os.O_RDONLY, os.ModeCharDevice) + if err != nil { + panic(err) + } + defer f.Close() + + pos := &Position{} + + syscall.Syscall( + syscall.SYS_IOCTL, + f.Fd(), + MTIOCPOS, + uintptr(unsafe.Pointer(pos)), + ) + + fmt.Println(pos.BlkNo) +}