-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathutils.go
136 lines (125 loc) · 2.43 KB
/
utils.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package upx
import (
"crypto/md5"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"
)
func parseMTime(value string, match *MatchConfig) error {
if value == "" {
return nil
}
v, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return err
}
if v < 0 {
match.After = time.Now().Add(time.Duration(v) * time.Hour * 24)
match.TimeType = TIME_AFTER
} else {
if strings.HasPrefix(value, "+") {
match.Before = time.Now().Add(time.Duration(-1*(v+1)) * time.Hour * 24)
match.TimeType = TIME_BEFORE
} else {
match.Before = time.Now().Add(time.Duration(-1*v) * time.Hour * 24)
match.After = time.Now().Add(time.Duration(-1*(v+1)) * time.Hour * 24)
match.TimeType = TIME_INTERVAL
}
}
return nil
}
func humanizeSize(b int64) string {
unit := []string{"B", "KB", "MB", "GB", "TB"}
u, v, s := 0, float64(b), ""
for {
if v < 1024.0 {
switch {
case v < 10:
s = fmt.Sprintf("%.3f", v)
case v < 100:
s = fmt.Sprintf("%.2f", v)
case v < 1000:
s = fmt.Sprintf("%.1f", v)
default:
s = fmt.Sprintf("%.0f", v)
}
break
}
v /= 1024
u++
}
if strings.Contains(s, ".") {
ed := len(s) - 1
for ; ed > 0; ed-- {
if s[ed] == '.' {
ed--
break
}
if s[ed] != '0' {
break
}
}
s = s[:ed+1]
}
return s + unit[u]
}
func md5File(fpath string) (string, error) {
fd, err := os.Open(fpath)
if err != nil {
return "", err
}
defer fd.Close()
hash := md5.New()
if _, err := io.Copy(hash, fd); err != nil {
return "", err
}
return fmt.Sprintf("%x", hash.Sum(nil)), nil
}
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func globFiles(patterns []string) []string {
filenames := make([]string, 0)
for _, filename := range patterns {
matches, err := filepath.Glob(filename)
if err == nil {
filenames = append(filenames, matches...)
}
}
return filenames
}
func isWindowsGOOS() bool {
return runtime.GOOS == "windows"
}
func cleanFilename(name string) string {
if !isWindowsGOOS() {
return name
}
var name2 string
if strings.HasPrefix(name, `\\?\`) {
name2 = `\\?\`
name = strings.TrimPrefix(name, `\\?\`)
}
if strings.HasPrefix(name, `//?/`) {
name2 = `//?/`
name = strings.TrimPrefix(name, `//?/`)
}
name2 += strings.Map(func(r rune) rune {
switch r {
case '<', '>', '"', '|', '?', '*', ':':
return '_'
}
return r
}, name)
return name2
}