summaryrefslogtreecommitdiff
path: root/libgo/go/io
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2021-07-30 14:28:58 -0700
committerIan Lance Taylor <iant@golang.org>2021-08-12 20:23:07 -0700
commitc5b21c3f4c17b0649155035d2f9aa97b2da8a813 (patch)
treec6d3a68b503ba5b16182acbb958e3e5dbc95a43b /libgo/go/io
parent72be20e20299ec57b4bc9ba03d5b7d6bf10e97cc (diff)
downloadgcc-c5b21c3f4c17b0649155035d2f9aa97b2da8a813.tar.gz
libgo: update to Go1.17rc2
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/341629
Diffstat (limited to 'libgo/go/io')
-rw-r--r--libgo/go/io/example_test.go4
-rw-r--r--libgo/go/io/fs/example_test.go25
-rw-r--r--libgo/go/io/fs/fs.go4
-rw-r--r--libgo/go/io/fs/readdir.go30
-rw-r--r--libgo/go/io/fs/readdir_test.go50
-rw-r--r--libgo/go/io/fs/readfile.go3
-rw-r--r--libgo/go/io/fs/sub.go19
-rw-r--r--libgo/go/io/fs/walk_test.go7
-rw-r--r--libgo/go/io/io.go2
-rw-r--r--libgo/go/io/ioutil/export_test.go7
-rw-r--r--libgo/go/io/ioutil/tempfile.go108
-rw-r--r--libgo/go/io/ioutil/tempfile_test.go13
12 files changed, 143 insertions, 129 deletions
diff --git a/libgo/go/io/example_test.go b/libgo/go/io/example_test.go
index 6d338acd140..a18df9feff6 100644
--- a/libgo/go/io/example_test.go
+++ b/libgo/go/io/example_test.go
@@ -103,7 +103,9 @@ func ExampleReadFull() {
}
func ExampleWriteString() {
- io.WriteString(os.Stdout, "Hello World")
+ if _, err := io.WriteString(os.Stdout, "Hello World"); err != nil {
+ log.Fatal(err)
+ }
// Output: Hello World
}
diff --git a/libgo/go/io/fs/example_test.go b/libgo/go/io/fs/example_test.go
new file mode 100644
index 00000000000..c9027034c4a
--- /dev/null
+++ b/libgo/go/io/fs/example_test.go
@@ -0,0 +1,25 @@
+// Copyright 2021 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 fs_test
+
+import (
+ "fmt"
+ "io/fs"
+ "log"
+ "os"
+)
+
+func ExampleWalkDir() {
+ root := "/usr/local/go/bin"
+ fileSystem := os.DirFS(root)
+
+ fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Println(path)
+ return nil
+ })
+}
diff --git a/libgo/go/io/fs/fs.go b/libgo/go/io/fs/fs.go
index 3d2e2ee2ac9..e1be32478e0 100644
--- a/libgo/go/io/fs/fs.go
+++ b/libgo/go/io/fs/fs.go
@@ -73,8 +73,8 @@ func ValidPath(name string) bool {
// A File provides access to a single file.
// The File interface is the minimum implementation required of the file.
-// A file may implement additional interfaces, such as
-// ReadDirFile, ReaderAt, or Seeker, to provide additional or optimized functionality.
+// Directory files should also implement ReadDirFile.
+// A file may implement io.ReaderAt or io.Seeker as optimizations.
type File interface {
Stat() (FileInfo, error)
Read([]byte) (int, error)
diff --git a/libgo/go/io/fs/readdir.go b/libgo/go/io/fs/readdir.go
index 3a5aa6d86a6..2b10ddb0a3f 100644
--- a/libgo/go/io/fs/readdir.go
+++ b/libgo/go/io/fs/readdir.go
@@ -45,3 +45,33 @@ func ReadDir(fsys FS, name string) ([]DirEntry, error) {
sort.Slice(list, func(i, j int) bool { return list[i].Name() < list[j].Name() })
return list, err
}
+
+// dirInfo is a DirEntry based on a FileInfo.
+type dirInfo struct {
+ fileInfo FileInfo
+}
+
+func (di dirInfo) IsDir() bool {
+ return di.fileInfo.IsDir()
+}
+
+func (di dirInfo) Type() FileMode {
+ return di.fileInfo.Mode().Type()
+}
+
+func (di dirInfo) Info() (FileInfo, error) {
+ return di.fileInfo, nil
+}
+
+func (di dirInfo) Name() string {
+ return di.fileInfo.Name()
+}
+
+// FileInfoToDirEntry returns a DirEntry that returns information from info.
+// If info is nil, FileInfoToDirEntry returns nil.
+func FileInfoToDirEntry(info FileInfo) DirEntry {
+ if info == nil {
+ return nil
+ }
+ return dirInfo{fileInfo: info}
+}
diff --git a/libgo/go/io/fs/readdir_test.go b/libgo/go/io/fs/readdir_test.go
index 405bfa67ca4..a2b2c121ffa 100644
--- a/libgo/go/io/fs/readdir_test.go
+++ b/libgo/go/io/fs/readdir_test.go
@@ -6,7 +6,10 @@ package fs_test
import (
. "io/fs"
+ "os"
"testing"
+ "testing/fstest"
+ "time"
)
type readDirOnly struct{ ReadDirFS }
@@ -41,3 +44,50 @@ func TestReadDir(t *testing.T) {
dirs, err = ReadDir(sub, ".")
check("sub(.)", dirs, err)
}
+
+func TestFileInfoToDirEntry(t *testing.T) {
+ testFs := fstest.MapFS{
+ "notadir.txt": {
+ Data: []byte("hello, world"),
+ Mode: 0,
+ ModTime: time.Now(),
+ Sys: &sysValue,
+ },
+ "adir": {
+ Data: nil,
+ Mode: os.ModeDir,
+ ModTime: time.Now(),
+ Sys: &sysValue,
+ },
+ }
+
+ tests := []struct {
+ path string
+ wantMode FileMode
+ wantDir bool
+ }{
+ {path: "notadir.txt", wantMode: 0, wantDir: false},
+ {path: "adir", wantMode: os.ModeDir, wantDir: true},
+ }
+
+ for _, test := range tests {
+ test := test
+ t.Run(test.path, func(t *testing.T) {
+ fi, err := Stat(testFs, test.path)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ dirEntry := FileInfoToDirEntry(fi)
+ if g, w := dirEntry.Type(), test.wantMode; g != w {
+ t.Errorf("FileMode mismatch: got=%v, want=%v", g, w)
+ }
+ if g, w := dirEntry.Name(), test.path; g != w {
+ t.Errorf("Name mismatch: got=%v, want=%v", g, w)
+ }
+ if g, w := dirEntry.IsDir(), test.wantDir; g != w {
+ t.Errorf("IsDir mismatch: got=%v, want=%v", g, w)
+ }
+ })
+ }
+}
diff --git a/libgo/go/io/fs/readfile.go b/libgo/go/io/fs/readfile.go
index 7ee9eadac4d..d3c181c0a9e 100644
--- a/libgo/go/io/fs/readfile.go
+++ b/libgo/go/io/fs/readfile.go
@@ -15,6 +15,9 @@ type ReadFileFS interface {
// A successful call returns a nil error, not io.EOF.
// (Because ReadFile reads the whole file, the expected EOF
// from the final Read is not treated as an error to be reported.)
+ //
+ // The caller is permitted to modify the returned byte slice.
+ // This method should return a copy of the underlying data.
ReadFile(name string) ([]byte, error)
}
diff --git a/libgo/go/io/fs/sub.go b/libgo/go/io/fs/sub.go
index 64cdffe6dec..ae20e030a93 100644
--- a/libgo/go/io/fs/sub.go
+++ b/libgo/go/io/fs/sub.go
@@ -19,10 +19,10 @@ type SubFS interface {
// Sub returns an FS corresponding to the subtree rooted at fsys's dir.
//
-// If fs implements SubFS, Sub calls returns fsys.Sub(dir).
-// Otherwise, if dir is ".", Sub returns fsys unchanged.
+// If dir is ".", Sub returns fsys unchanged.
+// Otherwise, if fs implements SubFS, Sub returns fsys.Sub(dir).
// Otherwise, Sub returns a new FS implementation sub that,
-// in effect, implements sub.Open(dir) as fsys.Open(path.Join(dir, name)).
+// in effect, implements sub.Open(name) as fsys.Open(path.Join(dir, name)).
// The implementation also translates calls to ReadDir, ReadFile, and Glob appropriately.
//
// Note that Sub(os.DirFS("/"), "prefix") is equivalent to os.DirFS("/prefix")
@@ -68,7 +68,7 @@ func (f *subFS) shorten(name string) (rel string, ok bool) {
return "", false
}
-// fixErr shortens any reported names in PathErrors by stripping dir.
+// fixErr shortens any reported names in PathErrors by stripping f.dir.
func (f *subFS) fixErr(err error) error {
if e, ok := err.(*PathError); ok {
if short, ok := f.shorten(e.Path); ok {
@@ -125,3 +125,14 @@ func (f *subFS) Glob(pattern string) ([]string, error) {
}
return list, f.fixErr(err)
}
+
+func (f *subFS) Sub(dir string) (FS, error) {
+ if dir == "." {
+ return f, nil
+ }
+ full, err := f.fullName("sub", dir)
+ if err != nil {
+ return nil, err
+ }
+ return &subFS{f.fsys, full}, nil
+}
diff --git a/libgo/go/io/fs/walk_test.go b/libgo/go/io/fs/walk_test.go
index ebc4e50fb31..5e127e71cd9 100644
--- a/libgo/go/io/fs/walk_test.go
+++ b/libgo/go/io/fs/walk_test.go
@@ -6,7 +6,6 @@ package fs_test
import (
. "io/fs"
- "io/ioutil"
"os"
pathpkg "path"
"testing"
@@ -96,11 +95,7 @@ func mark(entry DirEntry, err error, errors *[]error, clear bool) error {
}
func TestWalkDir(t *testing.T) {
- tmpDir, err := ioutil.TempDir("", "TestWalk")
- if err != nil {
- t.Fatal("creating temp dir:", err)
- }
- defer os.RemoveAll(tmpDir)
+ tmpDir := t.TempDir()
origDir, err := os.Getwd()
if err != nil {
diff --git a/libgo/go/io/io.go b/libgo/go/io/io.go
index ffd3cedc254..2724321ed9d 100644
--- a/libgo/go/io/io.go
+++ b/libgo/go/io/io.go
@@ -566,7 +566,7 @@ func (t *teeReader) Read(p []byte) (n int, err error) {
return
}
-// Discard is an Writer on which all Write calls succeed
+// Discard is a Writer on which all Write calls succeed
// without doing anything.
var Discard Writer = discard{}
diff --git a/libgo/go/io/ioutil/export_test.go b/libgo/go/io/ioutil/export_test.go
deleted file mode 100644
index dff55f07e26..00000000000
--- a/libgo/go/io/ioutil/export_test.go
+++ /dev/null
@@ -1,7 +0,0 @@
-// Copyright 2020 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 ioutil
-
-var ErrPatternHasSeparator = errPatternHasSeparator
diff --git a/libgo/go/io/ioutil/tempfile.go b/libgo/go/io/ioutil/tempfile.go
index af7c6fd7c1a..c43db2c080d 100644
--- a/libgo/go/io/ioutil/tempfile.go
+++ b/libgo/go/io/ioutil/tempfile.go
@@ -5,38 +5,9 @@
package ioutil
import (
- "errors"
"os"
- "path/filepath"
- "strconv"
- "strings"
- "sync"
- "time"
)
-// Random number state.
-// We generate random temporary file names so that there's a good
-// chance the file doesn't exist yet - keeps the number of tries in
-// TempFile to a minimum.
-var rand uint32
-var randmu sync.Mutex
-
-func reseed() uint32 {
- return uint32(time.Now().UnixNano() + int64(os.Getpid()))
-}
-
-func nextRandom() string {
- randmu.Lock()
- r := rand
- if r == 0 {
- r = reseed()
- }
- r = r*1664525 + 1013904223 // constants from Numerical Recipes
- rand = r
- randmu.Unlock()
- return strconv.Itoa(int(1e9 + r%1e9))[1:]
-}
-
// TempFile creates a new temporary file in the directory dir,
// opens the file for reading and writing, and returns the resulting *os.File.
// The filename is generated by taking pattern and adding a random
@@ -48,48 +19,10 @@ func nextRandom() string {
// will not choose the same file. The caller can use f.Name()
// to find the pathname of the file. It is the caller's responsibility
// to remove the file when no longer needed.
+//
+// As of Go 1.17, this function simply calls os.CreateTemp.
func TempFile(dir, pattern string) (f *os.File, err error) {
- if dir == "" {
- dir = os.TempDir()
- }
-
- prefix, suffix, err := prefixAndSuffix(pattern)
- if err != nil {
- return
- }
-
- nconflict := 0
- for i := 0; i < 10000; i++ {
- name := filepath.Join(dir, prefix+nextRandom()+suffix)
- f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
- if os.IsExist(err) {
- if nconflict++; nconflict > 10 {
- randmu.Lock()
- rand = reseed()
- randmu.Unlock()
- }
- continue
- }
- break
- }
- return
-}
-
-var errPatternHasSeparator = errors.New("pattern contains path separator")
-
-// prefixAndSuffix splits pattern by the last wildcard "*", if applicable,
-// returning prefix as the part before "*" and suffix as the part after "*".
-func prefixAndSuffix(pattern string) (prefix, suffix string, err error) {
- if strings.ContainsRune(pattern, os.PathSeparator) {
- err = errPatternHasSeparator
- return
- }
- if pos := strings.LastIndex(pattern, "*"); pos != -1 {
- prefix, suffix = pattern[:pos], pattern[pos+1:]
- } else {
- prefix = pattern
- }
- return
+ return os.CreateTemp(dir, pattern)
}
// TempDir creates a new temporary directory in the directory dir.
@@ -101,37 +34,8 @@ func prefixAndSuffix(pattern string) (prefix, suffix string, err error) {
// Multiple programs calling TempDir simultaneously
// will not choose the same directory. It is the caller's responsibility
// to remove the directory when no longer needed.
+//
+// As of Go 1.17, this function simply calls os.MkdirTemp.
func TempDir(dir, pattern string) (name string, err error) {
- if dir == "" {
- dir = os.TempDir()
- }
-
- prefix, suffix, err := prefixAndSuffix(pattern)
- if err != nil {
- return
- }
-
- nconflict := 0
- for i := 0; i < 10000; i++ {
- try := filepath.Join(dir, prefix+nextRandom()+suffix)
- err = os.Mkdir(try, 0700)
- if os.IsExist(err) {
- if nconflict++; nconflict > 10 {
- randmu.Lock()
- rand = reseed()
- randmu.Unlock()
- }
- continue
- }
- if os.IsNotExist(err) {
- if _, err := os.Stat(dir); os.IsNotExist(err) {
- return "", err
- }
- }
- if err == nil {
- name = try
- }
- break
- }
- return
+ return os.MkdirTemp(dir, pattern)
}
diff --git a/libgo/go/io/ioutil/tempfile_test.go b/libgo/go/io/ioutil/tempfile_test.go
index 440c7cffc67..5cef18c33b0 100644
--- a/libgo/go/io/ioutil/tempfile_test.go
+++ b/libgo/go/io/ioutil/tempfile_test.go
@@ -50,6 +50,9 @@ func TestTempFile_pattern(t *testing.T) {
}
}
+// This string is from os.errPatternHasSeparator.
+const patternHasSeparator = "pattern contains path separator"
+
func TestTempFile_BadPattern(t *testing.T) {
tmpDir, err := TempDir("", t.Name())
if err != nil {
@@ -81,9 +84,8 @@ func TestTempFile_BadPattern(t *testing.T) {
if tt.wantErr {
if err == nil {
t.Errorf("Expected an error for pattern %q", tt.pattern)
- }
- if g, w := err, ErrPatternHasSeparator; g != w {
- t.Errorf("Error mismatch: got %#v, want %#v for pattern %q", g, w, tt.pattern)
+ } else if !strings.Contains(err.Error(), patternHasSeparator) {
+ t.Errorf("Error mismatch: got %#v, want %q for pattern %q", err, patternHasSeparator, tt.pattern)
}
} else if err != nil {
t.Errorf("Unexpected error %v for pattern %q", err, tt.pattern)
@@ -183,9 +185,8 @@ func TestTempDir_BadPattern(t *testing.T) {
if tt.wantErr {
if err == nil {
t.Errorf("Expected an error for pattern %q", tt.pattern)
- }
- if g, w := err, ErrPatternHasSeparator; g != w {
- t.Errorf("Error mismatch: got %#v, want %#v for pattern %q", g, w, tt.pattern)
+ } else if !strings.Contains(err.Error(), patternHasSeparator) {
+ t.Errorf("Error mismatch: got %#v, want %q for pattern %q", err, patternHasSeparator, tt.pattern)
}
} else if err != nil {
t.Errorf("Unexpected error %v for pattern %q", err, tt.pattern)