-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
114 lines (98 loc) · 2.13 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
package scpw
import (
"fmt"
"github.com/google/uuid"
"os"
"path/filepath"
"strconv"
)
var (
letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
)
func MinInt(a, b int) int {
if a < b {
return a
}
return b
}
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
func MinInt64(a, b int64) int64 {
if a < b {
return a
}
return b
}
func MaxInt64(a, b int64) int64 {
if a > b {
return a
}
return b
}
func Addr(ip, port string) string {
return fmt.Sprintf("%s:%s", ip, port)
}
func FileModeV1(root string) (string, error) {
file, err := os.Stat(root)
if err != nil {
return "", err
}
return fmt.Sprintf("0%o", file.Mode().Perm()), nil
}
func FileModeV2(file os.FileInfo) string {
return fmt.Sprintf("0%o", file.Mode().Perm())
}
func StatDirMeta(root string) (name, mode, atime, mtime string, isDir bool, err error) {
stat, err := os.Stat(root)
if err != nil {
return
}
name = stat.Name()
mode = FileModeV2(stat)
atime, mtime = StatTimeV2(stat)
isDir = stat.IsDir()
return
}
func StatDirChild(root string) ([]os.DirEntry, error) {
dirs, err := os.ReadDir(root)
return dirs, err
}
func StatDir(root string) (entries []os.DirEntry, name, mode, atime, mtime string, isDir bool, err error) {
name, mode, atime, mtime, isDir, err = StatDirMeta(root)
if err != nil {
return
}
entries, err = StatDirChild(root)
return
}
func StatFile(root string) (name string, mode string, atime string, mtime string, size string, err error) {
stat, err := os.Stat(root)
if err != nil {
return "", "", "", "", "", err
}
name = stat.Name()
mode = FileModeV2(stat)
atime, mtime = StatTimeV2(stat)
size = strconv.FormatInt(stat.Size(), 10)
return name, mode, size, atime, mtime, nil
}
func ParseUnit32(str string) (uint32, error) {
res, err := strconv.ParseUint(str, 8, 32)
if err != nil {
return uint32(0), err
}
return uint32(res), err
}
func ParseInt64(str string) (int64, error) {
num, err := strconv.Atoi(str)
//res, err = strconv.ParseInt(str, 10, 64)
//return res, err
return int64(num), err
}
func RandName(root string) string {
return filepath.Join(root, uuid.NewString())
}