-
Notifications
You must be signed in to change notification settings - Fork 166
/
syncrename.go
58 lines (48 loc) · 1.26 KB
/
syncrename.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
package wal
import (
"bufio"
"fmt"
"io"
"os"
)
type WriterSeekerCloser interface {
io.Writer
io.Seeker
io.Closer
}
// SyncOnCloseRenameFile is a composite of buffered writer over a given file
// which flushes/sync on closing and renames to `targetName` as a last step.
// Typical usecase is to write data to a temporary file and only rename it
// to target one as the last step. This help avoid situation when writing is
// interrupted and unusable file but with target name exists.
type SyncOnCloseRenameFile struct {
file *os.File
targetName string
*bufio.Writer
}
func (s *SyncOnCloseRenameFile) Sync() error {
err := s.Flush()
if err != nil {
return fmt.Errorf("cannot flush buffer: %w", err)
}
return s.file.Sync()
}
func (s *SyncOnCloseRenameFile) Close() error {
err := s.Flush()
if err != nil {
return fmt.Errorf("cannot flush buffer: %w", err)
}
err = s.file.Sync()
if err != nil {
return fmt.Errorf("cannot sync file %s: %w", s.file.Name(), err)
}
err = s.file.Close()
if err != nil {
return fmt.Errorf("error while closing file %s: %w", s.file.Name(), err)
}
err = os.Rename(s.file.Name(), s.targetName)
if err != nil {
return fmt.Errorf("error while renaming from %s to %s: %w", s.file.Name(), s.targetName, err)
}
return nil
}