github.com/NVIDIA/aistore@v1.3.23-0.20240517131212-7df6609be51d/fs/fs_darwin.go (about)

     1  // Package fs provides mountpath and FQN abstractions and methods to resolve/map stored content
     2  /*
     3   * Copyright (c) 2021-2023, NVIDIA CORPORATION. All rights reserved.
     4   */
     5  package fs
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"syscall"
    11  )
    12  
    13  func (mi *Mountpath) resolveFS() error {
    14  	var fsStats syscall.Statfs_t
    15  	if err := syscall.Statfs(mi.Path, &fsStats); err != nil {
    16  		return fmt.Errorf("cannot statfs fspath %q, err: %w", mi.Path, err)
    17  	}
    18  
    19  	charsToString := func(x []int8) string {
    20  		s := ""
    21  		for _, c := range x {
    22  			if c == 0 {
    23  				break
    24  			}
    25  			s += string(uint8(c))
    26  		}
    27  		return s
    28  	}
    29  
    30  	mi.Fs = charsToString(fsStats.Fstypename[:])
    31  	mi.FsType = charsToString(fsStats.Mntfromname[:])
    32  	mi.FsID = fsStats.Fsid.Val
    33  	return nil
    34  }
    35  
    36  // DirectOpen opens a file with direct disk access (with OS caching disabled).
    37  func DirectOpen(path string, flag int, perm os.FileMode) (*os.File, error) {
    38  	file, err := os.OpenFile(path, flag, perm)
    39  	if err != nil {
    40  		return file, err
    41  	}
    42  
    43  	// Non-zero F_NOCACHE = caching off
    44  	_, _, errno := syscall.Syscall(syscall.SYS_FCNTL, file.Fd(), syscall.F_NOCACHE, 1)
    45  	if errno != 0 {
    46  		file.Close()
    47  		return nil, fmt.Errorf("failed to set F_NOCACHE: %s", errno)
    48  	}
    49  
    50  	return file, nil
    51  }