github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/system/disk/disk_darwin.go (about)

     1  //go:build darwin
     2  
     3  package disk
     4  
     5  import (
     6  	"context"
     7  	"github.com/isyscore/isc-gobase/system/common"
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  // PartitionsWithContext returns disk partition.
    12  // 'all' argument is ignored, see: https://github.com/giampaolo/psutil/issues/906
    13  func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
    14  	var ret []PartitionStat
    15  
    16  	count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
    17  	if err != nil {
    18  		return ret, err
    19  	}
    20  	fs := make([]unix.Statfs_t, count)
    21  	if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
    22  		return ret, err
    23  	}
    24  	for _, stat := range fs {
    25  		opts := "rw"
    26  		if stat.Flags&unix.MNT_RDONLY != 0 {
    27  			opts = "ro"
    28  		}
    29  		if stat.Flags&unix.MNT_SYNCHRONOUS != 0 {
    30  			opts += ",sync"
    31  		}
    32  		if stat.Flags&unix.MNT_NOEXEC != 0 {
    33  			opts += ",noexec"
    34  		}
    35  		if stat.Flags&unix.MNT_NOSUID != 0 {
    36  			opts += ",nosuid"
    37  		}
    38  		if stat.Flags&unix.MNT_UNION != 0 {
    39  			opts += ",union"
    40  		}
    41  		if stat.Flags&unix.MNT_ASYNC != 0 {
    42  			opts += ",async"
    43  		}
    44  		if stat.Flags&unix.MNT_DONTBROWSE != 0 {
    45  			opts += ",nobrowse"
    46  		}
    47  		if stat.Flags&unix.MNT_AUTOMOUNTED != 0 {
    48  			opts += ",automounted"
    49  		}
    50  		if stat.Flags&unix.MNT_JOURNALED != 0 {
    51  			opts += ",journaled"
    52  		}
    53  		if stat.Flags&unix.MNT_MULTILABEL != 0 {
    54  			opts += ",multilabel"
    55  		}
    56  		if stat.Flags&unix.MNT_NOATIME != 0 {
    57  			opts += ",noatime"
    58  		}
    59  		if stat.Flags&unix.MNT_NODEV != 0 {
    60  			opts += ",nodev"
    61  		}
    62  		d := PartitionStat{
    63  			Device:     common.ByteToString(stat.Mntfromname[:]),
    64  			Mountpoint: common.ByteToString(stat.Mntonname[:]),
    65  			Fstype:     common.ByteToString(stat.Fstypename[:]),
    66  			Opts:       opts,
    67  		}
    68  
    69  		ret = append(ret, d)
    70  	}
    71  
    72  	return ret, nil
    73  }
    74  
    75  func getFsType(stat unix.Statfs_t) string {
    76  	return common.ByteToString(stat.Fstypename[:])
    77  }