From 2b7d39883f5be8a2839e49938360b316217bc863 Mon Sep 17 00:00:00 2001 From: FRex Date: Thu, 5 Nov 2020 20:55:40 +0100 Subject: [PATCH 1/4] Add drives from GetLogicalDrives bitmap too --- mounts_windows.go | 82 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/mounts_windows.go b/mounts_windows.go index 4bfb6955..75a0f43f 100644 --- a/mounts_windows.go +++ b/mounts_windows.go @@ -111,6 +111,49 @@ func getMountFromGUID(guidBuf []uint16) (m Mount, skip bool, warnings []string) return } +func getMountFromMountPoint(mountPointBuf []uint16) (m Mount, warnings []string) { + var err error + mountPoint := windows.UTF16ToString(mountPointBuf) + + // Get volume name & filesystem type + volumeName, fsType, err := getVolumeInfo(mountPointBuf) + if err != nil { + warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) + } + + // Get space info + totalBytes, freeBytes, err := getSpaceInfo(mountPointBuf) + if err != nil { + warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) + } + + // Get cluster info + totalClusters, clusterSize, err := getClusterInfo(mountPointBuf) + if err != nil { + warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) + } + + // Use GUID as volume name if no label was set + if len(volumeName) == 0 { + volumeName = mountPoint + } + + 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 appendLocalMounts(mounts []Mount, warnings []string) ([]Mount, []string, error) { guidBuf := make([]uint16, guidBufLen) @@ -228,6 +271,42 @@ EnumLoop: 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 i := 0; i < 26; i++ { + if (drivebitmap & (1 << i)) == 0 { + continue + } + + mountPoint := fmt.Sprintf("%s:\\", string(65+i)) + 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) @@ -236,6 +315,9 @@ func mounts() (ret []Mount, warnings []string, err error) { return } + // Logical devices (from GetLogicalDrives bitflag) + ret, warnings = appendLogicalDrives(ret, warnings) + // Network devices if ret, warnings, err = appendNetworkMounts(ret, warnings); err != nil { return From d76cb7a0435fcc27f1007bbd21134529e51ae22e Mon Sep 17 00:00:00 2001 From: RangerCD Date: Sat, 14 Nov 2020 12:33:18 +0800 Subject: [PATCH 2/4] [Windows] fix network device reported twice --- mounts_windows.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mounts_windows.go b/mounts_windows.go index 75a0f43f..1bf008a0 100644 --- a/mounts_windows.go +++ b/mounts_windows.go @@ -315,14 +315,15 @@ func mounts() (ret []Mount, warnings []string, err error) { return } - // Logical devices (from GetLogicalDrives bitflag) - ret, warnings = appendLogicalDrives(ret, warnings) - // 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 } From ed081172a67a518fa14774cd3a6daa4602044c31 Mon Sep 17 00:00:00 2001 From: RangerCD Date: Sat, 14 Nov 2020 12:23:45 +0800 Subject: [PATCH 3/4] [Windows] refactor getMount* functions --- mounts_windows.go | 124 +++++++++++++--------------------------------- 1 file changed, 35 insertions(+), 89 deletions(-) diff --git a/mounts_windows.go b/mounts_windows.go index 1bf008a0..3e74733d 100644 --- a/mounts_windows.go +++ b/mounts_windows.go @@ -58,41 +58,39 @@ func getClusterInfo(guidOrMountPointBuf []uint16) (totalClusters uint32, cluster return } -func getMountFromGUID(guidBuf []uint16) (m Mount, skip bool, warnings []string) { +func getMount(guidOrMountPointBuf []uint16, isGUID bool) (m Mount, skip bool, warnings []string) { var err error - guid := windows.UTF16ToString(guidBuf) + guidOrMountPoint := windows.UTF16ToString(guidOrMountPointBuf) - mountPoint, err := getMountPoint(guidBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", guid, err)) - } - // Skip unmounted volumes - if len(mountPoint) == 0 { - skip = true - return + 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(guidBuf) + volumeName, fsType, err := getVolumeInfo(guidOrMountPointBuf) if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", guid, err)) + warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err)) } // Get space info - totalBytes, freeBytes, err := getSpaceInfo(guidBuf) + totalBytes, freeBytes, err := getSpaceInfo(guidOrMountPointBuf) if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", guid, err)) + warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err)) } // Get cluster info - totalClusters, clusterSize, err := getClusterInfo(guidBuf) + totalClusters, clusterSize, err := getClusterInfo(guidOrMountPointBuf) if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", guid, err)) - } - - // Use GUID as volume name if no label was set - if len(volumeName) == 0 { - volumeName = guid + warnings = append(warnings, fmt.Sprintf("%s: %s", guidOrMountPoint, err)) } m = Mount{ @@ -111,47 +109,26 @@ func getMountFromGUID(guidBuf []uint16) (m Mount, skip bool, warnings []string) return } -func getMountFromMountPoint(mountPointBuf []uint16) (m Mount, warnings []string) { - var err error - mountPoint := windows.UTF16ToString(mountPointBuf) +func getMountFromGUID(guidBuf []uint16) (m Mount, skip bool, warnings []string) { + m, skip, warnings = getMount(guidBuf, true) - // Get volume name & filesystem type - volumeName, fsType, err := getVolumeInfo(mountPointBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) + // Use GUID as volume name if no label was set + if len(m.Device) == 0 { + m.Device = windows.UTF16ToString(guidBuf) } - // Get space info - totalBytes, freeBytes, err := getSpaceInfo(mountPointBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) - } + return +} - // Get cluster info - totalClusters, clusterSize, err := getClusterInfo(mountPointBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) - } +func getMountFromMountPoint(mountPointBuf []uint16) (m Mount, warnings []string) { + m, _, warnings = getMount(mountPointBuf, false) - // Use GUID as volume name if no label was set - if len(volumeName) == 0 { - volumeName = mountPoint + // Use mount point as volume name if no label was set + if len(m.Device) == 0 { + m.Device = windows.UTF16ToString(mountPointBuf) } - 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 + return m, warnings } func appendLocalMounts(mounts []Mount, warnings []string) ([]Mount, []string, error) { @@ -188,50 +165,19 @@ VolumeLoop: // 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) - // Get volume name & filesystem type - volumeName, fsType, err := getVolumeInfo(mountPointBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) - } - - // Get space info - totalBytes, freeBytes, err := getSpaceInfo(mountPointBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) - } - - // Get cluster info - totalClusters, clusterSize, err := getClusterInfo(mountPointBuf) - if err != nil { - warnings = append(warnings, fmt.Sprintf("%s: %s", mountPoint, err)) - } + m, _, warnings = getMount(mountPointBuf, false) // Use remote name as volume name if no label was set - if len(volumeName) == 0 { - volumeName = windows.UTF16PtrToString(netResource.RemoteName) + if len(m.Device) == 0 { + m.Device = windows.UTF16PtrToString(netResource.RemoteName) } - m = Mount{ - Device: volumeName, - Mountpoint: mountPoint, - Fstype: fsType, - Type: fsType, - Opts: "", - Total: totalBytes, - Free: freeBytes, - Used: totalBytes - freeBytes, - Blocks: uint64(totalClusters), - BlockSize: uint64(clusterSize), - Metadata: &netResource, - } - m.DeviceType = deviceType(m) return } From 1f514c72be212d79f5f7b059f21e7eee56624f02 Mon Sep 17 00:00:00 2001 From: RangerCD Date: Fri, 13 Nov 2020 18:24:47 +0800 Subject: [PATCH 4/4] [Windows] refactor appendLogicalDrives --- mounts_windows.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mounts_windows.go b/mounts_windows.go index 3e74733d..3705b36b 100644 --- a/mounts_windows.go +++ b/mounts_windows.go @@ -228,18 +228,18 @@ func mountPointAlreadyPresent(mounts []Mount, mountPoint string) bool { } func appendLogicalDrives(mounts []Mount, warnings []string) ([]Mount, []string) { - drivebitmap, err := windows.GetLogicalDrives() + driveBitmap, err := windows.GetLogicalDrives() if err != nil { warnings = append(warnings, fmt.Sprintf("GetLogicalDrives(): %s", err)) return mounts, warnings } - for i := 0; i < 26; i++ { - if (drivebitmap & (1 << i)) == 0 { + for drive := 'A'; drive <= 'Z'; drive, driveBitmap = drive+1, driveBitmap>>1 { + if driveBitmap&0x1 == 0 { continue } - mountPoint := fmt.Sprintf("%s:\\", string(65+i)) + mountPoint := fmt.Sprintf("%c:\\", drive) if mountPointAlreadyPresent(mounts, mountPoint) { continue }