Skip to content

Commit

Permalink
fix: support reading from RO-protected cartridges
Browse files Browse the repository at this point in the history
  • Loading branch information
speed47 committed Jul 2, 2021
1 parent ffc26c8 commit d482378
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cm.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (cm *Cm) String() string {
if serial != "" {
s += fmt.Sprintf(" Session N-%d: Used in a device of vendor %s (serial %s)\n", i, devname, serial)
} else {
s += fmt.Sprintf(" Session N-%d: Used in device %s\n", i, devname)
s += fmt.Sprintf(" Session N-%d: Used in a device of vendor %s\n", i, devname)
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion tapedrive.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/benmcclelland/sgio"
"os"
"strings"
"syscall"
"unsafe"
)

/*
Expand Down Expand Up @@ -48,6 +50,28 @@ func (drive TapeDrive) IsFake() bool {
return drive.DeviceName == "FAKE"
}

// copy of sg.OpenScsiDevice() but with RDONLY instead of O_RDWR
func OpenScsiDeviceRO(fname string) (*os.File, error) {
f, err := os.OpenFile(fname, os.O_RDONLY, 0)
if err != nil {
return nil, err
}
var version uint32
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(f.Fd()),
uintptr(sgio.SG_GET_VERSION_NUM),
uintptr(unsafe.Pointer(&version)),
)
if errno != 0 {
return nil, fmt.Errorf("failed to get version info from sg device (errno=%d)", errno)
}
if version < 30000 {
return nil, fmt.Errorf("device does not appear to be an sg device")
}
return f, nil
}

func TapeDriveNew(devicename string) (*TapeDrive, error) {
if devicename == "" {
if os.Getenv("TAPE") != "" {
Expand All @@ -64,7 +88,7 @@ func TapeDriveNew(devicename string) (*TapeDrive, error) {
}

fmt.Printf("Opening device %s\n", devicename)
dev, err := sgio.OpenScsiDevice(devicename)
dev, err := OpenScsiDeviceRO(devicename)
if err != nil {
fmt.Println("Failed to open:", err)
return nil, err
Expand Down

0 comments on commit d482378

Please sign in to comment.