@@ -3,13 +3,21 @@ package code
33import (
44 "errors"
55 "fmt"
6- "math"
76 "os"
87 "path/filepath"
98 "strconv"
109 "strings"
1110)
1211
12+ const (
13+ KB = 1024
14+ MB = KB * 1024
15+ GB = MB * 1024
16+ TB = GB * 1024
17+ PB = TB * 1024
18+ EB = PB * 1024
19+ )
20+
1321func GetPathSize (path string , recursive , human , all bool ) (string , error ) {
1422 if path == "" {
1523 return "" , errors .New ("не указан путь" )
@@ -19,21 +27,16 @@ func GetPathSize(path string, recursive, human, all bool) (string, error) {
1927 return "" , fmt .Errorf ("%w" , err )
2028 }
2129 return fmt .Sprintf ("%v" , FormatSize (sum , human )), nil
22-
2330}
2431
2532func GetSize (path string , recursive , human , all bool ) (int64 , error ) {
26- info , err := os .Stat (path )
33+ info , err := os .Lstat (path )
2734 if err != nil {
2835 return 0 , errors .New ("не удалось прочитать путь к файлу или директории" )
2936 }
3037 // If path is a file but not a directory
3138 if ! info .IsDir () {
32- i , err := os .Lstat (path )
33- if err != nil {
34- return 0 , errors .New ("не удалось получить информацию о файле" )
35- }
36- return i .Size (), nil
39+ return info .Size (), nil
3740 }
3841
3942 // If path is a directory not a file
@@ -72,17 +75,17 @@ func FormatSize(size int64, human bool) string {
7275 case len (str ) <= 3 :
7376 return fmt .Sprintf ("%vB" , size )
7477 case len (str ) > 3 && len (str ) <= 6 :
75- return fmt .Sprintf ("%.1fKB" , float64 (size )/ 1000 )
78+ return fmt .Sprintf ("%.1fKB" , float64 (size )/ KB )
7679 case len (str ) >= 7 && len (str ) < 10 :
77- return fmt .Sprintf ("%.1fMB" , float64 (size )/ math . Pow ( 10 , 6 ) )
80+ return fmt .Sprintf ("%.1fMB" , float64 (size )/ MB )
7881 case len (str ) >= 10 && len (str ) < 13 :
79- return fmt .Sprintf ("%.1fGB" , float64 (size )/ math . Pow ( 10 , 9 ) )
82+ return fmt .Sprintf ("%.1fGB" , float64 (size )/ GB )
8083 case len (str ) >= 13 && len (str ) < 16 :
81- return fmt .Sprintf ("%.1fTB" , float64 (size )/ math . Pow ( 10 , 12 ) )
84+ return fmt .Sprintf ("%.1fTB" , float64 (size )/ TB )
8285 case len (str ) >= 16 && len (str ) < 19 :
83- return fmt .Sprintf ("%.1fPB" , float64 (size )/ math . Pow ( 10 , 15 ) )
86+ return fmt .Sprintf ("%.1fPB" , float64 (size )/ PB )
8487 default :
85- return fmt .Sprintf ("%.1fEB" , float64 (size )/ math . Pow ( 10 , 18 ) )
88+ return fmt .Sprintf ("%.1fEB" , float64 (size )/ EB )
8689 }
8790 }
8891 return fmt .Sprintf ("%vB" , size )
0 commit comments