refactor: Inject mtio package to allow for remote implementation

This commit is contained in:
Felicitas Pojtinger
2022-01-16 23:50:47 +01:00
parent 5b84583403
commit 5d3424df92
36 changed files with 183 additions and 100 deletions

View File

@@ -1,5 +0,0 @@
package mtio
const (
BlockSize = 512
)

View File

@@ -1,115 +0,0 @@
//go:build linux
package mtio
import (
"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
mtOffl = 7 // Rewind and put the drive offline (eject?)
mtEom = 12 // Goto end of recorded media (for appending files)
mtSeek = 22 // Seek to block
)
// 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 GetCurrentRecordFromTape(fd uintptr) (int64, error) {
pos := &position{}
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
mtioCpos,
uintptr(unsafe.Pointer(pos)),
); err != 0 {
return 0, err
}
return pos.blkNo, nil
}
func GoToEndOfTape(fd uintptr) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtEom,
},
)),
); err != 0 {
return err
}
return nil
}
func GoToNextFileOnTape(fd uintptr) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtFsf,
count: 1,
},
)),
); err != 0 {
return err
}
return nil
}
func EjectTape(fd uintptr) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtOffl,
},
)),
); err != 0 {
return err
}
return nil
}
func SeekToRecordOnTape(fd uintptr, record int32) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
op: mtSeek,
count: record,
},
)),
); err != 0 {
return err
}
return nil
}

View File

@@ -1,27 +0,0 @@
//go:build !linux
package mtio
import (
"github.com/pojntfx/stfs/pkg/config"
)
func GetCurrentRecordFromTape(fd uintptr) (int64, error) {
return -1, config.ErrTapeDrivesUnsupported
}
func GoToEndOfTape(fd uintptr) error {
return config.ErrTapeDrivesUnsupported
}
func GoToNextFileOnTape(fd uintptr) error {
return config.ErrTapeDrivesUnsupported
}
func EjectTape(fd uintptr) error {
return config.ErrTapeDrivesUnsupported
}
func SeekToRecordOnTape(fd uintptr, record int32) error {
return config.ErrTapeDrivesUnsupported
}

View File

@@ -6,7 +6,7 @@ import (
"io"
"github.com/pojntfx/stfs/internal/ioext"
"github.com/pojntfx/stfs/internal/mtio"
"github.com/pojntfx/stfs/pkg/config"
)
func NewTapeWriter(f io.Writer, isRegular bool, recordSize int) (tw *tar.Writer, cleanup func(dirty *bool) error, err error) {
@@ -15,7 +15,7 @@ func NewTapeWriter(f io.Writer, isRegular bool, recordSize int) (tw *tar.Writer,
if isRegular {
tw = tar.NewWriter(f)
} else {
bw = bufio.NewWriterSize(f, mtio.BlockSize*recordSize)
bw = bufio.NewWriterSize(f, config.MagneticTapeBlockSize*recordSize)
counter = &ioext.CounterWriter{Writer: bw, BytesRead: 0}
tw = tar.NewWriter(counter)
}
@@ -28,9 +28,9 @@ func NewTapeWriter(f io.Writer, isRegular bool, recordSize int) (tw *tar.Writer,
}
if !isRegular {
if mtio.BlockSize*recordSize-counter.BytesRead > 0 {
if config.MagneticTapeBlockSize*recordSize-counter.BytesRead > 0 {
// Fill the rest of the record with zeros
if _, err := bw.Write(make([]byte, mtio.BlockSize*recordSize-counter.BytesRead)); err != nil {
if _, err := bw.Write(make([]byte, config.MagneticTapeBlockSize*recordSize-counter.BytesRead)); err != nil {
return err
}
}