forked from soundscapecloud/soundscape
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
70 lines (61 loc) · 1.74 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
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
)
type diskInfo struct {
free int64
used int64
}
func (d *diskInfo) Total() int64 { return d.free + d.used }
func (d *diskInfo) TotalMB() int64 { return d.Total() / 1024 / 1024 }
func (d *diskInfo) TotalGB() int64 { return d.TotalMB() / 1024 }
func (d *diskInfo) Free() int64 { return d.free }
func (d *diskInfo) FreeMB() int64 { return d.free / 1024 / 1024 }
func (d *diskInfo) FreeGB() int64 { return d.FreeMB() / 1024 }
func (d *diskInfo) Used() int64 { return d.used }
func (d *diskInfo) UsedMB() int64 { return d.used / 1024 / 1024 }
func (d *diskInfo) UsedGB() int64 { return d.UsedMB() / 1024 }
func (d *diskInfo) UsedPercent() float64 {
return (float64(d.used) / float64(d.Total())) * 100
}
func newDiskInfo(path string) (*diskInfo, error) {
var stat syscall.Statfs_t
if err := syscall.Statfs(path, &stat); err != nil {
return nil, fmt.Errorf("diskinfo failed: %s", err)
}
free := stat.Bavail * uint64(stat.Bsize)
used := (stat.Blocks * uint64(stat.Bsize)) - free
return &diskInfo{int64(free), int64(used)}, nil
}
func randomNumber() (int, error) {
b := make([]byte, 4)
if _, err := rand.Read(b); err != nil {
return 0, err
}
return int(binary.LittleEndian.Uint32(b)), nil
}
func overwrite(filename string, data []byte, perm os.FileMode) error {
f, err := ioutil.TempFile(filepath.Dir(filename), filepath.Base(filename)+".tmp")
if err != nil {
return err
}
if _, err := f.Write(data); err != nil {
return err
}
if err := f.Sync(); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
if err := os.Chmod(f.Name(), perm); err != nil {
return err
}
return os.Rename(f.Name(), filename)
}