// Copyright 2023 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.

//go:build unix || (js && wasm)

package poll

import 

type SysFile struct {
	// Writev cache.
	iovecs *[]syscall.Iovec
}

func ( *SysFile) () {}

func ( *SysFile) ( int) error {
	// We don't use ignoringEINTR here because POSIX does not define
	// whether the descriptor is closed if close returns EINTR.
	// If the descriptor is indeed closed, using a loop would race
	// with some other goroutine opening a new descriptor.
	// (The Linux kernel guarantees that it is closed on an EINTR error.)
	return CloseFunc()
}

// dupCloseOnExecOld is the traditional way to dup an fd and
// set its O_CLOEXEC bit, using two system calls.
func ( int) (int, string, error) {
	syscall.ForkLock.RLock()
	defer syscall.ForkLock.RUnlock()
	,  := syscall.Dup()
	if  != nil {
		return -1, "dup", 
	}
	syscall.CloseOnExec()
	return , "", nil
}

// Fchdir wraps syscall.Fchdir.
func ( *FD) () error {
	if  := .incref();  != nil {
		return 
	}
	defer .decref()
	return syscall.Fchdir(.Sysfd)
}

// ReadDirent wraps syscall.ReadDirent.
// We treat this like an ordinary system call rather than a call
// that tries to fill the buffer.
func ( *FD) ( []byte) (int, error) {
	if  := .incref();  != nil {
		return 0, 
	}
	defer .decref()
	for {
		,  := ignoringEINTRIO(syscall.ReadDirent, .Sysfd, )
		if  != nil {
			 = 0
			if  == syscall.EAGAIN && .pd.pollable() {
				if  = .pd.waitRead(.isFile);  == nil {
					continue
				}
			}
		}
		// Do not call eofError; caller does not expect to see io.EOF.
		return , 
	}
}

// Seek wraps syscall.Seek.
func ( *FD) ( int64,  int) (int64, error) {
	if  := .incref();  != nil {
		return 0, 
	}
	defer .decref()
	return syscall.Seek(.Sysfd, , )
}