forked from quickfixgo/quickfix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileutil.go
63 lines (55 loc) · 1.59 KB
/
fileutil.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
package quickfix
import (
"fmt"
"os"
"strings"
"github.com/pkg/errors"
)
func sessionIDFilenamePrefix(s SessionID) string {
sender := []string{s.SenderCompID}
if s.SenderSubID != "" {
sender = append(sender, s.SenderSubID)
}
if s.SenderLocationID != "" {
sender = append(sender, s.SenderLocationID)
}
target := []string{s.TargetCompID}
if s.TargetSubID != "" {
target = append(target, s.TargetSubID)
}
if s.TargetLocationID != "" {
target = append(target, s.TargetLocationID)
}
fname := []string{s.BeginString, strings.Join(sender, "_"), strings.Join(target, "_")}
if s.Qualifier != "" {
fname = append(fname, s.Qualifier)
}
return strings.Join(fname, "-")
}
// closeFile behaves like Close, except that no error is returned if the file does not exist
func closeFile(f *os.File) error {
if f != nil {
if err := f.Close(); err != nil {
if !os.IsNotExist(err) {
return err
}
}
}
return nil
}
// removeFile behaves like os.Remove, except that no error is returned if the file does not exist
func removeFile(fname string) error {
if err := os.Remove(fname); (err != nil) && !os.IsNotExist(err) {
return errors.Wrapf(err, "remove %v", fname)
}
return nil
}
// openOrCreateFile opens a file for reading and writing, creating it if necessary
func openOrCreateFile(fname string, perm os.FileMode) (f *os.File, err error) {
if f, err = os.OpenFile(fname, os.O_RDWR, perm); err != nil {
if f, err = os.OpenFile(fname, os.O_RDWR|os.O_CREATE, perm); err != nil {
return nil, fmt.Errorf("error opening or creating file: %s: %s", fname, err.Error())
}
}
return f, nil
}