Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove deprecated io/ioutil methods #1277

Merged
merged 15 commits into from
Nov 8, 2023
3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"go/parser"
"go/token"
"io/ioutil"
"os"
"path/filepath"
"reflect"
Expand Down Expand Up @@ -1095,7 +1094,7 @@ func PackageNameFromFileBody(name, body string) Name {

// NOTE: panics if package name is invalid.
func ReadMemPackage(dir string, pkgPath string) *std.MemPackage {
files, err := ioutil.ReadDir(dir)
files, err := os.ReadDir(dir)
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions gnovm/pkg/gnolang/precompile.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"go/format"
"go/parser"
"go/token"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -112,7 +111,7 @@ func GetPrecompileFilenameAndTags(gnoFilePath string) (targetFilename, tags stri
func PrecompileAndCheckMempkg(mempkg *std.MemPackage) error {
gofmt := "gofmt"

tmpDir, err := ioutil.TempDir("", mempkg.Name)
tmpDir, err := os.MkdirTemp("", mempkg.Name)
if err != nil {
return err
}
Expand Down
5 changes: 2 additions & 3 deletions gnovm/pkg/gnomod/gnomod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gnomod
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -172,9 +171,9 @@ func CreateGnoModFile(rootDir, modPath string) error {
if modPath == "" {
// Check .gno files for package name
// and use it as modPath
files, err := ioutil.ReadDir(rootDir)
files, err := os.ReadDir(rootDir)
if err != nil {
fmt.Errorf("read dir %q: %w", rootDir, err)
return fmt.Errorf("read dir %q: %w", rootDir, err)
}

var pkgName gnolang.Name
Expand Down
6 changes: 5 additions & 1 deletion gnovm/stdlibs/io/export_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
package io

// exported for test
var ErrInvalidWrite = errInvalidWrite
var (
ErrInvalidWrite = errInvalidWrite
ErrWhence = errWhence
ErrOffset = errOffset
)
77 changes: 77 additions & 0 deletions gnovm/stdlibs/io/io.gno
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"errors"
)

// TODO: implement rest of io package after sync package added.

// Seek whence values.
const (
SeekStart = 0 // seek relative to the origin of the file
Expand Down Expand Up @@ -477,6 +479,15 @@ func (l *LimitedReader) Read(p []byte) (n int, err error) {
// NewSectionReader returns a SectionReader that reads from r
// starting at offset off and stops with EOF after n bytes.
func NewSectionReader(r ReaderAt, off int64, n int64) *SectionReader {
var remaining int64
const maxInt64 = 1<<63 - 1
if off <= maxInt64-n {
remaining = n + off
} else {
// Overflow, with no way to return error.
// Assume we can read up to an offset of `1<<63 - 1` bytes in this case.
remaining = maxInt64
}
return &SectionReader{r, off, off, off + n}
}

Expand Down Expand Up @@ -543,6 +554,53 @@ func (s *SectionReader) ReadAt(p []byte, off int64) (n int, err error) {
// Size returns the size of the section in bytes.
func (s *SectionReader) Size() int64 { return s.limit - s.base }

// An OffsetWriter maps writers at offset base to offset base + off in the underlying writer.
type OffsetWriter struct {
w WriterAt
base int64 // the original offset
off int64 // the current offset
}

// NewOffsetWriter returns a new OffsetWriter that writes to w starting at offset off.
func NewOffsetWriter(w WriterAt, off int64) *OffsetWriter {
return &OffsetWriter{w: w, off: off}
}

func (o *OffsetWriter) Write(p []byte) (n int, err error) {
// n, err = o.w.WriterAt(p, o.off)
wa := o.w
n, err = wa.WriteAt(p, o.off)
o.off += int64(n)
return
}

func (o *OffsetWriter) WriteAt(p []byte, off int64) (n int, err error) {
if off < 0 {
return 0, errOffset
}

off += o.base
return o.w.WriteAt(p, off)
}

func (o *OffsetWriter) Seek(offset int64, whence int) (int64, error) {
switch whence {
default:
return 0, errWhence
case SeekStart:
offset += o.base
case SeekCurrent:
offset += o.off
}

if offset < o.base {
return 0, errOffset
}

o.off = offset
return offset - o.base, nil
}

// TeeReader returns a Reader that writes to w what it reads from r.
// All reads from r performed through it are matched with
// corresponding writes to w. There is no internal buffering -
Expand Down Expand Up @@ -614,7 +672,12 @@ func (discard) ReadFrom(r Reader) (n int64, err error) {

// NopCloser returns a ReadCloser with a no-op Close method wrapping
// the provided Reader r.
// If r implements WriterTo, the returned ReadCloser will implement WriterTo
// by forwarding calls to r.
func NopCloser(r Reader) ReadCloser {
if _, ok := r.(WriterTo); ok {
return nopCloserWriterTo{r}
}
return nopCloser{r}
}

Expand All @@ -624,6 +687,16 @@ type nopCloser struct {

func (nopCloser) Close() error { return nil }

type nopCloserWriterTo struct {
Reader
}

func (nopCloserWriterTo) Close() error { return nil }

func (c nopCloserWriterTo) WriteTo(w Writer) (n int64, err error) {
return c.Reader.(WriterTo).WriteTo(w)
}

// ReadAll reads from r until an error or EOF and returns the data it read.
// A successful call returns err == nil, not err == EOF. Because ReadAll is
// defined to read from src until EOF, it does not treat an EOF from Read
Expand All @@ -643,5 +716,9 @@ func ReadAll(r Reader) ([]byte, error) {
}
return b, err
}
if len(b) == cap(b) {
// Add more capacity (let append pick how much).
b = append(b, 0)[:len(b)]
}
}
}
92 changes: 87 additions & 5 deletions gnovm/stdlibs/io/io_test.gno
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package io_test

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package io_test

import (
"bytes"
"errors"
"fmt"
"io"
"os"
"strings"
"testing"
)
Expand Down Expand Up @@ -443,19 +444,100 @@ func (w largeWriter) Write(p []byte) (int, error) {
}

func TestCopyLargeWriter(t *testing.T) {
want := io.ErrInvalidWrite
want := errInvalidWrite
rb := new(Buffer)
wb := largeWriter{}
rb.WriteString("hello, world.")
if _, err := io.Copy(wb, rb); err != want {
if _, err := Copy(wb, rb); err != want {
t.Errorf("Copy error: got %v, want %v", err, want)
}

want = errors.New("largeWriterError")
rb = new(Buffer)
wb = largeWriter{err: want}
rb.WriteString("hello, world.")
if _, err := io.Copy(wb, rb); err != want {
if _, err := Copy(wb, rb); err != want {
t.Errorf("Copy error: got %v, want %v", err, want)
}
}

func TestNopCloserWriterToForwarding(t *testing.T) {
for _, tc := range [...]struct {
Name string
r Reader
}{
{"not a WriterTo", Reader(nil)},
{"a WriterTo", struct {
Reader
WriterTo
}{}},
} {
nc := NopCloser(tc.r)

_, expected := tc.r.(WriterTo)
_, got := nc.(WriterTo)
if expected != got {
t.Errorf("NopCloser incorrectly forwards WriterTo for %s, got %t want %t", tc.Name, got, expected)
}
}
}

// XXX os.CreateTemp is not available for now
// func TestOffsetWriter_Seek(t *testing.T) {
// tmpfilename := "TestOffsetWriter_Seek"
// tmpfile, err := os.CreateTemp(t.TempDir(), tmpfilename)
// if err != nil || tmpfile == nil {
// t.Fatalf("CreateTemp(%s) failed: %v", tmpfilename, err)
// }
// defer tmpfile.Close()
// w := NewOffsetWriter(tmpfile, 0)

// // Should throw error errWhence if whence is not valid
// t.Run("errWhence", func(t *testing.T) {
// for _, whence := range []int{-3, -2, -1, 3, 4, 5} {
// var offset int64 = 0
// gotOff, gotErr := w.Seek(offset, whence)
// if gotOff != 0 || gotErr != ErrWhence {
// t.Errorf("For whence %d, offset %d, OffsetWriter.Seek got: (%d, %v), want: (%d, %v)",
// whence, offset, gotOff, gotErr, 0, ErrWhence)
// }
// }
// })

// // Should throw error errOffset if offset is negative
// t.Run("errOffset", func(t *testing.T) {
// for _, whence := range []int{SeekStart, SeekCurrent} {
// for offset := int64(-3); offset < 0; offset++ {
// gotOff, gotErr := w.Seek(offset, whence)
// if gotOff != 0 || gotErr != ErrOffset {
// t.Errorf("For whence %d, offset %d, OffsetWriter.Seek got: (%d, %v), want: (%d, %v)",
// whence, offset, gotOff, gotErr, 0, ErrOffset)
// }
// }
// }
// })

// // Normal tests
// t.Run("normal", func(t *testing.T) {
// tests := []struct {
// offset int64
// whence int
// returnOff int64
// }{
// // keep in order
// {whence: SeekStart, offset: 1, returnOff: 1},
// {whence: SeekStart, offset: 2, returnOff: 2},
// {whence: SeekStart, offset: 3, returnOff: 3},
// {whence: SeekCurrent, offset: 1, returnOff: 4},
// {whence: SeekCurrent, offset: 2, returnOff: 6},
// {whence: SeekCurrent, offset: 3, returnOff: 9},
// }
// for idx, tt := range tests {
// gotOff, gotErr := w.Seek(tt.offset, tt.whence)
// if gotOff != tt.returnOff || gotErr != nil {
// t.Errorf("%d:: For whence %d, offset %d, OffsetWriter.Seek got: (%d, %v), want: (%d, <nil>)",
// idx+1, tt.whence, tt.offset, gotOff, gotErr, tt.returnOff)
// }
// }
// })
// }
83 changes: 0 additions & 83 deletions gnovm/stdlibs/io/ioutil/ioutil.gno

This file was deleted.

Loading
Loading