From 9d6c92629470099edd40ead9a3d8cf1b38020ec6 Mon Sep 17 00:00:00 2001 From: Mariana Dima Date: Tue, 9 Nov 2021 10:57:46 +0100 Subject: [PATCH] Handle ERROR_INVALID_PARAMETER error for GetVolumeInformationW win32 api (#164) * fix * ignore error --- CHANGELOG.md | 6 ++++++ sigar_windows.go | 15 ++++++--------- sys/windows/syscall_windows.go | 14 +++++--------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bb96701a..ebc11d816 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Deprecated +## [0.14.2] + +### Fixed + +- Fix again unsupported devices for filesystem. [#164](https://github.com/elastic/gosigar/pull/164) + ## [0.14.1] ### Fixed diff --git a/sigar_windows.go b/sigar_windows.go index 70abb13b5..7d9652686 100644 --- a/sigar_windows.go +++ b/sigar_windows.go @@ -130,7 +130,6 @@ func (self *FileSystemList) Get() error { if err != nil { return errors.Wrap(err, "GetAccessPaths failed") } - for _, drive := range drives { dt, err := windows.GetDriveType(drive) if err != nil { @@ -140,14 +139,12 @@ func (self *FileSystemList) Get() error { if err != nil { return errors.Wrapf(err, "GetFilesystemType failed") } - if fsType != "" { - self.List = append(self.List, FileSystem{ - DirName: drive, - DevName: drive, - TypeName: dt.String(), - SysTypeName: fsType, - }) - } + self.List = append(self.List, FileSystem{ + DirName: drive, + DevName: drive, + TypeName: dt.String(), + SysTypeName: fsType, + }) } return nil } diff --git a/sys/windows/syscall_windows.go b/sys/windows/syscall_windows.go index 12695fa6c..b5c1b064c 100644 --- a/sys/windows/syscall_windows.go +++ b/sys/windows/syscall_windows.go @@ -346,21 +346,17 @@ func GetDriveType(rootPathName string) (DriveType, error) { // https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getvolumeinformationw func GetFilesystemType(rootPathName string) (string, error) { rootPathNamePtr, err := syscall.UTF16PtrFromString(rootPathName) + var systemType = "unavailable" if err != nil { return "", errors.Wrapf(err, "UTF16PtrFromString failed for rootPathName=%v", rootPathName) } - buffer := make([]uint16, MAX_PATH+1) + // _GetVolumeInformation will fail for external drives like CD-ROM or other type with error codes as ERROR_NOT_READY. ERROR_INVALID_FUNCTION, ERROR_INVALID_PARAMETER, etc., these types of errors will be ignored success, err := _GetVolumeInformation(rootPathNamePtr, nil, 0, nil, nil, nil, &buffer[0], MAX_PATH) - // check if CD-ROM or other type that is not supported in GetVolumeInformation function - if err == ERROR_NOT_READY || err == ERROR_INVALID_FUNCTION { - return "", nil - } - if !success { - return "", errors.Wrap(err, "GetVolumeInformationW failed") + if success { + systemType = strings.ToLower(syscall.UTF16ToString(buffer)) } - - return strings.ToLower(syscall.UTF16ToString(buffer)), nil + return systemType, nil } // EnumProcesses retrieves the process identifier for each process object in the