refactor: Decompose syscalls into pkg, start adding utility commands

This commit is contained in:
Felicitas Pojtinger
2021-11-19 00:39:28 +01:00
parent f41c96b59e
commit f426874cbd
35 changed files with 155 additions and 6100 deletions
+98
View File
@@ -0,0 +1,98 @@
package controllers
import (
"os"
"syscall"
"unsafe"
)
// See https://github.com/benmcclelland/mtio
const (
mtioCpos = 0x80086d03 // Get tape position
mtioCtop = 0x40086d01 // Do magnetic tape operation
mtFsf = 1 // Forward space over FileMark, position at first record of next file
mtEom = 12 // Goto end of recorded media (for appending files)
mtSeek = 22 // Seek to block
BlockSize = 512
)
// position is struct for MTIOCPOS
type position struct {
blkNo int64 // Current block number
}
// operation is struct for MTIOCTOP
type operation struct {
op int16 // Operation ID
pad int16 // Padding to match C structures
count int32 // Operation count
}
func GoToEndOfTape(f *os.File) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtEom,
},
)),
); err != 0 {
return err
}
return nil
}
func GoToNextFileOnTape(f *os.File) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtFsf,
count: 1,
},
)),
); err != 0 {
return err
}
return nil
}
func GetCurrentRecordFromTape(f *os.File) (int64, error) {
pos := &position{}
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
mtioCpos,
uintptr(unsafe.Pointer(pos)),
); err != 0 {
return 0, err
}
return pos.blkNo, nil
}
func SeekToRecordOnTape(f *os.File, record int32) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtSeek,
count: record,
},
)),
); err != 0 {
return err
}
return nil
}