From 1fd2203f66f62f15a3433f35a41c7a01c3ce6d07 Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Sun, 14 Nov 2021 11:52:36 +0100 Subject: [PATCH] feat: Add cmd that seeks to specific location in tape --- cmd/stfs-seek/main.go | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 cmd/stfs-seek/main.go diff --git a/cmd/stfs-seek/main.go b/cmd/stfs-seek/main.go new file mode 100644 index 0000000..6d1bf50 --- /dev/null +++ b/cmd/stfs-seek/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "flag" + "os" + "syscall" + "unsafe" +) + +// See https://github.com/benmcclelland/mtio/blob/f929531fb4fe6433f7198ccd89d1c1414ef8fa3f/mtst.go#L46 +const ( + MTIOCTOP = 0x40086d01 // Do magnetic tape operation + MTSEEK = 22 // Seek to block +) + +// Operation is struct for MTIOCTOP +type Operation struct { + op int16 // Operation ID + + pad int16 // Padding to match C structures + + count int32 // Operation count +} + +func main() { + file := flag.String("file", "/dev/nst0", "File of tape drive to open") + record := flag.Int("record", 0, "Record to seek too") + + flag.Parse() + + f, err := os.OpenFile(*file, os.O_RDONLY, os.ModeCharDevice) + if err != nil { + panic(err) + } + defer f.Close() + + syscall.Syscall( + syscall.SYS_IOCTL, + f.Fd(), + MTIOCTOP, + uintptr(unsafe.Pointer( + &Operation{ + op: MTSEEK, + count: int32(*record), + }, + )), + ) +}