Skip to content

Commit

Permalink
fix: division by zero possible
Browse files Browse the repository at this point in the history
some filesystem have zero Inode
  • Loading branch information
HeChuanXUPT committed Dec 23, 2016
1 parent a0f760e commit c73b668
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions disk/disk_unix.go
Expand Up @@ -24,11 +24,22 @@ func Usage(path string) (*UsageStat, error) {
// if could not get InodesTotal, return empty
if ret.InodesTotal < ret.InodesFree {
return ret, nil
}
}

ret.InodesUsed = (ret.InodesTotal - ret.InodesFree)
ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize)
ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0

if ret.InodesTotal == 0 {
ret.InodesUsedPercent = 0
} else {
ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0
}

if ret.Total == 0 {
ret.UsedPercent = 0
} else {
ret.UsedPercent = (float64(ret.Used) / float64(ret.Total)) * 100.0
}

return ret, nil
}

0 comments on commit c73b668

Please sign in to comment.