Skip to content

Commit

Permalink
fix: drop ReadFullAt
Browse files Browse the repository at this point in the history
It was never needed, as `ReadAt` is guaranteed to return an error on
incomplete read.

Ref: https://pkg.go.dev/io#ReaderAt

Signed-off-by: Andrey Smirnov <[email protected]>
  • Loading branch information
smira committed Aug 21, 2024
1 parent c34dfb6 commit bc73f6d
Show file tree
Hide file tree
Showing 14 changed files with 16 additions and 64 deletions.
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/ext/ext.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/utils"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

const sbOffset = 0x400
Expand Down Expand Up @@ -50,7 +49,7 @@ func (p *Probe) Name() string {
func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
buf := make([]byte, SUPERBLOCK_SIZE)

if err := ioutil.ReadFullAt(r, buf, sbOffset); err != nil {
if _, err := r.ReadAt(buf, sbOffset); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/iso9660/iso9660.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

const (
Expand Down Expand Up @@ -62,7 +61,7 @@ vdLoop:
for i := range vdMax {
buf := make([]byte, VOLUMEDESCRIPTOR_SIZE)

if err := ioutil.ReadFullAt(r, buf, superblockOffset+sectorSize*int64(i)); err != nil {
if _, err := r.ReadAt(buf, superblockOffset+sectorSize*int64(i)); err != nil {
break
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/luks/luks.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

var luksMagic = magic.Magic{
Expand All @@ -40,7 +39,7 @@ func (p *Probe) Name() string {
func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
buf := make([]byte, LUKS2HEADER_SIZE)

if err := ioutil.ReadFullAt(r, buf, 0); err != nil {
if _, err := r.ReadAt(buf, 0); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/lvm2/lvm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package lvm2
import (
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

var (
Expand Down Expand Up @@ -44,7 +43,7 @@ func (p *Probe) Name() string {
func (p *Probe) probe(r probe.Reader, offset int64) (LVM2Header, error) {
buf := make([]byte, LVM2HEADER_SIZE)

if err := ioutil.ReadFullAt(r, buf, offset); err != nil {
if _, err := r.ReadAt(buf, offset); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/squashfs/squashfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ package squashfs
import (
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

var squashfsMagic1 = magic.Magic{ // big endian
Expand Down Expand Up @@ -43,7 +42,7 @@ func (p *Probe) Name() string {
func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
buf := make([]byte, SUPERBLOCK_SIZE)

if err := ioutil.ReadFullAt(r, buf, 0); err != nil {
if _, err := r.ReadAt(buf, 0); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/swap/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

var (
Expand Down Expand Up @@ -99,7 +98,7 @@ func (p *Probe) Name() string {
func (p *Probe) Probe(r probe.Reader, m magic.Magic) (*probe.Result, error) {
buf := make([]byte, SWAPHEADER_SIZE)

if err := ioutil.ReadFullAt(r, buf, 1024); err != nil {
if _, err := r.ReadAt(buf, 1024); err != nil {
return nil, err
}

Expand Down
5 changes: 2 additions & 3 deletions blkid/internal/filesystems/talosmeta/talosmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

// META constants, from talos/internal/pkg/meta/internal/adv/talos.
Expand Down Expand Up @@ -45,15 +44,15 @@ func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
buf := make([]byte, 4)

for _, offset := range []int64{0, length} {
if err := ioutil.ReadFullAt(r, buf, offset); err != nil {
if _, err := r.ReadAt(buf, offset); err != nil {
return nil, err
}

if binary.BigEndian.Uint32(buf) != magic1 {
continue
}

if err := ioutil.ReadFullAt(r, buf, offset+length-4); err != nil {
if _, err := r.ReadAt(buf, offset+length-4); err != nil {
return nil, err
}

Expand Down
5 changes: 2 additions & 3 deletions blkid/internal/filesystems/vfat/vfat.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/utils"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

var (
Expand Down Expand Up @@ -73,11 +72,11 @@ func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
vfatBuf := make([]byte, VFATSB_SIZE)
msdosBuf := make([]byte, MSDOSSB_SIZE)

if err := ioutil.ReadFullAt(r, vfatBuf, 0); err != nil {
if _, err := r.ReadAt(vfatBuf, 0); err != nil {
return nil, err
}

if err := ioutil.ReadFullAt(r, msdosBuf, 0); err != nil {
if _, err := r.ReadAt(msdosBuf, 0); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/xfs/xfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

var xfsMagic = magic.Magic{
Expand All @@ -41,7 +40,7 @@ func (p *Probe) Name() string {
func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
buf := make([]byte, SUPERBLOCK_SIZE)

if err := ioutil.ReadFullAt(r, buf, 0); err != nil {
if _, err := r.ReadAt(buf, 0); err != nil {
return nil, err
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/internal/filesystems/zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/magic"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

// https://github.com/util-linux/util-linux/blob/c0207d354ee47fb56acfa64b03b5b559bb301280/libblkid/src/superblocks/zfs.c
Expand Down Expand Up @@ -70,7 +69,7 @@ func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) {
size - 2*zfsVdevLabelSize - lastLabelOffset,
size - zfsVdevLabelSize - lastLabelOffset,
} {
if err := ioutil.ReadFullAt(r, labelBuf, int64(labelOffset)); err != nil {
if _, err := r.ReadAt(labelBuf, int64(labelOffset)); err != nil {
return nil, fmt.Errorf("reading at offset %d: %w", labelOffset, err)
}

Expand Down
3 changes: 1 addition & 2 deletions blkid/probe_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"github.com/siderolabs/go-blockdevice/v2/blkid/internal/chain"
"github.com/siderolabs/go-blockdevice/v2/blkid/internal/probe"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

type probeReader struct {
Expand Down Expand Up @@ -116,7 +115,7 @@ func (i *Info) probe(f *os.File, chain chain.Chain, offset, length uint64, optio

buf := make([]byte, magicReadSize)

if err := ioutil.ReadFullAt(f, buf, int64(offset)); err != nil {
if _, err := f.ReadAt(buf, int64(offset)); err != nil {
return nil, nil, fmt.Errorf("error reading magic buffer: %w", err)
}

Expand Down
6 changes: 2 additions & 4 deletions internal/gptstructs/header_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"hash/crc32"
"io"
"slices"

"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

// HeaderSignature is the signature of the GPT header.
Expand Down Expand Up @@ -40,7 +38,7 @@ func ReadHeader(r HeaderReader, lba, lastLBA uint64) (*Header, []Entry, error) {
sectorSize := r.GetSectorSize()
buf := make([]byte, sectorSize)

if err := ioutil.ReadFullAt(r, buf, int64(lba)*int64(sectorSize)); err != nil {
if _, err := r.ReadAt(buf, int64(lba)*int64(sectorSize)); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -92,7 +90,7 @@ func ReadHeader(r HeaderReader, lba, lastLBA uint64) (*Header, []Entry, error) {
// read partition entries, verify checksum
entriesBuffer := make([]byte, hdr.Get_num_partition_entries()*ENTRY_SIZE)

if err := ioutil.ReadFullAt(r, entriesBuffer, int64(hdr.Get_partition_entries_lba())*int64(sectorSize)); err != nil {
if _, err := r.ReadAt(entriesBuffer, int64(hdr.Get_partition_entries_lba())*int64(sectorSize)); err != nil {
return nil, nil, err
}

Expand Down
34 changes: 0 additions & 34 deletions internal/ioutil/ioutil.go

This file was deleted.

3 changes: 1 addition & 2 deletions partitioning/gpt/gpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/siderolabs/go-blockdevice/v2/block"
"github.com/siderolabs/go-blockdevice/v2/internal/gptstructs"
"github.com/siderolabs/go-blockdevice/v2/internal/gptutil"
"github.com/siderolabs/go-blockdevice/v2/internal/ioutil"
)

// Device is an interface around actual block device.
Expand Down Expand Up @@ -553,7 +552,7 @@ func (t *Table) Write() error {
func (t *Table) writePMBR() error {
protectiveMBR := make([]byte, 512)

if err := ioutil.ReadFullAt(t.dev, protectiveMBR, 0); err != nil {
if _, err := t.dev.ReadAt(protectiveMBR, 0); err != nil {
return fmt.Errorf("failed to read protective MBR: %w", err)
}

Expand Down

0 comments on commit bc73f6d

Please sign in to comment.