summaryrefslogtreecommitdiff
path: root/web/log.go
blob: b45eec5eaef254a3c7f3ed3b53a2491fec075d0e (plain) (blame)
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
package web

import (
	"fmt"
	"os"
	"sync"
)

// logStore is implemented by the production jj sidecar and by the file-backed
// adapter used in tests. Append's callback makes session note numbering atomic
// with the content update.
type logStore interface {
	Read() ([]byte, error)
	Generation() (string, error)
	Append(func(current []byte) ([]byte, error)) error
	String() string
}

type fileLog struct {
	path string
	mu   sync.Mutex
}

func newFileLog(path string) *fileLog { return &fileLog{path: path} }

func (l *fileLog) String() string { return l.path }

func (l *fileLog) Read() ([]byte, error) { return os.ReadFile(l.path) }

func (l *fileLog) Generation() (string, error) {
	info, err := os.Stat(l.path)
	if err != nil {
		return "", err
	}
	return fmt.Sprintf("%d:%d", info.Size(), info.ModTime().UnixNano()), nil
}

func (l *fileLog) Append(makePayload func(current []byte) ([]byte, error)) error {
	l.mu.Lock()
	defer l.mu.Unlock()
	current, err := os.ReadFile(l.path)
	if err != nil && !os.IsNotExist(err) {
		return err
	}
	payload, err := makePayload(current)
	if err != nil {
		return err
	}
	f, err := os.OpenFile(l.path, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0o644)
	if err != nil {
		return err
	}
	defer f.Close()
	_, err = f.Write(payload)
	return err
}