Skip to content

Commit f16f7bd

Browse files
authored
Merge pull request #76 from keep-network/expose-persistence-functions
Expose EnsureDirectoryExists, Read, and Write These functions are abstract and generally useful, so we publicize them for other projects.
2 parents 3f1aa45 + d367684 commit f16f7bd

1 file changed

Lines changed: 15 additions & 13 deletions

File tree

pkg/persistence/disk_persistence.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ func NewDiskHandle(path string) (Handle, error) {
2323
return nil, err
2424
}
2525

26-
err = ensureDirectoryExists(path, currentDir)
26+
err = EnsureDirectoryExists(path, currentDir)
2727
if err != nil {
2828
return nil, err
2929
}
3030

31-
err = ensureDirectoryExists(path, archiveDir)
31+
err = EnsureDirectoryExists(path, archiveDir)
3232
if err != nil {
3333
return nil, err
3434
}
3535

36-
err = ensureDirectoryExists(path, snapshotDir)
36+
err = EnsureDirectoryExists(path, snapshotDir)
3737
if err != nil {
3838
return nil, err
3939
}
@@ -74,12 +74,12 @@ func (ds *diskPersistence) Save(data []byte, dirName, fileName string) error {
7474
}
7575

7676
dirPath := ds.getStorageCurrentDirPath()
77-
err := ensureDirectoryExists(dirPath, dirName)
77+
err := EnsureDirectoryExists(dirPath, dirName)
7878
if err != nil {
7979
return err
8080
}
8181

82-
return write(fmt.Sprintf("%s/%s/%s", dirPath, dirName, fileName), data)
82+
return Write(fmt.Sprintf("%s/%s/%s", dirPath, dirName, fileName), data)
8383
}
8484

8585
func (ds *diskPersistence) Snapshot(data []byte, dirName, fileName string) error {
@@ -106,7 +106,7 @@ func (ds *diskPersistence) Snapshot(data []byte, dirName, fileName string) error
106106
defer ds.snapshotMutex.Unlock()
107107

108108
dirPath := fmt.Sprintf("%s/%s", ds.dataDir, snapshotDir)
109-
err := ensureDirectoryExists(dirPath, dirName)
109+
err := EnsureDirectoryExists(dirPath, dirName)
110110
if err != nil {
111111
return err
112112
}
@@ -121,7 +121,7 @@ func (ds *diskPersistence) Snapshot(data []byte, dirName, fileName string) error
121121
)
122122
}
123123

124-
return write(filePath, data)
124+
return Write(filePath, data)
125125
}
126126

127127
func isNonExistingFile(filePath string) bool {
@@ -168,7 +168,9 @@ func checkStoragePermission(dirBasePath string) error {
168168
return nil
169169
}
170170

171-
func ensureDirectoryExists(dirBasePath, newDirName string) error {
171+
// EnsureDirectoryExists creates a new directory in a base path if it doesn't
172+
// exist, returns nil if it does, and errors out if the base path doesn't exist.
173+
func EnsureDirectoryExists(dirBasePath, newDirName string) error {
172174
dirPath := fmt.Sprintf("%s/%s", dirBasePath, newDirName)
173175
if _, err := os.Stat(dirPath); os.IsNotExist(err) {
174176
err = os.Mkdir(dirPath, os.ModePerm)
@@ -180,8 +182,8 @@ func ensureDirectoryExists(dirBasePath, newDirName string) error {
180182
return nil
181183
}
182184

183-
// create and write data to a file
184-
func write(filePath string, data []byte) error {
185+
// Write creates and writes data to a file
186+
func Write(filePath string, data []byte) error {
185187
writeFile, err := os.Create(filePath)
186188
if err != nil {
187189
return err
@@ -202,8 +204,8 @@ func write(filePath string, data []byte) error {
202204
return nil
203205
}
204206

205-
// read a file from a file system
206-
func read(filePath string) ([]byte, error) {
207+
// Read a file from a file system
208+
func Read(filePath string) ([]byte, error) {
207209
// #nosec G304 (file path provided as taint input)
208210
// This line opens a file from the predefined storage.
209211
// There is no user input.
@@ -270,7 +272,7 @@ func readAll(directoryPath string) (<-chan DataDescriptor, <-chan error) {
270272
fileName := dirFile.Name()
271273

272274
readFunc := func() ([]byte, error) {
273-
return read(fmt.Sprintf(
275+
return Read(fmt.Sprintf(
274276
"%s/%s/%s",
275277
directoryPath,
276278
dirName,

0 commit comments

Comments
 (0)