forked from muesli/duf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mounts_windows.go
376 lines (318 loc) · 11 KB
/
mounts_windows.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// +build windows
package main
import (
"fmt"
"golang.org/x/sys/windows"
"math"
"path/filepath"
"strings"
"syscall"
"unsafe"
)
// Local devices
const (
guidBufLen = windows.MAX_PATH + 1
volumeNameBufLen = windows.MAX_PATH + 1
rootPathBufLen = windows.MAX_PATH + 1
fileSystemBufLen = windows.MAX_PATH + 1
)
func getMountPoint(guidBuf []uint16) (mountPoint string, err error) {
var rootPathLen uint32
rootPathBuf := make([]uint16, rootPathBufLen)
err = windows.GetVolumePathNamesForVolumeName(&guidBuf[0], &rootPathBuf[0], rootPathBufLen*2, &rootPathLen)
if err != nil && err.(windows.Errno) == windows.ERROR_MORE_DATA {
// Retry if buffer size is too small
rootPathBuf = make([]uint16, (rootPathLen+1)/2)
err = windows.GetVolumePathNamesForVolumeName(
&guidBuf[0], &rootPathBuf[0], rootPathLen, &rootPathLen)
}
return windows.UTF16ToString(rootPathBuf), err
}
func getVolumeInfo(guidOrMountPointBuf []uint16) (volumeName string, fsType string, err error) {
volumeNameBuf := make([]uint16, volumeNameBufLen)
fsTypeBuf := make([]uint16, fileSystemBufLen)
err = windows.GetVolumeInformation(&guidOrMountPointBuf[0], &volumeNameBuf[0], volumeNameBufLen*2,
nil, nil, nil,
&fsTypeBuf[0], fileSystemBufLen*2)
return windows.UTF16ToString(volumeNameBuf), windows.UTF16ToString(fsTypeBuf), err
}
func getSpaceInfo(guidOrMountPointBuf []uint16) (totalBytes uint64, freeBytes uint64, err error) {
err = windows.GetDiskFreeSpaceEx(&guidOrMountPointBuf[0], nil, &totalBytes, &freeBytes)
return
}
func getClusterInfo(guidOrMountPointBuf []uint16) (totalClusters uint32, clusterSize uint32, err error) {
var sectorsPerCluster uint32
var bytesPerSector uint32
err = GetDiskFreeSpace(&guidOrMountPointBuf[0], §orsPerCluster, &bytesPerSector, nil, &totalClusters)
clusterSize = bytesPerSector * sectorsPerCluster
return
}
func getMount(guidOrMountPointBuf []uint16, isGUID bool) (m Mount, skip bool, warnings []string) {
var err error
guidOrMountPoint := windows.UTF16ToString(guidOrMountPointBuf)
mountPoint := guidOrMountPoint
if isGUID {
mountPoint, err = getMountPoint(guidOrMountPointBuf)
if err != nil {
warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err))
}
// Skip unmounted volumes
if len(mountPoint) == 0 {
skip = true
return
}
}
// Get volume name & filesystem type
volumeName, fsType, err := getVolumeInfo(guidOrMountPointBuf)
if err != nil {
warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err))
}
// Get space info
totalBytes, freeBytes, err := getSpaceInfo(guidOrMountPointBuf)
if err != nil {
warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err))
}
// Get cluster info
totalClusters, clusterSize, err := getClusterInfo(guidOrMountPointBuf)
if err != nil {
warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err))
}
m = Mount{
Device: volumeName,
Mountpoint: mountPoint,
Fstype: fsType,
Type: fsType,
Opts: "",
Total: totalBytes,
Free: freeBytes,
Used: totalBytes - freeBytes,
Blocks: uint64(totalClusters),
BlockSize: uint64(clusterSize),
}
m.DeviceType = deviceType(m)
return
}
func getMountFromGUID(guidBuf []uint16) (m Mount, skip bool, warnings []string) {
m, skip, warnings = getMount(guidBuf, true)
// Use GUID as volume name if no label was set
if len(m.Device) == 0 {
m.Device = windows.UTF16ToString(guidBuf)
}
return
}
func getMountFromMountPoint(mountPointBuf []uint16) (m Mount, warnings []string) {
m, _, warnings = getMount(mountPointBuf, false)
// Use mount point as volume name if no label was set
if len(m.Device) == 0 {
m.Device = windows.UTF16ToString(mountPointBuf)
}
return m, warnings
}
func appendLocalMounts(mounts []Mount, warnings []string) ([]Mount, []string, error) {
guidBuf := make([]uint16, guidBufLen)
hFindVolume, err := windows.FindFirstVolume(&guidBuf[0], guidBufLen*2)
if err != nil {
return mounts, warnings, err
}
VolumeLoop:
for ; ; err = windows.FindNextVolume(hFindVolume, &guidBuf[0], guidBufLen*2) {
if err != nil {
switch err.(windows.Errno) {
case windows.ERROR_NO_MORE_FILES:
break VolumeLoop
default:
warnings = append(warnings, fmt.Sprintf("%s: %s", windows.UTF16ToString(guidBuf), err))
continue VolumeLoop
}
}
if m, skip, w := getMountFromGUID(guidBuf); !skip {
mounts = append(mounts, m)
warnings = append(warnings, w...)
}
}
if err = windows.FindVolumeClose(hFindVolume); err != nil {
warnings = append(warnings, fmt.Sprintf("%s", err))
}
return mounts, warnings, nil
}
// Network devices
func getMountFromNetResource(netResource NetResource) (m Mount, warnings []string) {
mountPoint := windows.UTF16PtrToString(netResource.LocalName)
if !strings.HasSuffix(mountPoint, string(filepath.Separator)) {
mountPoint += string(filepath.Separator)
}
mountPointBuf := windows.StringToUTF16(mountPoint)
m, _, warnings = getMount(mountPointBuf, false)
// Use remote name as volume name if no label was set
if len(m.Device) == 0 {
m.Device = windows.UTF16PtrToString(netResource.RemoteName)
}
return
}
func appendNetworkMounts(mounts []Mount, warnings []string) ([]Mount, []string, error) {
hEnumResource, err := WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_DISK, RESOURCEUSAGE_CONNECTABLE, nil)
if err != nil {
return mounts, warnings, err
}
EnumLoop:
for {
// Reference: https://docs.microsoft.com/en-us/windows/win32/wnet/enumerating-network-resources
var nrBuf [16384]byte
count := uint32(math.MaxUint32)
size := uint32(len(nrBuf))
if err := WNetEnumResource(hEnumResource, &count, &nrBuf[0], &size); err != nil {
switch err.(windows.Errno) {
case windows.ERROR_NO_MORE_ITEMS:
break EnumLoop
default:
warnings = append(warnings, err.Error())
break EnumLoop
}
}
for i := uint32(0); i < count; i++ {
nr := (*NetResource)(unsafe.Pointer(&nrBuf[uintptr(i)*NetResourceSize]))
m, w := getMountFromNetResource(*nr)
mounts = append(mounts, m)
warnings = append(warnings, w...)
}
}
if err = WNetCloseEnum(hEnumResource); err != nil {
warnings = append(warnings, fmt.Sprintf("%s", err))
}
return mounts, warnings, nil
}
func mountPointAlreadyPresent(mounts []Mount, mountPoint string) bool {
for _, m := range mounts {
if m.Mountpoint == mountPoint {
return true
}
}
return false
}
func appendLogicalDrives(mounts []Mount, warnings []string) ([]Mount, []string) {
driveBitmap, err := windows.GetLogicalDrives()
if err != nil {
warnings = append(warnings, fmt.Sprintf("GetLogicalDrives(): %s", err))
return mounts, warnings
}
for drive := 'A'; drive <= 'Z'; drive, driveBitmap = drive+1, driveBitmap>>1 {
if driveBitmap&0x1 == 0 {
continue
}
mountPoint := fmt.Sprintf("%c:\\", drive)
if mountPointAlreadyPresent(mounts, mountPoint) {
continue
}
mountPointBuf := windows.StringToUTF16(mountPoint)
m, w := getMountFromMountPoint(mountPointBuf)
mounts = append(mounts, m)
warnings = append(warnings, w...)
}
return mounts, warnings
}
func mounts() (ret []Mount, warnings []string, err error) {
ret = make([]Mount, 0)
// Local devices
if ret, warnings, err = appendLocalMounts(ret, warnings); err != nil {
return
}
// Network devices
if ret, warnings, err = appendNetworkMounts(ret, warnings); err != nil {
return
}
// Logical devices (from GetLogicalDrives bitflag)
// Check any possible logical drives, in case of some special virtual devices, such as RAM disk
ret, warnings = appendLogicalDrives(ret, warnings)
return ret, warnings, nil
}
// Windows API
const (
// Windows Networking const
// Reference: https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetopenenumw
RESOURCE_CONNECTED = 0x00000001
RESOURCE_GLOBALNET = 0x00000002
RESOURCE_REMEMBERED = 0x00000003
RESOURCE_RECENT = 0x00000004
RESOURCE_CONTEXT = 0x00000005
RESOURCETYPE_ANY = 0x00000000
RESOURCETYPE_DISK = 0x00000001
RESOURCETYPE_PRINT = 0x00000002
RESOURCETYPE_RESERVED = 0x00000008
RESOURCETYPE_UNKNOWN = 0xFFFFFFFF
RESOURCEUSAGE_CONNECTABLE = 0x00000001
RESOURCEUSAGE_CONTAINER = 0x00000002
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004
RESOURCEUSAGE_SIBLING = 0x00000008
RESOURCEUSAGE_ATTACHED = 0x00000010
RESOURCEUSAGE_ALL = RESOURCEUSAGE_CONNECTABLE | RESOURCEUSAGE_CONTAINER | RESOURCEUSAGE_ATTACHED
RESOURCEUSAGE_RESERVED = 0x80000000
)
var (
// Windows syscall
modmpr = windows.NewLazySystemDLL("mpr.dll")
modkernel32 = windows.NewLazySystemDLL("kernel32.dll")
procWNetOpenEnumW = modmpr.NewProc("WNetOpenEnumW")
procWNetCloseEnum = modmpr.NewProc("WNetCloseEnum")
procWNetEnumResourceW = modmpr.NewProc("WNetEnumResourceW")
procGetDiskFreeSpaceW = modkernel32.NewProc("GetDiskFreeSpaceW")
NetResourceSize = unsafe.Sizeof(NetResource{})
)
// Reference: https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/ns-winnetwk-netresourcew
type NetResource struct {
Scope uint32
Type uint32
DisplayType uint32
Usage uint32
LocalName *uint16
RemoteName *uint16
Comment *uint16
Provider *uint16
}
// Reference: https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetopenenumw
func WNetOpenEnum(scope uint32, resourceType uint32, usage uint32, resource *NetResource) (handle windows.Handle, err error) {
r1, _, e1 := syscall.Syscall6(procWNetOpenEnumW.Addr(), 5, uintptr(scope), uintptr(resourceType), uintptr(usage), uintptr(unsafe.Pointer(resource)), uintptr(unsafe.Pointer(&handle)), 0)
if r1 != windows.NO_ERROR {
if e1 != 0 {
err = e1
} else {
err = syscall.EINVAL
}
}
return
}
// Reference: https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetenumresourcew
func WNetEnumResource(enumResource windows.Handle, count *uint32, buffer *byte, bufferSize *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(procWNetEnumResourceW.Addr(), 4, uintptr(enumResource), uintptr(unsafe.Pointer(count)), uintptr(unsafe.Pointer(buffer)), uintptr(unsafe.Pointer(bufferSize)), 0, 0)
if r1 != windows.NO_ERROR {
if e1 != 0 {
err = e1
} else {
err = syscall.EINVAL
}
}
return
}
// Reference: https://docs.microsoft.com/en-us/windows/win32/api/winnetwk/nf-winnetwk-wnetcloseenum
func WNetCloseEnum(enumResource windows.Handle) (err error) {
r1, _, e1 := syscall.Syscall(procWNetCloseEnum.Addr(), 1, uintptr(enumResource), 0, 0)
if r1 != windows.NO_ERROR {
if e1 != 0 {
err = e1
} else {
err = syscall.EINVAL
}
}
return
}
// Reference: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getdiskfreespacew
func GetDiskFreeSpace(directoryName *uint16, sectorsPerCluster *uint32, bytesPerSector *uint32, numberOfFreeClusters *uint32, totalNumberOfClusters *uint32) (err error) {
r1, _, e1 := syscall.Syscall6(procGetDiskFreeSpaceW.Addr(), 5, uintptr(unsafe.Pointer(directoryName)), uintptr(unsafe.Pointer(sectorsPerCluster)), uintptr(unsafe.Pointer(bytesPerSector)), uintptr(unsafe.Pointer(numberOfFreeClusters)), uintptr(unsafe.Pointer(totalNumberOfClusters)), 0)
if r1 == 0 {
if e1 != 0 {
err = e1
} else {
err = syscall.EINVAL
}
}
return
}