1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-05-02 22:17:08 +08:00

[v3] rename Label and SerialNumber to all platform from linux specific.

This commit is contained in:
shirou 2020-10-26 22:45:18 +09:00
parent 005a35c436
commit 33e4b67dfc
8 changed files with 72 additions and 21 deletions

View File

@ -80,3 +80,17 @@ func Partitions(all bool) ([]PartitionStat, error) {
func IOCounters(names ...string) (map[string]IOCountersStat, error) {
return IOCountersWithContext(context.Background(), names...)
}
// SerialNumber returns Serial Number of given device or empty string
// on error. Name of device is expected, eg. /dev/sda
func SerialNumber(name string) (string, error) {
return SerialNumberWithContext(context.Background(), name)
}
// Label returns label of given device or empty string on error.
// Name of device is expected, eg. /dev/sda
// Supports label based on devicemapper name
// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm
func Label(name string) (string, error) {
return LabelWithContext(context.Background(), name)
}

View File

@ -76,3 +76,11 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
func getFsType(stat unix.Statfs_t) string {
return common.ByteToString(stat.Fstypename[:])
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}

View File

@ -19,3 +19,11 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return nil, common.ErrNotImplementedError
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}

View File

@ -160,3 +160,11 @@ func parsedevstat(buf []byte) (devstat, error) {
func getFsType(stat unix.Statfs_t) string {
return common.ByteToString(stat.Fstypename[:])
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}

View File

@ -431,25 +431,19 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
}
d.Name = name
d.SerialNumber = GetDiskSerialNumber(name)
d.Label = GetLabel(name)
d.SerialNumber, _ = SerialNumberWithContext(ctx, name)
d.Label, _ = LabelWithContext(ctx, name)
ret[name] = d
}
return ret, nil
}
// GetDiskSerialNumber returns Serial Number of given device or empty string
// on error. Name of device is expected, eg. /dev/sda
func GetDiskSerialNumber(name string) string {
return GetDiskSerialNumberWithContext(context.Background(), name)
}
func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
var stat unix.Stat_t
err := unix.Stat(name, &stat)
if err != nil {
return ""
return "", err
}
major := unix.Major(uint64(stat.Rdev))
minor := unix.Minor(uint64(stat.Rdev))
@ -461,7 +455,7 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
for scanner.Scan() {
values := strings.Split(scanner.Text(), "=")
if len(values) == 2 && values[0] == "E:ID_SERIAL" {
return values[1]
return values[1], nil
}
}
}
@ -472,28 +466,24 @@ func GetDiskSerialNumberWithContext(ctx context.Context, name string) string {
model, _ := ioutil.ReadFile(filepath.Join(devicePath, "model"))
serial, _ := ioutil.ReadFile(filepath.Join(devicePath, "serial"))
if len(model) > 0 && len(serial) > 0 {
return fmt.Sprintf("%s_%s", string(model), string(serial))
return fmt.Sprintf("%s_%s", string(model), string(serial)), nil
}
return ""
return "", nil
}
// GetLabel returns label of given device or empty string on error.
// Name of device is expected, eg. /dev/sda
// Supports label based on devicemapper name
// See https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-block-dm
func GetLabel(name string) string {
func LabelWithContext(ctx context.Context, name string) (string, error) {
// Try label based on devicemapper name
dmname_filename := common.HostSys(fmt.Sprintf("block/%s/dm/name", name))
if !common.PathExists(dmname_filename) {
return ""
return "", nil
}
dmname, err := ioutil.ReadFile(dmname_filename)
if err != nil {
return ""
return "", err
} else {
return strings.TrimSpace(string(dmname))
return strings.TrimSpace(string(dmname)), nil
}
}

View File

@ -148,3 +148,11 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
func getFsType(stat unix.Statfs_t) string {
return common.IntToString(stat.F_fstypename[:])
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}

View File

@ -113,3 +113,10 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
return usageStat, nil
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}

View File

@ -181,3 +181,11 @@ func IOCountersWithContext(ctx context.Context, names ...string) (map[string]IOC
}
return drivemap, nil
}
func SerialNumberWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}
func LabelWithContext(ctx context.Context, name string) (string, error) {
return "", common.ErrNotImplementedError
}