diff --git a/internal/mtio/mtio_linux.go b/internal/mtio/mtio_linux.go index 5166c40..bceac16 100644 --- a/internal/mtio/mtio_linux.go +++ b/internal/mtio/mtio_linux.go @@ -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{ diff --git a/internal/mtio/mtio_stub.go b/internal/mtio/mtio_stub.go index 0907640..967df6c 100644 --- a/internal/mtio/mtio_stub.go +++ b/internal/mtio/mtio_stub.go @@ -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 } diff --git a/pkg/config/config.go b/pkg/config/config.go index d8a9a20..7618b36 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -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 } diff --git a/pkg/hardware/eject.go b/pkg/hardware/eject.go index 384e9b5..448396e 100644 --- a/pkg/hardware/eject.go +++ b/pkg/hardware/eject.go @@ -8,5 +8,5 @@ import ( func Eject( state config.DriveConfig, ) error { - return mtio.EjectTape(state.Drive) + return mtio.EjectTape(state.Drive.Fd()) } diff --git a/pkg/hardware/tell.go b/pkg/hardware/tell.go index 1506ffc..bfe6b5d 100644 --- a/pkg/hardware/tell.go +++ b/pkg/hardware/tell.go @@ -8,5 +8,5 @@ import ( func Tell( state config.DriveConfig, ) (int64, error) { - return mtio.GetCurrentRecordFromTape(state.Drive) + return mtio.GetCurrentRecordFromTape(state.Drive.Fd()) } diff --git a/pkg/recovery/fetch.go b/pkg/recovery/fetch.go index 53e80ae..4e1a97a 100644 --- a/pkg/recovery/fetch.go +++ b/pkg/recovery/fetch.go @@ -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 } diff --git a/pkg/recovery/index.go b/pkg/recovery/index.go index 50ba113..c1c350d 100644 --- a/pkg/recovery/index.go +++ b/pkg/recovery/index.go @@ -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 } diff --git a/pkg/recovery/query.go b/pkg/recovery/query.go index 191c7dc..68549d3 100644 --- a/pkg/recovery/query.go +++ b/pkg/recovery/query.go @@ -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 } diff --git a/pkg/tape/write.go b/pkg/tape/write.go index 4be2098..a3e3bfe 100644 --- a/pkg/tape/write.go +++ b/pkg/tape/write.go @@ -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 } }