src/os: implement os.File.Fd() method

This commits adds a OS-specific `Fd()` function for `file_anyos.go` and
`file_other.go`, which returns a uintptr to the underlying file
descriptor.
Этот коммит содержится в:
ZauberNerd 2022-03-04 15:44:55 +00:00 коммит произвёл Ron Evans
родитель 4a98db4c86
коммит 486b99961d
3 изменённых файлов: 19 добавлений и 1 удалений

Просмотреть файл

@ -179,9 +179,19 @@ func (f *File) SyscallConn() (syscall.RawConn, error) {
return nil, ErrNotImplemented return nil, ErrNotImplemented
} }
// fd is an internal interface that is used to try a type assertion in order to
// call the Fd() method of the underlying file handle if it is implemented.
type fd interface {
Fd() uintptr
}
// Fd returns the file handle referencing the open file. // Fd returns the file handle referencing the open file.
func (f *File) Fd() uintptr { func (f *File) Fd() uintptr {
panic("unimplemented: os.file.Fd()") handle, ok := f.handle.(fd)
if ok {
return handle.Fd()
}
return 0
} }
// Truncate is a stub, not yet implemented // Truncate is a stub, not yet implemented

Просмотреть файл

@ -118,6 +118,10 @@ func (f unixFileHandle) Close() error {
return handleSyscallError(syscall.Close(syscallFd(f))) return handleSyscallError(syscall.Close(syscallFd(f)))
} }
func (f unixFileHandle) Fd() uintptr {
return uintptr(f)
}
// Chmod changes the mode of the named file to mode. // Chmod changes the mode of the named file to mode.
// If the file is a symbolic link, it changes the mode of the link's target. // If the file is a symbolic link, it changes the mode of the link's target.
// If there is an error, it will be of type *PathError. // If there is an error, it will be of type *PathError.

Просмотреть файл

@ -71,6 +71,10 @@ func (f stdioFileHandle) Seek(offset int64, whence int) (int64, error) {
return -1, ErrUnsupported return -1, ErrUnsupported
} }
func (f stdioFileHandle) Fd() uintptr {
return uintptr(f)
}
//go:linkname putchar runtime.putchar //go:linkname putchar runtime.putchar
func putchar(c byte) func putchar(c byte)