From c0687fb39092f91cb982014d0d5aff9bb557dda8 Mon Sep 17 00:00:00 2001 From: Felicitas Pojtinger Date: Thu, 18 Nov 2021 00:07:12 +0100 Subject: [PATCH] feat: Add block-scoped seeking to `stfs-seek-tape` --- cmd/stfs-seek-tape/main.go | 29 ++++++++++++++++++++++++++--- cmd/stfs-tvf-simple/main.go | 4 ++-- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmd/stfs-seek-tape/main.go b/cmd/stfs-seek-tape/main.go index de37706..9d575e2 100644 --- a/cmd/stfs-seek-tape/main.go +++ b/cmd/stfs-seek-tape/main.go @@ -1,7 +1,10 @@ package main import ( + "archive/tar" + "bufio" "flag" + "log" "os" "syscall" "unsafe" @@ -11,6 +14,8 @@ import ( const ( MTIOCTOP = 0x40086d01 // Do magnetic tape operation MTSEEK = 22 // Seek to block + + blockSize = 512 ) // Operation is struct for MTIOCTOP @@ -21,8 +26,10 @@ type Operation struct { } func main() { - file := flag.String("file", "/dev/nst0", "File of tape drive to open") + file := flag.String("file", "/dev/nst0", "File (tape drive or tar file) to open") + recordSize := flag.Int("recordSize", 20, "Amount of 512-bit blocks per record") record := flag.Int("record", 0, "Record to seek too") + block := flag.Int("block", 0, "Block in record to seek too") flag.Parse() @@ -32,7 +39,7 @@ func main() { } defer f.Close() - syscall.Syscall( + if _, _, err := syscall.Syscall( syscall.SYS_IOCTL, f.Fd(), MTIOCTOP, @@ -42,5 +49,21 @@ func main() { Count: int32(*record), }, )), - ) + ); err != 0 { + panic(err) + } + + br := bufio.NewReaderSize(f, blockSize**recordSize) + if _, err := br.Read(make([]byte, *block*blockSize)); err != nil { + panic(err) + } + + tr := tar.NewReader(br) + + hdr, err := tr.Next() + if err != nil { + panic(err) + } + + log.Println(hdr) } diff --git a/cmd/stfs-tvf-simple/main.go b/cmd/stfs-tvf-simple/main.go index bd833a2..d4bd03b 100644 --- a/cmd/stfs-tvf-simple/main.go +++ b/cmd/stfs-tvf-simple/main.go @@ -125,7 +125,7 @@ func main() { for { hdr, err := tr.Next() if err == io.EOF { - if err := gotoNextFileOnTape(f); err != nil { + if err := goToNextFileOnTape(f); err != nil { panic(err) } @@ -166,7 +166,7 @@ func main() { } } -func gotoNextFileOnTape(f *os.File) error { +func goToNextFileOnTape(f *os.File) error { if _, _, err := syscall.Syscall( syscall.SYS_IOCTL, f.Fd(),