pkg.re/essentialkaos/ek.10@v12.41.0+incompatible/directio/directio_darwin.go (about)

     1  package directio
     2  
     3  // ////////////////////////////////////////////////////////////////////////////////// //
     4  //                                                                                    //
     5  //                         Copyright (c) 2022 ESSENTIAL KAOS                          //
     6  //      Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0>     //
     7  //                                                                                    //
     8  // ////////////////////////////////////////////////////////////////////////////////// //
     9  
    10  import (
    11  	"errors"
    12  	"os"
    13  	"syscall"
    14  )
    15  
    16  // ////////////////////////////////////////////////////////////////////////////////// //
    17  
    18  const (
    19  	BLOCK_SIZE = 4096 // Minimal block size
    20  	ALIGN_SIZE = 0    // Align size
    21  )
    22  
    23  // ////////////////////////////////////////////////////////////////////////////////// //
    24  
    25  func openFile(file string, flag int, perm os.FileMode) (*os.File, error) {
    26  	fd, err := os.OpenFile(file, flag, perm)
    27  
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	_, _, e := syscall.Syscall(syscall.SYS_FCNTL, uintptr(fd.Fd()), syscall.F_NOCACHE, 1)
    33  
    34  	if e != 0 {
    35  		fd.Close()
    36  		return nil, errors.New("Can't set F_NOCACHE for given file")
    37  	}
    38  
    39  	return fd, nil
    40  }