aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2020-07-06 09:49:20 -0400
committerRuss Cox <rsc@golang.org>2020-10-20 02:32:41 +0000
commitd4da735091986868015369e01c63794af9cc9b84 (patch)
tree4f23c46dc0786122d9d8883f1dfb8003c514909e /src/os
parent627959eb04ee0edc4a985a7526ed7fe838ad2573 (diff)
downloadgo-d4da735091986868015369e01c63794af9cc9b84.tar.gz
go-d4da735091986868015369e01c63794af9cc9b84.zip
io/fs: move FileInfo, FileMode, PathError, ErrInvalid, ... from os to io/fs
First step of creating the new io/fs package. For #41190. Change-Id: I1339b1abdd533b0f1deab283628088b2f706fb5b Reviewed-on: https://go-review.googlesource.com/c/go/+/243906 Trust: Russ Cox <rsc@golang.org> Run-TryBot: Russ Cox <rsc@golang.org> Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org> Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Diffstat (limited to 'src/os')
-rw-r--r--src/os/error.go32
-rw-r--r--src/os/types.go91
2 files changed, 26 insertions, 97 deletions
diff --git a/src/os/error.go b/src/os/error.go
index 875cc9711f..7cd9f22bfb 100644
--- a/src/os/error.go
+++ b/src/os/error.go
@@ -7,6 +7,7 @@ package os
import (
"internal/oserror"
"internal/poll"
+ "io/fs"
)
// Portable analogs of some common system call errors.
@@ -16,20 +17,17 @@ import (
var (
// ErrInvalid indicates an invalid argument.
// Methods on File will return this error when the receiver is nil.
- ErrInvalid = errInvalid() // "invalid argument"
+ ErrInvalid = fs.ErrInvalid // "invalid argument"
+
+ ErrPermission = fs.ErrPermission // "permission denied"
+ ErrExist = fs.ErrExist // "file already exists"
+ ErrNotExist = fs.ErrNotExist // "file does not exist"
+ ErrClosed = fs.ErrClosed // "file already closed"
- ErrPermission = errPermission() // "permission denied"
- ErrExist = errExist() // "file already exists"
- ErrNotExist = errNotExist() // "file does not exist"
- ErrClosed = errClosed() // "file already closed"
ErrNoDeadline = errNoDeadline() // "file type does not support deadline"
ErrDeadlineExceeded = errDeadlineExceeded() // "i/o timeout"
)
-func errInvalid() error { return oserror.ErrInvalid }
-func errPermission() error { return oserror.ErrPermission }
-func errExist() error { return oserror.ErrExist }
-func errNotExist() error { return oserror.ErrNotExist }
func errClosed() error { return oserror.ErrClosed }
func errNoDeadline() error { return poll.ErrNoDeadline }
@@ -47,21 +45,7 @@ type timeout interface {
}
// PathError records an error and the operation and file path that caused it.
-type PathError struct {
- Op string
- Path string
- Err error
-}
-
-func (e *PathError) Error() string { return e.Op + " " + e.Path + ": " + e.Err.Error() }
-
-func (e *PathError) Unwrap() error { return e.Err }
-
-// Timeout reports whether this error represents a timeout.
-func (e *PathError) Timeout() bool {
- t, ok := e.Err.(timeout)
- return ok && t.Timeout()
-}
+type PathError = fs.PathError
// SyscallError records an error from a specific system call.
type SyscallError struct {
diff --git a/src/os/types.go b/src/os/types.go
index 0f51a48286..d8edd98b68 100644
--- a/src/os/types.go
+++ b/src/os/types.go
@@ -5,8 +5,8 @@
package os
import (
+ "io/fs"
"syscall"
- "time"
)
// Getpagesize returns the underlying system's memory page size.
@@ -18,21 +18,14 @@ type File struct {
}
// A FileInfo describes a file and is returned by Stat and Lstat.
-type FileInfo interface {
- Name() string // base name of the file
- Size() int64 // length in bytes for regular files; system-dependent for others
- Mode() FileMode // file mode bits
- ModTime() time.Time // modification time
- IsDir() bool // abbreviation for Mode().IsDir()
- Sys() interface{} // underlying data source (can return nil)
-}
+type FileInfo = fs.FileInfo
// A FileMode represents a file's mode and permission bits.
// The bits have the same definition on all systems, so that
// information about files can be moved from one system
// to another portably. Not all bits apply to all systems.
// The only required bit is ModeDir for directories.
-type FileMode uint32
+type FileMode = fs.FileMode
// The defined file mode bits are the most significant bits of the FileMode.
// The nine least-significant bits are the standard Unix rwxrwxrwx permissions.
@@ -42,74 +35,26 @@ type FileMode uint32
const (
// The single letters are the abbreviations
// used by the String method's formatting.
- ModeDir FileMode = 1 << (32 - 1 - iota) // d: is a directory
- ModeAppend // a: append-only
- ModeExclusive // l: exclusive use
- ModeTemporary // T: temporary file; Plan 9 only
- ModeSymlink // L: symbolic link
- ModeDevice // D: device file
- ModeNamedPipe // p: named pipe (FIFO)
- ModeSocket // S: Unix domain socket
- ModeSetuid // u: setuid
- ModeSetgid // g: setgid
- ModeCharDevice // c: Unix character device, when ModeDevice is set
- ModeSticky // t: sticky
- ModeIrregular // ?: non-regular file; nothing else is known about this file
+ ModeDir = fs.ModeDir // d: is a directory
+ ModeAppend = fs.ModeAppend // a: append-only
+ ModeExclusive = fs.ModeExclusive // l: exclusive use
+ ModeTemporary = fs.ModeTemporary // T: temporary file; Plan 9 only
+ ModeSymlink = fs.ModeSymlink // L: symbolic link
+ ModeDevice = fs.ModeDevice // D: device file
+ ModeNamedPipe = fs.ModeNamedPipe // p: named pipe (FIFO)
+ ModeSocket = fs.ModeSocket // S: Unix domain socket
+ ModeSetuid = fs.ModeSetuid // u: setuid
+ ModeSetgid = fs.ModeSetgid // g: setgid
+ ModeCharDevice = fs.ModeCharDevice // c: Unix character device, when ModeDevice is set
+ ModeSticky = fs.ModeSticky // t: sticky
+ ModeIrregular = fs.ModeIrregular // ?: non-regular file; nothing else is known about this file
// Mask for the type bits. For regular files, none will be set.
- ModeType = ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
+ ModeType = fs.ModeType
- ModePerm FileMode = 0777 // Unix permission bits
+ ModePerm = fs.ModePerm // Unix permission bits, 0o777
)
-func (m FileMode) String() string {
- const str = "dalTLDpSugct?"
- var buf [32]byte // Mode is uint32.
- w := 0
- for i, c := range str {
- if m&(1<<uint(32-1-i)) != 0 {
- buf[w] = byte(c)
- w++
- }
- }
- if w == 0 {
- buf[w] = '-'
- w++
- }
- const rwx = "rwxrwxrwx"
- for i, c := range rwx {
- if m&(1<<uint(9-1-i)) != 0 {
- buf[w] = byte(c)
- } else {
- buf[w] = '-'
- }
- w++
- }
- return string(buf[:w])
-}
-
-// IsDir reports whether m describes a directory.
-// That is, it tests for the ModeDir bit being set in m.
-func (m FileMode) IsDir() bool {
- return m&ModeDir != 0
-}
-
-// IsRegular reports whether m describes a regular file.
-// That is, it tests that no mode type bits are set.
-func (m FileMode) IsRegular() bool {
- return m&ModeType == 0
-}
-
-// Perm returns the Unix permission bits in m (m & ModePerm).
-func (m FileMode) Perm() FileMode {
- return m & ModePerm
-}
-
-// Type returns type bits in m (m & ModeType).
-func (m FileMode) Type() FileMode {
- return m & ModeType
-}
-
func (fs *fileStat) Name() string { return fs.name }
func (fs *fileStat) IsDir() bool { return fs.Mode().IsDir() }