os: implement os.(*File).WriteAt (#3697)

os: implement os.(*File).WriteAt

Signed-off-by: Achille Roussel <achille.roussel@gmail.com>
This commit is contained in:
Achille
2023-05-05 00:40:15 -07:00
committed by GitHub
parent 1d5c5ca2ef
commit 602f35a5ca
8 changed files with 152 additions and 17 deletions
+17 -4
View File
@@ -37,9 +37,10 @@ func rename(oldname, newname string) error {
// can overwrite this data, which could cause the finalizer
// to close the wrong file descriptor.
type file struct {
handle FileHandle
name string
dirinfo *dirInfo // nil unless directory being read
handle FileHandle
name string
dirinfo *dirInfo // nil unless directory being read
appendMode bool
}
func (f *file) close() (err error) {
@@ -51,7 +52,7 @@ func (f *file) close() (err error) {
}
func NewFile(fd uintptr, name string) *File {
return &File{&file{unixFileHandle(fd), name, nil}}
return &File{&file{handle: unixFileHandle(fd), name: name}}
}
func Pipe() (r *File, w *File, err error) {
@@ -124,6 +125,18 @@ func (f unixFileHandle) ReadAt(b []byte, offset int64) (n int, err error) {
return
}
// WriteAt writes len(b) bytes to the File starting at byte offset off.
// It returns the number of bytes written and an error, if any.
// WriteAt returns a non-nil error when n != len(b).
//
// If file was opened with the O_APPEND flag, WriteAt returns an error.
//
// TODO: move to file_anyos once WriteAt is implemented for windows.
func (f unixFileHandle) WriteAt(b []byte, offset int64) (int, error) {
n, err := syscall.Pwrite(syscallFd(f), b, offset)
return n, handleSyscallError(err)
}
// Seek wraps syscall.Seek.
func (f unixFileHandle) Seek(offset int64, whence int) (int64, error) {
newoffset, err := syscall.Seek(syscallFd(f), offset, whence)