forked from coreos/etcd-operator
/
file.go
144 lines (122 loc) · 3.41 KB
/
file.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
// Copyright 2016 The etcd-operator Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package backup
import (
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/Sirupsen/logrus"
)
const (
backupTmpDir = "tmp"
backupFilePerm = 0600
backupFilenameSuffix = "etcd.backup"
)
// ensure fileBackend satisfies backend interface.
var _ backend = &fileBackend{}
type fileBackend struct {
dir string
}
func (fb *fileBackend) save(version string, snapRev int64, rc io.Reader) (int64, error) {
filename := makeBackupName(version, snapRev)
tmpfile, err := os.OpenFile(filepath.Join(fb.dir, backupTmpDir, filename), os.O_WRONLY|os.O_TRUNC|os.O_CREATE, backupFilePerm)
if err != nil {
return -1, fmt.Errorf("failed to create snapshot tempfile: %v", err)
}
n, err := io.Copy(tmpfile, rc)
if err != nil {
tmpfile.Close()
os.Remove(tmpfile.Name())
return -1, fmt.Errorf("failed to save snapshot: %v", err)
}
err = tmpfile.Sync()
if err != nil {
logrus.Warningf("filebackend: failed to sync backup file %s (%v)", filename, err)
}
tmpfile.Close()
nextSnapshotName := filepath.Join(fb.dir, filename)
err = os.Rename(tmpfile.Name(), nextSnapshotName)
if err != nil {
os.Remove(tmpfile.Name())
return -1, fmt.Errorf("rename snapshot from %s to %s failed: %v", tmpfile.Name(), nextSnapshotName, err)
}
logrus.Infof("saved snapshot %s (size: %d) successfully", nextSnapshotName, n)
return n, nil
}
func (fb *fileBackend) getLatest() (string, error) {
files, err := ioutil.ReadDir(fb.dir)
if err != nil {
return "", fmt.Errorf("failed to list dir (%s): error (%v)", fb.dir, err)
}
var names []string
for _, f := range files {
names = append(names, f.Name())
}
fn := getLatestBackupName(names)
if fn == "" {
return "", nil
}
return fn, err
}
func (fb *fileBackend) open(name string) (io.ReadCloser, error) {
return os.Open(filepath.Join(fb.dir, name))
}
func (fb *fileBackend) purge(maxBackupFiles int) error {
files, err := ioutil.ReadDir(fb.dir)
if err != nil {
return err
}
var names []string
for _, f := range files {
names = append(names, f.Name())
}
bnames := filterAndSortBackups(names)
if len(bnames) < maxBackupFiles {
return nil
}
for i := 0; i < len(bnames)-maxBackupFiles; i++ {
err := os.Remove(path.Join(fb.dir, bnames[i]))
if err != nil {
logrus.Errorf("failed to remove backup file (%s): %v", bnames[i], err)
} else {
logrus.Infof("removed backup file: %s", bnames[i])
}
}
return nil
}
func (fb *fileBackend) total() (int, error) {
files, err := ioutil.ReadDir(fb.dir)
if err != nil {
return -1, err
}
var names []string
for _, f := range files {
names = append(names, f.Name())
}
return len(filterAndSortBackups(names)), nil
}
func (fb *fileBackend) totalSize() (int64, error) {
files, err := ioutil.ReadDir(fb.dir)
if err != nil {
return -1, err
}
var size int64
for _, f := range files {
size += f.Size()
}
return size, nil
}