package realm

import (
	"os"
	"syscall"

	"github.com/tidbcloud/ffs/pkg/v1/proto"
)

func convertToStatfsResp(statfs *syscall.Statfs_t) *proto.StatfsResp {
	s := &proto.StatfsResp{}
	s.Blocks = statfs.Blocks
	s.Bfree = statfs.Bfree
	s.Bavail = statfs.Bavail
	s.Files = statfs.Files
	s.Ffree = statfs.Ffree
	s.Bsize = uint32(statfs.Iosize) // Iosize translates to Bsize: the optimal transfer size.
	s.Frsize = s.Bsize              // Bsize translates to Frsize: the minimum transfer size.

	// The block counts are in units of statfs.Bsize.
	// If s.Bsize != statfs.Bsize, we have to recalculate the block counts
	// accordingly (s.Bsize is usually 256*statfs.Bsize).
	if s.Bsize > statfs.Bsize {
		adj := uint64(s.Bsize / statfs.Bsize)
		s.Blocks /= adj
		s.Bfree /= adj
		s.Bavail /= adj
	}
	return s
}

func fdatasync(f *os.File) error {
	return f.Sync()
}

func fallocate(fd int, mode uint32, off int64, len int64) (err error) {
	return syscall.ENOSYS
}
