forked from saymedia/journald-cloudwatch-logs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate.go
57 lines (49 loc) · 933 Bytes
/
state.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
package main
import (
"fmt"
"os"
)
const stateFormat = "%s\n%s\n"
const mapSize = 64
type State struct {
file *os.File
}
func OpenState(fn string) (State, error) {
s := State{}
f, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0700)
if err != nil {
return s, err
}
s.file = f
return s, nil
}
func (s State) Close() error {
return s.file.Close()
}
func (s State) Sync() error {
return s.file.Sync()
}
func (s State) LastState() (string, string) {
var bootId string
var seqToken string
_, err := s.file.Seek(0, 0)
if err != nil {
return "", ""
}
n, err := fmt.Fscanf(s.file, stateFormat, &bootId, &seqToken)
if err != nil || n < 2 {
return "", ""
}
return bootId, seqToken
}
func (s State) SetState(bootId, seqToken string) error {
_, err := s.file.Seek(0, 0)
if err != nil {
return err
}
_, err = fmt.Fprintf(s.file, stateFormat, bootId, seqToken)
if err != nil {
return err
}
return nil
}