/
common.go
175 lines (158 loc) · 3.33 KB
/
common.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
Copyright (C) CESS. All rights reserved.
Copyright (C) Cumulus Encrypted Storage System. All rights reserved.
SPDX-License-Identifier: Apache-2.0
*/
package utils
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"runtime/debug"
"github.com/CESSProject/cess-go-sdk/core/pattern"
"github.com/pkg/errors"
"github.com/shirou/gopsutil/disk"
"github.com/shirou/gopsutil/mem"
)
// RecoverError is used to record the stack information of panic
func RecoverError(err interface{}) string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%v\n", "[panic]")
fmt.Fprintf(buf, "%v\n", err)
if debug.Stack() != nil {
fmt.Fprintf(buf, "%v\n", string(debug.Stack()))
}
return buf.String()
}
// InterfaceIsNIL returns the comparison between i and nil
func InterfaceIsNIL(i interface{}) bool {
ret := i == nil
if !ret {
defer func() {
recover()
}()
va := reflect.ValueOf(i)
if va.Kind() == reflect.Ptr {
return va.IsNil()
}
return false
}
return ret
}
func Int64ToBytes(i int64) []byte {
buf := make([]byte, 8)
binary.BigEndian.PutUint64(buf, uint64(i))
return buf
}
func BytesToInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf))
}
func CopyFile(dst, src string) error {
dstFile, err := os.Create(dst)
if err != nil {
return err
}
defer dstFile.Close()
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
_, err = io.Copy(dstFile, srcFile)
if err != nil {
return err
}
return nil
}
// Get the total size of all files in a directory and subdirectories
func DirFiles(path string, count uint32) ([]string, error) {
var files = make([]string, 0)
result, err := filepath.Glob(path + "/*")
if err != nil {
return nil, err
}
for _, v := range result {
f, err := os.Stat(v)
if err != nil {
continue
}
if !f.IsDir() {
files = append(files, v)
}
if count > 0 {
if len(files) >= int(count) {
break
}
}
}
return files, nil
}
// Get the total size of all files in a directory and subdirectories
func DirDirs(path string, count uint32) ([]string, error) {
var dirs = make([]string, 0)
result, err := filepath.Glob(path + "/*")
if err != nil {
return nil, err
}
for _, v := range result {
f, err := os.Stat(v)
if err != nil {
continue
}
if f.IsDir() {
dirs = append(dirs, v)
}
if count > 0 {
if len(dirs) >= int(count) {
break
}
}
}
return dirs, nil
}
func RenameDir(oldDir, newDir string) error {
files, err := DirFiles(oldDir, 0)
if err != nil {
return err
}
fstat, err := os.Stat(newDir)
if err != nil {
err = os.MkdirAll(newDir, pattern.DirMode)
if err != nil {
return err
}
} else {
if !fstat.IsDir() {
return fmt.Errorf("%s not a dir", newDir)
}
}
for _, v := range files {
name := filepath.Base(v)
err = os.Rename(filepath.Join(oldDir, name), filepath.Join(newDir, name))
if err != nil {
return err
}
}
return os.RemoveAll(oldDir)
}
func GetDirFreeSpace(dir string) (uint64, error) {
sageStat, err := disk.Usage(dir)
return sageStat.Free, err
}
func GetSysMemAvailable() (uint64, error) {
var result uint64
memInfo, err := mem.VirtualMemory()
if err != nil {
return 0, errors.Wrapf(err, "[mem.VirtualMemory]")
}
result = memInfo.Available
swapInfo, err := mem.SwapMemory()
if err != nil {
return result, nil
}
return result + swapInfo.Free, nil
}