From 486b99961d1ef918341ce2f7d29b20388da253dc Mon Sep 17 00:00:00 2001 From: ZauberNerd Date: Fri, 4 Mar 2022 15:44:55 +0000 Subject: [PATCH] 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. --- src/os/file.go | 12 +++++++++++- src/os/file_anyos.go | 4 ++++ src/os/file_other.go | 4 ++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/os/file.go b/src/os/file.go index d10e4afb..2f72c892 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -179,9 +179,19 @@ func (f *File) SyscallConn() (syscall.RawConn, error) { 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. 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 diff --git a/src/os/file_anyos.go b/src/os/file_anyos.go index 225b72f5..117f173f 100644 --- a/src/os/file_anyos.go +++ b/src/os/file_anyos.go @@ -118,6 +118,10 @@ func (f unixFileHandle) Close() error { return handleSyscallError(syscall.Close(syscallFd(f))) } +func (f unixFileHandle) Fd() uintptr { + return uintptr(f) +} + // 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 there is an error, it will be of type *PathError. diff --git a/src/os/file_other.go b/src/os/file_other.go index 9206d343..036ddf69 100644 --- a/src/os/file_other.go +++ b/src/os/file_other.go @@ -71,6 +71,10 @@ func (f stdioFileHandle) Seek(offset int64, whence int) (int64, error) { return -1, ErrUnsupported } +func (f stdioFileHandle) Fd() uintptr { + return uintptr(f) +} + //go:linkname putchar runtime.putchar func putchar(c byte)