-
Notifications
You must be signed in to change notification settings - Fork 211
/
util.go
148 lines (137 loc) · 3.82 KB
/
util.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package checkpoint
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"time"
"github.com/santhosh-tekuri/jsonschema/v5"
"github.com/spf13/afero"
)
type RecoveryFile struct {
file afero.File
fwriter *bufio.Writer
path string
}
func NewRecoveryFile(fs afero.Fs, path string) (*RecoveryFile, error) {
if err := fs.MkdirAll(filepath.Dir(path), dirPerm); err != nil {
return nil, fmt.Errorf("create dst dir %v: %w", filepath.Dir(path), err)
}
f, _ := fs.Stat(path)
if f != nil {
return nil, fmt.Errorf("%w: file already exist: %v", os.ErrExist, path)
}
tmpf, err := afero.TempFile(fs, filepath.Dir(path), filepath.Base(path))
if err != nil {
return nil, fmt.Errorf("%w: create tmp file", err)
}
return &RecoveryFile{
file: tmpf,
fwriter: bufio.NewWriter(tmpf),
path: path,
}, nil
}
func (rf *RecoveryFile) Copy(fs afero.Fs, src io.Reader) error {
n, err := io.Copy(rf.fwriter, src)
if err != nil {
return err
}
if n == 0 {
return fmt.Errorf("no recovery data")
}
return rf.Save(fs)
}
func (rf *RecoveryFile) Save(fs afero.Fs) error {
defer rf.file.Close()
if err := rf.fwriter.Flush(); err != nil {
return fmt.Errorf("flush tmp file: %w", err)
}
if err := rf.file.Sync(); err != nil {
return fmt.Errorf("%w: sync tmp file", err)
}
if err := rf.file.Close(); err != nil {
return fmt.Errorf("%w: close tmp file", err)
}
if err := fs.Rename(rf.file.Name(), rf.path); err != nil {
return fmt.Errorf("%w: rename tmp file %v to %v", err, rf.file.Name(), rf.path)
}
return nil
}
func ValidateSchema(data []byte) error {
sch, err := jsonschema.CompileString(schemaFile, Schema)
if err != nil {
return fmt.Errorf("compile checkpoint json schema: %w", err)
}
var v any
if err = json.Unmarshal(data, &v); err != nil {
return fmt.Errorf("unmarshal checkpoint data: %w", err)
}
if err = sch.Validate(v); err != nil {
return fmt.Errorf("validate checkpoint data: %w", err)
}
return nil
}
func CopyFile(fs afero.Fs, src, dst string) error {
rf, err := NewRecoveryFile(fs, dst)
if err != nil {
return fmt.Errorf("new recovery file %w", err)
}
srcf, err := fs.Open(src)
if err != nil {
return fmt.Errorf("open src recovery file: %w", err)
}
defer srcf.Close()
return rf.Copy(fs, srcf)
}
func httpToLocalFile(ctx context.Context, resource *url.URL, fs afero.Fs, dst string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, resource.String(), nil)
if err != nil {
return fmt.Errorf("create http request: %w", err)
}
resp, err := (&http.Client{}).Do(req)
if err != nil {
return fmt.Errorf("http get bootstrap file: %w", err)
}
defer resp.Body.Close()
rf, err := NewRecoveryFile(fs, dst)
if err != nil {
return fmt.Errorf("new recovery file %w", err)
}
return rf.Copy(fs, resp.Body)
}
func backupRecovery(fs afero.Fs, recoveryDir string) (string, error) {
if _, err := fs.Stat(recoveryDir); err != nil {
return "", nil
}
backupDir := fmt.Sprintf("%s.%d", recoveryDir, time.Now().UnixNano())
if err := fs.Rename(recoveryDir, backupDir); err != nil {
return "", fmt.Errorf("backup old checkpoint data: %w", err)
}
return backupDir, nil
}
func backupOldDb(fs afero.Fs, srcDir, dbFile string) (string, error) {
backupDir := filepath.Join(srcDir, fmt.Sprintf("%s.%d", "backup", time.Now().Unix()))
if err := fs.MkdirAll(backupDir, dirPerm); err != nil {
return "", fmt.Errorf("create backup dir: %w", err)
}
// sqlite create .sql, .sql-shm and .sql-wal files.
files, err := afero.Glob(fs, filepath.Join(srcDir, fmt.Sprintf("%s*", dbFile)))
if err != nil {
return "", fmt.Errorf("list db files: %w", err)
}
if len(files) == 0 {
return "", nil
}
for _, src := range files {
dst := filepath.Join(backupDir, filepath.Base(src))
if err = fs.Rename(src, dst); err != nil {
return "", err
}
}
return backupDir, nil
}