-
Notifications
You must be signed in to change notification settings - Fork 1
/
size.go
103 lines (91 loc) · 2.01 KB
/
size.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"fmt"
"log"
"math"
"os"
"path/filepath"
"strings"
)
//DirSize: Returns the size of the directory passed in as the path parameter or an error.
func DirSize(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}
//SizeInMB: Converts bytes to megabytes.
func SizeInMB(byteSize int64) float64 {
if byteSize < 0 {
return 0
}
return float64(byteSize) / 1000.0 / 1000.0
}
//SizeInGB: Converts bytes to gigabytes.
func SizeInGB(byteSize int64) float64 {
if byteSize < 0 {
return 0
}
return float64(byteSize) / 1000.0 / 1000.0 / 1000.0
}
//SizeMBToGB: Converts megabytes to gigabytes.
func SizeMBToGB(mbSize float64) float64 {
if mbSize < 0 {
return 0
}
return mbSize / 1000.0
}
//RoundGB: Rounds gigabyte size to two digits.
func RoundGB(gbSize float64) float64 {
if gbSize < 0 {
return 0
}
return math.Round(gbSize*100) / 100
}
//SizeMsg: Outputs message describing the size of space cleaned.
func SizeMsg(mbSize float64) {
if mbSize < 0 {
log.Printf("Successfully freed up %d MB.", 0)
return
}
if mbSize >= 1000.0 {
gbSize := RoundGB(SizeMBToGB(mbSize))
log.Printf("Successfully freed up %.2f GB.", gbSize)
} else {
log.Printf("Successfully freed up %.1f MB.", mbSize)
}
}
//Gets the size of the latest iOS version in the DeviceSupport directory
func DeviceSupportLatestVerDirSize() int64 {
d, err := os.Open(DeviceSupport())
if err != nil {
return 0
}
defer d.Close()
names, err := d.Readdirnames(-1)
if err != nil {
return 0
}
latest, err := getLatestVer(DeviceSupport())
if err != nil {
return 0
}
for _, name := range names {
if latest != nil && strings.Split(name, " ")[0] == latest.Original() {
latestDir := fmt.Sprintf("%s/%s", DeviceSupport(), name)
latestSize, err := DirSize(latestDir)
if err != nil {
return 0
}
return latestSize
}
}
return 0
}