diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e9a4aab..d1f878a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ ## Changelog +### 12.73.2 + +* `[fmtutil]` Fixed handling negative numbers in `PrettySize` +* `[fsutil]` Fixed handling empty paths in `ProperPath` + ### 12.73.1 * `[panel]` Panel rendering moved from `terminal` sub-package to it's own sub-package diff --git a/ek.go b/ek.go index 74083147..8b75b28c 100644 --- a/ek.go +++ b/ek.go @@ -20,7 +20,7 @@ import ( // ////////////////////////////////////////////////////////////////////////////////// // // VERSION is current ek package version -const VERSION = "12.73.1" +const VERSION = "12.73.2" // ////////////////////////////////////////////////////////////////////////////////// // diff --git a/fmtutil/fmtutil.go b/fmtutil/fmtutil.go index a0fba690..af0ec36a 100644 --- a/fmtutil/fmtutil.go +++ b/fmtutil/fmtutil.go @@ -33,10 +33,10 @@ const ( // ////////////////////////////////////////////////////////////////////////////////// // const ( - _KILO = 1024 - _MEGA = 1048576 - _GIGA = 1073741824 - _TERA = 1099511627776 + _KILO float64 = 1024 + _MEGA float64 = 1048576 + _GIGA float64 = 1073741824 + _TERA float64 = 1099511627776 ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -148,13 +148,13 @@ func PrettySize(i any, separator ...string) string { } switch { - case f >= _TERA: + case math.Abs(f) >= _TERA: return fmt.Sprintf("%g", Float(f/_TERA)) + sep + "TB" - case f >= _GIGA: + case math.Abs(f) >= _GIGA: return fmt.Sprintf("%g", Float(f/_GIGA)) + sep + "GB" - case f >= _MEGA: + case math.Abs(f) >= _MEGA: return fmt.Sprintf("%g", Float(f/_MEGA)) + sep + "MB" - case f >= _KILO: + case math.Abs(f) >= _KILO: return fmt.Sprintf("%g", Float(f/_KILO)) + sep + "KB" default: return fmt.Sprintf("%g", mathutil.Round(f, 0)) + sep + "B" diff --git a/fmtutil/fmtutil_test.go b/fmtutil/fmtutil_test.go index 2d101dcb..d4c5b34a 100644 --- a/fmtutil/fmtutil_test.go +++ b/fmtutil/fmtutil_test.go @@ -69,6 +69,7 @@ func (s *FmtUtilSuite) TestPretySize(c *C) { c.Assert(PrettySize(1024*1024*1024), Equals, "1GB") c.Assert(PrettySize(int64(1024*1024*1024*1024)), Equals, "1TB") c.Assert(PrettySize(1052500000), Equals, "1003.7MB") + c.Assert(PrettySize(-1052500000), Equals, "-1003.75MB") c.Assert(PrettySize(int32(3000125)), Equals, "2.86MB") c.Assert(PrettySize(int64(3000125)), Equals, "2.86MB") diff --git a/fsutil/fs.go b/fsutil/fs.go index 07f8f2b4..fd53c544 100644 --- a/fsutil/fs.go +++ b/fsutil/fs.go @@ -53,16 +53,15 @@ var ErrEmptyPath = errors.New("Path is empty") // CheckPerms checks many props at once // -// * F - is file -// * D - is directory -// * X - is executable -// * L - is link -// * W - is writable -// * R - is readable -// * B - is block device -// * C - is character device -// * S - not empty (only for files) -// +// - F: is file +// - D: is directory +// - X: is executable +// - L: is link +// - W: is writable +// - R: is readable +// - B: is block device +// - C: is character device +// - S: not empty (only for files) func CheckPerms(props, path string) bool { if props == "" || path == "" { return false @@ -242,6 +241,10 @@ func ValidatePerms(props, path string) error { // ProperPath returns the first proper path from a given slice func ProperPath(props string, paths []string) string { for _, path := range paths { + if strings.Trim(path, " ") == "" { + continue + } + path = PATH.Clean(path) if CheckPerms(props, path) { diff --git a/fsutil/fs_test.go b/fsutil/fs_test.go index 42700ed3..5382d581 100644 --- a/fsutil/fs_test.go +++ b/fsutil/fs_test.go @@ -223,7 +223,7 @@ func (s *FSSuite) TestProperPath(c *check.C) { os.OpenFile(tmpFile, os.O_CREATE, 0644) - paths := []string{"/etc/passwd", tmpFile, "/etc"} + paths := []string{"", "/etc/passwd", tmpFile, "/etc"} c.Assert(ProperPath("DR", paths), check.Equals, "/etc") c.Assert(ProperPath("FR", paths), check.Equals, "/etc/passwd")