github.com/fawick/restic@v0.1.1-0.20171126184616-c02923fbfc79/internal/fs/deviceid_unix.go (about)

     1  // +build !windows
     2  
     3  package fs
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  
     9  	"github.com/restic/restic/internal/errors"
    10  )
    11  
    12  // DeviceID extracts the device ID from an os.FileInfo object by casting it
    13  // to syscall.Stat_t
    14  func DeviceID(fi os.FileInfo) (deviceID uint64, err error) {
    15  	if fi == nil {
    16  		return 0, errors.New("unable to determine device: fi is nil")
    17  	}
    18  
    19  	if fi.Sys() == nil {
    20  		return 0, errors.New("unable to determine device: fi.Sys() is nil")
    21  	}
    22  
    23  	if st, ok := fi.Sys().(*syscall.Stat_t); ok {
    24  		// st.Dev is uint32 on Darwin and uint64 on Linux. Just cast
    25  		// everything to uint64.
    26  		return uint64(st.Dev), nil
    27  	}
    28  
    29  	return 0, errors.New("Could not cast to syscall.Stat_t")
    30  }