tinygo/src/os/file_unix.go
2022-01-11 14:23:14 +01:00

72 строки
1,7 КиБ
Go

// +build darwin linux,!baremetal
// Portions 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 os
import (
"io"
"syscall"
)
type syscallFd = int
// fixLongPath is a noop on non-Windows platforms.
func fixLongPath(path string) string {
return path
}
func rename(oldname, newname string) error {
// TODO: import rest of upstream tests, handle fancy cases
err := syscall.Rename(oldname, newname)
if err != nil {
return &LinkError{"rename", oldname, newname, err}
}
return nil
}
func Pipe() (r *File, w *File, err error) {
var p [2]int
err = handleSyscallError(syscall.Pipe2(p[:], syscall.O_CLOEXEC))
if err != nil {
return
}
r = &File{
handle: unixFileHandle(p[0]),
name: "|0",
}
w = &File{
handle: unixFileHandle(p[1]),
name: "|1",
}
return
}
func tempDir() string {
dir := Getenv("TMPDIR")
if dir == "" {
dir = "/tmp"
}
return dir
}
// ReadAt reads up to len(b) bytes from the File starting at the given absolute offset.
// It returns the number of bytes read and any error encountered, possibly io.EOF.
// At end of file, Pread returns 0, io.EOF.
// TODO: move to file_anyos once ReadAt is implemented for windows
func (f unixFileHandle) ReadAt(b []byte, offset int64) (n int, err error) {
n, err = syscall.Pread(syscallFd(f), b, offset)
err = handleSyscallError(err)
if n == 0 && len(b) > 0 && err == nil {
err = io.EOF
}
return
}
// Seek wraps syscall.Seek.
func (f unixFileHandle) Seek(offset int64, whence int) (int64, error) {
newoffset, err := syscall.Seek(syscallFd(f), offset, whence)
return newoffset, handleSyscallError(err)
}