github.com/rminnich/u-root@v7.0.0+incompatible/pkg/memio/seek.go (about)

     1  // Copyright 2012-2020 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package memio
     6  
     7  import (
     8  	"encoding/binary"
     9  	"io"
    10  	"os"
    11  
    12  	"github.com/u-root/u-root/pkg/ubinary"
    13  )
    14  
    15  func pathRead(path string, addr int64, data UintN) error {
    16  	f, err := os.OpenFile(path, os.O_RDONLY, 0)
    17  	if err != nil {
    18  		return err
    19  	}
    20  	defer f.Close()
    21  
    22  	if _, err := f.Seek(addr, io.SeekStart); err != nil {
    23  		return err
    24  	}
    25  	return binary.Read(f, ubinary.NativeEndian, data)
    26  }
    27  
    28  func pathWrite(path string, addr int64, data UintN) error {
    29  	f, err := os.OpenFile(path, os.O_WRONLY, 0)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer f.Close()
    34  
    35  	if _, err := f.Seek(addr, io.SeekStart); err != nil {
    36  		return err
    37  	}
    38  	return binary.Write(f, ubinary.NativeEndian, data)
    39  }