-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.go
100 lines (79 loc) · 1.77 KB
/
util.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
package fsdup
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math/rand"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func parseIntLE(b []byte, offset int64, length int64) int64 {
bpad := make([]byte, 8)
// Pad b to 8 bytes (uint64/int64 size), determine if negative
// and fill the array with the little endian number and the sign
sign := byte(0)
if b[offset+length-1] & 0x80 == 0x80 {
sign = 0xFF
}
for i := int64(0); i < length; i++ {
bpad[i] = b[offset+i]
}
for i := length; i < 8; i++ {
bpad[i] = sign
}
return int64(binary.LittleEndian.Uint64(bpad))
}
func parseUintLE(b []byte, offset int64, length int64) int64 {
bpad := make([]byte, 8)
for i := int64(0); i < length; i++ {
bpad[i] = b[offset+i]
}
return int64(binary.LittleEndian.Uint64(bpad))
}
func minInt64(a, b int64) int64 {
if a < b {
return a
} else {
return b
}
}
func maxInt64(a, b int64) int64 {
if a > b {
return a
} else {
return b
}
}
func readAndCompare(reader io.ReaderAt, offset int64, expected []byte) error {
actual := make([]byte, len(expected))
n, err := reader.ReadAt(actual, offset)
if err != nil {
return err
} else if n != len(actual) || bytes.Compare(expected, actual) != 0 {
return ErrUnexpectedMagic
}
return nil
}
func convertBytesToHumanReadable(b int64) string {
const unit = 1024
if b < unit {
return fmt.Sprintf("%d byte(s)", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp])
}
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randString(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}