From 6b2bfe408d8189bd62b444bdb167466379beb373 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Tue, 6 Sep 2022 23:13:22 -0700 Subject: [PATCH 1/2] Return all partitions on Windows and all errors rather than returning early --- disk/disk_windows.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/disk/disk_windows.go b/disk/disk_windows.go index 5fb9b5b..d07b29b 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -79,7 +79,21 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { return ret, nil } +type errorCollector []error + +func (c *errorCollector) collect(e error) { *c = append(*c, e) } + +func (c *errorCollector) Error() (err string) { + err = "" + for i, e := range *c { + err += fmt.Sprintf("\tError %d: %s\n", i, e.Error()) + } + + return err +} + func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { + collector := new(errorCollector) var ret []PartitionStat lpBuffer := make([]byte, 254) diskret, _, err := procGetLogicalDriveStringsW.Call( @@ -94,7 +108,9 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro typepath, _ := windows.UTF16PtrFromString(path) typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath))) if typeret == 0 { - return ret, windows.GetLastError() + err := windows.GetLastError() + collector.collect(err) + continue } // 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM @@ -118,7 +134,8 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro if typeret == 5 || typeret == 2 { continue // device is not ready will happen if there is no disk in the drive } - return ret, err + collector.collect(err) + continue } opts := []string{"rw"} if lpFileSystemFlags&fileReadOnlyVolume != 0 { @@ -138,7 +155,11 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro } } } - return ret, nil + if len(*collector) == 0 { + return ret, nil + } else { + return ret, collector + } } func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { From dbc0f20fe38841a0c1509de9dba5084b7c2c09d9 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Mon, 3 Oct 2022 13:55:14 -0700 Subject: [PATCH 2/2] code review --- disk/disk_windows.go | 27 ++++++--------------------- host/host_linux.go | 2 +- host/types.go | 24 ------------------------ internal/common/warnings.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 46 deletions(-) delete mode 100644 host/types.go create mode 100644 internal/common/warnings.go diff --git a/disk/disk_windows.go b/disk/disk_windows.go index d07b29b..b7f0c51 100644 --- a/disk/disk_windows.go +++ b/disk/disk_windows.go @@ -79,21 +79,10 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { return ret, nil } -type errorCollector []error - -func (c *errorCollector) collect(e error) { *c = append(*c, e) } - -func (c *errorCollector) Error() (err string) { - err = "" - for i, e := range *c { - err += fmt.Sprintf("\tError %d: %s\n", i, e.Error()) - } - - return err -} - func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { - collector := new(errorCollector) + warnings := common.Warnings{ + Verbose: true, + } var ret []PartitionStat lpBuffer := make([]byte, 254) diskret, _, err := procGetLogicalDriveStringsW.Call( @@ -109,7 +98,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro typeret, _, _ := procGetDriveType.Call(uintptr(unsafe.Pointer(typepath))) if typeret == 0 { err := windows.GetLastError() - collector.collect(err) + warnings.Add(err) continue } // 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 4: DRIVE_REMOTE 5: DRIVE_CDROM @@ -134,7 +123,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro if typeret == 5 || typeret == 2 { continue // device is not ready will happen if there is no disk in the drive } - collector.collect(err) + warnings.Add(err) continue } opts := []string{"rw"} @@ -155,11 +144,7 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro } } } - if len(*collector) == 0 { - return ret, nil - } else { - return ret, collector - } + return ret, warnings.Reference() } func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOCountersStat, error) { diff --git a/host/host_linux.go b/host/host_linux.go index 940415c..a6bb3fc 100644 --- a/host/host_linux.go +++ b/host/host_linux.go @@ -395,7 +395,7 @@ func SensorsTemperaturesWithContext(ctx context.Context) ([]TemperatureStat, err } } - var warns Warnings + var warns common.Warnings if len(files) == 0 { // handle distributions without hwmon, like raspbian #391, parse legacy thermal_zone files files, err = filepath.Glob(common.HostSys("/class/thermal/thermal_zone*/")) diff --git a/host/types.go b/host/types.go deleted file mode 100644 index c2e7c0b..0000000 --- a/host/types.go +++ /dev/null @@ -1,24 +0,0 @@ -package host - -import ( - "fmt" -) - -type Warnings struct { - List []error -} - -func (w *Warnings) Add(err error) { - w.List = append(w.List, err) -} - -func (w *Warnings) Reference() error { - if len(w.List) > 0 { - return w - } - return nil -} - -func (w *Warnings) Error() string { - return fmt.Sprintf("Number of warnings: %v", len(w.List)) -} diff --git a/internal/common/warnings.go b/internal/common/warnings.go new file mode 100644 index 0000000..a4aaada --- /dev/null +++ b/internal/common/warnings.go @@ -0,0 +1,30 @@ +package common + +import "fmt" + +type Warnings struct { + List []error + Verbose bool +} + +func (w *Warnings) Add(err error) { + w.List = append(w.List, err) +} + +func (w *Warnings) Reference() error { + if len(w.List) > 0 { + return w + } + return nil +} + +func (w *Warnings) Error() string { + if w.Verbose { + str := "" + for i, e := range w.List { + str += fmt.Sprintf("\tError %d: %s\n", i, e.Error()) + } + return str + } + return fmt.Sprintf("Number of warnings: %v", len(w.List)) +}