diff --git a/src/os/file.go b/src/os/file.go index 3589c377..6735aae2 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -180,19 +180,6 @@ func (f *File) Readdirnames(n int) (names []string, err error) { return nil, &PathError{"readdirnames", f.name, ErrNotImplemented} } -// 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. -// -// If f is a directory, the behavior of Seek varies by operating -// system; you can seek to the beginning of the directory on Unix-like -// operating systems, but not on Windows. -func (f *File) Seek(offset int64, whence int) (ret int64, err error) { - return f.handle.Seek(offset, whence) -} - func (f *File) SyscallConn() (syscall.RawConn, error) { return nil, ErrNotImplemented } diff --git a/src/os/file_windows.go b/src/os/file_windows.go index 3e1c63b9..be0c3ece 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -82,6 +82,20 @@ func (f unixFileHandle) Seek(offset int64, whence int) (int64, error) { return newoffset, handleSyscallError(err) } +// 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. +// +// If f is a directory, the behavior of Seek varies by operating +// system; you can seek to the beginning of the directory on Unix-like +// operating systems, but not on Windows. +// TODO: move this back to file.go once syscall.seek is implemented on 386 and arm. +func (f *File) Seek(offset int64, whence int) (ret int64, err error) { + return f.handle.Seek(offset, whence) +} + // isWindowsNulName reports whether name is os.DevNull ('NUL') on Windows. // True is returned if name is 'NUL' whatever the case. func isWindowsNulName(name string) bool { diff --git a/src/os/seek_unix_bad.go b/src/os/seek_unix_bad.go new file mode 100644 index 00000000..e143ac9c --- /dev/null +++ b/src/os/seek_unix_bad.go @@ -0,0 +1,31 @@ +// +build linux,!baremetal,386 linux,!baremetal,arm,!wasi + +// Functions broken by lack of seek(). +// Stat is broken because it uses Time, which has a preadn function that uses seek :-( +// +// TODO: remove this file once tinygo gets syscall.Seek support on i386 + +// Copyright 2016 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 + +// 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. +// +// If f is a directory, the behavior of Seek varies by operating +// system; you can seek to the beginning of the directory on Unix-like +// operating systems, but not on Windows. +func (f *File) Seek(offset int64, whence int) (ret int64, err error) { + return f.handle.Seek(offset, whence) +} + +// Stat returns the FileInfo structure describing file. +// If there is an error, it will be of type *PathError. +func (f *File) Stat() (FileInfo, error) { + return nil, &PathError{Op: "fstat", Path: f.name, Err: ErrNotImplemented} +} diff --git a/src/os/seek_unix_good.go b/src/os/seek_unix_good.go new file mode 100644 index 00000000..3a037402 --- /dev/null +++ b/src/os/seek_unix_good.go @@ -0,0 +1,39 @@ +// +build darwin linux,!baremetal,!386,!arm wasi + +// Functions broken by lack of seek(). +// Stat is broken because it uses Time, which has a preadn function that uses seek :-( +// +// TODO: merge these functions back where they belong once tinygo gets syscall.Seek support on i386 + +package os + +import ( + "syscall" +) + +// 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. +// +// If f is a directory, the behavior of Seek varies by operating +// system; you can seek to the beginning of the directory on Unix-like +// operating systems, but not on Windows. +func (f *File) Seek(offset int64, whence int) (ret int64, err error) { + return f.handle.Seek(offset, whence) +} + +// Stat returns the FileInfo structure describing file. +// If there is an error, it will be of type *PathError. +func (f *File) Stat() (FileInfo, error) { + var fs fileStat + err := ignoringEINTR(func() error { + return syscall.Fstat(int(f.handle.(unixFileHandle)), &fs.sys) + }) + if err != nil { + return nil, &PathError{Op: "fstat", Path: f.name, Err: err} + } + fillFileStatFromSys(&fs, f.name) + return &fs, nil +} diff --git a/src/os/stat_unix.go b/src/os/stat_unix.go index 02e3e1aa..9187b54e 100644 --- a/src/os/stat_unix.go +++ b/src/os/stat_unix.go @@ -15,20 +15,6 @@ func (f *File) Sync() error { return ErrNotImplemented } -// Stat returns the FileInfo structure describing file. -// If there is an error, it will be of type *PathError. -func (f *File) Stat() (FileInfo, error) { - var fs fileStat - err := ignoringEINTR(func() error { - return syscall.Fstat(int(f.handle.(unixFileHandle)), &fs.sys) - }) - if err != nil { - return nil, &PathError{Op: "fstat", Path: f.name, Err: err} - } - fillFileStatFromSys(&fs, f.name) - return &fs, nil -} - // statNolog stats a file with no test logging. func statNolog(name string) (FileInfo, error) { var fs fileStat