| 1 | //go:build windows |
| 2 | |
| 3 | package fileutil |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | |
| 9 | "golang.org/x/sys/windows" |
| 10 | ) |
| 11 | |
| 12 | func DirectoryIdentity(path string) (string, error) { |
| 13 | info, err := os.Lstat(path) |
| 14 | if err != nil { |
| 15 | return "", err |
| 16 | } |
| 17 | if !info.IsDir() || info.Mode()&os.ModeSymlink != 0 { |
| 18 | return "", fmt.Errorf("not a directory") |
| 19 | } |
| 20 | name, err := windows.UTF16PtrFromString(path) |
| 21 | if err != nil { |
| 22 | return "", err |
| 23 | } |
| 24 | handle, err := windows.CreateFile(name, windows.FILE_READ_ATTRIBUTES, windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, nil, windows.OPEN_EXISTING, windows.FILE_FLAG_BACKUP_SEMANTICS|windows.FILE_FLAG_OPEN_REPARSE_POINT, 0) |
| 25 | if err != nil { |
| 26 | return "", err |
| 27 | } |
| 28 | defer windows.CloseHandle(handle) |
| 29 | var identity windows.ByHandleFileInformation |
| 30 | if err := windows.GetFileInformationByHandle(handle, &identity); err != nil { |
| 31 | return "", err |
| 32 | } |
| 33 | if identity.FileAttributes&windows.FILE_ATTRIBUTE_REPARSE_POINT != 0 { |
| 34 | return "", fmt.Errorf("not a plain directory") |
| 35 | } |
| 36 | return fmt.Sprintf("%x:%x:%x", identity.VolumeSerialNumber, identity.FileIndexHigh, identity.FileIndexLow), nil |
| 37 | } |
| 38 |