Skip to content

Instantly share code, notes, and snippets.

@harshavardhana
Created August 12, 2017 05:22
Show Gist options
  • Save harshavardhana/fb05aee53e0d7e7c5960bbae969ab036 to your computer and use it in GitHub Desktop.
Save harshavardhana/fb05aee53e0d7e7c5960bbae969ab036 to your computer and use it in GitHub Desktop.
// Returns the volume serial number of the file or directory at fsPath.
func statVolumeSerialNumber(fsPath string) (uint32, error) {
pathp, err := syscall.UTF16PtrFromString(fsPath)
if err != nil {
return 0, err
}
h, err := syscall.CreateFile(pathp, 0, 0, nil, syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
if err != nil {
return 0, err
}
defer syscall.CloseHandle(h)
var i syscall.ByHandleFileInformation
if err = syscall.GetFileInformationByHandle(h, &i); err != nil {
return 0, err
}
return i.VolumeSerialNumber, nil
}
// sameMount reports whether childInfo and parentInfo describe
// the same file or directory in the same mount. For example,
// on Unix this means that the device fields of the two underlying
// structures are identical; on other systems the decision may
// be based on the path names. sameMount only applies to results
// returned by os.Stat. It returns false in other cases.
func sameMount(childInfo os.FileInfo, childPath string, parentInfo os.FileInfo, parentPath string) bool {
if childInfo == nil || parentInfo == nil {
return false
}
if childPath == "" || parentPath == "" {
return false
}
vol1, err := statVolumeSerialNumber(childPath)
if err != nil {
return false
}
vol2, err := statVolumeSerialNumber(parentPath)
if err != nil {
return false
}
return vol1 == vol2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment