forked from deepfabric/bkdtree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
126 lines (118 loc) · 2.57 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
package bkdtree
import (
"encoding/json"
"os"
"path/filepath"
"regexp"
"sort"
"syscall"
"github.com/pkg/errors"
)
//FileMmap mmaps the given file.
//https://medium.com/@arpith/adventures-with-mmap-463b33405223
func FileMmap(f *os.File) (data []byte, err error) {
info, err1 := f.Stat()
if err1 != nil {
err = errors.Wrap(err1, "")
return
}
prots := []int{syscall.PROT_WRITE | syscall.PROT_READ, syscall.PROT_READ}
for _, prot := range prots {
data, err = syscall.Mmap(int(f.Fd()), 0, int(info.Size()), prot, syscall.MAP_SHARED)
if err == nil {
break
}
}
if err != nil {
err = errors.Wrap(err, "")
return
}
return
}
//FileMunmap unmaps the given file.
func FileMunmap(data []byte) (err error) {
err = syscall.Munmap(data)
if err != nil {
err = errors.Wrap(err, "")
return
}
return
}
//FileUnmarshal unmarshals the given file to object.
func FileUnmarshal(fp string, v interface{}) (err error) {
var f *os.File
var data []byte
if f, err = os.Open(fp); err != nil {
return
}
defer f.Close()
if data, err = FileMmap(f); err != nil {
return
}
defer FileMunmap(data)
err = json.Unmarshal(data, v)
return
}
//FileMarshal marshals the given object to file.
func FileMarshal(fp string, v interface{}) (err error) {
var f *os.File
var data []byte
var count int
if data, err = json.Marshal(v); err != nil {
return
}
if f, err = os.OpenFile(fp, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600); err != nil {
err = errors.Wrap(err, "")
return
}
defer f.Close()
if count, err = f.Write(data); err != nil {
err = errors.Wrap(err, "")
return
}
if count != len(data) {
err = errors.Errorf("%s partial wirte %d, want %d", fp, count, len(data))
return
}
return
}
//FilepathGlob is enhanced version of standard path.filepath::Glob()
func FilepathGlob(dir, patt string) (matches [][]string, err error) {
var d *os.File
var fns []string
d, err = os.Open(dir)
if os.IsNotExist(err) {
err = nil
return
}
defer d.Close()
fns, err = d.Readdirnames(0)
if err != nil {
err = errors.Wrap(err, "")
return
}
sort.Strings(fns)
re := regexp.MustCompile(patt)
for _, fn := range fns {
subs := re.FindStringSubmatch(fn)
if subs == nil {
continue
}
matches = append(matches, subs)
}
return
}
//FilepathGlobRm remove given files under the given directory.
func FilepathGlobRm(dir, patt string) (err error) {
var matches [][]string
if matches, err = FilepathGlob(dir, patt); err != nil {
return
}
for _, match := range matches {
fp := filepath.Join(dir, match[0])
if err = os.Remove(fp); err != nil {
err = errors.Wrap(err, "")
}
}
return
}