patch: os.File
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"io"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Name returns the name of the file as presented to Open.
|
||||
@@ -172,36 +173,42 @@ func (f *File) WriteAt(b []byte, off int64) (n int, err error) {
|
||||
}
|
||||
return
|
||||
*/
|
||||
panic("todo")
|
||||
panic("todo: os.(*File).WriteAt")
|
||||
}
|
||||
|
||||
/*
|
||||
// Seek sets the offset for the next Read or Write on file to offset, interpreted
|
||||
// according to whence: 0 means relative to the origin of the file, 1 means
|
||||
// relative to the current offset, and 2 means relative to the end.
|
||||
// It returns the new offset and an error, if any.
|
||||
// The behavior of Seek on a file opened with O_APPEND is not specified.
|
||||
func (f *File) Seek(offset int64, whence int) (ret int64, err error) {
|
||||
if err := f.checkValid("seek"); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
r, e := f.seek(offset, whence)
|
||||
if e == nil && f.dirinfo != nil && r != 0 {
|
||||
e = syscall.EISDIR
|
||||
}
|
||||
if e != nil {
|
||||
return 0, f.wrapErr("seek", e)
|
||||
}
|
||||
return r, nil
|
||||
/*
|
||||
if err := f.checkValid("seek"); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
r, e := f.seek(offset, whence)
|
||||
if e == nil && f.dirinfo != nil && r != 0 {
|
||||
e = syscall.EISDIR
|
||||
}
|
||||
if e != nil {
|
||||
return 0, f.wrapErr("seek", e)
|
||||
}
|
||||
return r, nil
|
||||
*/
|
||||
panic("todo: os.(*File).Seek")
|
||||
}
|
||||
|
||||
// WriteString is like Write, but writes the contents of string s rather than
|
||||
// a slice of bytes.
|
||||
func (f *File) WriteString(s string) (n int, err error) {
|
||||
b := unsafe.Slice(unsafe.StringData(s), len(s))
|
||||
return f.Write(b)
|
||||
/*
|
||||
b := unsafe.Slice(unsafe.StringData(s), len(s))
|
||||
return f.Write(b)
|
||||
*/
|
||||
panic("todo: os.(*File).WriteString")
|
||||
}
|
||||
|
||||
/*
|
||||
// setStickyBit adds ModeSticky to the permission bits of path, non atomic.
|
||||
func setStickyBit(name string) error {
|
||||
fi, err := Stat(name)
|
||||
@@ -293,6 +300,7 @@ func (f *File) wrapErr(op string, err error) error {
|
||||
func TempDir() string {
|
||||
return tempDir()
|
||||
}
|
||||
*/
|
||||
|
||||
// Chmod changes the mode of the file to mode.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
@@ -347,12 +355,16 @@ func (f *File) SetWriteDeadline(t time.Time) error {
|
||||
// SyscallConn returns a raw file.
|
||||
// This implements the syscall.Conn interface.
|
||||
func (f *File) SyscallConn() (syscall.RawConn, error) {
|
||||
if err := f.checkValid("SyscallConn"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newRawConn(f)
|
||||
/*
|
||||
if err := f.checkValid("SyscallConn"); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return newRawConn(f)
|
||||
*/
|
||||
panic("todo: os.(*File).SyscallConn")
|
||||
}
|
||||
|
||||
/* TODO(xsw):
|
||||
// DirFS returns a file system (an fs.FS) for the tree of files rooted at the directory dir.
|
||||
//
|
||||
// Note that DirFS("/prefix") only guarantees that the Open calls it makes to the
|
||||
|
||||
243
internal/lib/os/file_posix.go
Normal file
243
internal/lib/os/file_posix.go
Normal file
@@ -0,0 +1,243 @@
|
||||
// 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.
|
||||
|
||||
//go:build unix || (js && wasm) || wasip1 || windows
|
||||
|
||||
package os
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Close closes the File, rendering it unusable for I/O.
|
||||
// On files that support SetDeadline, any pending I/O operations will
|
||||
// be canceled and return immediately with an ErrClosed error.
|
||||
// Close will return an error if it has already been called.
|
||||
func (f *File) Close() error {
|
||||
/*
|
||||
if f == nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return f.file.close()
|
||||
*/
|
||||
panic("todo: os.(*File).Close")
|
||||
}
|
||||
|
||||
// pread reads len(b) bytes from the File starting at byte offset off.
|
||||
// It returns the number of bytes read and the error, if any.
|
||||
// EOF is signaled by a zero count with err set to nil.
|
||||
func (f *File) pread(b []byte, off int64) (n int, err error) {
|
||||
/*
|
||||
n, err = f.pfd.Pread(b, off)
|
||||
runtime.KeepAlive(f)
|
||||
return n, err
|
||||
*/
|
||||
panic("todo: os.(*File).pread")
|
||||
}
|
||||
|
||||
// pwrite writes len(b) bytes to the File starting at byte offset off.
|
||||
// It returns the number of bytes written and an error, if any.
|
||||
func (f *File) pwrite(b []byte, off int64) (n int, err error) {
|
||||
/*
|
||||
n, err = f.pfd.Pwrite(b, off)
|
||||
runtime.KeepAlive(f)
|
||||
return n, err
|
||||
*/
|
||||
panic("todo: os.(*File).pwrite")
|
||||
}
|
||||
|
||||
/*
|
||||
// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
|
||||
func syscallMode(i FileMode) (o uint32) {
|
||||
o |= uint32(i.Perm())
|
||||
if i&ModeSetuid != 0 {
|
||||
o |= syscall.S_ISUID
|
||||
}
|
||||
if i&ModeSetgid != 0 {
|
||||
o |= syscall.S_ISGID
|
||||
}
|
||||
if i&ModeSticky != 0 {
|
||||
o |= syscall.S_ISVTX
|
||||
}
|
||||
// No mapping for Go's ModeTemporary (plan9 only).
|
||||
return
|
||||
}
|
||||
|
||||
// See docs in file.go:Chmod.
|
||||
func chmod(name string, mode FileMode) error {
|
||||
longName := fixLongPath(name)
|
||||
e := ignoringEINTR(func() error {
|
||||
return syscall.Chmod(longName, syscallMode(mode))
|
||||
})
|
||||
if e != nil {
|
||||
return &PathError{Op: "chmod", Path: name, Err: e}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
// See docs in file.go:(*File).Chmod.
|
||||
func (f *File) chmod(mode FileMode) error {
|
||||
/*
|
||||
if err := f.checkValid("chmod"); err != nil {
|
||||
return err
|
||||
}
|
||||
if e := f.pfd.Fchmod(syscallMode(mode)); e != nil {
|
||||
return f.wrapErr("chmod", e)
|
||||
}
|
||||
return nil
|
||||
*/
|
||||
panic("todo: os.(*File).chmod")
|
||||
}
|
||||
|
||||
// Chown changes the numeric uid and gid of the named file.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
//
|
||||
// On Windows, it always returns the syscall.EWINDOWS error, wrapped
|
||||
// in *PathError.
|
||||
func (f *File) Chown(uid, gid int) error {
|
||||
/*
|
||||
if err := f.checkValid("chown"); err != nil {
|
||||
return err
|
||||
}
|
||||
if e := f.pfd.Fchown(uid, gid); e != nil {
|
||||
return f.wrapErr("chown", e)
|
||||
}
|
||||
return nil
|
||||
*/
|
||||
panic("todo: os.(*File).Chown")
|
||||
}
|
||||
|
||||
// Truncate changes the size of the file.
|
||||
// It does not change the I/O offset.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func (f *File) Truncate(size int64) error {
|
||||
/*
|
||||
if err := f.checkValid("truncate"); err != nil {
|
||||
return err
|
||||
}
|
||||
if e := f.pfd.Ftruncate(size); e != nil {
|
||||
return f.wrapErr("truncate", e)
|
||||
}
|
||||
return nil
|
||||
*/
|
||||
panic("todo: os.(*File).Truncate")
|
||||
}
|
||||
|
||||
// Sync commits the current contents of the file to stable storage.
|
||||
// Typically, this means flushing the file system's in-memory copy
|
||||
// of recently written data to disk.
|
||||
func (f *File) Sync() error {
|
||||
/*
|
||||
if err := f.checkValid("sync"); err != nil {
|
||||
return err
|
||||
}
|
||||
if e := f.pfd.Fsync(); e != nil {
|
||||
return f.wrapErr("sync", e)
|
||||
}
|
||||
return nil
|
||||
*/
|
||||
panic("todo: os.(*File).Sync")
|
||||
}
|
||||
|
||||
/*
|
||||
// Chtimes changes the access and modification times of the named
|
||||
// file, similar to the Unix utime() or utimes() functions.
|
||||
// A zero time.Time value will leave the corresponding file time unchanged.
|
||||
//
|
||||
// The underlying filesystem may truncate or round the values to a
|
||||
// less precise time unit.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func Chtimes(name string, atime time.Time, mtime time.Time) error {
|
||||
var utimes [2]syscall.Timespec
|
||||
set := func(i int, t time.Time) {
|
||||
if t.IsZero() {
|
||||
utimes[i] = syscall.Timespec{Sec: _UTIME_OMIT, Nsec: _UTIME_OMIT}
|
||||
} else {
|
||||
utimes[i] = syscall.NsecToTimespec(t.UnixNano())
|
||||
}
|
||||
}
|
||||
set(0, atime)
|
||||
set(1, mtime)
|
||||
if e := syscall.UtimesNano(fixLongPath(name), utimes[0:]); e != nil {
|
||||
return &PathError{Op: "chtimes", Path: name, Err: e}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
// Chdir changes the current working directory to the file,
|
||||
// which must be a directory.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
func (f *File) Chdir() error {
|
||||
/*
|
||||
if err := f.checkValid("chdir"); err != nil {
|
||||
return err
|
||||
}
|
||||
if e := f.pfd.Fchdir(); e != nil {
|
||||
return f.wrapErr("chdir", e)
|
||||
}
|
||||
return nil
|
||||
*/
|
||||
panic("todo: os.(*File).Chdir")
|
||||
}
|
||||
|
||||
// setDeadline sets the read and write deadline.
|
||||
func (f *File) setDeadline(t time.Time) error {
|
||||
/*
|
||||
if err := f.checkValid("SetDeadline"); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.pfd.SetDeadline(t)
|
||||
*/
|
||||
panic("todo: os.(*File).setDeadline")
|
||||
}
|
||||
|
||||
// setReadDeadline sets the read deadline.
|
||||
func (f *File) setReadDeadline(t time.Time) error {
|
||||
/*
|
||||
if err := f.checkValid("SetReadDeadline"); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.pfd.SetReadDeadline(t)
|
||||
*/
|
||||
panic("todo: os.(*File).setReadDeadline")
|
||||
}
|
||||
|
||||
// setWriteDeadline sets the write deadline.
|
||||
func (f *File) setWriteDeadline(t time.Time) error {
|
||||
/*
|
||||
if err := f.checkValid("SetWriteDeadline"); err != nil {
|
||||
return err
|
||||
}
|
||||
return f.pfd.SetWriteDeadline(t)
|
||||
*/
|
||||
panic("todo: os.(*File).setWriteDeadline")
|
||||
}
|
||||
|
||||
// checkValid checks whether f is valid for use.
|
||||
// If not, it returns an appropriate error, perhaps incorporating the operation name op.
|
||||
func (f *File) checkValid(op string) error {
|
||||
if f == nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ignoringEINTR makes a function call and repeats it if it returns an
|
||||
// EINTR error. This appears to be required even though we install all
|
||||
// signal handlers with SA_RESTART: see #22838, #38033, #38836, #40846.
|
||||
// Also #20400 and #36644 are issues in which a signal handler is
|
||||
// installed without setting SA_RESTART. None of these are the common case,
|
||||
// but there are enough of them that it seems that we can't avoid
|
||||
// an EINTR loop.
|
||||
func ignoringEINTR(fn func() error) error {
|
||||
for {
|
||||
err := fn()
|
||||
if err != syscall.EINTR {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -120,6 +120,25 @@ func Chown(name string, uid, gid int) error {
|
||||
return toPathErr("chown", name, ret)
|
||||
}
|
||||
|
||||
/* TODO(xsw):
|
||||
// Chown changes the numeric uid and gid of the named file.
|
||||
// If the file is a symbolic link, it changes the uid and gid of the link's target.
|
||||
// A uid or gid of -1 means to not change that value.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
//
|
||||
// On Windows or Plan 9, Chown always returns the syscall.EWINDOWS or
|
||||
// EPLAN9 error, wrapped in *PathError.
|
||||
func Chown(name string, uid, gid int) error {
|
||||
e := ignoringEINTR(func() error {
|
||||
return syscall.Chown(name, uid, gid)
|
||||
})
|
||||
if e != nil {
|
||||
return &PathError{Op: "chown", Path: name, Err: e}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
// TODO(xsw):
|
||||
// func Chtimes(name string, atime time.Time, mtime time.Time) error
|
||||
|
||||
@@ -195,6 +214,24 @@ func Lchown(name string, uid, gid int) error {
|
||||
return toPathErr("lchown", name, ret)
|
||||
}
|
||||
|
||||
/* TODO(xsw):
|
||||
// Lchown changes the numeric uid and gid of the named file.
|
||||
// If the file is a symbolic link, it changes the uid and gid of the link itself.
|
||||
// If there is an error, it will be of type *PathError.
|
||||
//
|
||||
// On Windows, it always returns the syscall.EWINDOWS error, wrapped
|
||||
// in *PathError.
|
||||
func Lchown(name string, uid, gid int) error {
|
||||
e := ignoringEINTR(func() error {
|
||||
return syscall.Lchown(name, uid, gid)
|
||||
})
|
||||
if e != nil {
|
||||
return &PathError{Op: "lchown", Path: name, Err: e}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
*/
|
||||
|
||||
func Link(oldname, newname string) error {
|
||||
ret := os.Link(c.AllocaCStr(oldname), c.AllocaCStr(newname))
|
||||
if ret == 0 {
|
||||
|
||||
@@ -47,6 +47,16 @@ func (f *File) write(b []byte) (int, error) {
|
||||
return 0, syscall.Errno(os.Errno)
|
||||
}
|
||||
|
||||
/* TODO(xsw):
|
||||
// write writes len(b) bytes to the File.
|
||||
// It returns the number of bytes written and an error, if any.
|
||||
func (f *File) write(b []byte) (n int, err error) {
|
||||
n, err = f.pfd.Write(b)
|
||||
runtime.KeepAlive(f)
|
||||
return n, err
|
||||
}
|
||||
*/
|
||||
|
||||
// read reads up to len(b) bytes from the File.
|
||||
// It returns the number of bytes read and an error, if any.
|
||||
func (f *File) read(b []byte) (int, error) {
|
||||
@@ -60,14 +70,15 @@ func (f *File) read(b []byte) (int, error) {
|
||||
return 0, syscall.Errno(os.Errno)
|
||||
}
|
||||
|
||||
// checkValid checks whether f is valid for use.
|
||||
// If not, it returns an appropriate error, perhaps incorporating the operation name op.
|
||||
func (f *File) checkValid(op string) error {
|
||||
if f == nil {
|
||||
return ErrInvalid
|
||||
}
|
||||
return nil
|
||||
/* TODO(xsw):
|
||||
// read reads up to len(b) bytes from the File.
|
||||
// It returns the number of bytes read and an error, if any.
|
||||
func (f *File) read(b []byte) (n int, err error) {
|
||||
n, err = f.pfd.Read(b)
|
||||
runtime.KeepAlive(f)
|
||||
return n, err
|
||||
}
|
||||
*/
|
||||
|
||||
// A FileInfo describes a file and is returned by Stat and Lstat.
|
||||
type FileInfo = fs.FileInfo
|
||||
|
||||
Reference in New Issue
Block a user