-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopeninstrument.go
58 lines (52 loc) · 1.21 KB
/
openinstrument.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
package openinstrument
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"time"
"github.com/golang/protobuf/proto"
)
func NowMs() uint64 {
return uint64(time.Now().UnixNano() / 1000000)
}
func ReadDirNames(directory string) ([]string, error) {
dir, err := os.Open(directory)
if err != nil {
return nil, fmt.Errorf("Can't open %s for readdir: %s", directory, err)
}
defer dir.Close()
names, err := dir.Readdirnames(0)
if err != nil {
return nil, fmt.Errorf("Can't read file names in %s: %s", directory, err)
}
sort.Strings(names)
return names, nil
}
func ProtoText(msg proto.Message) string {
return proto.MarshalTextString(msg)
}
func SafeWriteFile(filename string, msg proto.Message) error {
f, err := ioutil.TempFile(filepath.Dir(filename), "tmpfile")
if err != nil {
return fmt.Errorf("can't create temporary file: %s", err)
}
w := bufio.NewWriter(f)
if err := proto.MarshalText(w, msg); err != nil {
f.Close()
os.Remove(f.Name())
return fmt.Errorf("marshal %s failed: %s", f.Name(), err)
}
w.Flush()
f.Sync()
f.Close()
f.Sync()
if err := os.Rename(f.Name(), filename); err != nil {
os.Remove(f.Name())
return fmt.Errorf("rename failed: %s", err)
}
f.Sync()
return nil
}