forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filerepo.go
176 lines (139 loc) · 4.36 KB
/
filerepo.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package filerepo
import (
"os"
"path/filepath"
"strings"
"sync"
"github.com/ZihuaZhang/fabric/internal/fileutil"
"github.com/pkg/errors"
)
const (
repoFilePermPrivateRW os.FileMode = 0o600
defaultTransientFileMarker = "~"
)
// Repo manages filesystem operations for saving files marked by the fileSuffix
// in order to support crash fault tolerance for components that need it by maintaining
// a file repo structure storing intermediate state.
type Repo struct {
mu sync.Mutex
fileRepoDir string
fileSuffix string
transientFileMarker string
}
// New initializes a new file repo at repoParentDir/fileSuffix.
// All file system operations on the returned file repo are thread safe.
func New(repoParentDir, fileSuffix string) (*Repo, error) {
if err := validateFileSuffix(fileSuffix); err != nil {
return nil, err
}
fileRepoDir := filepath.Join(repoParentDir, fileSuffix)
if _, err := fileutil.CreateDirIfMissing(fileRepoDir); err != nil {
return nil, err
}
if err := fileutil.SyncDir(repoParentDir); err != nil {
return nil, err
}
files, err := os.ReadDir(fileRepoDir)
if err != nil {
return nil, err
}
// Remove existing transient files in the repo
transientFilePattern := "*" + fileSuffix + defaultTransientFileMarker
for _, f := range files {
isTransientFile, err := filepath.Match(transientFilePattern, f.Name())
if err != nil {
return nil, err
}
if isTransientFile {
if err := os.Remove(filepath.Join(fileRepoDir, f.Name())); err != nil {
return nil, errors.Wrapf(err, "error cleaning up transient files")
}
}
}
if err := fileutil.SyncDir(fileRepoDir); err != nil {
return nil, err
}
return &Repo{
transientFileMarker: defaultTransientFileMarker,
fileSuffix: fileSuffix,
fileRepoDir: fileRepoDir,
}, nil
}
// Save atomically persists the content to suffix/baseName+suffix file by first writing it
// to a tmp file marked by the transientFileMarker and then moves the file to the final
// destination indicated by the FileSuffix.
func (r *Repo) Save(baseName string, content []byte) error {
r.mu.Lock()
defer r.mu.Unlock()
fileName := r.baseToFileName(baseName)
dest := r.baseToFilePath(baseName)
if _, err := os.Stat(dest); err == nil {
return os.ErrExist
}
tmpFileName := fileName + r.transientFileMarker
if err := fileutil.CreateAndSyncFileAtomically(r.fileRepoDir, tmpFileName, fileName, content, repoFilePermPrivateRW); err != nil {
return err
}
return fileutil.SyncDir(r.fileRepoDir)
}
// Remove removes the file associated with baseName from the file system.
func (r *Repo) Remove(baseName string) error {
r.mu.Lock()
defer r.mu.Unlock()
filePath := r.baseToFilePath(baseName)
if err := os.RemoveAll(filePath); err != nil {
return err
}
return fileutil.SyncDir(r.fileRepoDir)
}
// Read reads the file in the fileRepo associated with baseName's contents.
func (r *Repo) Read(baseName string) ([]byte, error) {
r.mu.Lock()
defer r.mu.Unlock()
filePath := r.baseToFilePath(baseName)
return os.ReadFile(filePath)
}
// List parses the directory and produce a list of file names, filtered by suffix.
func (r *Repo) List() ([]string, error) {
r.mu.Lock()
defer r.mu.Unlock()
var repoFiles []string
files, err := os.ReadDir(r.fileRepoDir)
if err != nil {
return nil, err
}
for _, f := range files {
isFileSuffix, err := filepath.Match("*"+r.fileSuffix, f.Name())
if err != nil {
return nil, err
}
if isFileSuffix {
repoFiles = append(repoFiles, f.Name())
}
}
return repoFiles, nil
}
// FileToBaseName strips the suffix from the file name to get the associated channel name.
func (r *Repo) FileToBaseName(fileName string) string {
baseFile := filepath.Base(fileName)
return strings.TrimSuffix(baseFile, "."+r.fileSuffix)
}
func (r *Repo) baseToFilePath(baseName string) string {
return filepath.Join(r.fileRepoDir, r.baseToFileName(baseName))
}
func (r *Repo) baseToFileName(baseName string) string {
return baseName + "." + r.fileSuffix
}
func validateFileSuffix(fileSuffix string) error {
if len(fileSuffix) == 0 {
return errors.New("fileSuffix illegal, cannot be empty")
}
if strings.Contains(fileSuffix, string(os.PathSeparator)) {
return errors.Errorf("fileSuffix [%s] illegal, cannot contain os path separator", fileSuffix)
}
return nil
}