From bc73f6d6b2e2bfcbb46f02f0ce93da1d76596c40 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Wed, 21 Aug 2024 18:54:31 +0400 Subject: [PATCH] fix: drop `ReadFullAt` 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 --- blkid/internal/filesystems/ext/ext.go | 3 +- blkid/internal/filesystems/iso9660/iso9660.go | 3 +- blkid/internal/filesystems/luks/luks.go | 3 +- blkid/internal/filesystems/lvm2/lvm2.go | 3 +- .../internal/filesystems/squashfs/squashfs.go | 3 +- blkid/internal/filesystems/swap/swap.go | 3 +- .../filesystems/talosmeta/talosmeta.go | 5 ++- blkid/internal/filesystems/vfat/vfat.go | 5 ++- blkid/internal/filesystems/xfs/xfs.go | 3 +- blkid/internal/filesystems/zfs/zfs.go | 3 +- blkid/probe_linux.go | 3 +- internal/gptstructs/header_extra.go | 6 ++-- internal/ioutil/ioutil.go | 34 ------------------- partitioning/gpt/gpt.go | 3 +- 14 files changed, 16 insertions(+), 64 deletions(-) delete mode 100644 internal/ioutil/ioutil.go diff --git a/blkid/internal/filesystems/ext/ext.go b/blkid/internal/filesystems/ext/ext.go index 9bdfeaf..6aeaa4d 100644 --- a/blkid/internal/filesystems/ext/ext.go +++ b/blkid/internal/filesystems/ext/ext.go @@ -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 @@ -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 } diff --git a/blkid/internal/filesystems/iso9660/iso9660.go b/blkid/internal/filesystems/iso9660/iso9660.go index bd35976..85ae542 100644 --- a/blkid/internal/filesystems/iso9660/iso9660.go +++ b/blkid/internal/filesystems/iso9660/iso9660.go @@ -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 ( @@ -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 } diff --git a/blkid/internal/filesystems/luks/luks.go b/blkid/internal/filesystems/luks/luks.go index e221f37..3a83e25 100644 --- a/blkid/internal/filesystems/luks/luks.go +++ b/blkid/internal/filesystems/luks/luks.go @@ -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{ @@ -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 } diff --git a/blkid/internal/filesystems/lvm2/lvm2.go b/blkid/internal/filesystems/lvm2/lvm2.go index af54ff8..4c897c2 100644 --- a/blkid/internal/filesystems/lvm2/lvm2.go +++ b/blkid/internal/filesystems/lvm2/lvm2.go @@ -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 ( @@ -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 } diff --git a/blkid/internal/filesystems/squashfs/squashfs.go b/blkid/internal/filesystems/squashfs/squashfs.go index 4564bb6..025ed05 100644 --- a/blkid/internal/filesystems/squashfs/squashfs.go +++ b/blkid/internal/filesystems/squashfs/squashfs.go @@ -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 @@ -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 } diff --git a/blkid/internal/filesystems/swap/swap.go b/blkid/internal/filesystems/swap/swap.go index 92247c9..af00a55 100644 --- a/blkid/internal/filesystems/swap/swap.go +++ b/blkid/internal/filesystems/swap/swap.go @@ -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 ( @@ -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 } diff --git a/blkid/internal/filesystems/talosmeta/talosmeta.go b/blkid/internal/filesystems/talosmeta/talosmeta.go index aff16db..00ba822 100644 --- a/blkid/internal/filesystems/talosmeta/talosmeta.go +++ b/blkid/internal/filesystems/talosmeta/talosmeta.go @@ -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. @@ -45,7 +44,7 @@ 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 } @@ -53,7 +52,7 @@ func (p *Probe) Probe(r probe.Reader, _ magic.Magic) (*probe.Result, error) { 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 } diff --git a/blkid/internal/filesystems/vfat/vfat.go b/blkid/internal/filesystems/vfat/vfat.go index 0b31cf1..27b9eca 100644 --- a/blkid/internal/filesystems/vfat/vfat.go +++ b/blkid/internal/filesystems/vfat/vfat.go @@ -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 ( @@ -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 } diff --git a/blkid/internal/filesystems/xfs/xfs.go b/blkid/internal/filesystems/xfs/xfs.go index 01e141c..41acca4 100644 --- a/blkid/internal/filesystems/xfs/xfs.go +++ b/blkid/internal/filesystems/xfs/xfs.go @@ -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{ @@ -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 } diff --git a/blkid/internal/filesystems/zfs/zfs.go b/blkid/internal/filesystems/zfs/zfs.go index 0f0e995..47f7c1c 100644 --- a/blkid/internal/filesystems/zfs/zfs.go +++ b/blkid/internal/filesystems/zfs/zfs.go @@ -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 @@ -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) } diff --git a/blkid/probe_linux.go b/blkid/probe_linux.go index e69f8ff..5a4f6e7 100644 --- a/blkid/probe_linux.go +++ b/blkid/probe_linux.go @@ -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 { @@ -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) } diff --git a/internal/gptstructs/header_extra.go b/internal/gptstructs/header_extra.go index 15f7588..3cf03a0 100644 --- a/internal/gptstructs/header_extra.go +++ b/internal/gptstructs/header_extra.go @@ -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. @@ -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 } @@ -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 } diff --git a/internal/ioutil/ioutil.go b/internal/ioutil/ioutil.go deleted file mode 100644 index bb6615a..0000000 --- a/internal/ioutil/ioutil.go +++ /dev/null @@ -1,34 +0,0 @@ -// This Source Code Form is subject to the terms of the Mozilla Public -// License, v. 2.0. If a copy of the MPL was not distributed with this -// file, You can obtain one at http://mozilla.org/MPL/2.0/. - -// Package ioutil provides IO utility functions. -package ioutil - -import ( - "io" -) - -// ReadFullAt is io.ReadFull for io.ReaderAt. -func ReadFullAt(r io.ReaderAt, buf []byte, offset int64) error { - for n := 0; n < len(buf); { - m, err := r.ReadAt(buf[n:], offset) - - n += m - offset += int64(m) - - if err != nil { - if err == io.EOF && n == len(buf) { - return nil - } - - if err == io.EOF { - err = io.ErrUnexpectedEOF - } - - return err - } - } - - return nil -} diff --git a/partitioning/gpt/gpt.go b/partitioning/gpt/gpt.go index a1f2fcf..bf7c4ed 100644 --- a/partitioning/gpt/gpt.go +++ b/partitioning/gpt/gpt.go @@ -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. @@ -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) }