// Copyright 2009 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) || wasip1 || windows

package os

import (
	
	
	
)

// Close closes the [File], rendering it unusable for I/O.
// On files that support [File.SetDeadline], any pending I/O operations will
// be canceled and return immediately with an [ErrClosed] error.
// Close will return an error if it has already been called.
func ( *File) () error {
	if  == nil {
		return ErrInvalid
	}
	return .file.close()
}

// read reads up to len(b) bytes from the File.
// It returns the number of bytes read and an error, if any.
func ( *File) ( []byte) ( int,  error) {
	,  = .pfd.Read()
	runtime.KeepAlive()
	return , 
}

// pread reads len(b) bytes from the File starting at byte offset off.
// It returns the number of bytes read and the error, if any.
// EOF is signaled by a zero count with err set to nil.
func ( *File) ( []byte,  int64) ( int,  error) {
	,  = .pfd.Pread(, )
	runtime.KeepAlive()
	return , 
}

// write writes len(b) bytes to the File.
// It returns the number of bytes written and an error, if any.
func ( *File) ( []byte) ( int,  error) {
	,  = .pfd.Write()
	runtime.KeepAlive()
	return , 
}

// pwrite writes len(b) bytes to the File starting at byte offset off.
// It returns the number of bytes written and an error, if any.
func ( *File) ( []byte,  int64) ( int,  error) {
	,  = .pfd.Pwrite(, )
	runtime.KeepAlive()
	return , 
}

// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
func syscallMode( FileMode) ( uint32) {
	 |= uint32(.Perm())
	if &ModeSetuid != 0 {
		 |= syscall.S_ISUID
	}
	if &ModeSetgid != 0 {
		 |= syscall.S_ISGID
	}
	if &ModeSticky != 0 {
		 |= syscall.S_ISVTX
	}
	// No mapping for Go's ModeTemporary (plan9 only).
	return
}

// See docs in file.go:Chmod.
func chmod( string,  FileMode) error {
	 := fixLongPath()
	 := ignoringEINTR(func() error {
		return syscall.Chmod(, syscallMode())
	})
	if  != nil {
		return &PathError{Op: "chmod", Path: , Err: }
	}
	return nil
}

// See docs in file.go:(*File).Chmod.
func ( *File) ( FileMode) error {
	if  := .checkValid("chmod");  != nil {
		return 
	}
	if  := .pfd.Fchmod(syscallMode());  != nil {
		return .wrapErr("chmod", )
	}
	return nil
}

// Chown changes the numeric uid and gid of the named file.
// If the file is a symbolic link, it changes the uid and gid of the link's target.
// A uid or gid of -1 means to not change that value.
// If there is an error, it will be of type [*PathError].
//
// On Windows or Plan 9, Chown always returns the [syscall.EWINDOWS] or
// EPLAN9 error, wrapped in *PathError.
func ( string, ,  int) error {
	 := ignoringEINTR(func() error {
		return syscall.Chown(, , )
	})
	if  != nil {
		return &PathError{Op: "chown", Path: , Err: }
	}
	return nil
}

// Lchown changes the numeric uid and gid of the named file.
// If the file is a symbolic link, it changes the uid and gid of the link itself.
// If there is an error, it will be of type [*PathError].
//
// On Windows, it always returns the [syscall.EWINDOWS] error, wrapped
// in *PathError.
func ( string, ,  int) error {
	 := ignoringEINTR(func() error {
		return syscall.Lchown(, , )
	})
	if  != nil {
		return &PathError{Op: "lchown", Path: , Err: }
	}
	return nil
}

// Chown changes the numeric uid and gid of the named file.
// If there is an error, it will be of type [*PathError].
//
// On Windows, it always returns the [syscall.EWINDOWS] error, wrapped
// in *PathError.
func ( *File) (,  int) error {
	if  := .checkValid("chown");  != nil {
		return 
	}
	if  := .pfd.Fchown(, );  != nil {
		return .wrapErr("chown", )
	}
	return nil
}

// Truncate changes the size of the file.
// It does not change the I/O offset.
// If there is an error, it will be of type [*PathError].
func ( *File) ( int64) error {
	if  := .checkValid("truncate");  != nil {
		return 
	}
	if  := .pfd.Ftruncate();  != nil {
		return .wrapErr("truncate", )
	}
	return nil
}

// Sync commits the current contents of the file to stable storage.
// Typically, this means flushing the file system's in-memory copy
// of recently written data to disk.
func ( *File) () error {
	if  := .checkValid("sync");  != nil {
		return 
	}
	if  := .pfd.Fsync();  != nil {
		return .wrapErr("sync", )
	}
	return nil
}

// Chtimes changes the access and modification times of the named
// file, similar to the Unix utime() or utimes() functions.
// A zero [time.Time] value will leave the corresponding file time unchanged.
//
// The underlying filesystem may truncate or round the values to a
// less precise time unit.
// If there is an error, it will be of type [*PathError].
func ( string,  time.Time,  time.Time) error {
	var  [2]syscall.Timespec
	 := func( int,  time.Time) {
		if .IsZero() {
			[] = syscall.Timespec{Sec: _UTIME_OMIT, Nsec: _UTIME_OMIT}
		} else {
			[] = syscall.NsecToTimespec(.UnixNano())
		}
	}
	(0, )
	(1, )
	if  := syscall.UtimesNano(fixLongPath(), [0:]);  != nil {
		return &PathError{Op: "chtimes", Path: , Err: }
	}
	return nil
}

// Chdir changes the current working directory to the file,
// which must be a directory.
// If there is an error, it will be of type [*PathError].
func ( *File) () error {
	if  := .checkValid("chdir");  != nil {
		return 
	}
	if  := .pfd.Fchdir();  != nil {
		return .wrapErr("chdir", )
	}
	return nil
}

// setDeadline sets the read and write deadline.
func ( *File) ( time.Time) error {
	if  := .checkValid("SetDeadline");  != nil {
		return 
	}
	return .pfd.SetDeadline()
}

// setReadDeadline sets the read deadline.
func ( *File) ( time.Time) error {
	if  := .checkValid("SetReadDeadline");  != nil {
		return 
	}
	return .pfd.SetReadDeadline()
}

// setWriteDeadline sets the write deadline.
func ( *File) ( time.Time) error {
	if  := .checkValid("SetWriteDeadline");  != nil {
		return 
	}
	return .pfd.SetWriteDeadline()
}

// checkValid checks whether f is valid for use.
// If not, it returns an appropriate error, perhaps incorporating the operation name op.
func ( *File) ( string) error {
	if  == nil {
		return ErrInvalid
	}
	return nil
}

// ignoringEINTR makes a function call and repeats it if it returns an
// EINTR error. This appears to be required even though we install all
// signal handlers with SA_RESTART: see #22838, #38033, #38836, #40846.
// Also #20400 and #36644 are issues in which a signal handler is
// installed without setting SA_RESTART. None of these are the common case,
// but there are enough of them that it seems that we can't avoid
// an EINTR loop.
func ignoringEINTR( func() error) error {
	for {
		 := ()
		if  != syscall.EINTR {
			return 
		}
	}
}