-
-
Notifications
You must be signed in to change notification settings - Fork 43
/
filesystem_windows.go
86 lines (73 loc) · 2.17 KB
/
filesystem_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
// Kraken
// Copyright (C) 2016-2020 Claudio Guarnieri
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"unsafe"
"golang.org/x/sys/windows"
)
var (
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
procGetDriveTypeW = kernel32.NewProc("GetDriveTypeW")
procGetLogicalDrives = kernel32.NewProc("GetLogicalDrives")
)
func getDrives(bitMap uint32) (drives []string) {
driveLetters := []string{
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
}
for _, letter := range driveLetters {
if bitMap&1 == 1 {
drives = append(drives, fmt.Sprintf("%s:\\", letter))
}
bitMap >>= 1
}
return
}
func getDriveType(driveType uint32) string {
switch driveType {
case windows.DRIVE_CDROM:
return "cd-rom"
case windows.DRIVE_FIXED:
return "fixed"
case windows.DRIVE_NO_ROOT_DIR:
return "no-root-dir"
case windows.DRIVE_RAMDISK:
return "ram-disk"
case windows.DRIVE_REMOTE:
return "remote"
case windows.DRIVE_REMOVABLE:
return "removable"
case windows.DRIVE_UNKNOWN:
return "unknown"
}
return "unrecognized"
}
func getFileSystemRoots() []string {
var drives []string
var toScan []string
ret, _, _ := procGetLogicalDrives.Call()
drives = getDrives(uint32(ret))
for _, drive := range drives {
dtp, _, _ := procGetDriveTypeW.Call(uintptr(unsafe.Pointer(windows.StringToUTF16Ptr(drive))))
driveType := getDriveType(uint32(dtp))
// TODO: Shall we scan also removables?
if driveType == "fixed" {
toScan = append(toScan, drive)
}
}
return toScan
}