Skip to content

Commit

Permalink
src/os: implement os.File.Fd() method
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ZauberNerd authored and deadprogram committed Mar 16, 2022
1 parent 4a98db4 commit 486b999
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/os/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions src/os/file_anyos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 4 additions & 0 deletions src/os/file_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 486b999

Please sign in to comment.