github.com/atlassian/git-lob@v0.0.0-20150806085256-2386a5ed291a/core/links_nonwindows.go (about)

     1  // +build !windows
     2  
     3  package core
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  )
     9  
    10  // Create a hard link to a file
    11  // This link can be deleted like any other file afterwards
    12  func CreateHardLink(target, link string) error {
    13  
    14  	// Go supports hard links natively for mac & linux
    15  	return os.Link(target, link)
    16  }
    17  
    18  // Get the number of hard links to a given file (min 1)
    19  func GetHardLinkCount(target string) (linkCount int, err error) {
    20  	// number of links is available in stat_t but not translated into Go's FileInfo
    21  	// Go returns the original stat_t result in FileInfo.sys though
    22  	// See source https://golang.org/src/pkg/os/stat_darwin.go (and _linux.go)
    23  	fi, err := os.Stat(target)
    24  	if err != nil {
    25  		return 0, err
    26  	}
    27  	var s *syscall.Stat_t
    28  	s = fi.Sys().(*syscall.Stat_t)
    29  	return int(s.Nlink), nil
    30  }