-
Notifications
You must be signed in to change notification settings - Fork 1
/
file.go
136 lines (108 loc) · 2.77 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
package atomicf
import (
"crypto/sha256"
"encoding/binary"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
const logFilePostfix = "-log-*.alog"
// Atomic write on linux https://danluu.com/file-consistency/
type AtomicFile struct {
*os.File
}
func OpenFile(name string, flag int, perm os.FileMode) (*AtomicFile, error) {
file, err := os.OpenFile(name, flag, perm)
if err != nil {
return nil, err
}
return &AtomicFile{file}, nil
}
// todo: for now working only with one log file at time
func (file *AtomicFile) Recover() (err error) {
logFiles, err := filepath.Glob(filepath.Join(file.dir(), file.logFilePattern()))
if err != nil || len(logFiles) == 0 {
return
}
// take only first file
data, err := ioutil.ReadFile(logFiles[0])
if err != nil {
return
}
// means that log file not corrupted. otherwise do nothing and delete file
if len(data) > 32 && VerifyHash(data[0:32], data[32:]) {
offset := binary.LittleEndian.Uint64(data[32:40])
_, err = file.writePostLog(logFiles[0], data[40:], int64(offset))
return
}
if err = os.Remove(logFiles[0]); err != nil {
return
}
return
}
func (file *AtomicFile) Write(p []byte) (n int, err error) {
fileInfo, err := file.Stat()
if err != nil {
return
}
return file.writeLog(p, fileInfo.Size())
}
func (file *AtomicFile) WriteAt(b []byte, off int64) (n int, err error) {
return file.writeLog(b, off)
}
func (file *AtomicFile) writeLog(b []byte, off int64) (n int, err error) {
logFileName, err := file.logOperation(b, off)
if err != nil {
return
}
return file.writePostLog(logFileName, b, off)
}
func (file *AtomicFile) writePostLog(logFileName string, data []byte, offset int64) (n int, err error) {
var dir *os.File
if dir, err = os.Open(file.dir()); err != nil {
return
}
defer dir.Close()
if err = dir.Sync(); err != nil {
return
}
if n, err = file.File.WriteAt(data, offset); err != nil {
return
}
if err = file.File.Sync(); err != nil {
return
}
if err = os.Remove(logFileName); err != nil {
return
}
if err = dir.Sync(); err != nil {
return
}
return
}
// return directory of current file
func (file *AtomicFile) dir() string {
return filepath.Dir(file.File.Name())
}
func (file *AtomicFile) logFilePattern() string {
fileName := filepath.Base(file.Name())
// remove dot postfix
dotIndex := strings.Index(fileName, ".")
if dotIndex != -1 {
fileName = fileName[0:dotIndex]
}
return fileName + logFilePostfix
}
// return path of create log file
func (file *AtomicFile) logOperation(b []byte, off int64) (path string, err error) {
offsetBytes := make([]byte, 8)
binary.LittleEndian.PutUint64(offsetBytes, uint64(off))
data := append(offsetBytes, b...)
dataHash := sha256.Sum256(data)
return WriteTempFile(
file.dir(),
file.logFilePattern(),
append(dataHash[:], data...),
)
}