refactor: Use explicit interface instead of os.File for drive backend

This commit is contained in:
Felicitas Pojtinger
2022-01-07 22:27:14 +01:00
parent 314e60f9b1
commit f45f8b1499
9 changed files with 32 additions and 29 deletions

View File

@@ -3,7 +3,6 @@
package mtio
import (
"os"
"syscall"
"unsafe"
)
@@ -31,11 +30,11 @@ type operation struct {
count int32 // Operation count
}
func GetCurrentRecordFromTape(f *os.File) (int64, error) {
func GetCurrentRecordFromTape(fd uintptr) (int64, error) {
pos := &position{}
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
fd,
mtioCpos,
uintptr(unsafe.Pointer(pos)),
); err != 0 {
@@ -45,10 +44,10 @@ func GetCurrentRecordFromTape(f *os.File) (int64, error) {
return pos.blkNo, nil
}
func GoToEndOfTape(f *os.File) error {
func GoToEndOfTape(fd uintptr) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
@@ -62,10 +61,10 @@ func GoToEndOfTape(f *os.File) error {
return nil
}
func GoToNextFileOnTape(f *os.File) error {
func GoToNextFileOnTape(fd uintptr) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
@@ -80,10 +79,10 @@ func GoToNextFileOnTape(f *os.File) error {
return nil
}
func EjectTape(f *os.File) error {
func EjectTape(fd uintptr) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{
@@ -97,10 +96,10 @@ func EjectTape(f *os.File) error {
return nil
}
func SeekToRecordOnTape(f *os.File, record int32) error {
func SeekToRecordOnTape(fd uintptr, record int32) error {
if _, _, err := syscall.Syscall(
syscall.SYS_IOCTL,
f.Fd(),
fd,
mtioCtop,
uintptr(unsafe.Pointer(
&operation{

View File

@@ -8,22 +8,22 @@ import (
"github.com/pojntfx/stfs/pkg/config"
)
func GetCurrentRecordFromTape(f *os.File) (int64, error) {
func GetCurrentRecordFromTape(fd uintptr) (int64, error) {
return -1, config.ErrTapeDrivesUnsupported
}
func GoToEndOfTape(f *os.File) error {
func GoToEndOfTape(fd uintptr) error {
return config.ErrTapeDrivesUnsupported
}
func GoToNextFileOnTape(f *os.File) error {
func GoToNextFileOnTape(fd uintptr) error {
return config.ErrTapeDrivesUnsupported
}
func EjectTape(f *os.File) error {
func EjectTape(fd uintptr) error {
return config.ErrTapeDrivesUnsupported
}
func SeekToRecordOnTape(f *os.File, record int32) error {
func SeekToRecordOnTape(fd uintptr, record int32) error {
return config.ErrTapeDrivesUnsupported
}

View File

@@ -4,7 +4,6 @@ import (
"context"
"io"
"io/fs"
"os"
"time"
)
@@ -18,8 +17,13 @@ type DriveWriterConfig struct {
DriveIsRegular bool
}
type Drive interface {
io.ReadSeeker
Fd() uintptr
}
type DriveConfig struct {
Drive *os.File
Drive Drive
DriveIsRegular bool
}

View File

@@ -8,5 +8,5 @@ import (
func Eject(
state config.DriveConfig,
) error {
return mtio.EjectTape(state.Drive)
return mtio.EjectTape(state.Drive.Fd())
}

View File

@@ -8,5 +8,5 @@ import (
func Tell(
state config.DriveConfig,
) (int64, error) {
return mtio.GetCurrentRecordFromTape(state.Drive)
return mtio.GetCurrentRecordFromTape(state.Drive.Fd())
}

View File

@@ -46,7 +46,7 @@ func Fetch(
tr = tar.NewReader(reader.Drive)
} else {
// Seek to record
if err := mtio.SeekToRecordOnTape(drive.Drive, int32(record)); err != nil {
if err := mtio.SeekToRecordOnTape(drive.Drive.Fd(), int32(record)); err != nil {
return err
}

View File

@@ -150,7 +150,7 @@ func Index(
}
} else {
// Seek to record
if err := mtio.SeekToRecordOnTape(drive.Drive, int32(record)); err != nil {
if err := mtio.SeekToRecordOnTape(drive.Drive.Fd(), int32(record)); err != nil {
return err
}
@@ -172,13 +172,13 @@ func Index(
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
if err := mtio.GoToNextFileOnTape(drive.Drive); err != nil {
if err := mtio.GoToNextFileOnTape(drive.Drive.Fd()); err != nil {
// EOD
break
}
record, err = mtio.GetCurrentRecordFromTape(drive.Drive)
record, err = mtio.GetCurrentRecordFromTape(drive.Drive.Fd())
if err != nil {
return err
}

View File

@@ -132,7 +132,7 @@ func Query(
}
} else {
// Seek to record
if err := mtio.SeekToRecordOnTape(state.Drive, int32(record)); err != nil {
if err := mtio.SeekToRecordOnTape(state.Drive.Fd(), int32(record)); err != nil {
return []*tar.Header{}, err
}
@@ -153,13 +153,13 @@ func Query(
hdr, err := tr.Next()
if err != nil {
if err == io.EOF {
if err := mtio.GoToNextFileOnTape(state.Drive); err != nil {
if err := mtio.GoToNextFileOnTape(state.Drive.Fd()); err != nil {
// EOD
break
}
record, err = mtio.GetCurrentRecordFromTape(state.Drive)
record, err = mtio.GetCurrentRecordFromTape(state.Drive.Fd())
if err != nil {
return []*tar.Header{}, err
}

View File

@@ -40,7 +40,7 @@ func OpenTapeWriteOnly(drive string, recordSize int, overwrite bool) (f *os.File
}
// Seek to the start of the tape
if err := mtio.SeekToRecordOnTape(f, 0); err != nil {
if err := mtio.SeekToRecordOnTape(f.Fd(), 0); err != nil {
return nil, false, err
}
@@ -65,7 +65,7 @@ func OpenTapeWriteOnly(drive string, recordSize int, overwrite bool) (f *os.File
if !overwrite {
// Go to end of tape
if err := mtio.GoToEndOfTape(f); err != nil {
if err := mtio.GoToEndOfTape(f.Fd()); err != nil {
return nil, false, err
}
}