Skip to content

Commit

Permalink
Merge pull request #146 from andrewstucki/volume-info
Browse files Browse the repository at this point in the history
Add filesystem type support on Windows
  • Loading branch information
Andrew Stucki authored Nov 10, 2020
2 parents 53e981e + c03570f commit aaf1bd3
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 226 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add AIX support. #133
- Add `Cached` data to Memory #145
- Add `SysTypeName` support on Windows #146

### Fixed

Expand Down Expand Up @@ -184,4 +185,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[0.4.0]: https://github.com/elastic/gosigar/releases/tag/v0.4.0
[0.3.0]: https://github.com/elastic/gosigar/releases/tag/v0.3.0
[0.2.0]: https://github.com/elastic/gosigar/releases/tag/v0.2.0
[0.1.0]: https://github.com/elastic/gosigar/releases/tag/v0.1.0
[0.1.0]: https://github.com/elastic/gosigar/releases/tag/v0.1.0
11 changes: 8 additions & 3 deletions sigar_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,16 @@ func (self *FileSystemList) Get() error {
if err != nil {
return errors.Wrapf(err, "GetDriveType failed")
}
fsType, err := windows.GetFilesystemType(drive)
if err != nil {
return errors.Wrapf(err, "GetFilesystemType failed")
}

self.List = append(self.List, FileSystem{
DirName: drive,
DevName: drive,
TypeName: dt.String(),
DirName: drive,
DevName: drive,
TypeName: dt.String(),
SysTypeName: fsType,
})
}
return nil
Expand Down
19 changes: 19 additions & 0 deletions sys/windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package windows

import (
"fmt"
"strings"
"syscall"
"time"
"unsafe"
Expand Down Expand Up @@ -335,6 +336,23 @@ func GetDriveType(rootPathName string) (DriveType, error) {
return dt, nil
}

// GetFilesystemType returns file system type information at the given root path.
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationw
func GetFilesystemType(rootPathName string) (string, error) {
rootPathNamePtr, err := syscall.UTF16PtrFromString(rootPathName)
if err != nil {
return "", errors.Wrapf(err, "UTF16PtrFromString failed for rootPathName=%v", rootPathName)
}

buffer := make([]uint16, MAX_PATH+1)
success, err := _GetVolumeInformation(rootPathNamePtr, nil, 0, nil, nil, nil, &buffer[0], MAX_PATH)
if !success {
return "", errors.Wrap(err, "GetVolumeInformationW failed")
}

return strings.ToLower(syscall.UTF16ToString(buffer)), nil
}

// EnumProcesses retrieves the process identifier for each process object in the
// system. This function can return a max of 65536 PIDs. If there are more
// processes than that then this will not return them all.
Expand Down Expand Up @@ -587,6 +605,7 @@ func GetTickCount64() (uptime uint64, err error) {
//sys _GetProcessImageFileName(handle syscall.Handle, outImageFileName *uint16, size uint32) (length uint32, err error) = psapi.GetProcessImageFileNameW
//sys _GetSystemTimes(idleTime *syscall.Filetime, kernelTime *syscall.Filetime, userTime *syscall.Filetime) (err error) = kernel32.GetSystemTimes
//sys _GetDriveType(rootPathName *uint16) (dt DriveType, err error) = kernel32.GetDriveTypeW
//sys _GetVolumeInformation(rootPathName *uint16, volumeName *uint16, volumeNameSize uint32, volumeSerialNumber *uint32, maximumComponentLength *uint32, fileSystemFlags *uint32, fileSystemName *uint16, fileSystemNameSize uint32) (success bool, err error) [true] = kernel32.GetVolumeInformationW
//sys _EnumProcesses(processIds *uint32, sizeBytes uint32, bytesReturned *uint32) (err error) = psapi.EnumProcesses
//sys _GetDiskFreeSpaceEx(directoryName *uint16, freeBytesAvailable *uint64, totalNumberOfBytes *uint64, totalNumberOfFreeBytes *uint64) (err error) = kernel32.GetDiskFreeSpaceExW
//sys _Process32First(handle syscall.Handle, processEntry32 *ProcessEntry32) (err error) = kernel32.Process32FirstW
Expand Down
15 changes: 15 additions & 0 deletions sys/windows/syscall_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,21 @@ func TestGetDiskFreeSpaceEx(t *testing.T) {
}
}

func TestGetFilesystemType(t *testing.T) {
drives, err := GetLogicalDriveStrings()
if err != nil {
t.Fatal(err)
}

for _, drive := range drives {
volumeType, err := GetFilesystemType(drive)
if err != nil {
t.Fatal(err)
}
t.Logf("GetFilesystemType: %v - %v", drive, volumeType)
}
}

func TestGetWindowsVersion(t *testing.T) {
ver := GetWindowsVersion()
assert.True(t, ver.Major >= 5)
Expand Down
Loading

0 comments on commit aaf1bd3

Please sign in to comment.