github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/mounter/mounter_osx.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  //go:build darwin
     5  // +build darwin
     6  
     7  package mounter
     8  
     9  import (
    10  	"regexp"
    11  	"strings"
    12  	"syscall"
    13  )
    14  
    15  // IsMounted returns true if directory is mounted (by kbfuse)
    16  func IsMounted(dir string, log Log) (bool, error) {
    17  	mountInfo, err := getMountInfo(dir)
    18  	if err != nil {
    19  		return false, err
    20  	}
    21  
    22  	log.Debug("Mount info: %s", mountInfo)
    23  	if strings.Contains(mountInfo, "@kbfuse") {
    24  		return true, nil
    25  	}
    26  
    27  	return false, nil
    28  }
    29  
    30  //
    31  // Below is from bazil/fuse fstestutil
    32  //
    33  
    34  var reBackslash = regexp.MustCompile(`\\(.)`)
    35  
    36  // unescapeBackslash removes backslash-escaping. The escaped characters are not
    37  // mapped in any way; that is, unescape(`\n` ) == `n`.
    38  func unescapeBackslash(s string) string {
    39  	return reBackslash.ReplaceAllString(s, `$1`)
    40  }
    41  
    42  // cstr converts a nil-terminated C string into a Go string
    43  func cstr(ca []int8) string {
    44  	s := make([]byte, 0, len(ca))
    45  	for _, c := range ca {
    46  		if c == 0x00 {
    47  			break
    48  		}
    49  		s = append(s, byte(c))
    50  	}
    51  	return string(s)
    52  }
    53  
    54  func getMountInfo(mnt string) (string, error) {
    55  	var st syscall.Statfs_t
    56  	err := syscall.Statfs(mnt, &st)
    57  	if err != nil {
    58  		return "", err
    59  	}
    60  	return unescapeBackslash(cstr(st.Mntfromname[:])), nil
    61  }