vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
|
215f8aeb
Keenan Nemetz
2d ago
|
1 | //go:build windows |
| 2 | ||
| 3 | package storage | |
| 4 | ||
| 5 | import ( | |
| 6 | "io/fs" | |
| 7 | "os" | |
| 8 | "unsafe" | |
| 9 | ||
| 10 | "golang.org/x/sys/windows" | |
| 11 | ) | |
| 12 | ||
| 13 | // fileKey identifies a file by the volume it lives on and the index the | |
| 14 | // filesystem gave it, which is how a hard link is measured once per inode rather | |
| 15 | // than once per name. Windows reports both through an open handle rather than | |
| 16 | // os.FileInfo, so the file is opened here; one that cannot be opened is counted | |
| 17 | // under every name it has, which over-reports disk use rather than losing bytes. | |
| 18 | func fileKeyFor(path string, _ fs.FileInfo) (fileKey, bool) { | |
| 19 | f, err := os.Open(path) | |
| 20 | if err != nil { | |
| 21 | return fileKey{}, false | |
| 22 | } | |
| 23 | defer f.Close() | |
| 24 | var info windows.ByHandleFileInformation | |
| 25 | if err := windows.GetFileInformationByHandle(windows.Handle(f.Fd()), &info); err != nil { | |
| 26 | return fileKey{}, false | |
| 27 | } | |
| 28 | return fileKey{ | |
| 29 | dev: uint64(info.VolumeSerialNumber), | |
| 30 | ino: uint64(info.FileIndexHigh)<<32 | uint64(info.FileIndexLow), | |
| 31 | }, true | |
| 32 | } | |
| 33 | ||
| 34 | var ( | |
| 35 | kernel32 = windows.NewLazySystemDLL("kernel32.dll") | |
| 36 | procGetCompressedFileSizeW = kernel32.NewProc("GetCompressedFileSizeW") | |
| 37 | ) | |
| 38 | ||
| 39 | // allocatedBytes is the space the file actually costs. GetCompressedFileSize is | |
| 40 | // how Windows answers that question: it reports the clusters allocated to the | |
| 41 | // file, so a sparse or compressed file does not claim its apparent size. When the | |
| 42 | // call fails, the apparent size is the answer that cannot under-report. | |
| 43 | func allocatedBytes(path string, info fs.FileInfo) int64 { | |
| 44 | p, err := windows.UTF16PtrFromString(path) | |
| 45 | if err != nil { | |
| 46 | return info.Size() | |
| 47 | } | |
| 48 | var high uint32 | |
| 49 | low, _, callErr := procGetCompressedFileSizeW.Call( | |
| 50 | uintptr(unsafe.Pointer(p)), | |
| 51 | uintptr(unsafe.Pointer(&high)), | |
| 52 | ) | |
| 53 | // The API signals failure with INVALID_FILE_SIZE, so a real 0xFFFFFFFF-byte | |
| 54 | // result is distinguished by a zero last-error, not by the value. | |
| 55 | if uint32(low) == 0xffffffff && callErr != nil && callErr != windows.ERROR_SUCCESS { | |
| 56 | return info.Size() | |
| 57 | } | |
| 58 | if n := int64(high)<<32 | int64(uint32(low)); n > 0 { | |
| 59 | return n | |
| 60 | } | |
| 61 | return info.Size() | |
| 62 | } | |
| 63 | ||
| 64 | // diskUsage is the capacity of the volume holding root. Available is what this | |
| 65 | // account can still write, which is the number an operator cares about; Used is | |
| 66 | // the whole volume's usage, not just Vole's. | |
| 67 | func diskUsage(root string) (Disk, error) { | |
| 68 | p, err := windows.UTF16PtrFromString(root) | |
| 69 | if err != nil { | |
| 70 | return Disk{}, err | |
| 71 | } | |
| 72 | var available, total, free uint64 | |
| 73 | if err := windows.GetDiskFreeSpaceEx(p, &available, &total, &free); err != nil { | |
| 74 | return Disk{}, err | |
| 75 | } | |
| 76 | return Disk{ | |
| 77 | Total: int64(total), | |
| 78 | Used: int64(total - free), | |
| 79 | Available: int64(available), | |
| 80 | }, nil | |
| 81 | } |